Allow init-less destructing bindings in for/in and for/of

This commit is contained in:
Marijn Haverbeke 2015-03-20 17:19:35 +01:00
parent 35c417d02b
commit e88a5431db
2 changed files with 44 additions and 3 deletions

View File

@ -364,7 +364,7 @@ pp.parseForIn = function(node, init) {
// Parse a list of variable declarations.
pp.parseVar = function(node, noIn, kind) {
pp.parseVar = function(node, isFor, kind) {
node.declarations = []
node.kind = kind.keyword
for (;;) {
@ -372,10 +372,10 @@ pp.parseVar = function(node, noIn, kind) {
decl.id = this.parseBindingAtom()
this.checkLVal(decl.id, true)
if (this.eat(tt.eq)) {
decl.init = this.parseMaybeAssign(noIn)
decl.init = this.parseMaybeAssign(isFor)
} else if (kind === tt._const && !(this.type === tt._in || (this.options.ecmaVersion >= 6 && this.isContextual("of")))) {
this.unexpected()
} else if (decl.id.type != "Identifier") {
} else if (decl.id.type != "Identifier" && !(isFor && (this.type === tt._in || this.isContextual("of")))) {
this.raise(this.lastTokEnd, "Complex binding patterns require an initialization value")
} else {
decl.init = null

View File

@ -15522,6 +15522,47 @@ test("[x,,] = 1", {
]
}, {ecmaVersion: 6});
test("for (var [name, value] in obj) {}", {
body: [
{
left: {
declarations: [
{
id: {
elements: [
{
name: "name",
type: "Identifier"
},
{
name: "value",
type: "Identifier"
}
],
type: "ArrayPattern"
},
init: null,
type: "VariableDeclarator"
}
],
kind: "var",
type: "VariableDeclaration"
},
right: {
name: "obj",
type: "Identifier"
},
body: {
body: [],
type: "BlockStatement"
},
type: "ForInStatement"
}
],
sourceType: "script",
type: "Program"
}, {ecmaVersion: 6})
testFail("let [x]", "Complex binding patterns require an initialization value (1:7)", {ecmaVersion: 6})
testFail("var [x]", "Complex binding patterns require an initialization value (1:7)", {ecmaVersion: 6})
testFail("var _𖫵 = 11;", "Unexpected character '𖫵' (1:5)", {ecmaVersion: 6});