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

Destructuring: Fix handling of impure computed keys with object rest #9416

Merged
merged 1 commit into from Mar 27, 2019

Conversation

motiz88
Copy link
Contributor

@motiz88 motiz88 commented Jan 26, 2019

Q                       A
Fixed Issues? described in PR
Patch: Bug Fix? 👍
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

Fixes an issue in destructuring where impure computed keys in object patterns would be evaluated twice in the presence of rest properties.

// Input
var { [fn()]: x, ...y } = z;

// Old output
var _z = z,
  x = _z[fn()],
  y = _objectWithoutProperties(_z, [fn()].map(_toPropertyKey));

// New output
var _z = z,
    _fn = fn(),
    x = _z[_fn],
    y = babelHelpers.objectWithoutProperties(_z, [_fn].map(babelHelpers.toPropertyKey));

The object-rest-spread plugin handles this correctly (with an implementation that couldn't be directly copied into destructuring, unfortunately) and is the source of the exec test I've added here. I've also amended this test in both plugins to assert that the RHS expression is evaluated before the computed key expressions.

@motiz88 motiz88 force-pushed the fix-destructuring-object-rest-computed branch from 919999f to 445d99d Compare January 26, 2019 16:29
@babel-bot
Copy link
Collaborator

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/9920/

@existentialism existentialism added the PR: Bug Fix 🐛 A type of pull request used for our changelog categories label Jan 26, 2019
Copy link
Member

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

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

I'd like to merge the detructuring and object-rest plugins in the future (like I did for classes), to avoid duplicating all this logic.

@danez danez merged commit 1f5444e into babel:master Mar 27, 2019
NMinhNguyen pushed a commit to NMinhNguyen/babel-plugin-transform-destructuring that referenced this pull request Aug 9, 2019
@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Oct 4, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 4, 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.

None yet

6 participants