From 70eb206c03d06b957313f092610622c184dc7110 Mon Sep 17 00:00:00 2001 From: Ben Newman Date: Fri, 18 May 2018 11:15:48 -0400 Subject: [PATCH] TypeScript: Fix TSInferType .typeParameter type. (#7967) Follow-up to https://github.com/babel/babel/pull/7404 There is no TypeParameter type in the Babylon TypeScript AST hierarchy. Flow does have a TypeParameter type, but it should not be confused with the TypeScript TSTypeParameter, since Flow !== TypeScript, and the types have totally different fields. Instead, the .typeParameter.type of a Babylon-parsed TSInferType node should be TSTypeParameter. It would probably be fine to leave the declared type of the TSInferType .typeParameter field as TSType instead of TSTypeParameter, but the more specific TSTypeParameter type should be safe/correct, since the TypeScript parseInferType function always uses SyntaxKind.TypeParameter: https://github.com/Microsoft/TypeScript/blob/66d6e5e6e007ce3ac70b7371c3e7ac46a99a0fcd/src/compiler/parser.ts#L3006 I noticed this typo because it has been causing ast-types test failures lately, e.g. https://travis-ci.org/benjamn/ast-types/jobs/369634972 --- packages/babel-types/README.md | 2 +- packages/babel-types/src/definitions/typescript.js | 2 +- packages/babylon/src/plugins/typescript.js | 2 +- .../fixtures/typescript/types/conditional-infer/output.json | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/babel-types/README.md b/packages/babel-types/README.md index a6cdf3593f..272d1099ba 100644 --- a/packages/babel-types/README.md +++ b/packages/babel-types/README.md @@ -2152,7 +2152,7 @@ See also `t.isTSInferType(node, opts)` and `t.assertTSInferType(node, opts)`. Aliases: `TSType` - - `typeParameter`: `TSType` (required) + - `typeParameter`: `TSTypeParameter` (required) --- diff --git a/packages/babel-types/src/definitions/typescript.js b/packages/babel-types/src/definitions/typescript.js index 3aaba9ec6b..270146c5ca 100644 --- a/packages/babel-types/src/definitions/typescript.js +++ b/packages/babel-types/src/definitions/typescript.js @@ -238,7 +238,7 @@ defineType("TSInferType", { aliases: ["TSType"], visitor: ["typeParameter"], fields: { - typeParameter: validateType("TSType"), + typeParameter: validateType("TSTypeParameter"), }, }); diff --git a/packages/babylon/src/plugins/typescript.js b/packages/babylon/src/plugins/typescript.js index e5494e92bd..6744ed16d9 100644 --- a/packages/babylon/src/plugins/typescript.js +++ b/packages/babylon/src/plugins/typescript.js @@ -637,7 +637,7 @@ export default (superClass: Class): Class => this.expectContextual("infer"); const typeParameter = this.startNode(); typeParameter.name = this.parseIdentifierName(typeParameter.start); - node.typeParameter = this.finishNode(typeParameter, "TypeParameter"); + node.typeParameter = this.finishNode(typeParameter, "TSTypeParameter"); return this.finishNode(node, "TSInferType"); } diff --git a/packages/babylon/test/fixtures/typescript/types/conditional-infer/output.json b/packages/babylon/test/fixtures/typescript/types/conditional-infer/output.json index dd94232cb8..92cd4d3d86 100644 --- a/packages/babylon/test/fixtures/typescript/types/conditional-infer/output.json +++ b/packages/babylon/test/fixtures/typescript/types/conditional-infer/output.json @@ -181,7 +181,7 @@ } }, "typeParameter": { - "type": "TypeParameter", + "type": "TSTypeParameter", "start": 35, "end": 36, "loc": {