Commit 9acbca18 authored by mike's avatar mike Committed by Commit bot

[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
R=adamk@chromium.org

Review URL: https://codereview.chromium.org/1859423002

Cr-Commit-Position: refs/heads/master@{#35334}
parent 6e281f1c
......@@ -79,7 +79,11 @@ bool Parser::PatternRewriter::IsBindingContext(PatternContext c) const {
Parser::PatternRewriter::PatternContext
Parser::PatternRewriter::SetAssignmentContextIfNeeded(Expression* node) {
PatternContext old_context = context();
if (node->IsAssignment() && node->AsAssignment()->op() == Token::ASSIGN) {
// AssignmentExpressions may occur in the Initializer position of a
// SingleNameBinding. Such expressions should not prompt a change in the
// pattern's context.
if (node->IsAssignment() && node->AsAssignment()->op() == Token::ASSIGN &&
!IsInitializerContext()) {
set_context(ASSIGNMENT);
}
return old_context;
......
......@@ -260,6 +260,63 @@
}());
(function TestAssignmentExprInInitializers() {
{
let x, y;
{
let { x = y = 1 } = {};
assertSame(x, 1);
assertSame(y, 1);
}
assertSame(undefined, x);
assertSame(1, y);
}
{
let x, y;
{
let { x: x = y = 1 } = {};
assertSame(1, x);
assertSame(1, y);
}
assertSame(undefined, x);
assertSame(1, y);
}
{
let x, y;
{
let [ x = y = 1 ] = [];
assertSame(1, x);
assertSame(1, y);
}
assertSame(undefined, x);
assertSame(1, y);
}
{
let x, y;
(function({ x = y = 1 }) {}({}));
assertSame(undefined, x);
assertSame(1, y);
}
{
let x, y;
(function({ x: x = y = 1 }) {}({}));
assertSame(undefined, x);
assertSame(1, y);
}
{
let x, y;
(function([ x = y = 1 ]) {}([]));
assertSame(undefined, x);
assertSame(1, y);
}
}());
(function TestMultipleAccesses() {
assertThrows(
"'use strict';"+
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment