From 732200147a695b5fee1147936e53c2c183f49f06 Mon Sep 17 00:00:00 2001 From: Daniel Tschinder Date: Tue, 31 Jan 2017 18:02:59 +0100 Subject: [PATCH] Fix #336 by correctly setting arrow-param --- src/parser/expression.js | 6 +- src/plugins/flow.js | 4 +- .../flow/regression/issue-336/actual.js | 1 + .../flow/regression/issue-336/expected.json | 249 ++++++++++++++++++ 4 files changed, 255 insertions(+), 5 deletions(-) create mode 100644 test/fixtures/flow/regression/issue-336/actual.js create mode 100644 test/fixtures/flow/regression/issue-336/expected.json diff --git a/src/parser/expression.js b/src/parser/expression.js index df90d47f0a..0c435bb64a 100644 --- a/src/parser/expression.js +++ b/src/parser/expression.js @@ -343,7 +343,7 @@ pp.parseCallExpressionArguments = function (close, possibleAsyncArrow) { innerParenStart = this.state.start; } - elts.push(this.parseExprListItem(undefined, possibleAsyncArrow ? { start: 0 } : undefined)); + elts.push(this.parseExprListItem(false, possibleAsyncArrow ? { start: 0 } : undefined, possibleAsyncArrow ? { start: 0 } : undefined)); } // we found an async arrow function so let's not allow any inner parens @@ -1021,14 +1021,14 @@ pp.parseExprList = function (close, allowEmpty, refShorthandDefaultPos) { return elts; }; -pp.parseExprListItem = function (allowEmpty, refShorthandDefaultPos) { +pp.parseExprListItem = function (allowEmpty, refShorthandDefaultPos, refNeedsArrowPos) { let elt; if (allowEmpty && this.match(tt.comma)) { elt = null; } else if (this.match(tt.ellipsis)) { elt = this.parseSpread(refShorthandDefaultPos); } else { - elt = this.parseMaybeAssign(false, refShorthandDefaultPos, this.parseParenItem); + elt = this.parseMaybeAssign(false, refShorthandDefaultPos, this.parseParenItem, refNeedsArrowPos); } return elt; }; diff --git a/src/plugins/flow.js b/src/plugins/flow.js index 8927bed198..04ea9527ac 100644 --- a/src/plugins/flow.js +++ b/src/plugins/flow.js @@ -1041,9 +1041,9 @@ export default function (instance) { // parse an item inside a expression list eg. `(NODE, NODE)` where NODE represents // the position where this function is called instance.extend("parseExprListItem", function (inner) { - return function (allowEmpty, refShorthandDefaultPos) { + return function (...args) { const container = this.startNode(); - const node = inner.call(this, allowEmpty, refShorthandDefaultPos); + const node = inner.call(this, ...args); if (this.match(tt.colon)) { container._exprListItem = true; container.expression = node; diff --git a/test/fixtures/flow/regression/issue-336/actual.js b/test/fixtures/flow/regression/issue-336/actual.js new file mode 100644 index 0000000000..aa55e12462 --- /dev/null +++ b/test/fixtures/flow/regression/issue-336/actual.js @@ -0,0 +1 @@ +const fn = async (a?: any): Promise => {}; diff --git a/test/fixtures/flow/regression/issue-336/expected.json b/test/fixtures/flow/regression/issue-336/expected.json new file mode 100644 index 0000000000..3c169bcd54 --- /dev/null +++ b/test/fixtures/flow/regression/issue-336/expected.json @@ -0,0 +1,249 @@ +{ + "type": "File", + "start": 0, + "end": 48, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 48 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 48, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 48 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 48, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 48 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 6, + "end": 47, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 47 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 8 + }, + "identifierName": "fn" + }, + "name": "fn" + }, + "init": { + "type": "ArrowFunctionExpression", + "start": 11, + "end": 47, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 47 + } + }, + "returnType": { + "type": "TypeAnnotation", + "start": 26, + "end": 41, + "loc": { + "start": { + "line": 1, + "column": 26 + }, + "end": { + "line": 1, + "column": 41 + } + }, + "typeAnnotation": { + "type": "GenericTypeAnnotation", + "start": 28, + "end": 41, + "loc": { + "start": { + "line": 1, + "column": 28 + }, + "end": { + "line": 1, + "column": 41 + } + }, + "typeParameters": { + "type": "TypeParameterInstantiation", + "start": 35, + "end": 41, + "loc": { + "start": { + "line": 1, + "column": 35 + }, + "end": { + "line": 1, + "column": 41 + } + }, + "params": [ + { + "type": "VoidTypeAnnotation", + "start": 36, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 36 + }, + "end": { + "line": 1, + "column": 40 + } + } + } + ] + }, + "id": { + "type": "Identifier", + "start": 28, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 28 + }, + "end": { + "line": 1, + "column": 35 + }, + "identifierName": "Promise" + }, + "name": "Promise" + } + } + }, + "id": null, + "generator": false, + "expression": false, + "async": true, + "params": [ + { + "type": "Identifier", + "start": 18, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 25 + }, + "identifierName": "a" + }, + "name": "a", + "optional": true, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 20, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 25 + } + }, + "typeAnnotation": { + "type": "AnyTypeAnnotation", + "start": 22, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 25 + } + } + } + } + } + ], + "body": { + "type": "BlockStatement", + "start": 45, + "end": 47, + "loc": { + "start": { + "line": 1, + "column": 45 + }, + "end": { + "line": 1, + "column": 47 + } + }, + "body": [], + "directives": [] + } + } + } + ], + "kind": "const" + } + ], + "directives": [] + } +} \ No newline at end of file