From 3419d761d3a4c4c475c931c29c2420c48d7347e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Thu, 7 Nov 2019 13:24:23 -0500 Subject: [PATCH 1/9] chore: rename testcase --- .../input.ts | 0 .../output.json | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename packages/babel-parser/test/fixtures/typescript/assert-predicate/{asserts-with-predicate => asserts-var-with-predicate}/input.ts (100%) rename packages/babel-parser/test/fixtures/typescript/assert-predicate/{asserts-with-predicate => asserts-var-with-predicate}/output.json (100%) diff --git a/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-with-predicate/input.ts b/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-var-with-predicate/input.ts similarity index 100% rename from packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-with-predicate/input.ts rename to packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-var-with-predicate/input.ts diff --git a/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-with-predicate/output.json b/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-var-with-predicate/output.json similarity index 100% rename from packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-with-predicate/output.json rename to packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-var-with-predicate/output.json From eb8a7d40f1c6a5eb2589db0a3199b57f7a5c3c9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Thu, 7 Nov 2019 13:27:27 -0500 Subject: [PATCH 2/9] add testcase --- .../assert-predicate/asserts-this-with-predicate/input.ts | 3 +++ .../fixtures/typescript/assert-predicate/asserts-this/input.ts | 3 +++ 2 files changed, 6 insertions(+) create mode 100644 packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-this-with-predicate/input.ts create mode 100644 packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-this/input.ts diff --git a/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-this-with-predicate/input.ts b/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-this-with-predicate/input.ts new file mode 100644 index 000000000000..c3b9d8fe7b7f --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-this-with-predicate/input.ts @@ -0,0 +1,3 @@ +class C { + m(): asserts this is C {}; +} diff --git a/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-this/input.ts b/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-this/input.ts new file mode 100644 index 000000000000..839483541d5b --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-this/input.ts @@ -0,0 +1,3 @@ +class C { + m(): asserts this {}; +} From b2de62cf284bf1444467fb1e12a140a82c7c4142 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Thu, 7 Nov 2019 15:38:48 -0500 Subject: [PATCH 3/9] fix: add asserts this support --- .../src/plugins/typescript/index.js | 50 +++- .../asserts-this-with-predicate/output.json | 263 ++++++++++++++++++ .../assert-predicate/asserts-this/output.json | 201 +++++++++++++ 3 files changed, 499 insertions(+), 15 deletions(-) create mode 100644 packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-this-with-predicate/output.json create mode 100644 packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-this/output.json diff --git a/packages/babel-parser/src/plugins/typescript/index.js b/packages/babel-parser/src/plugins/typescript/index.js index 9fabb50cd0a2..94602144a8d8 100644 --- a/packages/babel-parser/src/plugins/typescript/index.js +++ b/packages/babel-parser/src/plugins/typescript/index.js @@ -680,6 +680,15 @@ export default (superClass: Class): Class => return this.finishNode(node, "TSLiteralType"); } + tsParseThisTypeOrThisTypePredicate(): N.TsThisType | N.TsTypePredicate { + const thisKeyword = this.tsParseThisTypeNode(); + if (this.isContextual("is") && !this.hasPrecedingLineBreak()) { + return this.tsParseThisTypePredicate(thisKeyword); + } else { + return thisKeyword; + } + } + tsParseNonArrayType(): N.TsType { switch (this.state.type) { case tt.name: @@ -715,14 +724,8 @@ export default (superClass: Class): Class => return this.finishNode(node, "TSLiteralType"); } break; - case tt._this: { - const thisKeyword = this.tsParseThisTypeNode(); - if (this.isContextual("is") && !this.hasPrecedingLineBreak()) { - return this.tsParseThisTypePredicate(thisKeyword); - } else { - return thisKeyword; - } - } + case tt._this: + return this.tsParseThisTypeOrThisTypePredicate(); case tt._typeof: return this.tsParseTypeQuery(); case tt._import: @@ -937,9 +940,21 @@ export default (superClass: Class): Class => this.tsParseTypePredicateAsserts.bind(this), ); - const typePredicateVariable = - this.tsIsIdentifier() && - this.tsTryParse(this.tsParseTypePredicatePrefix.bind(this)); + let typePredicateVariable = null; + if (this.tsIsIdentifier()) { + // TypePredicate + // : foo is type + typePredicateVariable = this.tsTryParse( + this.tsParseTypePredicatePrefix.bind(this), + ); + } else if (asserts && this.match(tt._this)) { + // When asserts is false, thisKeyword is handled by tsParseNonArraytype + // TypePredicate + // : this is type + typePredicateVariable = this.tsTryParse( + this.tsParseThisTypeOrThisTypePredicate.bind(this), + ); + } if (!typePredicateVariable) { if (!asserts) { @@ -947,15 +962,20 @@ export default (superClass: Class): Class => return this.tsParseTypeAnnotation(/* eatColon */ false, t); } - // : asserts foo const node = this.startNodeAtNode(t); - node.parameterName = this.parseIdentifier(); + if (this.match(tt._this)) { + // : asserts this + node.parameterName = this.tsParseThisTypeNode(); + } else { + // : asserts foo + node.parameterName = this.parseIdentifier(); + } node.asserts = asserts; t.typeAnnotation = this.finishNode(node, "TSTypePredicate"); return this.finishNode(t, "TSTypeAnnotation"); } - // : foo is type + // : asserts TypePredicate const type = this.tsParseTypeAnnotation(/* eatColon */ false); const node = this.startNodeAtNode(t); node.parameterName = typePredicateVariable; @@ -997,7 +1017,7 @@ export default (superClass: Class): Class => if ( id.name !== "asserts" || this.hasPrecedingLineBreak() || - !this.tsIsIdentifier() + (!this.tsIsIdentifier() && !this.match(tt._this)) ) { return false; } 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 new file mode 100644 index 000000000000..6ce4c6de8424 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-this-with-predicate/output.json @@ -0,0 +1,263 @@ +{ + "type": "File", + "start": 0, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 8, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "TSDeclareMethod", + "start": 12, + "end": 38, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 28 + } + }, + "static": false, + "key": { + "type": "Identifier", + "start": 12, + "end": 13, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 3 + }, + "identifierName": "m" + }, + "name": "m" + }, + "computed": false, + "kind": "method", + "id": null, + "generator": false, + "async": false, + "params": [], + "returnType": { + "type": "TSTypeAnnotation", + "start": 15, + "end": 37, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 27 + } + }, + "typeAnnotation": { + "type": "TSTypePredicate", + "start": 15, + "end": 37, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 27 + } + }, + "parameterName": { + "type": "TSTypePredicate", + "start": 25, + "end": 34, + "loc": { + "start": { + "line": 2, + "column": 15 + }, + "end": { + "line": 2, + "column": 24 + } + }, + "parameterName": { + "type": "TSThisType", + "start": 25, + "end": 29, + "loc": { + "start": { + "line": 2, + "column": 15 + }, + "end": { + "line": 2, + "column": 19 + } + } + }, + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start": 33, + "end": 34, + "loc": { + "start": { + "line": 2, + "column": 23 + }, + "end": { + "line": 2, + "column": 24 + } + }, + "typeAnnotation": { + "type": "TSTypeReference", + "start": 33, + "end": 34, + "loc": { + "start": { + "line": 2, + "column": 23 + }, + "end": { + "line": 2, + "column": 24 + } + }, + "typeName": { + "type": "Identifier", + "start": 33, + "end": 34, + "loc": { + "start": { + "line": 2, + "column": 23 + }, + "end": { + "line": 2, + "column": 24 + }, + "identifierName": "C" + }, + "name": "C" + } + } + } + }, + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start": 35, + "end": 37, + "loc": { + "start": { + "line": 2, + "column": 25 + }, + "end": { + "line": 2, + "column": 27 + } + }, + "typeAnnotation": { + "type": "TSTypeLiteral", + "start": 35, + "end": 37, + "loc": { + "start": { + "line": 2, + "column": 25 + }, + "end": { + "line": 2, + "column": 27 + } + }, + "members": [] + } + }, + "asserts": true + } + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file 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 new file mode 100644 index 000000000000..54a8969f7860 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-this/output.json @@ -0,0 +1,201 @@ +{ + "type": "File", + "start": 0, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 8, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "TSDeclareMethod", + "start": 12, + "end": 33, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 23 + } + }, + "static": false, + "key": { + "type": "Identifier", + "start": 12, + "end": 13, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 3 + }, + "identifierName": "m" + }, + "name": "m" + }, + "computed": false, + "kind": "method", + "id": null, + "generator": false, + "async": false, + "params": [], + "returnType": { + "type": "TSTypeAnnotation", + "start": 15, + "end": 32, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 22 + } + }, + "typeAnnotation": { + "type": "TSTypePredicate", + "start": 15, + "end": 32, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 22 + } + }, + "parameterName": { + "type": "TSThisType", + "start": 25, + "end": 29, + "loc": { + "start": { + "line": 2, + "column": 15 + }, + "end": { + "line": 2, + "column": 19 + } + } + }, + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start": 30, + "end": 32, + "loc": { + "start": { + "line": 2, + "column": 20 + }, + "end": { + "line": 2, + "column": 22 + } + }, + "typeAnnotation": { + "type": "TSTypeLiteral", + "start": 30, + "end": 32, + "loc": { + "start": { + "line": 2, + "column": 20 + }, + "end": { + "line": 2, + "column": 22 + } + }, + "members": [] + } + }, + "asserts": true + } + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file From 3cbf20c9b8c8c7ad39ff47dc64dd526a958eae93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Thu, 7 Nov 2019 16:00:49 -0500 Subject: [PATCH 4/9] fix: it should throw when asserts contains escape sequence --- .../src/plugins/typescript/index.js | 6 + .../invalid-escaped-asserts-keyword/input.ts | 1 + .../output.json | 170 ++++++++++++++++++ 3 files changed, 177 insertions(+) create mode 100644 packages/babel-parser/test/fixtures/typescript/assert-predicate/invalid-escaped-asserts-keyword/input.ts create mode 100644 packages/babel-parser/test/fixtures/typescript/assert-predicate/invalid-escaped-asserts-keyword/output.json diff --git a/packages/babel-parser/src/plugins/typescript/index.js b/packages/babel-parser/src/plugins/typescript/index.js index 94602144a8d8..5af5fe51a871 100644 --- a/packages/babel-parser/src/plugins/typescript/index.js +++ b/packages/babel-parser/src/plugins/typescript/index.js @@ -1013,6 +1013,8 @@ export default (superClass: Class): Class => return false; } + const containsEsc = this.state.containsEsc; + const id = this.parseIdentifier(); if ( id.name !== "asserts" || @@ -1022,6 +1024,10 @@ export default (superClass: Class): Class => return false; } + if (containsEsc) { + this.raise(id.start, "Escape sequence in keyword asserts"); + } + return true; } diff --git a/packages/babel-parser/test/fixtures/typescript/assert-predicate/invalid-escaped-asserts-keyword/input.ts b/packages/babel-parser/test/fixtures/typescript/assert-predicate/invalid-escaped-asserts-keyword/input.ts new file mode 100644 index 000000000000..a89b5ad6c9fa --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/assert-predicate/invalid-escaped-asserts-keyword/input.ts @@ -0,0 +1 @@ +declare function assertIsString(value: unknown): \u{61}sserts value; 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 new file mode 100644 index 000000000000..90484c4b93a3 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/assert-predicate/invalid-escaped-asserts-keyword/output.json @@ -0,0 +1,170 @@ +{ + "type": "File", + "start": 0, + "end": 68, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 68 + } + }, + "errors": [ + "SyntaxError: Escape sequence in keyword asserts (1:49)" + ], + "program": { + "type": "Program", + "start": 0, + "end": 68, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 68 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "TSDeclareFunction", + "start": 0, + "end": 68, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 68 + } + }, + "id": { + "type": "Identifier", + "start": 17, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 31 + }, + "identifierName": "assertIsString" + }, + "name": "assertIsString" + }, + "generator": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 32, + "end": 46, + "loc": { + "start": { + "line": 1, + "column": 32 + }, + "end": { + "line": 1, + "column": 46 + }, + "identifierName": "value" + }, + "name": "value", + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start": 37, + "end": 46, + "loc": { + "start": { + "line": 1, + "column": 37 + }, + "end": { + "line": 1, + "column": 46 + } + }, + "typeAnnotation": { + "type": "TSUnknownKeyword", + "start": 39, + "end": 46, + "loc": { + "start": { + "line": 1, + "column": 39 + }, + "end": { + "line": 1, + "column": 46 + } + } + } + } + } + ], + "returnType": { + "type": "TSTypeAnnotation", + "start": 47, + "end": 67, + "loc": { + "start": { + "line": 1, + "column": 47 + }, + "end": { + "line": 1, + "column": 67 + } + }, + "typeAnnotation": { + "type": "TSTypePredicate", + "start": 47, + "end": 67, + "loc": { + "start": { + "line": 1, + "column": 47 + }, + "end": { + "line": 1, + "column": 67 + } + }, + "parameterName": { + "type": "Identifier", + "start": 62, + "end": 67, + "loc": { + "start": { + "line": 1, + "column": 62 + }, + "end": { + "line": 1, + "column": 67 + }, + "identifierName": "value" + }, + "name": "value" + }, + "asserts": true + } + }, + "declare": true + } + ], + "directives": [] + } +} \ No newline at end of file From c194168167576cb7064ea8f5a3f9744cc635605b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Fri, 8 Nov 2019 22:21:53 -0500 Subject: [PATCH 5/9] update test layout --- .../asserts-this-with-predicate/input.ts | 5 +- .../asserts-this-with-predicate/options.json | 5 + .../asserts-this-with-predicate/output.json | 316 ++++++++++++----- .../assert-predicate/asserts-this/output.json | 62 ++-- .../asserts-var-with-predicate/input.ts | 4 +- .../asserts-var-with-predicate/output.json | 318 +++++++++++------- .../input.ts | 1 + .../output.json | 197 +++++++++++ 8 files changed, 651 insertions(+), 257 deletions(-) create mode 100644 packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-this-with-predicate/options.json create mode 100644 packages/babel-parser/test/fixtures/typescript/assert-predicate/declare-asserts-var-with-predicate/input.ts create mode 100644 packages/babel-parser/test/fixtures/typescript/assert-predicate/declare-asserts-var-with-predicate/output.json diff --git a/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-this-with-predicate/input.ts b/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-this-with-predicate/input.ts index c3b9d8fe7b7f..cc1a550f07bb 100644 --- a/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-this-with-predicate/input.ts +++ b/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-this-with-predicate/input.ts @@ -1,3 +1,4 @@ -class C { - m(): asserts this is C {}; +class Foo { + isBar(): asserts this is Foo {} + isBaz = (): asserts this is Foo => {} } diff --git a/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-this-with-predicate/options.json b/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-this-with-predicate/options.json new file mode 100644 index 000000000000..efb11900ba6e --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-this-with-predicate/options.json @@ -0,0 +1,5 @@ +{ + "plugins": [ + "typescript", "classProperties" + ] +} 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 6ce4c6de8424..12979bd068bb 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 @@ -1,28 +1,28 @@ { "type": "File", "start": 0, - "end": 40, + "end": 87, "loc": { "start": { "line": 1, "column": 0 }, "end": { - "line": 3, + "line": 4, "column": 1 } }, "program": { "type": "Program", "start": 0, - "end": 40, + "end": 87, "loc": { "start": { "line": 1, "column": 0 }, "end": { - "line": 3, + "line": 4, "column": 1 } }, @@ -32,21 +32,21 @@ { "type": "ClassDeclaration", "start": 0, - "end": 40, + "end": 87, "loc": { "start": { "line": 1, "column": 0 }, "end": { - "line": 3, + "line": 4, "column": 1 } }, "id": { "type": "Identifier", "start": 6, - "end": 7, + "end": 9, "loc": { "start": { "line": 1, @@ -54,32 +54,32 @@ }, "end": { "line": 1, - "column": 7 + "column": 9 }, - "identifierName": "C" + "identifierName": "Foo" }, - "name": "C" + "name": "Foo" }, "superClass": null, "body": { "type": "ClassBody", - "start": 8, - "end": 40, + "start": 10, + "end": 87, "loc": { "start": { "line": 1, - "column": 8 + "column": 10 }, "end": { - "line": 3, + "line": 4, "column": 1 } }, "body": [ { - "type": "TSDeclareMethod", - "start": 12, - "end": 38, + "type": "ClassMethod", + "start": 14, + "end": 45, "loc": { "start": { "line": 2, @@ -87,14 +87,14 @@ }, "end": { "line": 2, - "column": 28 + "column": 33 } }, "static": false, "key": { "type": "Identifier", - "start": 12, - "end": 13, + "start": 14, + "end": 19, "loc": { "start": { "line": 2, @@ -102,11 +102,11 @@ }, "end": { "line": 2, - "column": 3 + "column": 7 }, - "identifierName": "m" + "identifierName": "isBar" }, - "name": "m" + "name": "isBar" }, "computed": false, "kind": "method", @@ -116,141 +116,273 @@ "params": [], "returnType": { "type": "TSTypeAnnotation", - "start": 15, - "end": 37, + "start": 21, + "end": 42, "loc": { "start": { "line": 2, - "column": 5 + "column": 9 }, "end": { "line": 2, - "column": 27 + "column": 30 } }, "typeAnnotation": { "type": "TSTypePredicate", - "start": 15, - "end": 37, + "start": 31, + "end": 42, "loc": { "start": { "line": 2, - "column": 5 + "column": 19 }, "end": { "line": 2, - "column": 27 + "column": 30 } }, "parameterName": { - "type": "TSTypePredicate", - "start": 25, - "end": 34, + "type": "TSThisType", + "start": 31, + "end": 35, "loc": { "start": { "line": 2, - "column": 15 + "column": 19 }, "end": { "line": 2, - "column": 24 + "column": 23 + } + } + }, + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start": 39, + "end": 42, + "loc": { + "start": { + "line": 2, + "column": 27 + }, + "end": { + "line": 2, + "column": 30 } }, - "parameterName": { - "type": "TSThisType", - "start": 25, - "end": 29, + "typeAnnotation": { + "type": "TSTypeReference", + "start": 39, + "end": 42, "loc": { "start": { "line": 2, - "column": 15 + "column": 27 }, "end": { "line": 2, - "column": 19 + "column": 30 + } + }, + "typeName": { + "type": "Identifier", + "start": 39, + "end": 42, + "loc": { + "start": { + "line": 2, + "column": 27 + }, + "end": { + "line": 2, + "column": 30 + }, + "identifierName": "Foo" + }, + "name": "Foo" + } + } + } + } + }, + "body": { + "type": "BlockStatement", + "start": 43, + "end": 45, + "loc": { + "start": { + "line": 2, + "column": 31 + }, + "end": { + "line": 2, + "column": 33 + } + }, + "body": [], + "directives": [] + } + }, + { + "type": "ClassProperty", + "start": 48, + "end": 85, + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 3, + "column": 39 + } + }, + "static": false, + "key": { + "type": "Identifier", + "start": 48, + "end": 53, + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 3, + "column": 7 + }, + "identifierName": "isBaz" + }, + "name": "isBaz" + }, + "computed": false, + "value": { + "type": "ArrowFunctionExpression", + "start": 56, + "end": 85, + "loc": { + "start": { + "line": 3, + "column": 10 + }, + "end": { + "line": 3, + "column": 39 + } + }, + "returnType": { + "type": "TSTypeAnnotation", + "start": 58, + "end": 79, + "loc": { + "start": { + "line": 3, + "column": 12 + }, + "end": { + "line": 3, + "column": 33 + } + }, + "typeAnnotation": { + "type": "TSTypePredicate", + "start": 68, + "end": 79, + "loc": { + "start": { + "line": 3, + "column": 22 + }, + "end": { + "line": 3, + "column": 33 + } + }, + "parameterName": { + "type": "TSThisType", + "start": 68, + "end": 72, + "loc": { + "start": { + "line": 3, + "column": 22 + }, + "end": { + "line": 3, + "column": 26 } } }, "typeAnnotation": { "type": "TSTypeAnnotation", - "start": 33, - "end": 34, + "start": 76, + "end": 79, "loc": { "start": { - "line": 2, - "column": 23 + "line": 3, + "column": 30 }, "end": { - "line": 2, - "column": 24 + "line": 3, + "column": 33 } }, "typeAnnotation": { "type": "TSTypeReference", - "start": 33, - "end": 34, + "start": 76, + "end": 79, "loc": { "start": { - "line": 2, - "column": 23 + "line": 3, + "column": 30 }, "end": { - "line": 2, - "column": 24 + "line": 3, + "column": 33 } }, "typeName": { "type": "Identifier", - "start": 33, - "end": 34, + "start": 76, + "end": 79, "loc": { "start": { - "line": 2, - "column": 23 + "line": 3, + "column": 30 }, "end": { - "line": 2, - "column": 24 + "line": 3, + "column": 33 }, - "identifierName": "C" + "identifierName": "Foo" }, - "name": "C" + "name": "Foo" } } } - }, - "typeAnnotation": { - "type": "TSTypeAnnotation", - "start": 35, - "end": 37, - "loc": { - "start": { - "line": 2, - "column": 25 - }, - "end": { - "line": 2, - "column": 27 - } + } + }, + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 83, + "end": 85, + "loc": { + "start": { + "line": 3, + "column": 37 }, - "typeAnnotation": { - "type": "TSTypeLiteral", - "start": 35, - "end": 37, - "loc": { - "start": { - "line": 2, - "column": 25 - }, - "end": { - "line": 2, - "column": 27 - } - }, - "members": [] + "end": { + "line": 3, + "column": 39 } }, - "asserts": true + "body": [], + "directives": [] } } } 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 54a8969f7860..e9d5458c1c89 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 @@ -77,9 +77,9 @@ }, "body": [ { - "type": "TSDeclareMethod", + "type": "ClassMethod", "start": 12, - "end": 33, + "end": 32, "loc": { "start": { "line": 2, @@ -87,7 +87,7 @@ }, "end": { "line": 2, - "column": 23 + "column": 22 } }, "static": false, @@ -117,7 +117,7 @@ "returnType": { "type": "TSTypeAnnotation", "start": 15, - "end": 32, + "end": 29, "loc": { "start": { "line": 2, @@ -125,13 +125,13 @@ }, "end": { "line": 2, - "column": 22 + "column": 19 } }, "typeAnnotation": { "type": "TSTypePredicate", "start": 15, - "end": 32, + "end": 29, "loc": { "start": { "line": 2, @@ -139,7 +139,7 @@ }, "end": { "line": 2, - "column": 22 + "column": 19 } }, "parameterName": { @@ -157,39 +157,25 @@ } } }, - "typeAnnotation": { - "type": "TSTypeAnnotation", - "start": 30, - "end": 32, - "loc": { - "start": { - "line": 2, - "column": 20 - }, - "end": { - "line": 2, - "column": 22 - } - }, - "typeAnnotation": { - "type": "TSTypeLiteral", - "start": 30, - "end": 32, - "loc": { - "start": { - "line": 2, - "column": 20 - }, - "end": { - "line": 2, - "column": 22 - } - }, - "members": [] - } - }, "asserts": true } + }, + "body": { + "type": "BlockStatement", + "start": 30, + "end": 32, + "loc": { + "start": { + "line": 2, + "column": 20 + }, + "end": { + "line": 2, + "column": 22 + } + }, + "body": [], + "directives": [] } } ] diff --git a/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-var-with-predicate/input.ts b/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-var-with-predicate/input.ts index 9456a5cb2e50..2b580e64e906 100644 --- a/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-var-with-predicate/input.ts +++ b/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-var-with-predicate/input.ts @@ -1 +1,3 @@ -declare function assertIsString(value: unknown): asserts value is string; +class C { + assertIsString(value: unknown): asserts value is string {} +} diff --git a/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-var-with-predicate/output.json b/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-var-with-predicate/output.json index 82cdaebbcf79..f63a5e0581c3 100644 --- a/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-var-with-predicate/output.json +++ b/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-var-with-predicate/output.json @@ -1,197 +1,267 @@ { "type": "File", "start": 0, - "end": 73, + "end": 72, "loc": { "start": { "line": 1, "column": 0 }, "end": { - "line": 1, - "column": 73 + "line": 3, + "column": 1 } }, "program": { "type": "Program", "start": 0, - "end": 73, + "end": 72, "loc": { "start": { "line": 1, "column": 0 }, "end": { - "line": 1, - "column": 73 + "line": 3, + "column": 1 } }, "sourceType": "module", "interpreter": null, "body": [ { - "type": "TSDeclareFunction", + "type": "ClassDeclaration", "start": 0, - "end": 73, + "end": 72, "loc": { "start": { "line": 1, "column": 0 }, "end": { - "line": 1, - "column": 73 + "line": 3, + "column": 1 } }, "id": { "type": "Identifier", - "start": 17, - "end": 31, + "start": 6, + "end": 7, "loc": { "start": { "line": 1, - "column": 17 + "column": 6 }, "end": { "line": 1, - "column": 31 + "column": 7 }, - "identifierName": "assertIsString" + "identifierName": "C" }, - "name": "assertIsString" + "name": "C" }, - "generator": false, - "async": false, - "params": [ - { - "type": "Identifier", - "start": 32, - "end": 46, - "loc": { - "start": { - "line": 1, - "column": 32 - }, - "end": { - "line": 1, - "column": 46 - }, - "identifierName": "value" - }, - "name": "value", - "typeAnnotation": { - "type": "TSTypeAnnotation", - "start": 37, - "end": 46, - "loc": { - "start": { - "line": 1, - "column": 37 - }, - "end": { - "line": 1, - "column": 46 - } - }, - "typeAnnotation": { - "type": "TSUnknownKeyword", - "start": 39, - "end": 46, - "loc": { - "start": { - "line": 1, - "column": 39 - }, - "end": { - "line": 1, - "column": 46 - } - } - } - } - } - ], - "returnType": { - "type": "TSTypeAnnotation", - "start": 47, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 8, "end": 72, "loc": { "start": { "line": 1, - "column": 47 + "column": 8 }, "end": { - "line": 1, - "column": 72 + "line": 3, + "column": 1 } }, - "typeAnnotation": { - "type": "TSTypePredicate", - "start": 47, - "end": 72, - "loc": { - "start": { - "line": 1, - "column": 47 - }, - "end": { - "line": 1, - "column": 72 - } - }, - "parameterName": { - "type": "Identifier", - "start": 57, - "end": 62, + "body": [ + { + "type": "ClassMethod", + "start": 12, + "end": 70, "loc": { "start": { - "line": 1, - "column": 57 + "line": 2, + "column": 2 }, "end": { - "line": 1, - "column": 62 + "line": 2, + "column": 60 + } + }, + "static": false, + "key": { + "type": "Identifier", + "start": 12, + "end": 26, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 16 + }, + "identifierName": "assertIsString" }, - "identifierName": "value" + "name": "assertIsString" }, - "name": "value" - }, - "typeAnnotation": { - "type": "TSTypeAnnotation", - "start": 66, - "end": 72, - "loc": { - "start": { - "line": 1, - "column": 66 + "computed": false, + "kind": "method", + "id": null, + "generator": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 27, + "end": 41, + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 31 + }, + "identifierName": "value" + }, + "name": "value", + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start": 32, + "end": 41, + "loc": { + "start": { + "line": 2, + "column": 22 + }, + "end": { + "line": 2, + "column": 31 + } + }, + "typeAnnotation": { + "type": "TSUnknownKeyword", + "start": 34, + "end": 41, + "loc": { + "start": { + "line": 2, + "column": 24 + }, + "end": { + "line": 2, + "column": 31 + } + } + } + } + } + ], + "returnType": { + "type": "TSTypeAnnotation", + "start": 42, + "end": 67, + "loc": { + "start": { + "line": 2, + "column": 32 + }, + "end": { + "line": 2, + "column": 57 + } }, - "end": { - "line": 1, - "column": 72 + "typeAnnotation": { + "type": "TSTypePredicate", + "start": 42, + "end": 67, + "loc": { + "start": { + "line": 2, + "column": 32 + }, + "end": { + "line": 2, + "column": 57 + } + }, + "parameterName": { + "type": "Identifier", + "start": 52, + "end": 57, + "loc": { + "start": { + "line": 2, + "column": 42 + }, + "end": { + "line": 2, + "column": 47 + }, + "identifierName": "value" + }, + "name": "value" + }, + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start": 61, + "end": 67, + "loc": { + "start": { + "line": 2, + "column": 51 + }, + "end": { + "line": 2, + "column": 57 + } + }, + "typeAnnotation": { + "type": "TSStringKeyword", + "start": 61, + "end": 67, + "loc": { + "start": { + "line": 2, + "column": 51 + }, + "end": { + "line": 2, + "column": 57 + } + } + } + }, + "asserts": true } }, - "typeAnnotation": { - "type": "TSStringKeyword", - "start": 66, - "end": 72, + "body": { + "type": "BlockStatement", + "start": 68, + "end": 70, "loc": { "start": { - "line": 1, - "column": 66 + "line": 2, + "column": 58 }, "end": { - "line": 1, - "column": 72 + "line": 2, + "column": 60 } - } + }, + "body": [], + "directives": [] } - }, - "asserts": true - } - }, - "declare": true + } + ] + } } ], "directives": [] } -} +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/assert-predicate/declare-asserts-var-with-predicate/input.ts b/packages/babel-parser/test/fixtures/typescript/assert-predicate/declare-asserts-var-with-predicate/input.ts new file mode 100644 index 000000000000..9456a5cb2e50 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/assert-predicate/declare-asserts-var-with-predicate/input.ts @@ -0,0 +1 @@ +declare function assertIsString(value: unknown): asserts value is string; diff --git a/packages/babel-parser/test/fixtures/typescript/assert-predicate/declare-asserts-var-with-predicate/output.json b/packages/babel-parser/test/fixtures/typescript/assert-predicate/declare-asserts-var-with-predicate/output.json new file mode 100644 index 000000000000..82cdaebbcf79 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/assert-predicate/declare-asserts-var-with-predicate/output.json @@ -0,0 +1,197 @@ +{ + "type": "File", + "start": 0, + "end": 73, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 73 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 73, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 73 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "TSDeclareFunction", + "start": 0, + "end": 73, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 73 + } + }, + "id": { + "type": "Identifier", + "start": 17, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 31 + }, + "identifierName": "assertIsString" + }, + "name": "assertIsString" + }, + "generator": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 32, + "end": 46, + "loc": { + "start": { + "line": 1, + "column": 32 + }, + "end": { + "line": 1, + "column": 46 + }, + "identifierName": "value" + }, + "name": "value", + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start": 37, + "end": 46, + "loc": { + "start": { + "line": 1, + "column": 37 + }, + "end": { + "line": 1, + "column": 46 + } + }, + "typeAnnotation": { + "type": "TSUnknownKeyword", + "start": 39, + "end": 46, + "loc": { + "start": { + "line": 1, + "column": 39 + }, + "end": { + "line": 1, + "column": 46 + } + } + } + } + } + ], + "returnType": { + "type": "TSTypeAnnotation", + "start": 47, + "end": 72, + "loc": { + "start": { + "line": 1, + "column": 47 + }, + "end": { + "line": 1, + "column": 72 + } + }, + "typeAnnotation": { + "type": "TSTypePredicate", + "start": 47, + "end": 72, + "loc": { + "start": { + "line": 1, + "column": 47 + }, + "end": { + "line": 1, + "column": 72 + } + }, + "parameterName": { + "type": "Identifier", + "start": 57, + "end": 62, + "loc": { + "start": { + "line": 1, + "column": 57 + }, + "end": { + "line": 1, + "column": 62 + }, + "identifierName": "value" + }, + "name": "value" + }, + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start": 66, + "end": 72, + "loc": { + "start": { + "line": 1, + "column": 66 + }, + "end": { + "line": 1, + "column": 72 + } + }, + "typeAnnotation": { + "type": "TSStringKeyword", + "start": 66, + "end": 72, + "loc": { + "start": { + "line": 1, + "column": 66 + }, + "end": { + "line": 1, + "column": 72 + } + } + } + }, + "asserts": true + } + }, + "declare": true + } + ], + "directives": [] + } +} From e256526b307c3f9a4a5ded634ce454a78cf909a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Fri, 8 Nov 2019 22:22:03 -0500 Subject: [PATCH 6/9] reimplement asserts-this --- .../src/plugins/typescript/index.js | 43 +++++++++---------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/packages/babel-parser/src/plugins/typescript/index.js b/packages/babel-parser/src/plugins/typescript/index.js index 5af5fe51a871..448c62c94abe 100644 --- a/packages/babel-parser/src/plugins/typescript/index.js +++ b/packages/babel-parser/src/plugins/typescript/index.js @@ -940,22 +940,26 @@ export default (superClass: Class): Class => this.tsParseTypePredicateAsserts.bind(this), ); - let typePredicateVariable = null; - if (this.tsIsIdentifier()) { - // TypePredicate - // : foo is type - typePredicateVariable = this.tsTryParse( - this.tsParseTypePredicatePrefix.bind(this), - ); - } else if (asserts && this.match(tt._this)) { - // When asserts is false, thisKeyword is handled by tsParseNonArraytype - // TypePredicate - // : this is type - typePredicateVariable = this.tsTryParse( - this.tsParseThisTypeOrThisTypePredicate.bind(this), - ); + 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; + node.asserts = true; + thisTypePredicate = this.finishNode(node, "TSTypePredicate"); + } + t.typeAnnotation = thisTypePredicate; + return this.finishNode(t, "TSTypeAnnotation"); } + const typePredicateVariable = + this.tsIsIdentifier() && + this.tsTryParse(this.tsParseTypePredicatePrefix.bind(this)); + if (!typePredicateVariable) { if (!asserts) { // : type @@ -963,19 +967,14 @@ export default (superClass: Class): Class => } const node = this.startNodeAtNode(t); - if (this.match(tt._this)) { - // : asserts this - node.parameterName = this.tsParseThisTypeNode(); - } else { - // : asserts foo - node.parameterName = this.parseIdentifier(); - } + // : asserts foo + node.parameterName = this.parseIdentifier(); node.asserts = asserts; t.typeAnnotation = this.finishNode(node, "TSTypePredicate"); return this.finishNode(t, "TSTypeAnnotation"); } - // : asserts TypePredicate + // : asserts foo is type const type = this.tsParseTypeAnnotation(/* eatColon */ false); const node = this.startNodeAtNode(t); node.parameterName = typePredicateVariable; From 2842e77b3f3d17486c2cc0f79ed8106366ab141b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Fri, 8 Nov 2019 22:59:30 -0500 Subject: [PATCH 7/9] refactor: improve tsParseTypePredicateAsserts --- .../src/plugins/typescript/index.js | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/packages/babel-parser/src/plugins/typescript/index.js b/packages/babel-parser/src/plugins/typescript/index.js index 448c62c94abe..8df9d4e7b239 100644 --- a/packages/babel-parser/src/plugins/typescript/index.js +++ b/packages/babel-parser/src/plugins/typescript/index.js @@ -1008,23 +1008,24 @@ export default (superClass: Class): Class => } tsParseTypePredicateAsserts(): boolean { - if (!this.tsIsIdentifier()) { + if ( + !this.match(tt.name) || + this.state.value !== "asserts" || + this.hasPrecedingLineBreak() + ) { return false; } - const containsEsc = this.state.containsEsc; - - const id = this.parseIdentifier(); - if ( - id.name !== "asserts" || - this.hasPrecedingLineBreak() || - (!this.tsIsIdentifier() && !this.match(tt._this)) - ) { + this.next(); + if (!this.match(tt.name) && !this.match(tt._this)) { return false; } if (containsEsc) { - this.raise(id.start, "Escape sequence in keyword asserts"); + this.raise( + this.state.lastTokStart, + "Escape sequence in keyword asserts", + ); } return true; 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 8/9] 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, From 0e491b393bcd52c0d4b4ba0f3afd92c8c0cc87e5 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:56:23 -0500 Subject: [PATCH 9/9] fix: update asserts types --- packages/babel-parser/src/types.js | 1 + packages/babel-types/src/definitions/typescript.js | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/babel-parser/src/types.js b/packages/babel-parser/src/types.js index 8392a58db0f2..82815b1443d6 100644 --- a/packages/babel-parser/src/types.js +++ b/packages/babel-parser/src/types.js @@ -1227,6 +1227,7 @@ export type TsTypePredicate = TsTypeBase & { type: "TSTypePredicate", parameterName: Identifier | TsThisType, typeAnnotation: TsTypeAnnotation, + asserts?: boolean, }; // `typeof` operator diff --git a/packages/babel-types/src/definitions/typescript.js b/packages/babel-types/src/definitions/typescript.js index bba8a5b73c8e..608ea8ab2207 100644 --- a/packages/babel-types/src/definitions/typescript.js +++ b/packages/babel-types/src/definitions/typescript.js @@ -175,11 +175,12 @@ defineType("TSTypeReference", { defineType("TSTypePredicate", { aliases: ["TSType"], - visitor: ["parameterName", "typeAnnotation", "asserts"], + visitor: ["parameterName", "typeAnnotation"], + builder: ["parameterName", "typeAnnotation", "asserts"], fields: { parameterName: validateType(["Identifier", "TSThisType"]), typeAnnotation: validateOptionalType("TSTypeAnnotation"), - asserts: validate(bool), + asserts: validateOptional(bool), }, });