From 2f76fc88beeed9381b4b8a88b897903c028b6898 Mon Sep 17 00:00:00 2001 From: Brian Ng Date: Mon, 28 Aug 2017 15:51:37 -0500 Subject: [PATCH] Fix regression with generic type and jsx (#702) --- src/plugins/flow.js | 6 +- .../flow/type-generics/with-jsx/actual.js | 1 + .../flow/type-generics/with-jsx/expected.json | 328 ++++++++++++++++++ 3 files changed, 333 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/flow/type-generics/with-jsx/actual.js create mode 100644 test/fixtures/flow/type-generics/with-jsx/expected.json diff --git a/src/plugins/flow.js b/src/plugins/flow.js index 7195534039..b5fdd6ee39 100644 --- a/src/plugins/flow.js +++ b/src/plugins/flow.js @@ -1132,8 +1132,10 @@ export default (superClass: Class): Class => this.state.inType = true; const type = this.flowParseUnionType(); this.state.inType = oldInType; - // noAnonFunctionType is true when parsing an arrow function - this.state.exprAllowed = this.state.noAnonFunctionType; + // Ensure that a brace after a function generic type annotation is a + // statement, except in arrow functions (noAnonFunctionType) + this.state.exprAllowed = + this.state.exprAllowed || this.state.noAnonFunctionType; return type; } diff --git a/test/fixtures/flow/type-generics/with-jsx/actual.js b/test/fixtures/flow/type-generics/with-jsx/actual.js new file mode 100644 index 0000000000..9552f4734f --- /dev/null +++ b/test/fixtures/flow/type-generics/with-jsx/actual.js @@ -0,0 +1 @@ +let child: Element = ; diff --git a/test/fixtures/flow/type-generics/with-jsx/expected.json b/test/fixtures/flow/type-generics/with-jsx/expected.json new file mode 100644 index 0000000000..1c47f631cc --- /dev/null +++ b/test/fixtures/flow/type-generics/with-jsx/expected.json @@ -0,0 +1,328 @@ +{ + "type": "File", + "start": 0, + "end": 54, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 54 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 54, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 54 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 54, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 54 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 53, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 53 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 23 + }, + "identifierName": "child" + }, + "name": "child", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 9, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "typeAnnotation": { + "type": "GenericTypeAnnotation", + "start": 11, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "typeParameters": { + "type": "TypeParameterInstantiation", + "start": 18, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "params": [ + { + "type": "AnyTypeAnnotation", + "start": 19, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 22 + } + } + } + ] + }, + "id": { + "type": "Identifier", + "start": 11, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 18 + }, + "identifierName": "Element" + }, + "name": "Element" + } + } + } + }, + "init": { + "type": "JSXElement", + "start": 26, + "end": 53, + "loc": { + "start": { + "line": 1, + "column": 26 + }, + "end": { + "line": 1, + "column": 53 + } + }, + "openingElement": { + "type": "JSXOpeningElement", + "start": 26, + "end": 53, + "loc": { + "start": { + "line": 1, + "column": 26 + }, + "end": { + "line": 1, + "column": 53 + } + }, + "attributes": [ + { + "type": "JSXAttribute", + "start": 31, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 31 + }, + "end": { + "line": 1, + "column": 40 + } + }, + "name": { + "type": "JSXIdentifier", + "start": 31, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 31 + }, + "end": { + "line": 1, + "column": 34 + } + }, + "name": "src" + }, + "value": { + "type": "JSXExpressionContainer", + "start": 35, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 35 + }, + "end": { + "line": 1, + "column": 40 + } + }, + "expression": { + "type": "Identifier", + "start": 36, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 36 + }, + "end": { + "line": 1, + "column": 39 + }, + "identifierName": "url" + }, + "name": "url" + } + } + }, + { + "type": "JSXAttribute", + "start": 41, + "end": 50, + "loc": { + "start": { + "line": 1, + "column": 41 + }, + "end": { + "line": 1, + "column": 50 + } + }, + "name": { + "type": "JSXIdentifier", + "start": 41, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 41 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "name": "key" + }, + "value": { + "type": "StringLiteral", + "start": 45, + "end": 50, + "loc": { + "start": { + "line": 1, + "column": 45 + }, + "end": { + "line": 1, + "column": 50 + } + }, + "extra": { + "rawValue": "img", + "raw": "\"img\"" + }, + "value": "img" + } + } + ], + "name": { + "type": "JSXIdentifier", + "start": 27, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 27 + }, + "end": { + "line": 1, + "column": 30 + } + }, + "name": "img" + }, + "selfClosing": true + }, + "closingElement": null, + "children": [] + } + } + ], + "kind": "let" + } + ], + "directives": [] + } +} \ No newline at end of file