From 020db6381a5200e14bee191b52f16ab396ef81ff Mon Sep 17 00:00:00 2001 From: Adrian Heine Date: Fri, 27 Oct 2017 13:42:44 +0200 Subject: [PATCH] Simplify parseBindingAtom 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. --- src/lval.js | 28 ++++++++++++---------------- test/tests-harmony.js | 12 ++++++------ 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/src/lval.js b/src/lval.js index 6d7dfed50..1c47574ef 100644 --- a/src/lval.js +++ b/src/lval.js @@ -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) { diff --git a/test/tests-harmony.js b/test/tests-harmony.js index f2307577b..48034a7a2 100644 --- a/test/tests-harmony.js +++ b/test/tests-harmony.js @@ -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}); @@ -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});