diff --git a/src/plugins/flow.js b/src/plugins/flow.js index 4a61599ba1..ba8593e8ba 100644 --- a/src/plugins/flow.js +++ b/src/plugins/flow.js @@ -229,9 +229,12 @@ pp.flowParseTypeParameter = function () { }; pp.flowParseTypeParameterDeclaration = function () { + const oldInType = this.state.inType; let node = this.startNode(); node.params = []; + this.state.inType = true; + if (this.isRelational("<") || this.match(tt.jsxTagStart)) { this.next(); } else { @@ -246,6 +249,8 @@ pp.flowParseTypeParameterDeclaration = function () { } while (!this.isRelational(">")); this.expectRelational(">"); + this.state.inType = oldInType; + return this.finishNode(node, "TypeParameterDeclaration"); }; @@ -1041,12 +1046,9 @@ export default function (instance) { // parse function type parameters - function foo() {} instance.extend("parseFunctionParams", function (inner) { return function (node) { - const oldInType = this.state.inType; - this.state.inType = true; if (this.isRelational("<")) { node.typeParameters = this.flowParseTypeParameterDeclaration(); } - this.state.inType = oldInType; inner.call(this, node); }; }); @@ -1114,10 +1116,7 @@ export default function (instance) { let arrowExpression; let typeParameters; try { - const oldInType = this.state.inType; - this.state.inType = true; typeParameters = this.flowParseTypeParameterDeclaration(); - this.state.inType = oldInType; arrowExpression = inner.apply(this, args); arrowExpression.typeParameters = typeParameters; diff --git a/test/fixtures/flow/type-parameter-declaration/default/actual.js b/test/fixtures/flow/type-parameter-declaration/default/actual.js index 03cbbb7985..a894895809 100644 --- a/test/fixtures/flow/type-parameter-declaration/default/actual.js +++ b/test/fixtures/flow/type-parameter-declaration/default/actual.js @@ -19,3 +19,4 @@ interface A {} interface A {} interface A {} interface A {} +type A = T diff --git a/test/fixtures/flow/type-parameter-declaration/default/expected.json b/test/fixtures/flow/type-parameter-declaration/default/expected.json index 28c87e74d0..32f556fd7e 100644 --- a/test/fixtures/flow/type-parameter-declaration/default/expected.json +++ b/test/fixtures/flow/type-parameter-declaration/default/expected.json @@ -1,29 +1,29 @@ { "type": "File", "start": 0, - "end": 743, + "end": 764, "loc": { "start": { "line": 1, "column": 0 }, "end": { - "line": 21, - "column": 47 + "line": 22, + "column": 20 } }, "program": { "type": "Program", "start": 0, - "end": 743, + "end": 764, "loc": { "start": { "line": 1, "column": 0 }, "end": { - "line": 21, - "column": 47 + "line": 22, + "column": 20 } }, "sourceType": "module", @@ -3122,6 +3122,117 @@ "properties": [], "indexers": [] } + }, + { + "type": "TypeAlias", + "start": 744, + "end": 764, + "loc": { + "start": { + "line": 22, + "column": 0 + }, + "end": { + "line": 22, + "column": 20 + } + }, + "id": { + "type": "Identifier", + "start": 749, + "end": 750, + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 22, + "column": 6 + } + }, + "name": "A" + }, + "typeParameters": { + "type": "TypeParameterDeclaration", + "start": 750, + "end": 760, + "loc": { + "start": { + "line": 22, + "column": 6 + }, + "end": { + "line": 22, + "column": 16 + } + }, + "params": [ + { + "type": "TypeParameter", + "start": 751, + "end": 759, + "loc": { + "start": { + "line": 22, + "column": 7 + }, + "end": { + "line": 22, + "column": 15 + } + }, + "name": "T", + "default": { + "type": "VoidTypeAnnotation", + "start": 755, + "end": 759, + "loc": { + "start": { + "line": 22, + "column": 11 + }, + "end": { + "line": 22, + "column": 15 + } + } + } + } + ] + }, + "right": { + "type": "GenericTypeAnnotation", + "start": 763, + "end": 764, + "loc": { + "start": { + "line": 22, + "column": 19 + }, + "end": { + "line": 22, + "column": 20 + } + }, + "typeParameters": null, + "id": { + "type": "Identifier", + "start": 763, + "end": 764, + "loc": { + "start": { + "line": 22, + "column": 19 + }, + "end": { + "line": 22, + "column": 20 + } + }, + "name": "T" + } + } } ], "directives": []