Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disallow let as identifier name in patterns #839

Merged
merged 2 commits into from Jun 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion acorn/src/lval.js
@@ -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
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
23 changes: 20 additions & 3 deletions test/tests-harmony.js
Expand Up @@ -13403,15 +13403,32 @@ test("var let = 1", {
]
}, {ecmaVersion: 6})

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

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

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("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 + 1", "The keyword 'let' is reserved (1:14)", {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", "let is disallowed as a lexically bound name (1:20)", {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