From f66434df164ed8531ac7600c7b84b84b17e7a44b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Sat, 9 Nov 2019 09:55:15 -0500 Subject: [PATCH] fix: it should set asserts: true for ThisTypePredicate --- packages/babel-parser/src/plugins/typescript/index.js | 8 +++++--- .../asserts-this-with-predicate/output.json | 6 ++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/babel-parser/src/plugins/typescript/index.js b/packages/babel-parser/src/plugins/typescript/index.js index 8df9d4e7b239..4985acc8cd14 100644 --- a/packages/babel-parser/src/plugins/typescript/index.js +++ b/packages/babel-parser/src/plugins/typescript/index.js @@ -942,15 +942,17 @@ export default (superClass: Class): Class => if (asserts && this.match(tt._this)) { // When asserts is false, thisKeyword is handled by tsParseNonArrayType - const node = this.startNodeAtNode(t); // : asserts this is type let thisTypePredicate = this.tsParseThisTypeOrThisTypePredicate(); // if it turns out to be a `TSThisType`, wrap it with `TSTypePredicate` // : asserts this if (thisTypePredicate.type === "TSThisType") { - node.parameterName = thisTypePredicate; + const node: N.TsTypePredicate = this.startNodeAtNode(t); + node.parameterName = (thisTypePredicate: N.TsThisType); node.asserts = true; thisTypePredicate = this.finishNode(node, "TSTypePredicate"); + } else { + (thisTypePredicate: N.TsTypePredicate).asserts = true; } t.typeAnnotation = thisTypePredicate; return this.finishNode(t, "TSTypeAnnotation"); @@ -966,7 +968,7 @@ export default (superClass: Class): Class => return this.tsParseTypeAnnotation(/* eatColon */ false, t); } - const node = this.startNodeAtNode(t); + const node: N.TsTypePredicate = this.startNodeAtNode(t); // : asserts foo node.parameterName = this.parseIdentifier(); node.asserts = asserts; diff --git a/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-this-with-predicate/output.json b/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-this-with-predicate/output.json index 12979bd068bb..bed7e168b396 100644 --- a/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-this-with-predicate/output.json +++ b/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-this-with-predicate/output.json @@ -203,7 +203,8 @@ "name": "Foo" } } - } + }, + "asserts": true } }, "body": { @@ -360,7 +361,8 @@ "name": "Foo" } } - } + }, + "asserts": true } }, "id": null,