diff --git a/packages/babel-plugin-transform-es2015-modules-amd/test/fixtures/amd/exports-variable/expected.js b/packages/babel-plugin-transform-es2015-modules-amd/test/fixtures/amd/exports-variable/expected.js index 775d9692d6..2f4b91be0d 100644 --- a/packages/babel-plugin-transform-es2015-modules-amd/test/fixtures/amd/exports-variable/expected.js +++ b/packages/babel-plugin-transform-es2015-modules-amd/test/fixtures/amd/exports-variable/expected.js @@ -11,9 +11,9 @@ define(["exports"], function (exports) { var foo3 = exports.foo3 = function () {}; - var foo4 = exports.foo4 = undefined; + var foo4 = exports.foo4 = void 0; let foo5 = exports.foo5 = 2; - let foo6 = exports.foo6 = undefined; + let foo6 = exports.foo6 = void 0; const foo7 = exports.foo7 = 3; function foo8() {} @@ -21,4 +21,4 @@ define(["exports"], function (exports) { class foo9 {} exports.foo9 = foo9; -}); \ No newline at end of file +}); diff --git a/packages/babel-plugin-transform-es2015-modules-commonjs/src/index.js b/packages/babel-plugin-transform-es2015-modules-commonjs/src/index.js index 48f6f57e6b..39d2fbf8a1 100644 --- a/packages/babel-plugin-transform-es2015-modules-commonjs/src/index.js +++ b/packages/babel-plugin-transform-es2015-modules-commonjs/src/index.js @@ -372,55 +372,27 @@ export default function() { ]); nonHoistedExportNames[id.name] = true; } else if (declaration.isVariableDeclaration()) { - const declarators = declaration.get("declarations"); - for (const decl of declarators) { - const id = decl.get("id"); + const ids = declaration.getBindingIdentifierPaths(); + const exportsToInsert = []; + for (const name in ids) { + const id = ids[name]; + const { parentPath, node } = id; - const init = decl.get("init"); - const exportsToInsert = []; + addTo(exports, name, node); + nonHoistedExportNames[name] = true; - if (!init.node) init.replaceWith(t.identifier("undefined")); - - if (id.isIdentifier()) { - addTo(exports, id.node.name, id.node); - init.replaceWith( - buildExportsAssignment(id.node, init.node).expression, + if (parentPath.isVariableDeclarator()) { + const init = parentPath.get("init"); + const assignment = buildExportsAssignment( + node, + init.node || path.scope.buildUndefinedNode(), ); - nonHoistedExportNames[id.node.name] = true; - } else if (id.isObjectPattern()) { - for (let i = 0; i < id.node.properties.length; i++) { - const prop = id.node.properties[i]; - let propValue = prop.value; - if (t.isAssignmentPattern(propValue)) { - propValue = propValue.left; - } else if (t.isRestProperty(prop)) { - propValue = prop.argument; - } - addTo(exports, propValue.name, propValue); - exportsToInsert.push( - buildExportsAssignment(propValue, propValue), - ); - nonHoistedExportNames[propValue.name] = true; - } - } else if (id.isArrayPattern() && id.node.elements) { - for (let i = 0; i < id.node.elements.length; i++) { - let elem = id.node.elements[i]; - if (!elem) continue; - if (t.isAssignmentPattern(elem)) { - elem = elem.left; - } else if (t.isRestElement(elem)) { - elem = elem.argument; - } - const name = elem.name; - addTo(exports, name, elem); - exportsToInsert.push( - buildExportsAssignment(elem, elem), - ); - nonHoistedExportNames[name] = true; - } + init.replaceWith(assignment.expression); + } else { + exportsToInsert.push(buildExportsAssignment(node, node)); } - path.insertAfter(exportsToInsert); } + path.insertAfter(exportsToInsert); path.replaceWith(declaration.node); } continue; diff --git a/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/interop/exports-variable/expected.js b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/interop/exports-variable/expected.js index 022990538f..6ad52ad534 100644 --- a/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/interop/exports-variable/expected.js +++ b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/interop/exports-variable/expected.js @@ -10,13 +10,13 @@ var foo2 = exports.foo2 = 1, var foo3 = exports.foo3 = function () {}; -var foo4 = exports.foo4 = undefined; +var foo4 = exports.foo4 = void 0; let foo5 = exports.foo5 = 2; -let foo6 = exports.foo6 = undefined; +let foo6 = exports.foo6 = void 0; const foo7 = exports.foo7 = 3; function foo8() {} class foo9 {} -exports.foo9 = foo9; \ No newline at end of file +exports.foo9 = foo9; diff --git a/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/export-const-destructuring-deep/actual.js b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/export-const-destructuring-deep/actual.js new file mode 100644 index 0000000000..7687ffa221 --- /dev/null +++ b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/export-const-destructuring-deep/actual.js @@ -0,0 +1 @@ +export const { foo: { bar: [baz, qux] } } = {}; diff --git a/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/export-const-destructuring-deep/expected.js b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/export-const-destructuring-deep/expected.js new file mode 100644 index 0000000000..0c3e44bd39 --- /dev/null +++ b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/export-const-destructuring-deep/expected.js @@ -0,0 +1,9 @@ +"use strict"; + +const { + foo: { + bar: [baz, qux] + } +} = {}; +exports.baz = baz; +exports.qux = qux; \ No newline at end of file diff --git a/packages/babel-plugin-transform-es2015-modules-umd/test/fixtures/umd/exports-variable/expected.js b/packages/babel-plugin-transform-es2015-modules-umd/test/fixtures/umd/exports-variable/expected.js index f4ab3049b5..3ffb666d08 100644 --- a/packages/babel-plugin-transform-es2015-modules-umd/test/fixtures/umd/exports-variable/expected.js +++ b/packages/babel-plugin-transform-es2015-modules-umd/test/fixtures/umd/exports-variable/expected.js @@ -23,9 +23,9 @@ var foo3 = exports.foo3 = function () {}; - var foo4 = exports.foo4 = undefined; + var foo4 = exports.foo4 = void 0; let foo5 = exports.foo5 = 2; - let foo6 = exports.foo6 = undefined; + let foo6 = exports.foo6 = void 0; const foo7 = exports.foo7 = 3; function foo8() {} @@ -33,4 +33,4 @@ class foo9 {} exports.foo9 = foo9; -}); \ No newline at end of file +});