add support for trailing commas in arrow function parameter lists - fixes #1841

This commit is contained in:
Sebastian McKenzie 2015-06-26 00:37:33 +01:00
parent 59ed7977ef
commit e4083fbbd7
4 changed files with 62 additions and 2 deletions

View File

@ -432,9 +432,18 @@ pp.parseParenAndDistinguishExpression = function(start, isAsync, canBeArrow) {
}
let innerStart = this.markPosition(), exprList = [], first = true
let refShorthandDefaultPos = {start: 0}, spreadStart, innerParenStart
let refShorthandDefaultPos = {start: 0}, spreadStart, innerParenStart, optionalCommaStart
while (this.type !== tt.parenR) {
first ? first = false : this.expect(tt.comma)
if (first) {
first = false;
} else {
this.expect(tt.comma)
if (this.type === tt.parenR && this.options.features["es7.trailingFunctionCommas"]) {
optionalCommaStart = this.start
break
}
}
if (this.type === tt.ellipsis) {
let spreadNodeStart = this.markPosition()
spreadStart = this.start
@ -462,6 +471,7 @@ pp.parseParenAndDistinguishExpression = function(start, isAsync, canBeArrow) {
this.unexpected(this.lastTokStart)
}
}
if (optionalCommaStart) this.unexpected(optionalCommaStart)
if (spreadStart) this.unexpected(spreadStart)
if (refShorthandDefaultPos.start) this.unexpected(refShorthandDefaultPos.start)

View File

@ -3374,6 +3374,49 @@ test("class Foo { bar(a,) { } }", {
features: { "es7.trailingFunctionCommas": true }
});
test("(x, y, ) => 1;", {
start: 0,
body: [{
start: 0,
expression: {
start: 0,
id: null,
generator: false,
expression: true,
params: [
{
start: 1,
name: "x",
type: "Identifier",
end: 2
},
{
start: 4,
name: "y",
type: "Identifier",
end: 5
}
],
body: {
start: 12,
value: 1,
raw: "1",
type: "Literal",
end: 13
},
type: "ArrowFunctionExpression",
end: 13
},
type: "ExpressionStatement",
end: 14
}],
type: "Program",
end: 14
}, {
ecmaVersion: 7,
features: { "es7.trailingFunctionCommas": true }
});
testFail("log(,);", "Unexpected token (1:4)", {
ecmaVersion: 7,
features: { "es7.trailingFunctionCommas": true }
@ -3383,3 +3426,8 @@ testFail("function log(,) { }", "Unexpected token (1:13)", {
ecmaVersion: 7,
features: { "es7.trailingFunctionCommas": true }
});
testFail("('foo',)", "Unexpected token (1:7)", {
ecmaVersion: 7,
features: { "es7.trailingFunctionCommas": true }
});

View File

@ -0,0 +1 @@
(function (x, y) {});