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
Expand Up @@ -129,6 +129,10 @@ export default function () {
return {
inherits: require("babel-plugin-transform-strict-mode"),

manipulateOptions(opts, parserOpts) {
parserOpts.plugins.push("objectRestSpread");
},
Copy link
Member

Choose a reason for hiding this comment

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

Would be better to inherits from babel-plugin-syntax-object-rest-spread. It seems multiple inheritance is not possible (at least didn't found an example).

To avoid that duplicated code, we could merge the config with the syntax plugin. What do you think?

Copy link
Member Author

@yavorsky yavorsky Mar 19, 2017

Choose a reason for hiding this comment

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

@xtuc Yes! Unfortunately, I didn't find multiple inheritance too. Can you please point me if there util for merging configs?

Copy link
Member

Choose a reason for hiding this comment

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

It seems there are no merging util for configuration. I was thinking to use Object.assign.

Copy link
Member Author

@yavorsky yavorsky Mar 19, 2017

Choose a reason for hiding this comment

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

Yes, just added Object.assign with babel-plugin-transform-object-assign as devDep to support node < 4.


visitor: {
ThisExpression(path, state) {
// If other plugins run after this plugin's Program#exit handler, we allow them to
Expand Down Expand Up @@ -300,15 +304,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 } = {};
@@ -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;