Skip to content

Commit

Permalink
Merge pull request #5469 from yavorsky/fix-commonjs-destructuring
Browse files Browse the repository at this point in the history
Fix commonjs exports with destructuring.
  • Loading branch information
jridgewell committed Jun 21, 2017
2 parents 3b28bd2 + f2f226b commit 64eafad
Show file tree
Hide file tree
Showing 15 changed files with 75 additions and 5 deletions.
Expand Up @@ -15,6 +15,7 @@
"babel-plugin"
],
"devDependencies": {
"babel-helper-plugin-test-runner": "^6.24.0"
"babel-helper-plugin-test-runner": "^6.24.0",
"babel-plugin-syntax-object-rest-spread": "^6.13.0"
}
}
@@ -1,5 +1,7 @@
import { basename, extname } from "path";
import template from "babel-template";
import babelPluginTransformStrictMode from "babel-plugin-transform-strict-mode";

import * as t from "babel-types";

const buildRequire = template(`
Expand Down Expand Up @@ -127,7 +129,7 @@ export default function () {
};

return {
inherits: require("babel-plugin-transform-strict-mode"),
inherits: babelPluginTransformStrictMode,

visitor: {
ThisExpression(path, state) {
Expand Down Expand Up @@ -300,15 +302,42 @@ export default function () {
const id = decl.get("id");

const init = decl.get("init");
const exportsToInsert = [];
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);
nonHoistedExportNames[id.node.name] = true;
} else {
// todo
} 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;
}
}
path.insertAfter(exportsToInsert);
}
path.replaceWith(declaration.node);
}
Expand Down
@@ -0,0 +1 @@
export const [foo, bar = 2] = [];
@@ -0,0 +1,5 @@
"use strict";

const [foo, bar = 2] = [];
exports.foo = foo;
exports.bar = bar;
@@ -0,0 +1 @@
export const [foo, bar, ...baz] = [];
@@ -0,0 +1,6 @@
"use strict";

const [foo, bar, ...baz] = [];
exports.foo = foo;
exports.bar = bar;
exports.baz = baz;
@@ -0,0 +1 @@
export const [foo, bar] = [];
@@ -0,0 +1,5 @@
"use strict";

const [foo, bar] = [];
exports.foo = foo;
exports.bar = bar;
@@ -0,0 +1 @@
export const { foo, bar = 1 } = {};
@@ -0,0 +1,5 @@
"use strict";

const { foo, bar = 1 } = {};
exports.foo = foo;
exports.bar = bar;
@@ -0,0 +1 @@
export const { foo, ...bar } = {};
@@ -0,0 +1,5 @@
"use strict";

const { foo, ...bar } = {};
exports.foo = foo;
exports.bar = bar;
@@ -0,0 +1 @@
export const { foo: bar, baz } = {};
@@ -0,0 +1,5 @@
"use strict";

const { foo: bar, baz } = {};
exports.bar = bar;
exports.baz = baz;
@@ -1,3 +1,6 @@
{
"plugins": ["external-helpers", ["transform-es2015-modules-commonjs", { "strict": true }]]
"plugins": [
"external-helpers",
"syntax-object-rest-spread",
["transform-es2015-modules-commonjs", { "strict": true }]]
}

0 comments on commit 64eafad

Please sign in to comment.