Skip to content

Commit 691a31b

Browse files
成仕伟marijnh
authored andcommitted
Forbid LHS in for-of loops from starting with 'let'
FIX: Implement restriction that `for`/`of` loop LHS can't start with `let`. Closes #1009
1 parent 785ec32 commit 691a31b

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

acorn/src/statement.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,14 +234,16 @@ pp.parseForStatement = function(node) {
234234
if (awaitAt > -1) this.unexpected(awaitAt)
235235
return this.parseFor(node, init)
236236
}
237+
let startsWithLet = this.isContextual("let"), isForOf = false
237238
let refDestructuringErrors = new DestructuringErrors
238239
let init = this.parseExpression(awaitAt > -1 ? "await" : true, refDestructuringErrors)
239-
if (this.type === tt._in || (this.options.ecmaVersion >= 6 && this.isContextual("of"))) {
240+
if (this.type === tt._in || (isForOf = this.options.ecmaVersion >= 6 && this.isContextual("of"))) {
240241
if (this.options.ecmaVersion >= 9) {
241242
if (this.type === tt._in) {
242243
if (awaitAt > -1) this.unexpected(awaitAt)
243244
} else node.await = awaitAt > -1
244245
}
246+
if (startsWithLet && isForOf) this.raise(init.start, "The left-hand side of a for-of loop may not start with 'let'.")
245247
this.toAssignable(init, false, refDestructuringErrors)
246248
this.checkLValPattern(init)
247249
return this.parseForIn(node, init)

test/tests.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19751,6 +19751,11 @@ testFail("for (var [x] = 0 in list) process(x);", "for-in loop variable declarat
1975119751
testFail("for (var {x} = 0 in list) process(x);", "for-in loop variable declaration may not have an initializer (1:5)", { ecmaVersion: 6 })
1975219752
testFail("for (var x = 42 in list) process(x);", "for-in loop variable declaration may not have an initializer (1:5)", { ecmaVersion: 6 })
1975319753

19754+
testFail("for (let.bar of list);", "The left-hand side of a for-of loop may not start with 'let'. (1:5)", { ecmaVersion: 6 })
19755+
testFail("for (let().bar of list);", "The left-hand side of a for-of loop may not start with 'let'. (1:5)", { ecmaVersion: 6 })
19756+
testFail("for (let``.bar of list);", "The left-hand side of a for-of loop may not start with 'let'. (1:5)", { ecmaVersion: 6 })
19757+
testFail("'use strict'; for (let in list);", "The keyword 'let' is reserved (1:19)")
19758+
1975419759
test("for (var x = 42 in list) process(x);", {
1975519760
type: "Program",
1975619761
body: [

0 commit comments

Comments
 (0)