-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Fix destructuring evaluation order for initializers #41094
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
Conversation
Thanks for working on this! I'm excited that this is going to be fixed. FWIW it could be useful to double-check this PR with the fuzzer I wrote: https://github.com/evanw/js-destructuring-fuzzer. For example, I just ran this branch through the fuzzer and I think there are still cases that result in generating code with syntax errors. Here's one such case: let a, b, c = {x: {a: 1, y: 2}};
({x: {a, ...b} = foo} = c);
console.log(a, b); That should print out var _a;
var a, b, c = { x: { a: 1, y: 2 } };
((_a = foo.a, foo, b = __rest(foo, ["a"]), foo) = c.x);
console.log(a, b); |
@evanw: Thanks, I'll take a look at that |
@weswigham @sandersn, either of you have a moment to spare to take a look? |
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 emit looks OK (it's admittedly dense), but should we also be testing that control for narrowing reflects this order of execution as well? (Eg, replace order(0)
with (x, x = 0)
and so on)
Looks like control-flow is wrong as well: Case 1
Case 2Here Cases 3 & 4We also don't seem to narrow when we know the default won't be evaluated: In both cases above, Case 5In this case Case 6This case should probably be considered correct. |
03bd565
to
8a15291
Compare
I've updated the control flow for destructuring, though I've decided to leave cases 3 & 4 as is (you will still end up with the union of the assigned value and the default value). |
@weswigham can you take a look at the binder changes? |
This ensures we correctly follow the order of evaluation for destructuring (initializers are evaluated before computed property names in an object pattern), per the following algorithms (emphasis added):
12.15.7.5 - Runtime Semantics: IteratorDestructuringAssignmentEvaluation
12.15.7.6 Runtime Semantics: KeyedDestructuringAssignmentEvaluation
13.3.3.8 - Runtime Semantics: IteratorBindingInitialization
13.3.3.9 Runtime Semantics: KeyedBindingInitialization
Fixes #39205
Fixes #39181