diff --git a/src/acorn/src/expression.js b/src/acorn/src/expression.js index dccf9962a8..ba94cd5249 100755 --- a/src/acorn/src/expression.js +++ b/src/acorn/src/expression.js @@ -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) diff --git a/test/acorn/tests-babel.js b/test/acorn/tests-babel.js index 03aeae4ec7..086ecf1c29 100644 --- a/test/acorn/tests-babel.js +++ b/test/acorn/tests-babel.js @@ -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 } +}); diff --git a/test/core/fixtures/transformation/es7.trailing-function-commas/arrow-function/actual.js b/test/core/fixtures/transformation/es7.trailing-function-commas/arrow-function/actual.js new file mode 100644 index 0000000000..79ea641f6c --- /dev/null +++ b/test/core/fixtures/transformation/es7.trailing-function-commas/arrow-function/actual.js @@ -0,0 +1 @@ +(x, y, ) => {}; diff --git a/test/core/fixtures/transformation/es7.trailing-function-commas/arrow-function/expected.js b/test/core/fixtures/transformation/es7.trailing-function-commas/arrow-function/expected.js new file mode 100644 index 0000000000..cf585a0a16 --- /dev/null +++ b/test/core/fixtures/transformation/es7.trailing-function-commas/arrow-function/expected.js @@ -0,0 +1 @@ +(function (x, y) {});