Skip to content

Commit 9acbca1

Browse files
jugglinmikeCommit bot
authored andcommitted
[es6] Fix bug in pattern re-writing
As originally implemented, a SingleNameBinding within a BindingPattern was incorrectly interpreted as an assignment if an initializer was present and that initializer was itself an AssignmentExpresion. For example: let x; { let [x = y = 1] = []; } print(x); // expected: undefined, actual: 1 Extend the heuristic that detects the "context" of a destructuring pattern to account for AssignmentExpressions within SingleNameBindings. BUG=v8:4891 LOG=N [email protected] Review URL: https://codereview.chromium.org/1859423002 Cr-Commit-Position: refs/heads/master@{#35334}
1 parent 6e281f1 commit 9acbca1

2 files changed

Lines changed: 62 additions & 1 deletion

File tree

src/parsing/pattern-rewriter.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,11 @@ bool Parser::PatternRewriter::IsBindingContext(PatternContext c) const {
7979
Parser::PatternRewriter::PatternContext
8080
Parser::PatternRewriter::SetAssignmentContextIfNeeded(Expression* node) {
8181
PatternContext old_context = context();
82-
if (node->IsAssignment() && node->AsAssignment()->op() == Token::ASSIGN) {
82+
// AssignmentExpressions may occur in the Initializer position of a
83+
// SingleNameBinding. Such expressions should not prompt a change in the
84+
// pattern's context.
85+
if (node->IsAssignment() && node->AsAssignment()->op() == Token::ASSIGN &&
86+
!IsInitializerContext()) {
8387
set_context(ASSIGNMENT);
8488
}
8589
return old_context;

test/mjsunit/es6/destructuring.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,63 @@
260260
}());
261261

262262

263+
(function TestAssignmentExprInInitializers() {
264+
{
265+
let x, y;
266+
{
267+
let { x = y = 1 } = {};
268+
assertSame(x, 1);
269+
assertSame(y, 1);
270+
}
271+
assertSame(undefined, x);
272+
assertSame(1, y);
273+
}
274+
275+
{
276+
let x, y;
277+
{
278+
let { x: x = y = 1 } = {};
279+
assertSame(1, x);
280+
assertSame(1, y);
281+
}
282+
assertSame(undefined, x);
283+
assertSame(1, y);
284+
}
285+
286+
{
287+
let x, y;
288+
{
289+
let [ x = y = 1 ] = [];
290+
assertSame(1, x);
291+
assertSame(1, y);
292+
}
293+
assertSame(undefined, x);
294+
assertSame(1, y);
295+
}
296+
297+
{
298+
let x, y;
299+
(function({ x = y = 1 }) {}({}));
300+
assertSame(undefined, x);
301+
assertSame(1, y);
302+
}
303+
304+
{
305+
let x, y;
306+
(function({ x: x = y = 1 }) {}({}));
307+
assertSame(undefined, x);
308+
assertSame(1, y);
309+
}
310+
311+
{
312+
let x, y;
313+
(function([ x = y = 1 ]) {}([]));
314+
assertSame(undefined, x);
315+
assertSame(1, y);
316+
}
317+
}());
318+
319+
263320
(function TestMultipleAccesses() {
264321
assertThrows(
265322
"'use strict';"+

0 commit comments

Comments
 (0)