diff --git a/src/plugins/flow.js b/src/plugins/flow.js index 0dcf624fe9..869ba2fc0d 100644 --- a/src/plugins/flow.js +++ b/src/plugins/flow.js @@ -1,7 +1,6 @@ /* eslint max-len: 0 */ import { types as tt } from "../tokenizer/types"; -import { types as ct } from "../tokenizer/context"; const primitiveTypes = [ "any", @@ -1437,6 +1436,12 @@ export default (superClass) => class extends superClass { } catch (err) { if (err instanceof SyntaxError) { this.state = state; + + // Remove `tc.j_expr` and `tc.j_oTag` from context added + // by parsing `jsxTagStart` to stop the JSX plugin from + // messing with the tokens + this.state.context.length -= 2; + jsxError = err; } else { // istanbul ignore next: no such error is expected @@ -1446,9 +1451,6 @@ export default (superClass) => class extends superClass { } if (jsxError != null || this.isRelational("<")) { - // Need to push something onto the context to stop - // the JSX plugin from messing with the tokens - this.state.context.push(ct.parenExpression); let arrowExpression; let typeParameters; try { @@ -1458,13 +1460,9 @@ export default (superClass) => class extends superClass { arrowExpression.typeParameters = typeParameters; this.resetStartLocationFromNode(arrowExpression, typeParameters); } catch (err) { - this.state.context.pop(); - throw jsxError || err; } - this.state.context.pop(); - if (arrowExpression.type === "ArrowFunctionExpression") { return arrowExpression; } else if (jsxError != null) { diff --git a/test/fixtures/flow/type-generics/1/actual.js b/test/fixtures/flow/type-generics/1/actual.js new file mode 100644 index 0000000000..f27ba81379 --- /dev/null +++ b/test/fixtures/flow/type-generics/1/actual.js @@ -0,0 +1 @@ +const functionReturningIdentityAsAField = () => ({ id: (value: T): T => value }); diff --git a/test/fixtures/flow/type-generics/1/expected.json b/test/fixtures/flow/type-generics/1/expected.json new file mode 100644 index 0000000000..40dcb01fe0 --- /dev/null +++ b/test/fixtures/flow/type-generics/1/expected.json @@ -0,0 +1,346 @@ +{ + "type": "File", + "start": 0, + "end": 84, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 84 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 84, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 84 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 84, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 84 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 6, + "end": 83, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 83 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 39 + }, + "identifierName": "functionReturningIdentityAsAField" + }, + "name": "functionReturningIdentityAsAField" + }, + "init": { + "type": "ArrowFunctionExpression", + "start": 42, + "end": 83, + "loc": { + "start": { + "line": 1, + "column": 42 + }, + "end": { + "line": 1, + "column": 83 + } + }, + "id": null, + "generator": false, + "expression": true, + "async": false, + "params": [], + "body": { + "type": "ObjectExpression", + "start": 49, + "end": 82, + "loc": { + "start": { + "line": 1, + "column": 49 + }, + "end": { + "line": 1, + "column": 82 + } + }, + "properties": [ + { + "type": "ObjectProperty", + "start": 51, + "end": 80, + "loc": { + "start": { + "line": 1, + "column": 51 + }, + "end": { + "line": 1, + "column": 80 + } + }, + "method": false, + "shorthand": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 51, + "end": 53, + "loc": { + "start": { + "line": 1, + "column": 51 + }, + "end": { + "line": 1, + "column": 53 + }, + "identifierName": "id" + }, + "name": "id" + }, + "value": { + "type": "ArrowFunctionExpression", + "start": 55, + "end": 80, + "loc": { + "start": { + "line": 1, + "column": 55 + }, + "end": { + "line": 1, + "column": 80 + } + }, + "predicate": null, + "returnType": { + "type": "TypeAnnotation", + "start": 68, + "end": 71, + "loc": { + "start": { + "line": 1, + "column": 68 + }, + "end": { + "line": 1, + "column": 71 + } + }, + "typeAnnotation": { + "type": "GenericTypeAnnotation", + "start": 70, + "end": 71, + "loc": { + "start": { + "line": 1, + "column": 70 + }, + "end": { + "line": 1, + "column": 71 + } + }, + "typeParameters": null, + "id": { + "type": "Identifier", + "start": 70, + "end": 71, + "loc": { + "start": { + "line": 1, + "column": 70 + }, + "end": { + "line": 1, + "column": 71 + }, + "identifierName": "T" + }, + "name": "T" + } + } + }, + "id": null, + "generator": false, + "expression": true, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 59, + "end": 67, + "loc": { + "start": { + "line": 1, + "column": 59 + }, + "end": { + "line": 1, + "column": 67 + }, + "identifierName": "value" + }, + "name": "value", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 64, + "end": 67, + "loc": { + "start": { + "line": 1, + "column": 64 + }, + "end": { + "line": 1, + "column": 67 + } + }, + "typeAnnotation": { + "type": "GenericTypeAnnotation", + "start": 66, + "end": 67, + "loc": { + "start": { + "line": 1, + "column": 66 + }, + "end": { + "line": 1, + "column": 67 + } + }, + "typeParameters": null, + "id": { + "type": "Identifier", + "start": 66, + "end": 67, + "loc": { + "start": { + "line": 1, + "column": 66 + }, + "end": { + "line": 1, + "column": 67 + }, + "identifierName": "T" + }, + "name": "T" + } + } + } + } + ], + "body": { + "type": "Identifier", + "start": 75, + "end": 80, + "loc": { + "start": { + "line": 1, + "column": 75 + }, + "end": { + "line": 1, + "column": 80 + }, + "identifierName": "value" + }, + "name": "value" + }, + "typeParameters": { + "type": "TypeParameterDeclaration", + "start": 55, + "end": 58, + "loc": { + "start": { + "line": 1, + "column": 55 + }, + "end": { + "line": 1, + "column": 58 + } + }, + "params": [ + { + "type": "TypeParameter", + "start": 56, + "end": 57, + "loc": { + "start": { + "line": 1, + "column": 56 + }, + "end": { + "line": 1, + "column": 57 + } + }, + "name": "T", + "variance": null + } + ] + } + } + } + ], + "extra": { + "parenthesized": true, + "parenStart": 48 + } + } + } + } + ], + "kind": "const" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/flow/type-generics/2/actual.js b/test/fixtures/flow/type-generics/2/actual.js new file mode 100644 index 0000000000..e3a4739249 --- /dev/null +++ b/test/fixtures/flow/type-generics/2/actual.js @@ -0,0 +1,2 @@ +const identity = (t: T): T => t; +const a = 1; diff --git a/test/fixtures/flow/type-generics/2/expected.json b/test/fixtures/flow/type-generics/2/expected.json new file mode 100644 index 0000000000..07043bb8c9 --- /dev/null +++ b/test/fixtures/flow/type-generics/2/expected.json @@ -0,0 +1,340 @@ +{ + "type": "File", + "start": 0, + "end": 48, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 12 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 48, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 12 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 35 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 6, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 34 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 14 + }, + "identifierName": "identity" + }, + "name": "identity" + }, + "init": { + "type": "ArrowFunctionExpression", + "start": 17, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 34 + } + }, + "predicate": null, + "returnType": { + "type": "TypeAnnotation", + "start": 26, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 26 + }, + "end": { + "line": 1, + "column": 29 + } + }, + "typeAnnotation": { + "type": "GenericTypeAnnotation", + "start": 28, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 28 + }, + "end": { + "line": 1, + "column": 29 + } + }, + "typeParameters": null, + "id": { + "type": "Identifier", + "start": 28, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 28 + }, + "end": { + "line": 1, + "column": 29 + }, + "identifierName": "T" + }, + "name": "T" + } + } + }, + "id": null, + "generator": false, + "expression": true, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 21, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 21 + }, + "end": { + "line": 1, + "column": 25 + }, + "identifierName": "t" + }, + "name": "t", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 22, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 25 + } + }, + "typeAnnotation": { + "type": "GenericTypeAnnotation", + "start": 24, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 24 + }, + "end": { + "line": 1, + "column": 25 + } + }, + "typeParameters": null, + "id": { + "type": "Identifier", + "start": 24, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 24 + }, + "end": { + "line": 1, + "column": 25 + }, + "identifierName": "T" + }, + "name": "T" + } + } + } + } + ], + "body": { + "type": "Identifier", + "start": 33, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 33 + }, + "end": { + "line": 1, + "column": 34 + }, + "identifierName": "t" + }, + "name": "t" + }, + "typeParameters": { + "type": "TypeParameterDeclaration", + "start": 17, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 20 + } + }, + "params": [ + { + "type": "TypeParameter", + "start": 18, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 19 + } + }, + "name": "T", + "variance": null + } + ] + } + } + } + ], + "kind": "const" + }, + { + "type": "VariableDeclaration", + "start": 36, + "end": 48, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 12 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 42, + "end": 47, + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 11 + } + }, + "id": { + "type": "Identifier", + "start": 42, + "end": 43, + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 7 + }, + "identifierName": "a" + }, + "name": "a" + }, + "init": { + "type": "NumericLiteral", + "start": 46, + "end": 47, + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 11 + } + }, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + } + } + ], + "kind": "const" + } + ], + "directives": [] + } +} \ No newline at end of file