add support for trailing commas in arrow function parameter lists - fixes #1841
This commit is contained in:
parent
59ed7977ef
commit
e4083fbbd7
@ -432,9 +432,18 @@ pp.parseParenAndDistinguishExpression = function(start, isAsync, canBeArrow) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let innerStart = this.markPosition(), exprList = [], first = true
|
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) {
|
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) {
|
if (this.type === tt.ellipsis) {
|
||||||
let spreadNodeStart = this.markPosition()
|
let spreadNodeStart = this.markPosition()
|
||||||
spreadStart = this.start
|
spreadStart = this.start
|
||||||
@ -462,6 +471,7 @@ pp.parseParenAndDistinguishExpression = function(start, isAsync, canBeArrow) {
|
|||||||
this.unexpected(this.lastTokStart)
|
this.unexpected(this.lastTokStart)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (optionalCommaStart) this.unexpected(optionalCommaStart)
|
||||||
if (spreadStart) this.unexpected(spreadStart)
|
if (spreadStart) this.unexpected(spreadStart)
|
||||||
if (refShorthandDefaultPos.start) this.unexpected(refShorthandDefaultPos.start)
|
if (refShorthandDefaultPos.start) this.unexpected(refShorthandDefaultPos.start)
|
||||||
|
|
||||||
|
|||||||
@ -3374,6 +3374,49 @@ test("class Foo { bar(a,) { } }", {
|
|||||||
features: { "es7.trailingFunctionCommas": true }
|
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)", {
|
testFail("log(,);", "Unexpected token (1:4)", {
|
||||||
ecmaVersion: 7,
|
ecmaVersion: 7,
|
||||||
features: { "es7.trailingFunctionCommas": true }
|
features: { "es7.trailingFunctionCommas": true }
|
||||||
@ -3383,3 +3426,8 @@ testFail("function log(,) { }", "Unexpected token (1:13)", {
|
|||||||
ecmaVersion: 7,
|
ecmaVersion: 7,
|
||||||
features: { "es7.trailingFunctionCommas": true }
|
features: { "es7.trailingFunctionCommas": true }
|
||||||
});
|
});
|
||||||
|
|
||||||
|
testFail("('foo',)", "Unexpected token (1:7)", {
|
||||||
|
ecmaVersion: 7,
|
||||||
|
features: { "es7.trailingFunctionCommas": true }
|
||||||
|
});
|
||||||
|
|||||||
@ -0,0 +1 @@
|
|||||||
|
(x, y, ) => {};
|
||||||
@ -0,0 +1 @@
|
|||||||
|
(function (x, y) {});
|
||||||
Loading…
x
Reference in New Issue
Block a user