Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix commonjs exports with destructuring. #5469

Merged
merged 13 commits into from Jun 21, 2017
@@ -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()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we be handling ArrayPattern too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@loganfsmyth Sure! Going to add. Thanks!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could the ArrayPattern be a separate PR?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xtuc I've started work on it but could pick it out to a separate PR

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, baz } = {};
@@ -0,0 +1,5 @@
"use strict";

const { foo: bar, baz } = {};
exports.bar = bar;
exports.baz = baz;