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

Conversation

yavorsky
Copy link
Member

Q A
Patch: Bug Fix? y
Major: Breaking Change? n
Minor: New Feature? n
Deprecations? n
Spec Compliancy? y
Tests Added/Pass? y
Fixed Tickets Fixes babel/babel-preset-env#193
License MIT
Doc PR
Dependency Changes

Original issue
While we are exporting VariableDeclaration with ObjectPattern inside using just transform-es2015-modules-commonjs we are loosing ExportsAssignment:
In

export const { baz } = {};

Out

const { baz } = {};

Expected

const { baz } = {};
exports.baz = baz;

It works as expected with transform-es2015-destructuring:
Out with transform-es2015-destructuring

var _ref = {};
const baz = _ref.baz;
exports.baz = baz;

Also 1 test was added.

@mention-bot
Copy link

@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.

@babel-bot
Copy link
Collaborator

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
Copy link

codecov bot commented Mar 15, 2017

Codecov Report

Merging #5469 into master will increase coverage by 0.02%.
The diff coverage is 91.3%.

@@            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
Impacted Files Coverage Δ
...gin-transform-es2015-modules-commonjs/src/index.js 94.6% <91.3%> (+0.11%) ⬆️
packages/babel-traverse/src/visitors.js 85.71% <0%> (-0.96%) ⬇️
packages/babel-types/src/validators.js 84.95% <0%> (+0.27%) ⬆️
...bel-plugin-transform-es2015-classes/src/vanilla.js 90.59% <0%> (+0.42%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 71e4336...f2f226b. Read the comment docs.

@existentialism existentialism added the PR: Bug Fix 🐛 A type of pull request used for our changelog categories label Mar 16, 2017
@aaronang
Copy link
Member

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)) {
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.

@@ -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

@babel-bot
Copy link
Collaborator

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.

@babel-bot
Copy link
Collaborator

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");
},
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.

@babel-bot
Copy link
Collaborator

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(
Copy link
Member

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 😕

Copy link
Member Author

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?

Copy link
Member

Choose a reason for hiding this comment

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

Not really 😢

Copy link
Member Author

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 } = {};
Copy link
Member

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.

@idris
Copy link

idris commented Jun 8, 2017

Ping! Looks like this is done. Can it be merged?

mpyw added a commit to moducks/moducks that referenced this pull request Jun 21, 2017
Fix commonjs exports with destructuring. by yavorsky · Pull Request #5469 · babel/babel
babel/babel#5469
@mpyw
Copy link

mpyw commented Jun 21, 2017

I'm also waiting this PR merged...

moducks/moducks@0d3a92d

ghost pushed a commit to moducks/moducks that referenced this pull request Jun 21, 2017
Fix commonjs exports with destructuring. by yavorsky · Pull Request #5469 · babel/babel
babel/babel#5469
@jridgewell
Copy link
Member

This will also need to be ported to 7.0.

@yavorsky
Copy link
Member Author

@jridgewell yes! on it.

@Yeti-or
Copy link

Yeti-or commented Jul 24, 2017

Guys, will it be released under 6.x ?

mpyw added a commit to moducks/moducks that referenced this pull request Sep 4, 2017
@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Oct 6, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 6, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: Bug Fix 🐛 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

export const {variable} = otherVariable