From d6a5f6190dc96a94ebc9aca6315929fc6139aa2e Mon Sep 17 00:00:00 2001 From: Sosuke Suzuki Date: Mon, 24 May 2021 03:12:42 +0900 Subject: [PATCH] [ts] Default `typeAnnotation` of `TSTypePredicate` to `null` (#13354) --- packages/babel-parser/src/plugins/typescript/index.js | 2 ++ packages/babel-parser/src/types.js | 2 +- .../typescript/assert-predicate/arrow-function/output.json | 3 ++- .../typescript/assert-predicate/asserts-this/output.json | 3 ++- .../typescript/assert-predicate/asserts-var/output.json | 3 ++- .../assert-predicate/function-declaration/output.json | 3 ++- .../invalid-escaped-asserts-keyword/output.json | 3 ++- 7 files changed, 13 insertions(+), 6 deletions(-) diff --git a/packages/babel-parser/src/plugins/typescript/index.js b/packages/babel-parser/src/plugins/typescript/index.js index 542c3bc282..cdc9b160dc 100644 --- a/packages/babel-parser/src/plugins/typescript/index.js +++ b/packages/babel-parser/src/plugins/typescript/index.js @@ -1242,6 +1242,7 @@ export default (superClass: Class): Class => if (thisTypePredicate.type === "TSThisType") { node.parameterName = (thisTypePredicate: N.TsThisType); node.asserts = true; + (node: N.TsTypePredicate).typeAnnotation = null; thisTypePredicate = this.finishNode(node, "TSTypePredicate"); } else { this.resetStartLocationFromNode(thisTypePredicate, node); @@ -1264,6 +1265,7 @@ export default (superClass: Class): Class => // : asserts foo node.parameterName = this.parseIdentifier(); node.asserts = asserts; + (node: N.TsTypePredicate).typeAnnotation = null; t.typeAnnotation = this.finishNode(node, "TSTypePredicate"); return this.finishNode(t, "TSTypeAnnotation"); } diff --git a/packages/babel-parser/src/types.js b/packages/babel-parser/src/types.js index 4fd8649ed0..aaf7afdf11 100644 --- a/packages/babel-parser/src/types.js +++ b/packages/babel-parser/src/types.js @@ -1347,7 +1347,7 @@ export type TsTypeReference = TsTypeBase & { export type TsTypePredicate = TsTypeBase & { type: "TSTypePredicate", parameterName: Identifier | TsThisType, - typeAnnotation: TsTypeAnnotation, + typeAnnotation: TsTypeAnnotation | null, asserts: boolean, }; diff --git a/packages/babel-parser/test/fixtures/typescript/assert-predicate/arrow-function/output.json b/packages/babel-parser/test/fixtures/typescript/assert-predicate/arrow-function/output.json index 5d921ad952..4a7aaf7474 100644 --- a/packages/babel-parser/test/fixtures/typescript/assert-predicate/arrow-function/output.json +++ b/packages/babel-parser/test/fixtures/typescript/assert-predicate/arrow-function/output.json @@ -99,7 +99,8 @@ "start":106,"end":111,"loc":{"start":{"line":2,"column":42},"end":{"line":2,"column":47},"identifierName":"value"}, "name": "value" }, - "asserts": true + "asserts": true, + "typeAnnotation": null } }, "id": null, diff --git a/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-this/output.json b/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-this/output.json index 733703069e..a8e6e51f7a 100644 --- a/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-this/output.json +++ b/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-this/output.json @@ -45,7 +45,8 @@ "type": "TSThisType", "start":25,"end":29,"loc":{"start":{"line":2,"column":15},"end":{"line":2,"column":19}} }, - "asserts": true + "asserts": true, + "typeAnnotation": null } }, "body": { diff --git a/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-var/output.json b/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-var/output.json index 4a28b0a63f..863f0d2d72 100644 --- a/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-var/output.json +++ b/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-var/output.json @@ -44,7 +44,8 @@ "start":57,"end":62,"loc":{"start":{"line":1,"column":57},"end":{"line":1,"column":62},"identifierName":"value"}, "name": "value" }, - "asserts": true + "asserts": true, + "typeAnnotation": null } } } diff --git a/packages/babel-parser/test/fixtures/typescript/assert-predicate/function-declaration/output.json b/packages/babel-parser/test/fixtures/typescript/assert-predicate/function-declaration/output.json index fd3cd4e56d..ac607a83aa 100644 --- a/packages/babel-parser/test/fixtures/typescript/assert-predicate/function-declaration/output.json +++ b/packages/babel-parser/test/fixtures/typescript/assert-predicate/function-declaration/output.json @@ -97,7 +97,8 @@ "start":108,"end":113,"loc":{"start":{"line":2,"column":44},"end":{"line":2,"column":49},"identifierName":"value"}, "name": "value" }, - "asserts": true + "asserts": true, + "typeAnnotation": null } }, "body": { diff --git a/packages/babel-parser/test/fixtures/typescript/assert-predicate/invalid-escaped-asserts-keyword/output.json b/packages/babel-parser/test/fixtures/typescript/assert-predicate/invalid-escaped-asserts-keyword/output.json index f1ae2d68f2..0b8ff954ab 100644 --- a/packages/babel-parser/test/fixtures/typescript/assert-predicate/invalid-escaped-asserts-keyword/output.json +++ b/packages/babel-parser/test/fixtures/typescript/assert-predicate/invalid-escaped-asserts-keyword/output.json @@ -47,7 +47,8 @@ "start":62,"end":67,"loc":{"start":{"line":1,"column":62},"end":{"line":1,"column":67},"identifierName":"value"}, "name": "value" }, - "asserts": true + "asserts": true, + "typeAnnotation": null } } }