Skip to content

Commit

Permalink
Simplify parseBindingAtom
Browse files Browse the repository at this point in the history
This way it's clearer that ES6 only adds specific features instead of
completely altering this behavior. Also, this provides better error messages
in ES6+ for keywords as lvalues.
  • Loading branch information
adrianheine authored and marijnh committed Oct 29, 2017
1 parent cedb8a4 commit 020db63
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
28 changes: 12 additions & 16 deletions src/lval.js
Expand Up @@ -110,23 +110,19 @@ pp.parseRestBinding = function() {
// Parses lvalue (assignable) atom.

pp.parseBindingAtom = function() {
if (this.options.ecmaVersion < 6) return this.parseIdent()
switch (this.type) {
case tt.name:
return this.parseIdent()

case tt.bracketL:
let node = this.startNode()
this.next()
node.elements = this.parseBindingList(tt.bracketR, true, true)
return this.finishNode(node, "ArrayPattern")

case tt.braceL:
return this.parseObj(true)

default:
this.unexpected()
if (this.options.ecmaVersion >= 6) {
switch (this.type) {
case tt.bracketL:
let node = this.startNode()
this.next()
node.elements = this.parseBindingList(tt.bracketR, true, true)
return this.finishNode(node, "ArrayPattern")

case tt.braceL:
return this.parseObj(true)
}
}
return this.parseIdent()
}

pp.parseBindingList = function(close, allowEmpty, allowTrailingComma) {
Expand Down
12 changes: 6 additions & 6 deletions test/tests-harmony.js
Expand Up @@ -12692,13 +12692,13 @@ testFail("function hello() {'use strict'; ({ i: 10, s(eval) { } }); }", "Binding

testFail("function a() { \"use strict\"; ({ b(t, t) { } }); }", "Argument name clash (1:37)", {ecmaVersion: 6});

testFail("var super", "Unexpected token (1:4)", {ecmaVersion: 6});
testFail("var super", "Unexpected keyword 'super' (1:4)", {ecmaVersion: 6});

testFail("var default", "Unexpected token (1:4)", {ecmaVersion: 6});
testFail("var default", "Unexpected keyword 'default' (1:4)", {ecmaVersion: 6});

testFail("let default", "Unexpected token (1:4)", {ecmaVersion: 6});

testFail("const default", "Unexpected token (1:6)", {ecmaVersion: 6});
testFail("const default", "Unexpected keyword 'default' (1:6)", {ecmaVersion: 6});

testFail("\"use strict\"; ({ v: eval } = obj)", "Assigning to eval in strict mode (1:20)", {ecmaVersion: 6});

Expand Down Expand Up @@ -12750,10 +12750,10 @@ testFail("yield 10", "Unexpected token (1:6)", {ecmaVersion: 6});

testFail("void { [1, 2]: 3 };", "Unexpected token (1:9)", {ecmaVersion: 6});

testFail("let [this] = [10]", "Unexpected token (1:5)", {ecmaVersion: 6});
testFail("let [this] = [10]", "Unexpected keyword 'this' (1:5)", {ecmaVersion: 6});
testFail("let {this} = x", "Unexpected keyword 'this' (1:5)", {ecmaVersion: 6});
testFail("let [function] = [10]", "Unexpected token (1:5)", {ecmaVersion: 6});
testFail("let [function] = x", "Unexpected token (1:5)", {ecmaVersion: 6});
testFail("let [function] = [10]", "Unexpected keyword 'function' (1:5)", {ecmaVersion: 6});
testFail("let [function] = x", "Unexpected keyword 'function' (1:5)", {ecmaVersion: 6});
testFail("([function] = [10])", "Unexpected token (1:10)", {ecmaVersion: 6});
testFail("([this] = [10])", "Assigning to rvalue (1:2)", {ecmaVersion: 6});
testFail("({this} = x)", "Unexpected keyword 'this' (1:2)", {ecmaVersion: 6});
Expand Down

0 comments on commit 020db63

Please sign in to comment.