From 732588c45145bc8a1f0182425183c45ce48ab758 Mon Sep 17 00:00:00 2001 From: Tan Li Hau Date: Sun, 13 Oct 2019 02:08:48 +0800 Subject: [PATCH 1/5] add asserts predicate --- .../src/plugins/typescript/index.js | 49 +- packages/babel-parser/src/types.js | 3 +- .../assert-predicate/arrow-function/input.ts | 3 + .../arrow-function/output.json | 638 ++++++++++++++++++ .../asserts-as-identifier/input.ts | 1 + .../asserts-as-identifier/output.json | 166 +++++ .../assert-predicate/asserts-var/input.ts | 1 + .../assert-predicate/asserts-var/output.json | 181 +++++ .../asserts-with-predicate/input.ts | 1 + .../asserts-with-predicate/output.json | 211 ++++++ .../function-declaration/input.ts | 3 + .../function-declaration/output.json | 536 +++++++++++++++ .../babel-types/src/definitions/typescript.js | 4 +- 13 files changed, 1788 insertions(+), 9 deletions(-) create mode 100644 packages/babel-parser/test/fixtures/typescript/assert-predicate/arrow-function/input.ts create mode 100644 packages/babel-parser/test/fixtures/typescript/assert-predicate/arrow-function/output.json create mode 100644 packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-as-identifier/input.ts create mode 100644 packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-as-identifier/output.json create mode 100644 packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-var/input.ts create mode 100644 packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-var/output.json create mode 100644 packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-with-predicate/input.ts create mode 100644 packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-with-predicate/output.json create mode 100644 packages/babel-parser/test/fixtures/typescript/assert-predicate/function-declaration/input.ts create mode 100644 packages/babel-parser/test/fixtures/typescript/assert-predicate/function-declaration/output.json diff --git a/packages/babel-parser/src/plugins/typescript/index.js b/packages/babel-parser/src/plugins/typescript/index.js index 8adf82f1568a..06d4f8e0f3f4 100644 --- a/packages/babel-parser/src/plugins/typescript/index.js +++ b/packages/babel-parser/src/plugins/typescript/index.js @@ -904,21 +904,33 @@ export default (superClass: Class): Class => const t: N.TsTypeAnnotation = this.startNode(); this.expect(returnToken); + const assertsModifier = this.tsTryParse( + this.tsParseTypePredicateAssertsModifier.bind(this), + ); + const typePredicateVariable = this.tsIsIdentifier() && this.tsTryParse(this.tsParseTypePredicatePrefix.bind(this)); - if (!typePredicateVariable) { + if (!assertsModifier && !typePredicateVariable) { return this.tsParseTypeAnnotation(/* eatColon */ false, t); } - const type = this.tsParseTypeAnnotation(/* eatColon */ false); + let node: N.TsTypePredicate; - const node: N.TsTypePredicate = this.startNodeAtNode( - typePredicateVariable, - ); - node.parameterName = typePredicateVariable; - node.typeAnnotation = type; + if (!typePredicateVariable) { + node = this.startNodeAtNode(assertsModifier); + node.parameterName = this.parseIdentifier(); + } else { + const type = this.tsParseTypeAnnotation(/* eatColon */ false); + node = this.startNodeAtNode( + assertsModifier ? assertsModifier : typePredicateVariable, + ); + node.parameterName = typePredicateVariable; + node.typeAnnotation = type; + } + + node.assertsModifier = assertsModifier; t.typeAnnotation = this.finishNode(node, "TSTypePredicate"); return this.finishNode(t, "TSTypeAnnotation"); }); @@ -946,6 +958,29 @@ export default (superClass: Class): Class => } } + tsParseTypePredicateAssertsModifier(): ?N.TsKeywordType { + if (!this.tsIsIdentifier()) { + return; + } + + const id = this.parseIdentifier(); + if ( + id.name !== "asserts" || + this.hasPrecedingLineBreak() || + !this.tsIsIdentifier() + ) { + return; + } + + const assertsKeyword = this.startNodeAtNode(id); + return this.finishNodeAt( + assertsKeyword, + "TSAssertsKeyword", + id.end, + id.loc.end, + ); + } + tsParseTypeAnnotation( eatColon = true, t: N.TsTypeAnnotation = this.startNode(), diff --git a/packages/babel-parser/src/types.js b/packages/babel-parser/src/types.js index 8560da3a3f73..3a440e234afc 100644 --- a/packages/babel-parser/src/types.js +++ b/packages/babel-parser/src/types.js @@ -1186,7 +1186,8 @@ export type TsKeywordTypeType = | "TSVoidKeyword" | "TSUndefinedKeyword" | "TSNullKeyword" - | "TSNeverKeyword"; + | "TSNeverKeyword" + | "TSAssertsKeyword"; export type TsKeywordType = TsTypeBase & { type: TsKeywordTypeType, }; diff --git a/packages/babel-parser/test/fixtures/typescript/assert-predicate/arrow-function/input.ts b/packages/babel-parser/test/fixtures/typescript/assert-predicate/arrow-function/input.ts new file mode 100644 index 000000000000..5989d8d4f2b4 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/assert-predicate/arrow-function/input.ts @@ -0,0 +1,3 @@ +const assert1 = (value: unknown): asserts value is string => {} +const assert2 = (value: unknown): asserts value => {} +const assert3 = (value: unknown): asserts => {} 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 new file mode 100644 index 000000000000..5a372009493c --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/assert-predicate/arrow-function/output.json @@ -0,0 +1,638 @@ +{ + "type": "File", + "start": 0, + "end": 165, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 47 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 165, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 47 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 63, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 63 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 6, + "end": 63, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 63 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 13 + }, + "identifierName": "assert1" + }, + "name": "assert1" + }, + "init": { + "type": "ArrowFunctionExpression", + "start": 16, + "end": 63, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 63 + } + }, + "returnType": { + "type": "TSTypeAnnotation", + "start": 32, + "end": 57, + "loc": { + "start": { + "line": 1, + "column": 32 + }, + "end": { + "line": 1, + "column": 57 + } + }, + "typeAnnotation": { + "type": "TSTypePredicate", + "start": 34, + "end": 57, + "loc": { + "start": { + "line": 1, + "column": 34 + }, + "end": { + "line": 1, + "column": 57 + } + }, + "parameterName": { + "type": "Identifier", + "start": 42, + "end": 47, + "loc": { + "start": { + "line": 1, + "column": 42 + }, + "end": { + "line": 1, + "column": 47 + }, + "identifierName": "value" + }, + "name": "value" + }, + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start": 51, + "end": 57, + "loc": { + "start": { + "line": 1, + "column": 51 + }, + "end": { + "line": 1, + "column": 57 + } + }, + "typeAnnotation": { + "type": "TSStringKeyword", + "start": 51, + "end": 57, + "loc": { + "start": { + "line": 1, + "column": 51 + }, + "end": { + "line": 1, + "column": 57 + } + } + } + }, + "assertsModifier": { + "type": "TSAssertsKeyword", + "start": 34, + "end": 41, + "loc": { + "start": { + "line": 1, + "column": 34 + }, + "end": { + "line": 1, + "column": 41 + } + } + } + } + }, + "id": null, + "generator": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 17, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 31 + }, + "identifierName": "value" + }, + "name": "value", + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start": 22, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 31 + } + }, + "typeAnnotation": { + "type": "TSUnknownKeyword", + "start": 24, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 24 + }, + "end": { + "line": 1, + "column": 31 + } + } + } + } + } + ], + "body": { + "type": "BlockStatement", + "start": 61, + "end": 63, + "loc": { + "start": { + "line": 1, + "column": 61 + }, + "end": { + "line": 1, + "column": 63 + } + }, + "body": [], + "directives": [] + } + } + } + ], + "kind": "const" + }, + { + "type": "VariableDeclaration", + "start": 64, + "end": 117, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 53 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 70, + "end": 117, + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 53 + } + }, + "id": { + "type": "Identifier", + "start": 70, + "end": 77, + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 13 + }, + "identifierName": "assert2" + }, + "name": "assert2" + }, + "init": { + "type": "ArrowFunctionExpression", + "start": 80, + "end": 117, + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 2, + "column": 53 + } + }, + "returnType": { + "type": "TSTypeAnnotation", + "start": 96, + "end": 111, + "loc": { + "start": { + "line": 2, + "column": 32 + }, + "end": { + "line": 2, + "column": 47 + } + }, + "typeAnnotation": { + "type": "TSTypePredicate", + "start": 98, + "end": 111, + "loc": { + "start": { + "line": 2, + "column": 34 + }, + "end": { + "line": 2, + "column": 47 + } + }, + "parameterName": { + "type": "Identifier", + "start": 106, + "end": 111, + "loc": { + "start": { + "line": 2, + "column": 42 + }, + "end": { + "line": 2, + "column": 47 + }, + "identifierName": "value" + }, + "name": "value" + }, + "assertsModifier": { + "type": "TSAssertsKeyword", + "start": 98, + "end": 105, + "loc": { + "start": { + "line": 2, + "column": 34 + }, + "end": { + "line": 2, + "column": 41 + } + } + } + } + }, + "id": null, + "generator": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 81, + "end": 95, + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 31 + }, + "identifierName": "value" + }, + "name": "value", + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start": 86, + "end": 95, + "loc": { + "start": { + "line": 2, + "column": 22 + }, + "end": { + "line": 2, + "column": 31 + } + }, + "typeAnnotation": { + "type": "TSUnknownKeyword", + "start": 88, + "end": 95, + "loc": { + "start": { + "line": 2, + "column": 24 + }, + "end": { + "line": 2, + "column": 31 + } + } + } + } + } + ], + "body": { + "type": "BlockStatement", + "start": 115, + "end": 117, + "loc": { + "start": { + "line": 2, + "column": 51 + }, + "end": { + "line": 2, + "column": 53 + } + }, + "body": [], + "directives": [] + } + } + } + ], + "kind": "const" + }, + { + "type": "VariableDeclaration", + "start": 118, + "end": 165, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 47 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 124, + "end": 165, + "loc": { + "start": { + "line": 3, + "column": 6 + }, + "end": { + "line": 3, + "column": 47 + } + }, + "id": { + "type": "Identifier", + "start": 124, + "end": 131, + "loc": { + "start": { + "line": 3, + "column": 6 + }, + "end": { + "line": 3, + "column": 13 + }, + "identifierName": "assert3" + }, + "name": "assert3" + }, + "init": { + "type": "ArrowFunctionExpression", + "start": 134, + "end": 165, + "loc": { + "start": { + "line": 3, + "column": 16 + }, + "end": { + "line": 3, + "column": 47 + } + }, + "returnType": { + "type": "TSTypeAnnotation", + "start": 150, + "end": 159, + "loc": { + "start": { + "line": 3, + "column": 32 + }, + "end": { + "line": 3, + "column": 41 + } + }, + "typeAnnotation": { + "type": "TSTypeReference", + "start": 152, + "end": 159, + "loc": { + "start": { + "line": 3, + "column": 34 + }, + "end": { + "line": 3, + "column": 41 + } + }, + "typeName": { + "type": "Identifier", + "start": 152, + "end": 159, + "loc": { + "start": { + "line": 3, + "column": 34 + }, + "end": { + "line": 3, + "column": 41 + }, + "identifierName": "asserts" + }, + "name": "asserts" + } + } + }, + "id": null, + "generator": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 135, + "end": 149, + "loc": { + "start": { + "line": 3, + "column": 17 + }, + "end": { + "line": 3, + "column": 31 + }, + "identifierName": "value" + }, + "name": "value", + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start": 140, + "end": 149, + "loc": { + "start": { + "line": 3, + "column": 22 + }, + "end": { + "line": 3, + "column": 31 + } + }, + "typeAnnotation": { + "type": "TSUnknownKeyword", + "start": 142, + "end": 149, + "loc": { + "start": { + "line": 3, + "column": 24 + }, + "end": { + "line": 3, + "column": 31 + } + } + } + } + } + ], + "body": { + "type": "BlockStatement", + "start": 163, + "end": 165, + "loc": { + "start": { + "line": 3, + "column": 45 + }, + "end": { + "line": 3, + "column": 47 + } + }, + "body": [], + "directives": [] + } + } + } + ], + "kind": "const" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-as-identifier/input.ts b/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-as-identifier/input.ts new file mode 100644 index 000000000000..d5ee9a42ce01 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-as-identifier/input.ts @@ -0,0 +1 @@ +declare function assertIsString(value: unknown): asserts; diff --git a/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-as-identifier/output.json b/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-as-identifier/output.json new file mode 100644 index 000000000000..dc6f8702bdce --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-as-identifier/output.json @@ -0,0 +1,166 @@ +{ + "type": "File", + "start": 0, + "end": 57, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 57 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 57, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 57 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "TSDeclareFunction", + "start": 0, + "end": 57, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 57 + } + }, + "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": 56, + "loc": { + "start": { + "line": 1, + "column": 47 + }, + "end": { + "line": 1, + "column": 56 + } + }, + "typeAnnotation": { + "type": "TSTypeReference", + "start": 49, + "end": 56, + "loc": { + "start": { + "line": 1, + "column": 49 + }, + "end": { + "line": 1, + "column": 56 + } + }, + "typeName": { + "type": "Identifier", + "start": 49, + "end": 56, + "loc": { + "start": { + "line": 1, + "column": 49 + }, + "end": { + "line": 1, + "column": 56 + }, + "identifierName": "asserts" + }, + "name": "asserts" + } + } + }, + "declare": true + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-var/input.ts b/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-var/input.ts new file mode 100644 index 000000000000..63613906e9fd --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-var/input.ts @@ -0,0 +1 @@ +declare function assertIsString(value: unknown): asserts value; 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 new file mode 100644 index 000000000000..6654e164cdb5 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-var/output.json @@ -0,0 +1,181 @@ +{ + "type": "File", + "start": 0, + "end": 63, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 63 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 63, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 63 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "TSDeclareFunction", + "start": 0, + "end": 63, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 63 + } + }, + "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": 62, + "loc": { + "start": { + "line": 1, + "column": 47 + }, + "end": { + "line": 1, + "column": 62 + } + }, + "typeAnnotation": { + "type": "TSTypePredicate", + "start": 49, + "end": 62, + "loc": { + "start": { + "line": 1, + "column": 49 + }, + "end": { + "line": 1, + "column": 62 + } + }, + "parameterName": { + "type": "Identifier", + "start": 57, + "end": 62, + "loc": { + "start": { + "line": 1, + "column": 57 + }, + "end": { + "line": 1, + "column": 62 + }, + "identifierName": "value" + }, + "name": "value" + }, + "assertsModifier": { + "type": "TSAssertsKeyword", + "start": 49, + "end": 56, + "loc": { + "start": { + "line": 1, + "column": 49 + }, + "end": { + "line": 1, + "column": 56 + } + } + } + } + }, + "declare": true + } + ], + "directives": [] + } +} \ No newline at end of file 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-with-predicate/input.ts new file mode 100644 index 000000000000..9456a5cb2e50 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-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/asserts-with-predicate/output.json b/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-with-predicate/output.json new file mode 100644 index 000000000000..2615ded5136d --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-with-predicate/output.json @@ -0,0 +1,211 @@ +{ + "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": 49, + "end": 72, + "loc": { + "start": { + "line": 1, + "column": 49 + }, + "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 + } + } + } + }, + "assertsModifier": { + "type": "TSAssertsKeyword", + "start": 49, + "end": 56, + "loc": { + "start": { + "line": 1, + "column": 49 + }, + "end": { + "line": 1, + "column": 56 + } + } + } + } + }, + "declare": true + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/assert-predicate/function-declaration/input.ts b/packages/babel-parser/test/fixtures/typescript/assert-predicate/function-declaration/input.ts new file mode 100644 index 000000000000..b04da8349ca2 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/assert-predicate/function-declaration/input.ts @@ -0,0 +1,3 @@ +function asserts1 (value: unknown): asserts value is string {} +function asserts2 (value: unknown): asserts value {} +function asserts3 (value: unknown): asserts {} 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 new file mode 100644 index 000000000000..e9332dff47f8 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/assert-predicate/function-declaration/output.json @@ -0,0 +1,536 @@ +{ + "type": "File", + "start": 0, + "end": 163, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 46 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 163, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 46 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "FunctionDeclaration", + "start": 0, + "end": 62, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 62 + } + }, + "id": { + "type": "Identifier", + "start": 9, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 17 + }, + "identifierName": "asserts1" + }, + "name": "asserts1" + }, + "generator": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 19, + "end": 33, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 33 + }, + "identifierName": "value" + }, + "name": "value", + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start": 24, + "end": 33, + "loc": { + "start": { + "line": 1, + "column": 24 + }, + "end": { + "line": 1, + "column": 33 + } + }, + "typeAnnotation": { + "type": "TSUnknownKeyword", + "start": 26, + "end": 33, + "loc": { + "start": { + "line": 1, + "column": 26 + }, + "end": { + "line": 1, + "column": 33 + } + } + } + } + } + ], + "returnType": { + "type": "TSTypeAnnotation", + "start": 34, + "end": 59, + "loc": { + "start": { + "line": 1, + "column": 34 + }, + "end": { + "line": 1, + "column": 59 + } + }, + "typeAnnotation": { + "type": "TSTypePredicate", + "start": 36, + "end": 59, + "loc": { + "start": { + "line": 1, + "column": 36 + }, + "end": { + "line": 1, + "column": 59 + } + }, + "parameterName": { + "type": "Identifier", + "start": 44, + "end": 49, + "loc": { + "start": { + "line": 1, + "column": 44 + }, + "end": { + "line": 1, + "column": 49 + }, + "identifierName": "value" + }, + "name": "value" + }, + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start": 53, + "end": 59, + "loc": { + "start": { + "line": 1, + "column": 53 + }, + "end": { + "line": 1, + "column": 59 + } + }, + "typeAnnotation": { + "type": "TSStringKeyword", + "start": 53, + "end": 59, + "loc": { + "start": { + "line": 1, + "column": 53 + }, + "end": { + "line": 1, + "column": 59 + } + } + } + }, + "assertsModifier": { + "type": "TSAssertsKeyword", + "start": 36, + "end": 43, + "loc": { + "start": { + "line": 1, + "column": 36 + }, + "end": { + "line": 1, + "column": 43 + } + } + } + } + }, + "body": { + "type": "BlockStatement", + "start": 60, + "end": 62, + "loc": { + "start": { + "line": 1, + "column": 60 + }, + "end": { + "line": 1, + "column": 62 + } + }, + "body": [], + "directives": [] + } + }, + { + "type": "FunctionDeclaration", + "start": 64, + "end": 116, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 52 + } + }, + "id": { + "type": "Identifier", + "start": 73, + "end": 81, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 17 + }, + "identifierName": "asserts2" + }, + "name": "asserts2" + }, + "generator": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 83, + "end": 97, + "loc": { + "start": { + "line": 2, + "column": 19 + }, + "end": { + "line": 2, + "column": 33 + }, + "identifierName": "value" + }, + "name": "value", + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start": 88, + "end": 97, + "loc": { + "start": { + "line": 2, + "column": 24 + }, + "end": { + "line": 2, + "column": 33 + } + }, + "typeAnnotation": { + "type": "TSUnknownKeyword", + "start": 90, + "end": 97, + "loc": { + "start": { + "line": 2, + "column": 26 + }, + "end": { + "line": 2, + "column": 33 + } + } + } + } + } + ], + "returnType": { + "type": "TSTypeAnnotation", + "start": 98, + "end": 113, + "loc": { + "start": { + "line": 2, + "column": 34 + }, + "end": { + "line": 2, + "column": 49 + } + }, + "typeAnnotation": { + "type": "TSTypePredicate", + "start": 100, + "end": 113, + "loc": { + "start": { + "line": 2, + "column": 36 + }, + "end": { + "line": 2, + "column": 49 + } + }, + "parameterName": { + "type": "Identifier", + "start": 108, + "end": 113, + "loc": { + "start": { + "line": 2, + "column": 44 + }, + "end": { + "line": 2, + "column": 49 + }, + "identifierName": "value" + }, + "name": "value" + }, + "assertsModifier": { + "type": "TSAssertsKeyword", + "start": 100, + "end": 107, + "loc": { + "start": { + "line": 2, + "column": 36 + }, + "end": { + "line": 2, + "column": 43 + } + } + } + } + }, + "body": { + "type": "BlockStatement", + "start": 114, + "end": 116, + "loc": { + "start": { + "line": 2, + "column": 50 + }, + "end": { + "line": 2, + "column": 52 + } + }, + "body": [], + "directives": [] + } + }, + { + "type": "FunctionDeclaration", + "start": 117, + "end": 163, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 46 + } + }, + "id": { + "type": "Identifier", + "start": 126, + "end": 134, + "loc": { + "start": { + "line": 3, + "column": 9 + }, + "end": { + "line": 3, + "column": 17 + }, + "identifierName": "asserts3" + }, + "name": "asserts3" + }, + "generator": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 136, + "end": 150, + "loc": { + "start": { + "line": 3, + "column": 19 + }, + "end": { + "line": 3, + "column": 33 + }, + "identifierName": "value" + }, + "name": "value", + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start": 141, + "end": 150, + "loc": { + "start": { + "line": 3, + "column": 24 + }, + "end": { + "line": 3, + "column": 33 + } + }, + "typeAnnotation": { + "type": "TSUnknownKeyword", + "start": 143, + "end": 150, + "loc": { + "start": { + "line": 3, + "column": 26 + }, + "end": { + "line": 3, + "column": 33 + } + } + } + } + } + ], + "returnType": { + "type": "TSTypeAnnotation", + "start": 151, + "end": 160, + "loc": { + "start": { + "line": 3, + "column": 34 + }, + "end": { + "line": 3, + "column": 43 + } + }, + "typeAnnotation": { + "type": "TSTypeReference", + "start": 153, + "end": 160, + "loc": { + "start": { + "line": 3, + "column": 36 + }, + "end": { + "line": 3, + "column": 43 + } + }, + "typeName": { + "type": "Identifier", + "start": 153, + "end": 160, + "loc": { + "start": { + "line": 3, + "column": 36 + }, + "end": { + "line": 3, + "column": 43 + }, + "identifierName": "asserts" + }, + "name": "asserts" + } + } + }, + "body": { + "type": "BlockStatement", + "start": 161, + "end": 163, + "loc": { + "start": { + "line": 3, + "column": 44 + }, + "end": { + "line": 3, + "column": 46 + } + }, + "body": [], + "directives": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-types/src/definitions/typescript.js b/packages/babel-types/src/definitions/typescript.js index af34dbfc6255..bcdb878b22c5 100644 --- a/packages/babel-types/src/definitions/typescript.js +++ b/packages/babel-types/src/definitions/typescript.js @@ -139,6 +139,7 @@ const tsKeywordTypes = [ "TSUndefinedKeyword", "TSUnknownKeyword", "TSVoidKeyword", + "TSAssertsKeyword", ]; for (const type of tsKeywordTypes) { @@ -175,8 +176,9 @@ defineType("TSTypeReference", { defineType("TSTypePredicate", { aliases: ["TSType"], - visitor: ["parameterName", "typeAnnotation"], + visitor: ["parameterName", "typeAnnotation", "assertsModifier"], fields: { + assertsModifier: validateOptionalType("TSAssertsKeyword"), parameterName: validateType(["Identifier", "TSThisType"]), typeAnnotation: validateType("TSTypeAnnotation"), }, From 5c86f681ac8a02c9d2908fcbf99339627ea9a7c5 Mon Sep 17 00:00:00 2001 From: Tan Li Hau Date: Sun, 13 Oct 2019 12:22:49 +0800 Subject: [PATCH 2/5] fix flow --- .../src/plugins/typescript/index.js | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/packages/babel-parser/src/plugins/typescript/index.js b/packages/babel-parser/src/plugins/typescript/index.js index 06d4f8e0f3f4..1b211762e2ec 100644 --- a/packages/babel-parser/src/plugins/typescript/index.js +++ b/packages/babel-parser/src/plugins/typescript/index.js @@ -912,24 +912,27 @@ export default (superClass: Class): Class => this.tsIsIdentifier() && this.tsTryParse(this.tsParseTypePredicatePrefix.bind(this)); - if (!assertsModifier && !typePredicateVariable) { - return this.tsParseTypeAnnotation(/* eatColon */ false, t); - } - - let node: N.TsTypePredicate; - if (!typePredicateVariable) { - node = this.startNodeAtNode(assertsModifier); + if (!assertsModifier) { + // : type + return this.tsParseTypeAnnotation(/* eatColon */ false, t); + } + + // : asserts foo + const node = this.startNodeAtNode(assertsModifier); node.parameterName = this.parseIdentifier(); - } else { - const type = this.tsParseTypeAnnotation(/* eatColon */ false); - node = this.startNodeAtNode( - assertsModifier ? assertsModifier : typePredicateVariable, - ); - node.parameterName = typePredicateVariable; - node.typeAnnotation = type; + node.assertsModifier = assertsModifier; + t.typeAnnotation = this.finishNode(node, "TSTypePredicate"); + return this.finishNode(t, "TSTypeAnnotation"); } + // : foo is type + const type = this.tsParseTypeAnnotation(/* eatColon */ false); + const node = this.startNodeAtNode( + assertsModifier ? assertsModifier : typePredicateVariable, + ); + node.parameterName = typePredicateVariable; + node.typeAnnotation = type; node.assertsModifier = assertsModifier; t.typeAnnotation = this.finishNode(node, "TSTypePredicate"); return this.finishNode(t, "TSTypeAnnotation"); From 1991a7e84c318e9abfe2b01a173b20e307b438dc Mon Sep 17 00:00:00 2001 From: Tan Li Hau Date: Sun, 13 Oct 2019 12:58:23 +0800 Subject: [PATCH 3/5] babel-generator for typescript assertions --- .../src/generators/typescript.js | 17 +++++++++++++---- .../arrow-function-assertion/input.js | 2 ++ .../arrow-function-assertion/output.js | 3 +++ .../typescript/function-assertion/input.js | 2 ++ .../typescript/function-assertion/output.js | 3 +++ 5 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 packages/babel-generator/test/fixtures/typescript/arrow-function-assertion/input.js create mode 100644 packages/babel-generator/test/fixtures/typescript/arrow-function-assertion/output.js create mode 100644 packages/babel-generator/test/fixtures/typescript/function-assertion/input.js create mode 100644 packages/babel-generator/test/fixtures/typescript/function-assertion/output.js diff --git a/packages/babel-generator/src/generators/typescript.js b/packages/babel-generator/src/generators/typescript.js index 4ff2fe1ad188..e21ee7649013 100644 --- a/packages/babel-generator/src/generators/typescript.js +++ b/packages/babel-generator/src/generators/typescript.js @@ -162,6 +162,9 @@ export function TSNullKeyword() { export function TSNeverKeyword() { this.word("never"); } +export function TSAssertsKeyword() { + this.word("asserts"); +} export function TSThisType() { this.word("this"); @@ -197,11 +200,17 @@ export function TSTypeReference(node) { } export function TSTypePredicate(node) { + if (node.assertsModifier) { + this.print(node.assertsModifier); + this.space(); + } this.print(node.parameterName); - this.space(); - this.word("is"); - this.space(); - this.print(node.typeAnnotation.typeAnnotation); + if (node.typeAnnotation) { + this.space(); + this.word("is"); + this.space(); + this.print(node.typeAnnotation.typeAnnotation); + } } export function TSTypeQuery(node) { diff --git a/packages/babel-generator/test/fixtures/typescript/arrow-function-assertion/input.js b/packages/babel-generator/test/fixtures/typescript/arrow-function-assertion/input.js new file mode 100644 index 000000000000..67e467cda8ed --- /dev/null +++ b/packages/babel-generator/test/fixtures/typescript/arrow-function-assertion/input.js @@ -0,0 +1,2 @@ +(x: any): asserts x => true; +(x: any): asserts x is boolean => true; \ No newline at end of file diff --git a/packages/babel-generator/test/fixtures/typescript/arrow-function-assertion/output.js b/packages/babel-generator/test/fixtures/typescript/arrow-function-assertion/output.js new file mode 100644 index 000000000000..b0befa69dc24 --- /dev/null +++ b/packages/babel-generator/test/fixtures/typescript/arrow-function-assertion/output.js @@ -0,0 +1,3 @@ +(x: any): asserts x => true; + +(x: any): asserts x is boolean => true; \ No newline at end of file diff --git a/packages/babel-generator/test/fixtures/typescript/function-assertion/input.js b/packages/babel-generator/test/fixtures/typescript/function-assertion/input.js new file mode 100644 index 000000000000..17db65c5a77f --- /dev/null +++ b/packages/babel-generator/test/fixtures/typescript/function-assertion/input.js @@ -0,0 +1,2 @@ +function f(x: any): asserts x {} +(function(x: any): asserts x is boolean {}) diff --git a/packages/babel-generator/test/fixtures/typescript/function-assertion/output.js b/packages/babel-generator/test/fixtures/typescript/function-assertion/output.js new file mode 100644 index 000000000000..b55c84620cc9 --- /dev/null +++ b/packages/babel-generator/test/fixtures/typescript/function-assertion/output.js @@ -0,0 +1,3 @@ +function f(x: any): asserts x {} + +(function (x: any): asserts x is boolean {}); \ No newline at end of file From 637bdbc60deedfc216bcf92ed095a08293490b8f Mon Sep 17 00:00:00 2001 From: Tan Li Hau Date: Sun, 13 Oct 2019 12:58:44 +0800 Subject: [PATCH 4/5] babel-types for typescript assertions --- .../babel-types/src/asserts/generated/index.js | 3 +++ .../babel-types/src/builders/generated/index.js | 5 +++++ .../babel-types/src/definitions/typescript.js | 2 +- .../babel-types/src/validators/generated/index.js | 15 +++++++++++++++ 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/packages/babel-types/src/asserts/generated/index.js b/packages/babel-types/src/asserts/generated/index.js index 60a91b88178d..db77578f01d1 100644 --- a/packages/babel-types/src/asserts/generated/index.js +++ b/packages/babel-types/src/asserts/generated/index.js @@ -836,6 +836,9 @@ export function assertTSUnknownKeyword(node: Object, opts?: Object = {}): void { export function assertTSVoidKeyword(node: Object, opts?: Object = {}): void { assert("TSVoidKeyword", node, opts); } +export function assertTSAssertsKeyword(node: Object, opts?: Object = {}): void { + assert("TSAssertsKeyword", node, opts); +} export function assertTSThisType(node: Object, opts?: Object = {}): void { assert("TSThisType", node, opts); } diff --git a/packages/babel-types/src/builders/generated/index.js b/packages/babel-types/src/builders/generated/index.js index 2cb9d5490c9d..926d4e3d62d8 100644 --- a/packages/babel-types/src/builders/generated/index.js +++ b/packages/babel-types/src/builders/generated/index.js @@ -781,6 +781,11 @@ export function TSVoidKeyword(...args: Array): Object { } export { TSVoidKeyword as tsVoidKeyword }; export { TSVoidKeyword as tSVoidKeyword }; +export function TSAssertsKeyword(...args: Array): Object { + return builder("TSAssertsKeyword", ...args); +} +export { TSAssertsKeyword as tsAssertsKeyword }; +export { TSAssertsKeyword as tSAssertsKeyword }; export function TSThisType(...args: Array): Object { return builder("TSThisType", ...args); } diff --git a/packages/babel-types/src/definitions/typescript.js b/packages/babel-types/src/definitions/typescript.js index bcdb878b22c5..c3a650f69e0c 100644 --- a/packages/babel-types/src/definitions/typescript.js +++ b/packages/babel-types/src/definitions/typescript.js @@ -180,7 +180,7 @@ defineType("TSTypePredicate", { fields: { assertsModifier: validateOptionalType("TSAssertsKeyword"), parameterName: validateType(["Identifier", "TSThisType"]), - typeAnnotation: validateType("TSTypeAnnotation"), + typeAnnotation: validateOptionalType("TSTypeAnnotation"), }, }); diff --git a/packages/babel-types/src/validators/generated/index.js b/packages/babel-types/src/validators/generated/index.js index 84f44ef882cd..15ebabecaeee 100644 --- a/packages/babel-types/src/validators/generated/index.js +++ b/packages/babel-types/src/validators/generated/index.js @@ -2691,6 +2691,20 @@ export function isTSVoidKeyword(node: ?Object, opts?: Object): boolean { return false; } +export function isTSAssertsKeyword(node: ?Object, opts?: Object): boolean { + if (!node) return false; + + const nodeType = node.type; + if (nodeType === "TSAssertsKeyword") { + if (typeof opts === "undefined") { + return true; + } else { + return shallowEqual(node, opts); + } + } + + return false; +} export function isTSThisType(node: ?Object, opts?: Object): boolean { if (!node) return false; @@ -4365,6 +4379,7 @@ export function isTSType(node: ?Object, opts?: Object): boolean { "TSUndefinedKeyword" === nodeType || "TSUnknownKeyword" === nodeType || "TSVoidKeyword" === nodeType || + "TSAssertsKeyword" === nodeType || "TSThisType" === nodeType || "TSFunctionType" === nodeType || "TSConstructorType" === nodeType || From 128cd3dd627e8cfd346b1ba58d4b03fe02b9c02b Mon Sep 17 00:00:00 2001 From: Tan Li Hau Date: Mon, 14 Oct 2019 22:22:09 +0800 Subject: [PATCH 5/5] asserts modifier as boolean --- .../src/generators/typescript.js | 5 +-- .../src/plugins/typescript/index.js | 20 +++------- packages/babel-parser/src/types.js | 3 +- .../predicate-types/output.json | 4 +- .../arrow-function/output.json | 40 +++---------------- .../assert-predicate/asserts-var/output.json | 20 ++-------- .../asserts-with-predicate/output.json | 20 ++-------- .../function-declaration/output.json | 40 +++---------------- .../function/predicate-types/output.json | 8 ++-- .../src/asserts/generated/index.js | 3 -- .../src/builders/generated/index.js | 5 --- .../babel-types/src/definitions/typescript.js | 3 +- .../src/validators/generated/index.js | 15 ------- 13 files changed, 33 insertions(+), 153 deletions(-) diff --git a/packages/babel-generator/src/generators/typescript.js b/packages/babel-generator/src/generators/typescript.js index e21ee7649013..aa5e4ce4cde6 100644 --- a/packages/babel-generator/src/generators/typescript.js +++ b/packages/babel-generator/src/generators/typescript.js @@ -162,9 +162,6 @@ export function TSNullKeyword() { export function TSNeverKeyword() { this.word("never"); } -export function TSAssertsKeyword() { - this.word("asserts"); -} export function TSThisType() { this.word("this"); @@ -201,7 +198,7 @@ export function TSTypeReference(node) { export function TSTypePredicate(node) { if (node.assertsModifier) { - this.print(node.assertsModifier); + this.word("asserts"); this.space(); } this.print(node.parameterName); diff --git a/packages/babel-parser/src/plugins/typescript/index.js b/packages/babel-parser/src/plugins/typescript/index.js index 1b211762e2ec..7ebe0ad44420 100644 --- a/packages/babel-parser/src/plugins/typescript/index.js +++ b/packages/babel-parser/src/plugins/typescript/index.js @@ -919,7 +919,7 @@ export default (superClass: Class): Class => } // : asserts foo - const node = this.startNodeAtNode(assertsModifier); + const node = this.startNodeAtNode(t); node.parameterName = this.parseIdentifier(); node.assertsModifier = assertsModifier; t.typeAnnotation = this.finishNode(node, "TSTypePredicate"); @@ -928,9 +928,7 @@ export default (superClass: Class): Class => // : foo is type const type = this.tsParseTypeAnnotation(/* eatColon */ false); - const node = this.startNodeAtNode( - assertsModifier ? assertsModifier : typePredicateVariable, - ); + const node = this.startNodeAtNode(t); node.parameterName = typePredicateVariable; node.typeAnnotation = type; node.assertsModifier = assertsModifier; @@ -961,9 +959,9 @@ export default (superClass: Class): Class => } } - tsParseTypePredicateAssertsModifier(): ?N.TsKeywordType { + tsParseTypePredicateAssertsModifier(): boolean { if (!this.tsIsIdentifier()) { - return; + return false; } const id = this.parseIdentifier(); @@ -972,16 +970,10 @@ export default (superClass: Class): Class => this.hasPrecedingLineBreak() || !this.tsIsIdentifier() ) { - return; + return false; } - const assertsKeyword = this.startNodeAtNode(id); - return this.finishNodeAt( - assertsKeyword, - "TSAssertsKeyword", - id.end, - id.loc.end, - ); + return true; } tsParseTypeAnnotation( diff --git a/packages/babel-parser/src/types.js b/packages/babel-parser/src/types.js index 3a440e234afc..8560da3a3f73 100644 --- a/packages/babel-parser/src/types.js +++ b/packages/babel-parser/src/types.js @@ -1186,8 +1186,7 @@ export type TsKeywordTypeType = | "TSVoidKeyword" | "TSUndefinedKeyword" | "TSNullKeyword" - | "TSNeverKeyword" - | "TSAssertsKeyword"; + | "TSNeverKeyword"; export type TsKeywordType = TsTypeBase & { type: TsKeywordTypeType, }; diff --git a/packages/babel-parser/test/fixtures/typescript/arrow-function/predicate-types/output.json b/packages/babel-parser/test/fixtures/typescript/arrow-function/predicate-types/output.json index e1f9142855b1..45239b7a0c11 100644 --- a/packages/babel-parser/test/fixtures/typescript/arrow-function/predicate-types/output.json +++ b/packages/babel-parser/test/fixtures/typescript/arrow-function/predicate-types/output.json @@ -73,12 +73,12 @@ }, "typeAnnotation": { "type": "TSTypePredicate", - "start": 10, + "start": 8, "end": 21, "loc": { "start": { "line": 1, - "column": 10 + "column": 8 }, "end": { "line": 1, 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 5a372009493c..e368637de0a9 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 @@ -105,12 +105,12 @@ }, "typeAnnotation": { "type": "TSTypePredicate", - "start": 34, + "start": 32, "end": 57, "loc": { "start": { "line": 1, - "column": 34 + "column": 32 }, "end": { "line": 1, @@ -164,21 +164,7 @@ } } }, - "assertsModifier": { - "type": "TSAssertsKeyword", - "start": 34, - "end": 41, - "loc": { - "start": { - "line": 1, - "column": 34 - }, - "end": { - "line": 1, - "column": 41 - } - } - } + "assertsModifier": true } }, "id": null, @@ -331,12 +317,12 @@ }, "typeAnnotation": { "type": "TSTypePredicate", - "start": 98, + "start": 96, "end": 111, "loc": { "start": { "line": 2, - "column": 34 + "column": 32 }, "end": { "line": 2, @@ -360,21 +346,7 @@ }, "name": "value" }, - "assertsModifier": { - "type": "TSAssertsKeyword", - "start": 98, - "end": 105, - "loc": { - "start": { - "line": 2, - "column": 34 - }, - "end": { - "line": 2, - "column": 41 - } - } - } + "assertsModifier": true } }, "id": null, 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 6654e164cdb5..a7b32e23d170 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 @@ -127,12 +127,12 @@ }, "typeAnnotation": { "type": "TSTypePredicate", - "start": 49, + "start": 47, "end": 62, "loc": { "start": { "line": 1, - "column": 49 + "column": 47 }, "end": { "line": 1, @@ -156,21 +156,7 @@ }, "name": "value" }, - "assertsModifier": { - "type": "TSAssertsKeyword", - "start": 49, - "end": 56, - "loc": { - "start": { - "line": 1, - "column": 49 - }, - "end": { - "line": 1, - "column": 56 - } - } - } + "assertsModifier": true } }, "declare": true 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-with-predicate/output.json index 2615ded5136d..ca83a725e2d5 100644 --- a/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-with-predicate/output.json +++ b/packages/babel-parser/test/fixtures/typescript/assert-predicate/asserts-with-predicate/output.json @@ -127,12 +127,12 @@ }, "typeAnnotation": { "type": "TSTypePredicate", - "start": 49, + "start": 47, "end": 72, "loc": { "start": { "line": 1, - "column": 49 + "column": 47 }, "end": { "line": 1, @@ -186,21 +186,7 @@ } } }, - "assertsModifier": { - "type": "TSAssertsKeyword", - "start": 49, - "end": 56, - "loc": { - "start": { - "line": 1, - "column": 49 - }, - "end": { - "line": 1, - "column": 56 - } - } - } + "assertsModifier": true } }, "declare": true 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 e9332dff47f8..96aea85b2926 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 @@ -127,12 +127,12 @@ }, "typeAnnotation": { "type": "TSTypePredicate", - "start": 36, + "start": 34, "end": 59, "loc": { "start": { "line": 1, - "column": 36 + "column": 34 }, "end": { "line": 1, @@ -186,21 +186,7 @@ } } }, - "assertsModifier": { - "type": "TSAssertsKeyword", - "start": 36, - "end": 43, - "loc": { - "start": { - "line": 1, - "column": 36 - }, - "end": { - "line": 1, - "column": 43 - } - } - } + "assertsModifier": true } }, "body": { @@ -319,12 +305,12 @@ }, "typeAnnotation": { "type": "TSTypePredicate", - "start": 100, + "start": 98, "end": 113, "loc": { "start": { "line": 2, - "column": 36 + "column": 34 }, "end": { "line": 2, @@ -348,21 +334,7 @@ }, "name": "value" }, - "assertsModifier": { - "type": "TSAssertsKeyword", - "start": 100, - "end": 107, - "loc": { - "start": { - "line": 2, - "column": 36 - }, - "end": { - "line": 2, - "column": 43 - } - } - } + "assertsModifier": true } }, "body": { diff --git a/packages/babel-parser/test/fixtures/typescript/function/predicate-types/output.json b/packages/babel-parser/test/fixtures/typescript/function/predicate-types/output.json index 85ab4987f1c2..90bae5b4ceda 100644 --- a/packages/babel-parser/test/fixtures/typescript/function/predicate-types/output.json +++ b/packages/babel-parser/test/fixtures/typescript/function/predicate-types/output.json @@ -127,12 +127,12 @@ }, "typeAnnotation": { "type": "TSTypePredicate", - "start": 20, + "start": 18, "end": 32, "loc": { "start": { "line": 1, - "column": 20 + "column": 18 }, "end": { "line": 1, @@ -302,12 +302,12 @@ }, "typeAnnotation": { "type": "TSTypePredicate", - "start": 55, + "start": 53, "end": 67, "loc": { "start": { "line": 2, - "column": 19 + "column": 17 }, "end": { "line": 2, diff --git a/packages/babel-types/src/asserts/generated/index.js b/packages/babel-types/src/asserts/generated/index.js index db77578f01d1..60a91b88178d 100644 --- a/packages/babel-types/src/asserts/generated/index.js +++ b/packages/babel-types/src/asserts/generated/index.js @@ -836,9 +836,6 @@ export function assertTSUnknownKeyword(node: Object, opts?: Object = {}): void { export function assertTSVoidKeyword(node: Object, opts?: Object = {}): void { assert("TSVoidKeyword", node, opts); } -export function assertTSAssertsKeyword(node: Object, opts?: Object = {}): void { - assert("TSAssertsKeyword", node, opts); -} export function assertTSThisType(node: Object, opts?: Object = {}): void { assert("TSThisType", node, opts); } diff --git a/packages/babel-types/src/builders/generated/index.js b/packages/babel-types/src/builders/generated/index.js index 926d4e3d62d8..2cb9d5490c9d 100644 --- a/packages/babel-types/src/builders/generated/index.js +++ b/packages/babel-types/src/builders/generated/index.js @@ -781,11 +781,6 @@ export function TSVoidKeyword(...args: Array): Object { } export { TSVoidKeyword as tsVoidKeyword }; export { TSVoidKeyword as tSVoidKeyword }; -export function TSAssertsKeyword(...args: Array): Object { - return builder("TSAssertsKeyword", ...args); -} -export { TSAssertsKeyword as tsAssertsKeyword }; -export { TSAssertsKeyword as tSAssertsKeyword }; export function TSThisType(...args: Array): Object { return builder("TSThisType", ...args); } diff --git a/packages/babel-types/src/definitions/typescript.js b/packages/babel-types/src/definitions/typescript.js index c3a650f69e0c..b20448af8d1e 100644 --- a/packages/babel-types/src/definitions/typescript.js +++ b/packages/babel-types/src/definitions/typescript.js @@ -139,7 +139,6 @@ const tsKeywordTypes = [ "TSUndefinedKeyword", "TSUnknownKeyword", "TSVoidKeyword", - "TSAssertsKeyword", ]; for (const type of tsKeywordTypes) { @@ -178,9 +177,9 @@ defineType("TSTypePredicate", { aliases: ["TSType"], visitor: ["parameterName", "typeAnnotation", "assertsModifier"], fields: { - assertsModifier: validateOptionalType("TSAssertsKeyword"), parameterName: validateType(["Identifier", "TSThisType"]), typeAnnotation: validateOptionalType("TSTypeAnnotation"), + assertsModifier: validate(bool), }, }); diff --git a/packages/babel-types/src/validators/generated/index.js b/packages/babel-types/src/validators/generated/index.js index 15ebabecaeee..84f44ef882cd 100644 --- a/packages/babel-types/src/validators/generated/index.js +++ b/packages/babel-types/src/validators/generated/index.js @@ -2691,20 +2691,6 @@ export function isTSVoidKeyword(node: ?Object, opts?: Object): boolean { return false; } -export function isTSAssertsKeyword(node: ?Object, opts?: Object): boolean { - if (!node) return false; - - const nodeType = node.type; - if (nodeType === "TSAssertsKeyword") { - if (typeof opts === "undefined") { - return true; - } else { - return shallowEqual(node, opts); - } - } - - return false; -} export function isTSThisType(node: ?Object, opts?: Object): boolean { if (!node) return false; @@ -4379,7 +4365,6 @@ export function isTSType(node: ?Object, opts?: Object): boolean { "TSUndefinedKeyword" === nodeType || "TSUnknownKeyword" === nodeType || "TSVoidKeyword" === nodeType || - "TSAssertsKeyword" === nodeType || "TSThisType" === nodeType || "TSFunctionType" === nodeType || "TSConstructorType" === nodeType ||