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

Avoid duplicating impure expressions in object rest destructuring #5151

Merged
merged 2 commits into from Jan 19, 2017

Conversation

erikdesjardins
Copy link
Contributor

@erikdesjardins erikdesjardins commented Jan 18, 2017

Q A
Patch: Bug Fix? yes
Major: Breaking Change? no
Minor: New Feature? no
Deprecations? no
Spec Compliancy? no
Tests Added/Pass? yes
Fixed Tickets fixes #4904
License MIT
Doc PR no
Dependency Changes no

There were two main issues here (fixed in the first commit):

  1. path.traverse was called instead of path.get("id").traverse in the VariableDeclarator visitor. This means that in const { a } = foo(({ ...x }) => {/* ... */}), the visitor visiting { a } = ... would traverse down to the ...x RestProperty, causing unexpected behaviour.

  2. The VariableDeclarator's init expression was duplicated directly, which is unsafe if it may have side effects, e.g. in const { x, ...y } = foo();

Edit: I found a few more (will push fixes to this branch fixed in the second commit)

  1. Variables may be reordered incorrectly (repl), e.g.
const { x, ...y } = a,
      z = foo(y);

becomes

const { x } = a,
      z = foo(y);

const y = _objectWithoutProperties(a, ["x"]);
  1. The entire VariableDeclaration is removed when all properties are rest properties, killing any other variables (repl), e.g.
const { ...y } = a,
      z = foo(y);

becomes

const y = _objectWithoutProperties(a, []);

@mention-bot
Copy link

@erikdesjardins, thanks for your PR! By analyzing the history of the files in this pull request, we identified @hzoo and @christophehurpeau to be potential reviewers.

@codecov-io
Copy link

codecov-io commented Jan 18, 2017

Current coverage is 89.23% (diff: 100%)

No coverage report found for master at 3871236.

Powered by Codecov. Last update 3871236...433eade

…fix two issues:

1. inserting an additional VariableDeclaration after the current one may change order of operations, which is unsafe if a future VariableDeclarator refers to a destructured variable.

2. The entire VariableDeclaration is removed when all properties are rest properties, indiscriminately removing other variables
@hzoo hzoo added the PR: Bug Fix 🐛 A type of pull request used for our changelog categories label Jan 18, 2017
@hzoo
Copy link
Member

hzoo commented Jan 18, 2017

Nice work @erikdesjardins, very clear to me!

@erikdesjardins
Copy link
Contributor Author

erikdesjardins commented Jan 18, 2017

@hzoo Thanks! Just to make sure you've seen it (I'm about to push after tests pass):

Edit: I found a few more (will push fixes to this branch)

@erikdesjardins
Copy link
Contributor Author

Alright, should be good now.

The second commit changes a lot of test expectations, but it's just replacing

var a = ...;
var b = ...;

with

var a = ...,
    b = ...;

@hzoo
Copy link
Member

hzoo commented Jan 18, 2017

Ah yeah both an optimization and also prevents those issues you mention

@hzoo hzoo merged commit bca170a into babel:master Jan 19, 2017
@hzoo
Copy link
Member

hzoo commented Jan 19, 2017

lgtm, nice work!

@erikdesjardins erikdesjardins deleted the tsfm-obj-rest-spr-dup branch January 19, 2017 03:27
@hzoo
Copy link
Member

hzoo commented Jan 20, 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.

transform-object-rest-spread@6.19.0 duplicates function calls
4 participants