Allow trailing commas in array patterns

Closes #975
This commit is contained in:
Marijn Haverbeke 2015-03-12 22:39:00 +01:00
parent 811d19ab76
commit 16a342082a
2 changed files with 116 additions and 7 deletions

View File

@ -1605,7 +1605,7 @@
case tt.bracketL: case tt.bracketL:
var node = this.startNode(); var node = this.startNode();
this.next(); this.next();
node.elements = this.parseBindingList(tt.bracketR, true); node.elements = this.parseBindingList(tt.bracketR, true, true);
return this.finishNode(node, "ArrayPattern"); return this.finishNode(node, "ArrayPattern");
case tt.braceL: case tt.braceL:
@ -1616,16 +1616,22 @@
} }
}; };
pp.parseBindingList = function(close, allowEmpty) { pp.parseBindingList = function(close, allowEmpty, allowTrailingComma) {
var elts = [], first = true; var elts = [], first = true;
while (!this.eat(close)) { while (!this.eat(close)) {
first ? first = false : this.expect(tt.comma); if (first) first = false;
if (this.type === tt.ellipsis) { else this.expect(tt.comma);
if (allowEmpty && this.type === tt.comma) {
elts.push(null);
} else if (allowTrailingComma && this.afterTrailingComma(close)) {
break;
} else if (this.type === tt.ellipsis) {
elts.push(this.parseRest()); elts.push(this.parseRest());
this.expect(close); this.expect(close);
break; break;
} else {
elts.push(this.parseMaybeDefault());
} }
elts.push(allowEmpty && this.type === tt.comma ? null : this.parseMaybeDefault());
} }
return elts; return elts;
}; };
@ -2567,7 +2573,7 @@
node.id = this.parseIdent(); node.id = this.parseIdent();
} }
this.expect(tt.parenL); this.expect(tt.parenL);
node.params = this.parseBindingList(tt.parenR, false); node.params = this.parseBindingList(tt.parenR, false, false);
this.parseFunctionBody(node, allowExpressionBody); this.parseFunctionBody(node, allowExpressionBody);
return this.finishNode(node, isStatement ? "FunctionDeclaration" : "FunctionExpression"); return this.finishNode(node, isStatement ? "FunctionDeclaration" : "FunctionExpression");
}; };
@ -2578,7 +2584,7 @@
var node = this.startNode(); var node = this.startNode();
this.initFunction(node); this.initFunction(node);
this.expect(tt.parenL); this.expect(tt.parenL);
node.params = this.parseBindingList(tt.parenR, false); node.params = this.parseBindingList(tt.parenR, false, false);
var allowExpressionBody; var allowExpressionBody;
if (this.options.ecmaVersion >= 6) { if (this.options.ecmaVersion >= 6) {
node.generator = isGenerator; node.generator = isGenerator;

View File

@ -15263,6 +15263,109 @@ test("var _\\u{104A6} = 10;", {
] ]
}, {ecmaVersion: 6}); }, {ecmaVersion: 6});
test("let [x,] = [1]", {
"start": 0,
"body": [
{
"start": 0,
"declarations": [
{
"start": 4,
"id": {
"start": 4,
"elements": [
{
"start": 5,
"name": "x",
"type": "Identifier",
"end": 6
}
],
"type": "ArrayPattern",
"end": 8
},
"init": {
"start": 11,
"elements": [
{
"start": 12,
"value": 1,
"raw": "1",
"type": "Literal",
"end": 13
}
],
"type": "ArrayExpression",
"end": 14
},
"type": "VariableDeclarator",
"end": 14
}
],
"kind": "let",
"type": "VariableDeclaration",
"end": 14
}
],
"type": "Program",
"end": 14
}, {ecmaVersion: 6});
test("let {x} = y", {
"start": 0,
"body": [
{
"start": 0,
"declarations": [
{
"start": 4,
"id": {
"start": 4,
"properties": [
{
"start": 5,
"method": false,
"shorthand": true,
"computed": false,
"key": {
"start": 5,
"name": "x",
"type": "Identifier",
"end": 6
},
"kind": "init",
"value": {
"start": 5,
"name": "x",
"type": "Identifier",
"end": 6
},
"type": "Property",
"end": 6
}
],
"type": "ObjectPattern",
"end": 7
},
"init": {
"start": 10,
"name": "y",
"type": "Identifier",
"end": 11
},
"type": "VariableDeclarator",
"end": 11
}
],
"kind": "let",
"type": "VariableDeclaration",
"end": 11
}
],
"type": "Program",
"end": 11
}, {ecmaVersion: 6})
testFail("var _𖫵 = 11;", "Unexpected character '𖫵' (1:5)", {ecmaVersion: 6}); testFail("var _𖫵 = 11;", "Unexpected character '𖫵' (1:5)", {ecmaVersion: 6});
testFail("var 𫠞_ = 12;", "Unexpected character '𫠞' (1:4)", {ecmaVersion: 6}); testFail("var 𫠞_ = 12;", "Unexpected character '𫠞' (1:4)", {ecmaVersion: 6});
testFail("var 𫠝_ = 10;", "Unexpected character '𫠝' (1:4)", {ecmaVersion: 5}); testFail("var 𫠝_ = 10;", "Unexpected character '𫠝' (1:4)", {ecmaVersion: 5});