From 584532cc2ceae7f096543c0fffcad4c8ece56032 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alu=C3=ADsio=20Augusto=20Silva=20Gon=C3=A7alves?= Date: Mon, 6 Apr 2015 09:10:44 -0300 Subject: [PATCH 1/2] [ES7] Trailing comma in function parameter list Currenly a stage 1 proposal. See https://github.com/jeffmo/es-trailing-function-commas. --- src/acorn/src/expression.js | 2 +- src/acorn/src/statement.js | 2 +- .../transformers/es7/trailing-function-commas.js | 7 +++++++ src/babel/transformation/transformers/index.js | 1 + .../es7.trailing-function-commas/call/actual.js | 4 ++++ .../es7.trailing-function-commas/call/expected.js | 3 +++ .../es7.trailing-function-commas/declaration/actual.js | 8 ++++++++ .../es7.trailing-function-commas/declaration/expected.js | 5 +++++ .../es7.trailing-function-commas/options.json | 3 +++ 9 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 src/babel/transformation/transformers/es7/trailing-function-commas.js create mode 100644 test/core/fixtures/transformation/es7.trailing-function-commas/call/actual.js create mode 100644 test/core/fixtures/transformation/es7.trailing-function-commas/call/expected.js create mode 100644 test/core/fixtures/transformation/es7.trailing-function-commas/declaration/actual.js create mode 100644 test/core/fixtures/transformation/es7.trailing-function-commas/declaration/expected.js create mode 100644 test/core/fixtures/transformation/es7.trailing-function-commas/options.json diff --git a/src/acorn/src/expression.js b/src/acorn/src/expression.js index d1d0b7457a..7662b4cdba 100755 --- a/src/acorn/src/expression.js +++ b/src/acorn/src/expression.js @@ -224,7 +224,7 @@ pp.parseSubscripts = function(base, start, noCalls) { } else if (!noCalls && this.eat(tt.parenL)) { let node = this.startNodeAt(start) node.callee = base - node.arguments = this.parseExprList(tt.parenR, false) + node.arguments = this.parseExprList(tt.parenR, this.options.features["es7.trailingFunctionCommas"]) return this.parseSubscripts(this.finishNode(node, "CallExpression"), start, noCalls) } else if (this.type === tt.backQuote) { let node = this.startNodeAt(start) diff --git a/src/acorn/src/statement.js b/src/acorn/src/statement.js index 08ae517d3d..2a73536bca 100755 --- a/src/acorn/src/statement.js +++ b/src/acorn/src/statement.js @@ -458,7 +458,7 @@ pp.parseFunction = function(node, isStatement, allowExpressionBody, isAsync) { pp.parseFunctionParams = function(node) { this.expect(tt.parenL) - node.params = this.parseBindingList(tt.parenR, false, false) + node.params = this.parseBindingList(tt.parenR, false, this.options.features["es7.trailingFunctionCommas"]) } // Parse a class declaration or literal (depending on the diff --git a/src/babel/transformation/transformers/es7/trailing-function-commas.js b/src/babel/transformation/transformers/es7/trailing-function-commas.js new file mode 100644 index 0000000000..486130cebe --- /dev/null +++ b/src/babel/transformation/transformers/es7/trailing-function-commas.js @@ -0,0 +1,7 @@ +export var metadata = { + stage: 1 +}; + +export function check() { + return false; +} diff --git a/src/babel/transformation/transformers/index.js b/src/babel/transformation/transformers/index.js index 42ccc1d6b5..5d94722f50 100644 --- a/src/babel/transformation/transformers/index.js +++ b/src/babel/transformation/transformers/index.js @@ -1,5 +1,6 @@ export default { "es7.classProperties": require("./es7/class-properties"), + "es7.trailingFunctionCommas": require("./es7/trailing-function-commas"), "es7.asyncFunctions": require("./es7/async-functions"), "es7.decorators": require("./es7/decorators"), diff --git a/test/core/fixtures/transformation/es7.trailing-function-commas/call/actual.js b/test/core/fixtures/transformation/es7.trailing-function-commas/call/actual.js new file mode 100644 index 0000000000..a00f52d4c6 --- /dev/null +++ b/test/core/fixtures/transformation/es7.trailing-function-commas/call/actual.js @@ -0,0 +1,4 @@ +Math.max(1, + 2, + 3, +); diff --git a/test/core/fixtures/transformation/es7.trailing-function-commas/call/expected.js b/test/core/fixtures/transformation/es7.trailing-function-commas/call/expected.js new file mode 100644 index 0000000000..2dd6272d57 --- /dev/null +++ b/test/core/fixtures/transformation/es7.trailing-function-commas/call/expected.js @@ -0,0 +1,3 @@ +"use strict"; + +Math.max(1, 2, 3); diff --git a/test/core/fixtures/transformation/es7.trailing-function-commas/declaration/actual.js b/test/core/fixtures/transformation/es7.trailing-function-commas/declaration/actual.js new file mode 100644 index 0000000000..eaedb39571 --- /dev/null +++ b/test/core/fixtures/transformation/es7.trailing-function-commas/declaration/actual.js @@ -0,0 +1,8 @@ +function thisIsAFunctionWithAVeryLongNameAndWayTooManyParameters( + thisIsTheFirstParameter, andThisOneIsRelatedToIt, + butNotThisOne, + andNeitherThis, + inFactThereArentThatManyParameters, +) { + throw null; +} diff --git a/test/core/fixtures/transformation/es7.trailing-function-commas/declaration/expected.js b/test/core/fixtures/transformation/es7.trailing-function-commas/declaration/expected.js new file mode 100644 index 0000000000..08d7e90a5c --- /dev/null +++ b/test/core/fixtures/transformation/es7.trailing-function-commas/declaration/expected.js @@ -0,0 +1,5 @@ +"use strict"; + +function thisIsAFunctionWithAVeryLongNameAndWayTooManyParameters(thisIsTheFirstParameter, andThisOneIsRelatedToIt, butNotThisOne, andNeitherThis, inFactThereArentThatManyParameters) { + throw null; +} diff --git a/test/core/fixtures/transformation/es7.trailing-function-commas/options.json b/test/core/fixtures/transformation/es7.trailing-function-commas/options.json new file mode 100644 index 0000000000..08f3cc3d41 --- /dev/null +++ b/test/core/fixtures/transformation/es7.trailing-function-commas/options.json @@ -0,0 +1,3 @@ +{ + "optional": ["es7.trailingFunctionCommas"] +} From 6c5e0e6590efba33ceac24be9c99e6400c456b7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alu=C3=ADsio=20Augusto=20Silva=20Gon=C3=A7alves?= Date: Mon, 6 Apr 2015 09:16:39 -0300 Subject: [PATCH 2/2] Add Acorn tests for trailing function commas --- test/acorn/tests-babel.js | 96 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) diff --git a/test/acorn/tests-babel.js b/test/acorn/tests-babel.js index b46ddf9628..482d9ad0ad 100644 --- a/test/acorn/tests-babel.js +++ b/test/acorn/tests-babel.js @@ -2940,3 +2940,99 @@ test('export * as foo from "bar";', { sourceType: "module", features: { "es7.exportExtensions": true } }); + +// ES7: Trailing Function Commas + +test("log(n, '=', 2,);", { + type: "Program", + start: 0, + end: 16, + body: [{ + type: "ExpressionStatement", + start: 0, + end: 16, + expression: { + type: "CallExpression", + callee: { + type: "Identifier", + name: "log", + start: 0, + end: 3 + }, + arguments: [{ + type: "Identifier", + name: "n", + start: 4, + end: 5 + }, { + type: "Literal", + value: "=", + raw: "'='", + start: 7, + end: 10 + }, { + type: "Literal", + raw: "2", + value: 2, + start: 12, + end: 13 + }] + } + }] +}, { + ecmaVersion: 7, + features: { "es7.trailingFunctionCommas": true } +}); + +test("function log(n, op, val,) { }", { + type: "Program", + start: 0, + end: 29, + body: [{ + type: "FunctionDeclaration", + id: { + type: "Identifier", + name: "log", + start: 9, + end: 12 + }, + start: 0, + end: 29, + expression: false, + params: [{ + type: "Identifier", + name: "n", + start: 13, + end: 14 + }, { + type: "Identifier", + name: "op", + start: 16, + end: 18 + }, { + type: "Identifier", + name: "val", + start: 20, + end: 23 + }], + body: { + type: "BlockStatement", + start: 26, + end: 29, + body: [] + } + }] +}, { + ecmaVersion: 7, + features: { "es7.trailingFunctionCommas": true } +}); + +testFail("log(,);", "Unexpected token (1:4)", { + ecmaVersion: 7, + features: { "es7.trailingFunctionCommas": true } +}); + +testFail("function log(,) { }", "Unexpected token (1:13)", { + ecmaVersion: 7, + features: { "es7.trailingFunctionCommas": true } +});