diff --git a/src/babel/transformation/transformers/internal/modules.js b/src/babel/transformation/transformers/internal/modules.js index 580ffd778c..d0be865e6e 100644 --- a/src/babel/transformation/transformers/internal/modules.js +++ b/src/babel/transformation/transformers/internal/modules.js @@ -52,6 +52,16 @@ export function ExportDeclaration(node, parent, scope) { node.declaration = null; node._blockHoist = 2; return [getDeclar(), node]; + } else if (t.isVariableDeclaration(declar)) { + var specifiers = []; + + var bindings = t.getBindingIdentifiers(declar); + for (var key in bindings) { + var id = bindings[key]; + specifiers.push(t.exportSpecifier(id, id)); + } + + return [declar, t.exportDeclaration(null, specifiers)]; } } } diff --git a/test/_transformation-helper.js b/test/_transformation-helper.js index 7edfb09dba..45b942b723 100644 --- a/test/_transformation-helper.js +++ b/test/_transformation-helper.js @@ -86,8 +86,8 @@ var run = function (task, done) { } }; - var fn = new Function("require", "done", execCode); - fn.call(global, fakeRequire, chai.assert, done); + var fn = new Function("require", "done", "exports", execCode); + fn.call(global, fakeRequire, chai.assert, {}, done); } catch (err) { err.message = exec.loc + ": " + err.message; err.message += codeFrame(execCode); diff --git a/test/fixtures/transformation/es6-block-scoping-tdz-fail/export.js b/test/fixtures/transformation/es6-block-scoping-tdz-fail/export.js new file mode 100644 index 0000000000..5ccb845615 --- /dev/null +++ b/test/fixtures/transformation/es6-block-scoping-tdz-fail/export.js @@ -0,0 +1,2 @@ +a; +export const a = 1; diff --git a/test/fixtures/transformation/es6-block-scoping-tdz-pass/export.js b/test/fixtures/transformation/es6-block-scoping-tdz-pass/export.js new file mode 100644 index 0000000000..5a433d6003 --- /dev/null +++ b/test/fixtures/transformation/es6-block-scoping-tdz-pass/export.js @@ -0,0 +1,2 @@ +export const a = 1; +a; diff --git a/test/fixtures/transformation/es6-modules-amd/exports-variable/expected.js b/test/fixtures/transformation/es6-modules-amd/exports-variable/expected.js index 758bedb23a..f53c6adb75 100644 --- a/test/fixtures/transformation/es6-modules-amd/exports-variable/expected.js +++ b/test/fixtures/transformation/es6-modules-amd/exports-variable/expected.js @@ -4,14 +4,22 @@ define(["exports"], function (exports) { var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; exports.foo7 = foo7; - var foo = exports.foo = 1; - var foo = exports.foo = 1; - var bar = exports.bar = 2; - var foo2 = exports.foo2 = function foo2() {}; - var foo3 = exports.foo3 = undefined; - var foo4 = exports.foo4 = 2; - var foo5 = exports.foo5 = undefined; - var foo6 = exports.foo6 = 3; + var foo = 1; + exports.foo = foo; + var foo = 1, + bar = 2; + exports.foo = foo; + exports.bar = bar; + var foo2 = function foo2() {}; + exports.foo2 = foo2; + var foo3; + exports.foo3 = foo3; + var foo4 = 2; + exports.foo4 = foo4; + var foo5 = undefined; + exports.foo5 = foo5; + var foo6 = 3; + exports.foo6 = foo6; function foo7() {} diff --git a/test/fixtures/transformation/es6-modules-amd/hoist-function-exports/expected.js b/test/fixtures/transformation/es6-modules-amd/hoist-function-exports/expected.js index 67be17eac4..7afc49e1bb 100644 --- a/test/fixtures/transformation/es6-modules-amd/hoist-function-exports/expected.js +++ b/test/fixtures/transformation/es6-modules-amd/hoist-function-exports/expected.js @@ -8,11 +8,12 @@ define(["exports", "./evens"], function (exports, _evens) { return isEven(n) ? n + 1 : n + 2; } - var isOdd = exports.isOdd = (function (isEven) { + var isOdd = (function (isEven) { return function (n) { return !isEven(n); }; })(isEven); + exports.isOdd = isOdd; Object.defineProperty(exports, "__esModule", { value: true }); diff --git a/test/fixtures/transformation/es6-modules-amd/overview/expected.js b/test/fixtures/transformation/es6-modules-amd/overview/expected.js index a9993c1612..777baa6c76 100644 --- a/test/fixtures/transformation/es6-modules-amd/overview/expected.js +++ b/test/fixtures/transformation/es6-modules-amd/overview/expected.js @@ -9,8 +9,9 @@ define(["exports", "foo", "foo-bar", "./directory/foo-bar"], function (exports, var bar = _foo.bar; var bar2 = _foo.foo; exports.test = test; - var test2 = exports.test2 = 5; + var test2 = 5; + exports.test2 = test2; exports["default"] = test; Object.defineProperty(exports, "__esModule", { value: true diff --git a/test/fixtures/transformation/es6-modules-amd/remap/expected.js b/test/fixtures/transformation/es6-modules-amd/remap/expected.js index accde1fb6e..243a843682 100644 --- a/test/fixtures/transformation/es6-modules-amd/remap/expected.js +++ b/test/fixtures/transformation/es6-modules-amd/remap/expected.js @@ -1,7 +1,8 @@ define(["exports"], function (exports) { "use strict"; - var test = exports.test = 2; + var test = 2; + exports.test = test; test = exports.test = 5; test = exports.test += 1; diff --git a/test/fixtures/transformation/es6-modules-common/exports-variable/expected.js b/test/fixtures/transformation/es6-modules-common/exports-variable/expected.js index d2cb09cbef..d6976167ba 100644 --- a/test/fixtures/transformation/es6-modules-common/exports-variable/expected.js +++ b/test/fixtures/transformation/es6-modules-common/exports-variable/expected.js @@ -3,14 +3,22 @@ var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; exports.foo7 = foo7; -var foo = exports.foo = 1; -var foo = exports.foo = 1; -var bar = exports.bar = 2; -var foo2 = exports.foo2 = function foo2() {}; -var foo3 = exports.foo3 = undefined; -var foo4 = exports.foo4 = 2; -var foo5 = exports.foo5 = undefined; -var foo6 = exports.foo6 = 3; +var foo = 1; +exports.foo = foo; +var foo = 1, + bar = 2; +exports.foo = foo; +exports.bar = bar; +var foo2 = function foo2() {}; +exports.foo2 = foo2; +var foo3; +exports.foo3 = foo3; +var foo4 = 2; +exports.foo4 = foo4; +var foo5 = undefined; +exports.foo5 = foo5; +var foo6 = 3; +exports.foo6 = foo6; function foo7() {} diff --git a/test/fixtures/transformation/es6-modules-common/hoist-function-exports/expected.js b/test/fixtures/transformation/es6-modules-common/hoist-function-exports/expected.js index d68f62cebb..e4ae21b6fb 100644 --- a/test/fixtures/transformation/es6-modules-common/hoist-function-exports/expected.js +++ b/test/fixtures/transformation/es6-modules-common/hoist-function-exports/expected.js @@ -8,11 +8,12 @@ function nextOdd(n) { return isEven(n) ? n + 1 : n + 2; } -var isOdd = exports.isOdd = (function (isEven) { +var isOdd = (function (isEven) { return function (n) { return !isEven(n); }; })(isEven); +exports.isOdd = isOdd; Object.defineProperty(exports, "__esModule", { value: true }); \ No newline at end of file diff --git a/test/fixtures/transformation/es6-modules-common/loose/expected.js b/test/fixtures/transformation/es6-modules-common/loose/expected.js index dbb5abcc31..5fa26a81fa 100644 --- a/test/fixtures/transformation/es6-modules-common/loose/expected.js +++ b/test/fixtures/transformation/es6-modules-common/loose/expected.js @@ -1,4 +1,5 @@ "use strict"; -var foo = exports.foo = 5; -exports.__esModule = true; +var foo = 5; +exports.foo = foo; +exports.__esModule = true; \ No newline at end of file diff --git a/test/fixtures/transformation/es6-modules-common/overview/expected.js b/test/fixtures/transformation/es6-modules-common/overview/expected.js index 88351aa91f..3ff558eaec 100644 --- a/test/fixtures/transformation/es6-modules-common/overview/expected.js +++ b/test/fixtures/transformation/es6-modules-common/overview/expected.js @@ -19,7 +19,8 @@ var bar = require("foo4").bar; var bar2 = require("foo5").foo; exports.test = test; -var test = exports.test = 5; +var test = 5; +exports.test = test; Object.defineProperty(exports, "__esModule", { value: true }); \ No newline at end of file diff --git a/test/fixtures/transformation/es6-modules-common/remap/expected.js b/test/fixtures/transformation/es6-modules-common/remap/expected.js index fb63edccd8..5562ddce4e 100644 --- a/test/fixtures/transformation/es6-modules-common/remap/expected.js +++ b/test/fixtures/transformation/es6-modules-common/remap/expected.js @@ -1,6 +1,7 @@ "use strict"; -var test = exports.test = 2; +var test = 2; +exports.test = test; test = exports.test = 5; test = exports.test += 1; diff --git a/test/fixtures/transformation/es6-modules-ignore/exports-variable/expected.js b/test/fixtures/transformation/es6-modules-ignore/exports-variable/expected.js index 1a08078682..5231f638e6 100644 --- a/test/fixtures/transformation/es6-modules-ignore/exports-variable/expected.js +++ b/test/fixtures/transformation/es6-modules-ignore/exports-variable/expected.js @@ -6,7 +6,7 @@ var foo = 1; var foo = 1, bar = 2; var foo2 = function foo2() {}; -var foo3 = undefined; +var foo3; var foo4 = 2; var foo5 = undefined; var foo6 = 3; diff --git a/test/fixtures/transformation/es6-modules-system/exports-variable/expected.js b/test/fixtures/transformation/es6-modules-system/exports-variable/expected.js index c94cf23409..8626db3270 100644 --- a/test/fixtures/transformation/es6-modules-system/exports-variable/expected.js +++ b/test/fixtures/transformation/es6-modules-system/exports-variable/expected.js @@ -12,12 +12,28 @@ System.register([], function (_export) { _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; - foo = _export("foo", 1); - foo2 = _export("foo2", function foo2() {}); - foo3 = _export("foo3", undefined); - foo4 = _export("foo4", 2); - foo5 = _export("foo5", undefined); - foo6 = _export("foo6", 3); + foo = 1; + + _export("foo", foo); + + foo2 = function foo2() {}; + + _export("foo2", foo2); + + _export("foo3", foo3); + + foo4 = 2; + + _export("foo4", foo4); + + foo5 = undefined; + + _export("foo5", foo5); + + foo6 = 3; + + _export("foo6", foo6); + foo8 = _export("foo8", function foo8() { _classCallCheck(this, foo8); }); diff --git a/test/fixtures/transformation/es6-modules-system/hoist-function-exports/expected.js b/test/fixtures/transformation/es6-modules-system/hoist-function-exports/expected.js index 0e4643803d..4056aa7a5e 100644 --- a/test/fixtures/transformation/es6-modules-system/hoist-function-exports/expected.js +++ b/test/fixtures/transformation/es6-modules-system/hoist-function-exports/expected.js @@ -14,12 +14,17 @@ System.register(["./evens"], function (_export) { execute: function () { "use strict"; - p = _export("p", 5); - isOdd = _export("isOdd", (function (isEven) { + p = 5; + + _export("p", p); + + isOdd = (function (isEven) { return function (n) { return !isEven(n); }; - })(isEven)); + })(isEven); + + _export("isOdd", isOdd); } }; }); \ No newline at end of file diff --git a/test/fixtures/transformation/es6-modules-system/overview/expected.js b/test/fixtures/transformation/es6-modules-system/overview/expected.js index c8e042ef50..b4b80d7d38 100644 --- a/test/fixtures/transformation/es6-modules-system/overview/expected.js +++ b/test/fixtures/transformation/es6-modules-system/overview/expected.js @@ -12,7 +12,9 @@ System.register(["foo", "foo-bar", "./directory/foo-bar"], function (_export) { _export("test", test); - test2 = _export("test2", 5); + test2 = 5; + + _export("test2", test2); _export("default", test); } diff --git a/test/fixtures/transformation/es6-modules-system/remap/expected.js b/test/fixtures/transformation/es6-modules-system/remap/expected.js index 16a16011c6..3407e14ed9 100644 --- a/test/fixtures/transformation/es6-modules-system/remap/expected.js +++ b/test/fixtures/transformation/es6-modules-system/remap/expected.js @@ -5,7 +5,9 @@ System.register([], function (_export) { execute: function () { "use strict"; - test = _export("test", 2); + test = 2; + + _export("test", test); _export("test", test = 5); _export("test", test += 1); diff --git a/test/fixtures/transformation/es6-modules-umd/exports-variable/expected.js b/test/fixtures/transformation/es6-modules-umd/exports-variable/expected.js index f162e35935..f504a01385 100644 --- a/test/fixtures/transformation/es6-modules-umd/exports-variable/expected.js +++ b/test/fixtures/transformation/es6-modules-umd/exports-variable/expected.js @@ -10,14 +10,22 @@ var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; exports.foo7 = foo7; - var foo = exports.foo = 1; - var foo = exports.foo = 1; - var bar = exports.bar = 2; - var foo2 = exports.foo2 = function foo2() {}; - var foo3 = exports.foo3 = undefined; - var foo4 = exports.foo4 = 2; - var foo5 = exports.foo5 = undefined; - var foo6 = exports.foo6 = 3; + var foo = 1; + exports.foo = foo; + var foo = 1, + bar = 2; + exports.foo = foo; + exports.bar = bar; + var foo2 = function foo2() {}; + exports.foo2 = foo2; + var foo3; + exports.foo3 = foo3; + var foo4 = 2; + exports.foo4 = foo4; + var foo5 = undefined; + exports.foo5 = foo5; + var foo6 = 3; + exports.foo6 = foo6; function foo7() {} diff --git a/test/fixtures/transformation/es6-modules-umd/hoist-function-exports/expected.js b/test/fixtures/transformation/es6-modules-umd/hoist-function-exports/expected.js index cf7ce81abb..162cc9f711 100644 --- a/test/fixtures/transformation/es6-modules-umd/hoist-function-exports/expected.js +++ b/test/fixtures/transformation/es6-modules-umd/hoist-function-exports/expected.js @@ -14,11 +14,12 @@ return isEven(n) ? n + 1 : n + 2; } - var isOdd = exports.isOdd = (function (isEven) { + var isOdd = (function (isEven) { return function (n) { return !isEven(n); }; })(isEven); + exports.isOdd = isOdd; Object.defineProperty(exports, "__esModule", { value: true }); diff --git a/test/fixtures/transformation/es6-modules-umd/overview/expected.js b/test/fixtures/transformation/es6-modules-umd/overview/expected.js index 8f0262a8ee..d5c3640494 100644 --- a/test/fixtures/transformation/es6-modules-umd/overview/expected.js +++ b/test/fixtures/transformation/es6-modules-umd/overview/expected.js @@ -15,8 +15,9 @@ var bar = _foo.bar; var bar2 = _foo.foo; exports.test = test; - var test2 = exports.test2 = 5; + var test2 = 5; + exports.test2 = test2; exports["default"] = test; Object.defineProperty(exports, "__esModule", { value: true diff --git a/test/fixtures/transformation/es6-modules-umd/remap/expected.js b/test/fixtures/transformation/es6-modules-umd/remap/expected.js index e44f8e70c5..a8acb3f535 100644 --- a/test/fixtures/transformation/es6-modules-umd/remap/expected.js +++ b/test/fixtures/transformation/es6-modules-umd/remap/expected.js @@ -7,7 +7,8 @@ })(function (exports) { "use strict"; - var test = exports.test = 2; + var test = 2; + exports.test = test; test = exports.test = 5; test = exports.test += 1; diff --git a/test/fixtures/transformation/runtime/full/expected.js b/test/fixtures/transformation/runtime/full/expected.js index 4c824766ad..307aab4110 100644 --- a/test/fixtures/transformation/runtime/full/expected.js +++ b/test/fixtures/transformation/runtime/full/expected.js @@ -28,7 +28,8 @@ var foo = _babelHelpers.interopRequire(_someModule); var bar = _babelHelpers.interopRequireWildcard(_someModule); -var myWord = exports.myWord = _core.Symbol("abc"); +var myWord = _core.Symbol("abc"); +exports.myWord = myWord; Object.defineProperty(exports, "__esModule", { value: true }); \ No newline at end of file