From b31834ad298ca1017148c2b54786d4395c47824f Mon Sep 17 00:00:00 2001 From: Daniel Tschinder Date: Fri, 14 Oct 2016 18:07:46 +0200 Subject: [PATCH] Allow typeAnnotations for yield epressions (#174) --- src/parser/expression.js | 10 +- .../flow/regression/issue-166/actual.js | 4 + .../flow/regression/issue-166/expected.json | 323 ++++++++++++++++++ 3 files changed, 333 insertions(+), 4 deletions(-) create mode 100644 test/fixtures/flow/regression/issue-166/actual.js create mode 100644 test/fixtures/flow/regression/issue-166/expected.json diff --git a/src/parser/expression.js b/src/parser/expression.js index 891ab4d3cf..62b9143194 100644 --- a/src/parser/expression.js +++ b/src/parser/expression.js @@ -89,8 +89,13 @@ pp.parseExpression = function (noIn, refShorthandDefaultPos) { // operators like `+=`. pp.parseMaybeAssign = function (noIn, refShorthandDefaultPos, afterLeftParse, refNeedsArrowPos) { + let startPos = this.state.start; + let startLoc = this.state.startLoc; + if (this.match(tt._yield) && this.state.inGenerator) { - return this.parseYield(); + let left = this.parseYield(); + if (afterLeftParse) left = afterLeftParse.call(this, left, startPos, startLoc); + return left; } let failOnShorthandAssign; @@ -101,9 +106,6 @@ pp.parseMaybeAssign = function (noIn, refShorthandDefaultPos, afterLeftParse, re failOnShorthandAssign = true; } - let startPos = this.state.start; - let startLoc = this.state.startLoc; - if (this.match(tt.parenL) || this.match(tt.name)) { this.state.potentialArrowAt = this.state.start; } diff --git a/test/fixtures/flow/regression/issue-166/actual.js b/test/fixtures/flow/regression/issue-166/actual.js new file mode 100644 index 0000000000..21bf2bcb68 --- /dev/null +++ b/test/fixtures/flow/regression/issue-166/actual.js @@ -0,0 +1,4 @@ +function *foo() { + const x = (yield 5: any); + x ? yield 1 : x; +} diff --git a/test/fixtures/flow/regression/issue-166/expected.json b/test/fixtures/flow/regression/issue-166/expected.json new file mode 100644 index 0000000000..8d34b98069 --- /dev/null +++ b/test/fixtures/flow/regression/issue-166/expected.json @@ -0,0 +1,323 @@ +{ + "type": "File", + "start": 0, + "end": 66, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 66, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "FunctionDeclaration", + "start": 0, + "end": 66, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 10, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 13 + }, + "identifierName": "foo" + }, + "name": "foo" + }, + "generator": true, + "expression": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 16, + "end": 66, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "body": [ + { + "type": "VariableDeclaration", + "start": 20, + "end": 45, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 27 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 26, + "end": 44, + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 26 + } + }, + "id": { + "type": "Identifier", + "start": 26, + "end": 27, + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 9 + }, + "identifierName": "x" + }, + "name": "x" + }, + "init": { + "type": "TypeCastExpression", + "start": 31, + "end": 43, + "loc": { + "start": { + "line": 2, + "column": 13 + }, + "end": { + "line": 2, + "column": 25 + } + }, + "expression": { + "type": "YieldExpression", + "start": 31, + "end": 38, + "loc": { + "start": { + "line": 2, + "column": 13 + }, + "end": { + "line": 2, + "column": 20 + } + }, + "delegate": false, + "argument": { + "type": "NumericLiteral", + "start": 37, + "end": 38, + "loc": { + "start": { + "line": 2, + "column": 19 + }, + "end": { + "line": 2, + "column": 20 + } + }, + "extra": { + "rawValue": 5, + "raw": "5" + }, + "value": 5 + } + }, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 38, + "end": 43, + "loc": { + "start": { + "line": 2, + "column": 20 + }, + "end": { + "line": 2, + "column": 25 + } + }, + "typeAnnotation": { + "type": "AnyTypeAnnotation", + "start": 40, + "end": 43, + "loc": { + "start": { + "line": 2, + "column": 22 + }, + "end": { + "line": 2, + "column": 25 + } + } + } + }, + "extra": { + "parenthesized": true, + "parenStart": 30 + } + } + } + ], + "kind": "const" + }, + { + "type": "ExpressionStatement", + "start": 48, + "end": 64, + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 3, + "column": 18 + } + }, + "expression": { + "type": "ConditionalExpression", + "start": 48, + "end": 63, + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 3, + "column": 17 + } + }, + "test": { + "type": "Identifier", + "start": 48, + "end": 49, + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 3, + "column": 3 + }, + "identifierName": "x" + }, + "name": "x" + }, + "consequent": { + "type": "YieldExpression", + "start": 52, + "end": 59, + "loc": { + "start": { + "line": 3, + "column": 6 + }, + "end": { + "line": 3, + "column": 13 + } + }, + "delegate": false, + "argument": { + "type": "NumericLiteral", + "start": 58, + "end": 59, + "loc": { + "start": { + "line": 3, + "column": 12 + }, + "end": { + "line": 3, + "column": 13 + } + }, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + } + }, + "alternate": { + "type": "Identifier", + "start": 62, + "end": 63, + "loc": { + "start": { + "line": 3, + "column": 16 + }, + "end": { + "line": 3, + "column": 17 + }, + "identifierName": "x" + }, + "name": "x" + } + } + } + ], + "directives": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file