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 @@ -306,6 +306,15 @@ export default function () {
addTo(exports, id.node.name, id.node);
init.replaceWith(buildExportsAssignment(id.node, init.node).expression);
nonHoistedExportNames[id.node.name] = true;
} 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];
if (!t.isRestProperty(prop)) {
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't the rest property also be handled?

for example

export const {foo: test, ...tester} = {};

Copy link
Member Author

Choose a reason for hiding this comment

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

@danez Probably yes, but for now: 1) we haven't envs to use it without transform-object-rest-spread and 2) seems like we need babel-plugin-syntax-object-rest-spread for rest properties: repl

Copy link
Member

Choose a reason for hiding this comment

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

It's fine for people to have enabled support for the syntax without enabling the actual transformation, so I think it would be good to handle.

addTo(exports, prop.value.name, prop.value);
path.insertAfter(buildExportsAssignment(prop.value, prop.value));
nonHoistedExportNames[prop.value.name] = true;
}
}
} else {
// todo
}
Expand Down
@@ -0,0 +1 @@
export const { foo: bar, baz } = {};
@@ -0,0 +1,5 @@
"use strict";

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