Skip to content

Commit

Permalink
Allow init-less destructing bindings in for/in and for/of
Browse files Browse the repository at this point in the history
  • Loading branch information
marijnh committed Mar 20, 2015
1 parent 35c417d commit e88a543
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/statement.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,18 +364,18 @@ 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 (;;) {
let decl = this.startNode()
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
Expand Down
41 changes: 41 additions & 0 deletions test/tests-harmony.js
Original file line number Diff line number Diff line change
Expand Up @@ -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});
Expand Down

0 comments on commit e88a543

Please sign in to comment.