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
Conversation
@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. |
f730d9d
to
4a328b1
Compare
4a328b1
to
8b6b8ee
Compare
Current coverage is 89.23% (diff: 100%)
|
…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
Nice work @erikdesjardins, very clear to me! |
@hzoo Thanks! Just to make sure you've seen it (I'm about to push after tests pass):
|
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 = ...; |
Ah yeah both an optimization and also prevents those issues you mention |
lgtm, nice work! |
There were two main issues here (fixed in the first commit):
path.traverse
was called instead ofpath.get("id").traverse
in theVariableDeclarator
visitor. This means that inconst { a } = foo(({ ...x }) => {/* ... */})
, the visitor visiting{ a } = ...
would traverse down to the...x
RestProperty
, causing unexpected behaviour.The
VariableDeclarator
'sinit
expression was duplicated directly, which is unsafe if it may have side effects, e.g. inconst { x, ...y } = foo();
Edit: I found a few more (
will push fixes to this branchfixed in the second commit)becomes
VariableDeclaration
is removed when all properties are rest properties, killing any other variables (repl), e.g.becomes