You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Possible parsing error with destructuring assignment #23142
Passing an arrow function and a destructuring assignment as two consecutive parameters of a function causes a syntax error.
Description
The following piece of code does not make much sense (and putting the initialization of [x, y] in the 2nd parameter of map() is definitely not a good idea), but I'd expect it to work anyway:
This is a language-level issue and not node-specific.
The problem is that without braces, there is some ambiguity, since if you add them it is parsed just fine. The parser doesn't know whether the a => a is the entirety of the function and [x, y] = ... is a second argument being passed to foo or if it's a continuation of the a => a, ... function argument. By adding braces you are breaking that ambiguity: foo(a => {a, [x, y] = [2, 1]});.
FWIW this is why I personally always use braces (and semicolons to avoid that class of issues) to avoid such tricky behavior with the language.
Minimal repro: a(a=>a,[]=a)
This seems like a spec-violation to me.
Since the same behavior is seen in Chrome, if it is a bug, it is a v8 bug and has nothing to do with Node.js
@mscdex The intended behavior is that [x, y] = [2, 1] is parsed as a second argument for foo. We can force a correct parsing by adding parentheses: foo(a => a, ([x, y] = [2, 1])) (or foo(a => { return a }, ([x, y] = [2, 1])) with braces). Doing assignments within passed parameters can be considered a dubious practice, but I don't think there's anything wrong with this syntax with strict regards to the spec.
Now, this does indeed look like a V8 issue rather than a Node.js one.
Activity
mscdex commentedon Sep 28, 2018
This is a language-level issue and not node-specific.
The problem is that without braces, there is some ambiguity, since if you add them it is parsed just fine. The parser doesn't know whether the
a => ais the entirety of the function and[x, y] = ...is a second argument being passed tofooor if it's a continuation of thea => a, ...function argument. By adding braces you are breaking that ambiguity:foo(a => {a, [x, y] = [2, 1]});.FWIW this is why I personally always use braces (and semicolons to avoid that class of issues) to avoid such tricky behavior with the language.
Hakerh400 commentedon Sep 28, 2018
Minimal repro:
a(a=>a,[]=a)This seems like a spec-violation to me.
Since the same behavior is seen in Chrome, if it is a bug, it is a v8 bug and has nothing to do with Node.js
@ArnauldChevallier
Can you please report it to https://bugs.chromium.org/p/v8/issues/list
mscdex commentedon Sep 28, 2018
It does appear that Firefox does not have this issue.
/cc @nodejs/v8 ?
hashseed commentedon Sep 28, 2018
@gsathya
ArnauldChevallier commentedon Sep 28, 2018
@mscdex The intended behavior is that
[x, y] = [2, 1]is parsed as a second argument forfoo. We can force a correct parsing by adding parentheses:foo(a => a, ([x, y] = [2, 1]))(orfoo(a => { return a }, ([x, y] = [2, 1]))with braces). Doing assignments within passed parameters can be considered a dubious practice, but I don't think there's anything wrong with this syntax with strict regards to the spec.Now, this does indeed look like a V8 issue rather than a Node.js one.
gsathya commentedon Sep 28, 2018
Filed https://bugs.chromium.org/p/v8/issues/detail?id=8241 to track this
Hakerh400 commentedon Oct 23, 2018
Seems to be fixed in v8/v8@cd21f71
Trott commentedon Nov 24, 2018
Should we float v8/v8@cd21f71? @nodejs/v8-update
deps: V8: backport cd21f71f9cb5
deps: V8: backport cd21f71f9cb5
richardlau commentedon Jan 19, 2021
Fixed in v10.22.0 by #33862.