From 464df13c692df409c9c9869762b477c57ea07f90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Wed, 22 Nov 2017 22:28:37 +0100 Subject: [PATCH] Allow yielding an arrow function withour parens around the param (#6877) --- packages/babylon/src/parser/expression.js | 18 +- .../fixtures/es2015/regression/6864/actual.js | 5 + .../es2015/regression/6864/expected.json | 358 ++++++++++++++++++ 3 files changed, 378 insertions(+), 3 deletions(-) create mode 100644 packages/babylon/test/fixtures/es2015/regression/6864/actual.js create mode 100644 packages/babylon/test/fixtures/es2015/regression/6864/expected.json diff --git a/packages/babylon/src/parser/expression.js b/packages/babylon/src/parser/expression.js index f4a39379c8..97b3d9f2dd 100644 --- a/packages/babylon/src/parser/expression.js +++ b/packages/babylon/src/parser/expression.js @@ -620,8 +620,12 @@ export default class ExpressionParser extends LValParser { node: N.ArrowFunctionExpression, call: N.CallExpression, ): N.ArrowFunctionExpression { + const oldYield = this.state.yieldInPossibleArrowParameters; + this.state.yieldInPossibleArrowParameters = null; this.expect(tt.arrow); - return this.parseArrowExpression(node, call.arguments, true); + this.parseArrowExpression(node, call.arguments, true); + this.state.yieldInPossibleArrowParameters = oldYield; + return node; } // Parse a no-call expression (like argument of `new` or `::` operators). @@ -715,14 +719,22 @@ export default class ExpressionParser extends LValParser { this.next(); return this.parseFunction(node, false, false, true); } else if (canBeArrow && id.name === "async" && this.match(tt.name)) { + const oldYield = this.state.yieldInPossibleArrowParameters; + this.state.yieldInPossibleArrowParameters = null; const params = [this.parseIdentifier()]; this.expect(tt.arrow); // let foo = bar => {}; - return this.parseArrowExpression(node, params, true); + this.parseArrowExpression(node, params, true); + this.state.yieldInPossibleArrowParameters = oldYield; + return node; } if (canBeArrow && !this.canInsertSemicolon() && this.eat(tt.arrow)) { - return this.parseArrowExpression(node, [id]); + const oldYield = this.state.yieldInPossibleArrowParameters; + this.state.yieldInPossibleArrowParameters = null; + this.parseArrowExpression(node, [id]); + this.state.yieldInPossibleArrowParameters = oldYield; + return node; } return id; diff --git a/packages/babylon/test/fixtures/es2015/regression/6864/actual.js b/packages/babylon/test/fixtures/es2015/regression/6864/actual.js new file mode 100644 index 0000000000..fbdc5efd95 --- /dev/null +++ b/packages/babylon/test/fixtures/es2015/regression/6864/actual.js @@ -0,0 +1,5 @@ +function *f() { + (yield a => a); + (yield async a => a); + (yield async (a) => a); +} diff --git a/packages/babylon/test/fixtures/es2015/regression/6864/expected.json b/packages/babylon/test/fixtures/es2015/regression/6864/expected.json new file mode 100644 index 0000000000..a74cc5f1dd --- /dev/null +++ b/packages/babylon/test/fixtures/es2015/regression/6864/expected.json @@ -0,0 +1,358 @@ +{ + "type": "File", + "start": 0, + "end": 85, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 85, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "sourceType": "script", + "body": [ + { + "type": "FunctionDeclaration", + "start": 0, + "end": 85, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "f" + }, + "name": "f" + }, + "generator": true, + "expression": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 14, + "end": 85, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "body": [ + { + "type": "ExpressionStatement", + "start": 18, + "end": 33, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 17 + } + }, + "expression": { + "type": "YieldExpression", + "start": 19, + "end": 31, + "loc": { + "start": { + "line": 2, + "column": 3 + }, + "end": { + "line": 2, + "column": 15 + } + }, + "delegate": false, + "argument": { + "type": "ArrowFunctionExpression", + "start": 25, + "end": 31, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 15 + } + }, + "id": null, + "generator": false, + "expression": true, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 25, + "end": 26, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 10 + }, + "identifierName": "a" + }, + "name": "a" + } + ], + "body": { + "type": "Identifier", + "start": 30, + "end": 31, + "loc": { + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 15 + }, + "identifierName": "a" + }, + "name": "a" + } + }, + "extra": { + "parenthesized": true, + "parenStart": 18 + } + } + }, + { + "type": "ExpressionStatement", + "start": 36, + "end": 57, + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 3, + "column": 23 + } + }, + "expression": { + "type": "YieldExpression", + "start": 37, + "end": 55, + "loc": { + "start": { + "line": 3, + "column": 3 + }, + "end": { + "line": 3, + "column": 21 + } + }, + "delegate": false, + "argument": { + "type": "ArrowFunctionExpression", + "start": 43, + "end": 55, + "loc": { + "start": { + "line": 3, + "column": 9 + }, + "end": { + "line": 3, + "column": 21 + } + }, + "id": null, + "generator": false, + "expression": true, + "async": true, + "params": [ + { + "type": "Identifier", + "start": 49, + "end": 50, + "loc": { + "start": { + "line": 3, + "column": 15 + }, + "end": { + "line": 3, + "column": 16 + }, + "identifierName": "a" + }, + "name": "a" + } + ], + "body": { + "type": "Identifier", + "start": 54, + "end": 55, + "loc": { + "start": { + "line": 3, + "column": 20 + }, + "end": { + "line": 3, + "column": 21 + }, + "identifierName": "a" + }, + "name": "a" + } + }, + "extra": { + "parenthesized": true, + "parenStart": 36 + } + } + }, + { + "type": "ExpressionStatement", + "start": 60, + "end": 83, + "loc": { + "start": { + "line": 4, + "column": 2 + }, + "end": { + "line": 4, + "column": 25 + } + }, + "expression": { + "type": "YieldExpression", + "start": 61, + "end": 81, + "loc": { + "start": { + "line": 4, + "column": 3 + }, + "end": { + "line": 4, + "column": 23 + } + }, + "delegate": false, + "argument": { + "type": "ArrowFunctionExpression", + "start": 67, + "end": 81, + "loc": { + "start": { + "line": 4, + "column": 9 + }, + "end": { + "line": 4, + "column": 23 + } + }, + "id": null, + "generator": false, + "expression": true, + "async": true, + "params": [ + { + "type": "Identifier", + "start": 74, + "end": 75, + "loc": { + "start": { + "line": 4, + "column": 16 + }, + "end": { + "line": 4, + "column": 17 + }, + "identifierName": "a" + }, + "name": "a" + } + ], + "body": { + "type": "Identifier", + "start": 80, + "end": 81, + "loc": { + "start": { + "line": 4, + "column": 22 + }, + "end": { + "line": 4, + "column": 23 + }, + "identifierName": "a" + }, + "name": "a" + } + }, + "extra": { + "parenthesized": true, + "parenStart": 60 + } + } + } + ], + "directives": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file