Skip to content

Commit

Permalink
Disallow let as identifier name in patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
kaicataldo authored and marijnh committed Jun 20, 2019
1 parent 1723094 commit 0b5f7a7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
4 changes: 3 additions & 1 deletion acorn/src/lval.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {types as tt} from "./tokentype"
import {Parser} from "./state"
import {has} from "./util"
import {BIND_NONE, BIND_OUTSIDE} from "./scopeflags"
import {BIND_NONE, BIND_OUTSIDE, BIND_LEXICAL} from "./scopeflags"

const pp = Parser.prototype

Expand Down Expand Up @@ -189,6 +189,8 @@ pp.parseMaybeDefault = function(startPos, startLoc, left) {
pp.checkLVal = function(expr, bindingType = BIND_NONE, checkClashes) {
switch (expr.type) {
case "Identifier":
if (bindingType === BIND_LEXICAL && expr.name === "let")
this.raiseRecoverable(expr.start, "let is disallowed as a lexically bound name")
if (this.strict && this.reservedWordsStrictBind.test(expr.name))
this.raiseRecoverable(expr.start, (bindingType ? "Binding " : "Assigning to ") + expr.name + " in strict mode")
if (checkClashes) {
Expand Down
3 changes: 0 additions & 3 deletions acorn/src/statement.js
Original file line number Diff line number Diff line change
Expand Up @@ -501,9 +501,6 @@ pp.parseVar = function(node, isFor, kind) {
}

pp.parseVarId = function(decl, kind) {
if ((kind === "const" || kind === "let") && this.isContextual("let")) {
this.raiseRecoverable(this.start, "let is disallowed as a lexically bound name")
}
decl.id = this.parseBindingAtom()
this.checkLVal(decl.id, kind === "var" ? BIND_VAR : BIND_LEXICAL, false)
}
Expand Down
20 changes: 18 additions & 2 deletions test/tests-harmony.js
Original file line number Diff line number Diff line change
Expand Up @@ -13409,9 +13409,25 @@ testFail("let let", "let is disallowed as a lexically bound name (1:4)", {ecmaVe

testFail("const let", "let is disallowed as a lexically bound name (1:6)", {ecmaVersion: 6})

testFail("'use strict'; let let", "let is disallowed as a lexically bound name (1:18)", {ecmaVersion: 6})
testFail("let { let } = {};", "let is disallowed as a lexically bound name (1:6)", {ecmaVersion: 6})

testFail("'use strict'; const let", "let is disallowed as a lexically bound name (1:20)", {ecmaVersion: 6})
testFail("const { let } = {};", "let is disallowed as a lexically bound name (1:8)", {ecmaVersion: 6})

testFail("let [let] = [];", "let is disallowed as a lexically bound name (1:5)", {ecmaVersion: 6})

testFail("const [let] = [];", "let is disallowed as a lexically bound name (1:7)", {ecmaVersion: 6})

testFail("'use strict'; let let", "The keyword 'let' is reserved (1:18)", {ecmaVersion: 6})

testFail("'use strict'; const let", "The keyword 'let' is reserved (1:20)", {ecmaVersion: 6})

testFail("'use strict'; let { let } = {};", "The keyword 'let' is reserved (1:20)", {ecmaVersion: 6})

testFail("'use strict'; const { let } = {};", "The keyword 'let' is reserved (1:22)", {ecmaVersion: 6})

testFail("'use strict'; let [let] = [];", "The keyword 'let' is reserved (1:19)", {ecmaVersion: 6})

testFail("'use strict'; const [let] = [];", "The keyword 'let' is reserved (1:21)", {ecmaVersion: 6})

test("if (1) let\n{}", {}, {ecmaVersion: 6})

Expand Down

0 comments on commit 0b5f7a7

Please sign in to comment.