From af2c974473751a914460e86d0ce61885096f826d Mon Sep 17 00:00:00 2001 From: Brian Ng Date: Fri, 25 Aug 2017 09:54:16 -0500 Subject: [PATCH] Fix TypeScript TypeParameterDeclaration location start values --- src/plugins/typescript.js | 13 +++++++++---- .../arrow-function/async-generic/expected.json | 4 ++-- .../arrow-function/generic-tsx/expected.json | 4 ++-- .../typescript/arrow-function/generic/expected.json | 4 ++-- .../class/expression-generic/expected.json | 8 ++++---- .../fixtures/typescript/class/generic/expected.json | 4 ++-- .../typescript/class/get-generic/expected.json | 4 ++-- .../class/members-with-modifier-names/expected.json | 4 ++-- .../typescript/class/method-generic/expected.json | 8 ++++---- .../typescript/function/annotated/expected.json | 4 ++-- .../typescript/function/anonymous/expected.json | 4 ++-- .../typescript/function/declare/expected.json | 4 ++-- .../typescript/interface/generic/expected.json | 4 ++-- .../interface/method-generic/expected.json | 4 ++-- .../type-alias/generic-complex/expected.json | 4 ++-- .../typescript/type-alias/generic/expected.json | 4 ++-- .../typescript/types/function-generic/expected.json | 4 ++-- 17 files changed, 45 insertions(+), 40 deletions(-) diff --git a/src/plugins/typescript.js b/src/plugins/typescript.js index 1360b04be4..d99aeaf6cd 100644 --- a/src/plugins/typescript.js +++ b/src/plugins/typescript.js @@ -271,13 +271,20 @@ export default (superClass: Class): Class => } tsTryParseTypeParameters(): ?N.TypeParameterDeclaration { - if (this.eatRelational("<")) { + if (this.isRelational("<")) { return this.tsParseTypeParameters(); } } - tsParseTypeParameters(): N.TypeParameterDeclaration { + tsParseTypeParameters() { const node: N.TypeParameterDeclaration = this.startNode(); + + if (this.isRelational("<") || this.match(tt.jsxTagStart)) { + this.next(); + } else { + this.unexpected(); + } + node.params = this.tsParseBracketedList( "TypeParametersOrArguments", this.tsParseTypeParameter.bind(this), @@ -1150,7 +1157,6 @@ export default (superClass: Class): Class => startPos, startLoc, ); - this.expectRelational("<"); node.typeParameters = this.tsParseTypeParameters(); // Don't use overloaded parseFunctionParams which would look for "<" again. super.parseFunctionParams(node); @@ -1733,7 +1739,6 @@ export default (superClass: Class): Class => let arrowExpression; let typeParameters: N.TypeParameterDeclaration; const state = this.state.clone(); - this.next(); // skip the jsx start try { // This is similar to TypeScript's `tryParseParenthesizedArrowFunctionExpression`. typeParameters = this.tsParseTypeParameters(); diff --git a/test/fixtures/typescript/arrow-function/async-generic/expected.json b/test/fixtures/typescript/arrow-function/async-generic/expected.json index 86035a3967..5457320839 100644 --- a/test/fixtures/typescript/arrow-function/async-generic/expected.json +++ b/test/fixtures/typescript/arrow-function/async-generic/expected.json @@ -58,12 +58,12 @@ }, "typeParameters": { "type": "TypeParameterDeclaration", - "start": 7, + "start": 6, "end": 9, "loc": { "start": { "line": 1, - "column": 7 + "column": 6 }, "end": { "line": 1, diff --git a/test/fixtures/typescript/arrow-function/generic-tsx/expected.json b/test/fixtures/typescript/arrow-function/generic-tsx/expected.json index b61d4ea254..0bb14a1a3b 100644 --- a/test/fixtures/typescript/arrow-function/generic-tsx/expected.json +++ b/test/fixtures/typescript/arrow-function/generic-tsx/expected.json @@ -192,12 +192,12 @@ }, "typeParameters": { "type": "TypeParameterDeclaration", - "start": 62, + "start": 61, "end": 64, "loc": { "start": { "line": 2, - "column": 1 + "column": 0 }, "end": { "line": 2, diff --git a/test/fixtures/typescript/arrow-function/generic/expected.json b/test/fixtures/typescript/arrow-function/generic/expected.json index 6c5f53a23d..3a645e6228 100644 --- a/test/fixtures/typescript/arrow-function/generic/expected.json +++ b/test/fixtures/typescript/arrow-function/generic/expected.json @@ -192,12 +192,12 @@ }, "typeParameters": { "type": "TypeParameterDeclaration", - "start": 1, + "start": 0, "end": 3, "loc": { "start": { "line": 1, - "column": 1 + "column": 0 }, "end": { "line": 1, diff --git a/test/fixtures/typescript/class/expression-generic/expected.json b/test/fixtures/typescript/class/expression-generic/expected.json index 6082a36e18..f4a01c6c0c 100644 --- a/test/fixtures/typescript/class/expression-generic/expected.json +++ b/test/fixtures/typescript/class/expression-generic/expected.json @@ -59,12 +59,12 @@ "id": null, "typeParameters": { "type": "TypeParameterDeclaration", - "start": 7, + "start": 6, "end": 9, "loc": { "start": { "line": 1, - "column": 7 + "column": 6 }, "end": { "line": 1, @@ -160,12 +160,12 @@ }, "typeParameters": { "type": "TypeParameterDeclaration", - "start": 24, + "start": 23, "end": 26, "loc": { "start": { "line": 2, - "column": 9 + "column": 8 }, "end": { "line": 2, diff --git a/test/fixtures/typescript/class/generic/expected.json b/test/fixtures/typescript/class/generic/expected.json index 2a84f7e0c1..4774774452 100644 --- a/test/fixtures/typescript/class/generic/expected.json +++ b/test/fixtures/typescript/class/generic/expected.json @@ -61,12 +61,12 @@ }, "typeParameters": { "type": "TypeParameterDeclaration", - "start": 8, + "start": 7, "end": 41, "loc": { "start": { "line": 1, - "column": 8 + "column": 7 }, "end": { "line": 1, diff --git a/test/fixtures/typescript/class/get-generic/expected.json b/test/fixtures/typescript/class/get-generic/expected.json index d8733a2b3e..a868aa656b 100644 --- a/test/fixtures/typescript/class/get-generic/expected.json +++ b/test/fixtures/typescript/class/get-generic/expected.json @@ -111,12 +111,12 @@ "kind": "method", "typeParameters": { "type": "TypeParameterDeclaration", - "start": 26, + "start": 25, "end": 28, "loc": { "start": { "line": 2, - "column": 8 + "column": 7 }, "end": { "line": 2, diff --git a/test/fixtures/typescript/class/members-with-modifier-names/expected.json b/test/fixtures/typescript/class/members-with-modifier-names/expected.json index e61ec54c61..932fcdc44b 100644 --- a/test/fixtures/typescript/class/members-with-modifier-names/expected.json +++ b/test/fixtures/typescript/class/members-with-modifier-names/expected.json @@ -306,12 +306,12 @@ "kind": "method", "typeParameters": { "type": "TypeParameterDeclaration", - "start": 85, + "start": 84, "end": 87, "loc": { "start": { "line": 5, - "column": 10 + "column": 9 }, "end": { "line": 5, diff --git a/test/fixtures/typescript/class/method-generic/expected.json b/test/fixtures/typescript/class/method-generic/expected.json index 99c4ee7ffe..4c113cb8b8 100644 --- a/test/fixtures/typescript/class/method-generic/expected.json +++ b/test/fixtures/typescript/class/method-generic/expected.json @@ -111,12 +111,12 @@ "kind": "method", "typeParameters": { "type": "TypeParameterDeclaration", - "start": 16, + "start": 15, "end": 18, "loc": { "start": { "line": 2, - "column": 6 + "column": 5 }, "end": { "line": 2, @@ -505,12 +505,12 @@ "kind": "method", "typeParameters": { "type": "TypeParameterDeclaration", - "start": 71, + "start": 70, "end": 73, "loc": { "start": { "line": 3, - "column": 22 + "column": 21 }, "end": { "line": 3, diff --git a/test/fixtures/typescript/function/annotated/expected.json b/test/fixtures/typescript/function/annotated/expected.json index 206cd1c3a3..6b3a99c857 100644 --- a/test/fixtures/typescript/function/annotated/expected.json +++ b/test/fixtures/typescript/function/annotated/expected.json @@ -64,12 +64,12 @@ "async": false, "typeParameters": { "type": "TypeParameterDeclaration", - "start": 11, + "start": 10, "end": 13, "loc": { "start": { "line": 1, - "column": 11 + "column": 10 }, "end": { "line": 1, diff --git a/test/fixtures/typescript/function/anonymous/expected.json b/test/fixtures/typescript/function/anonymous/expected.json index ba875b73e8..0c7c8c5cca 100644 --- a/test/fixtures/typescript/function/anonymous/expected.json +++ b/test/fixtures/typescript/function/anonymous/expected.json @@ -94,12 +94,12 @@ "async": false, "typeParameters": { "type": "TypeParameterDeclaration", - "start": 19, + "start": 18, "end": 21, "loc": { "start": { "line": 1, - "column": 19 + "column": 18 }, "end": { "line": 1, diff --git a/test/fixtures/typescript/function/declare/expected.json b/test/fixtures/typescript/function/declare/expected.json index 1b115529a9..77d5677c1c 100644 --- a/test/fixtures/typescript/function/declare/expected.json +++ b/test/fixtures/typescript/function/declare/expected.json @@ -131,12 +131,12 @@ "async": false, "typeParameters": { "type": "TypeParameterDeclaration", - "start": 47, + "start": 46, "end": 49, "loc": { "start": { "line": 2, - "column": 19 + "column": 18 }, "end": { "line": 2, diff --git a/test/fixtures/typescript/interface/generic/expected.json b/test/fixtures/typescript/interface/generic/expected.json index d3f1878e05..7c73c06f59 100644 --- a/test/fixtures/typescript/interface/generic/expected.json +++ b/test/fixtures/typescript/interface/generic/expected.json @@ -61,12 +61,12 @@ }, "typeParameters": { "type": "TypeParameterDeclaration", - "start": 12, + "start": 11, "end": 45, "loc": { "start": { "line": 1, - "column": 12 + "column": 11 }, "end": { "line": 1, diff --git a/test/fixtures/typescript/interface/method-generic/expected.json b/test/fixtures/typescript/interface/method-generic/expected.json index 1293957e3f..9e4df1220a 100644 --- a/test/fixtures/typescript/interface/method-generic/expected.json +++ b/test/fixtures/typescript/interface/method-generic/expected.json @@ -108,12 +108,12 @@ }, "typeParameters": { "type": "TypeParameterDeclaration", - "start": 20, + "start": 19, "end": 53, "loc": { "start": { "line": 2, - "column": 6 + "column": 5 }, "end": { "line": 2, diff --git a/test/fixtures/typescript/type-alias/generic-complex/expected.json b/test/fixtures/typescript/type-alias/generic-complex/expected.json index f06680eaa4..88e59f74cd 100644 --- a/test/fixtures/typescript/type-alias/generic-complex/expected.json +++ b/test/fixtures/typescript/type-alias/generic-complex/expected.json @@ -61,12 +61,12 @@ }, "typeParameters": { "type": "TypeParameterDeclaration", - "start": 7, + "start": 6, "end": 40, "loc": { "start": { "line": 1, - "column": 7 + "column": 6 }, "end": { "line": 1, diff --git a/test/fixtures/typescript/type-alias/generic/expected.json b/test/fixtures/typescript/type-alias/generic/expected.json index d4ffab2901..5fbf8a028f 100644 --- a/test/fixtures/typescript/type-alias/generic/expected.json +++ b/test/fixtures/typescript/type-alias/generic/expected.json @@ -61,12 +61,12 @@ }, "typeParameters": { "type": "TypeParameterDeclaration", - "start": 7, + "start": 6, "end": 9, "loc": { "start": { "line": 1, - "column": 7 + "column": 6 }, "end": { "line": 1, diff --git a/test/fixtures/typescript/types/function-generic/expected.json b/test/fixtures/typescript/types/function-generic/expected.json index 11d92aed58..1ea2727f33 100644 --- a/test/fixtures/typescript/types/function-generic/expected.json +++ b/test/fixtures/typescript/types/function-generic/expected.json @@ -103,12 +103,12 @@ }, "typeParameters": { "type": "TypeParameterDeclaration", - "start": 8, + "start": 7, "end": 10, "loc": { "start": { "line": 1, - "column": 8 + "column": 7 }, "end": { "line": 1,