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
Fix commonjs exports with destructuring. #5469
Conversation
@yavorsky, thanks for your PR! By analyzing the history of the files in this pull request, we identified @hzoo, @loganfsmyth and @benjamn to be potential reviewers. |
Hey @yavorsky! It looks like one or more of your builds have failed. I've copied the relevant info below to save you some time. |
Codecov Report
@@ Coverage Diff @@
## master #5469 +/- ##
==========================================
+ Coverage 84.49% 84.52% +0.02%
==========================================
Files 204 204
Lines 9597 9622 +25
Branches 2695 2703 +8
==========================================
+ Hits 8109 8133 +24
Misses 1001 1001
- Partials 487 488 +1
Continue to review full report at Codecov.
|
Looks good to me except that the test is failing. |
} else if (id.isObjectPattern()) { | ||
for (let i = 0; i < id.node.properties.length; i++) { | ||
const prop = id.node.properties[i]; | ||
if (!t.isRestProperty(prop)) { |
There was a problem hiding this comment.
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} = {};
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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.
@@ -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()) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
Hey @yavorsky! It looks like one or more of your builds have failed. I've copied the relevant info below to save you some time. |
Hey @yavorsky! It looks like one or more of your builds have failed. I've copied the relevant info below to save you some time. |
@@ -129,6 +129,10 @@ export default function () { | |||
return { | |||
inherits: require("babel-plugin-transform-strict-mode"), | |||
|
|||
manipulateOptions(opts, parserOpts) { | |||
parserOpts.plugins.push("objectRestSpread"); | |||
}, |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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.
Hey @yavorsky! It looks like one or more of your builds have failed. I've copied the relevant info below to save you some time. |
path.replaceWith(t.identifier("undefined")); | ||
} | ||
}, | ||
return Object.assign( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This only works by coincidence because no keys clash, no actual merging is done.
Maybe it's ok in this case, but it's still... weird 😕
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, I agree. Do you know other ways to inherit from multiple plugins?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not really 😢
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's worth to add this feature to core. Going to try today 😕
@@ -1 +0,0 @@ | |||
export const { foo, ...bar } = {}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test specifically seems like a reasonable thing to have, if you'd like to keep it as a devDep for the test.
Ping! Looks like this is done. Can it be merged? |
Fix commonjs exports with destructuring. by yavorsky · Pull Request #5469 · babel/babel babel/babel#5469
I'm also waiting this PR merged... |
Fix commonjs exports with destructuring. by yavorsky · Pull Request #5469 · babel/babel babel/babel#5469
This will also need to be ported to 7.0. |
@jridgewell yes! on it. |
Guys, will it be released under |
Original issue
While we are exporting VariableDeclaration with ObjectPattern inside using just
transform-es2015-modules-commonjs
we are loosing ExportsAssignment:In
Out
Expected
It works as expected with
transform-es2015-destructuring
:Out with transform-es2015-destructuring
Also 1 test was added.