From 3fa4f53d0acb933c1864da7a51095a497545a187 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Mon, 8 Oct 2018 05:30:02 +0200 Subject: [PATCH] System module format - fixes function hoisting failure case (#8820) * failing test case * fix function hoist bug --- .../src/index.js | 19 +++++++++++++++++-- .../systemjs/export-fn-decl/input.mjs | 10 ++++++++++ .../systemjs/export-fn-decl/output.mjs | 18 ++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 packages/babel-plugin-transform-modules-systemjs/test/fixtures/systemjs/export-fn-decl/input.mjs create mode 100644 packages/babel-plugin-transform-modules-systemjs/test/fixtures/systemjs/export-fn-decl/output.mjs diff --git a/packages/babel-plugin-transform-modules-systemjs/src/index.js b/packages/babel-plugin-transform-modules-systemjs/src/index.js index 5f93094be5..242d132163 100644 --- a/packages/babel-plugin-transform-modules-systemjs/src/index.js +++ b/packages/babel-plugin-transform-modules-systemjs/src/index.js @@ -332,8 +332,23 @@ export default declare((api, options) => { const nodes = []; for (const specifier of specifiers) { - // only globals exported this way - if (!path.scope.getBinding(specifier.local.name)) { + const binding = path.scope.getBinding( + specifier.local.name, + ); + // hoisted function export + if ( + binding && + t.isFunctionDeclaration(binding.path.node) + ) { + beforeBody.push( + buildExportCall( + specifier.exported.name, + t.cloneNode(specifier.local), + ), + ); + } + // only globals also exported this way + else if (!binding) { nodes.push( buildExportCall( specifier.exported.name, diff --git a/packages/babel-plugin-transform-modules-systemjs/test/fixtures/systemjs/export-fn-decl/input.mjs b/packages/babel-plugin-transform-modules-systemjs/test/fixtures/systemjs/export-fn-decl/input.mjs new file mode 100644 index 0000000000..3c230f1867 --- /dev/null +++ b/packages/babel-plugin-transform-modules-systemjs/test/fixtures/systemjs/export-fn-decl/input.mjs @@ -0,0 +1,10 @@ +var testProp = 'test property'; + +function testFunc() { + return 'test function'; +} + +export { + testFunc, + testProp +}; diff --git a/packages/babel-plugin-transform-modules-systemjs/test/fixtures/systemjs/export-fn-decl/output.mjs b/packages/babel-plugin-transform-modules-systemjs/test/fixtures/systemjs/export-fn-decl/output.mjs new file mode 100644 index 0000000000..c3c2159be3 --- /dev/null +++ b/packages/babel-plugin-transform-modules-systemjs/test/fixtures/systemjs/export-fn-decl/output.mjs @@ -0,0 +1,18 @@ +System.register([], function (_export, _context) { + "use strict"; + + var testProp; + + function testFunc() { + return 'test function'; + } + + _export("testFunc", testFunc); + + return { + setters: [], + execute: function () { + _export("testProp", testProp = 'test property'); + } + }; +}); \ No newline at end of file