From d1b9187cc259bb4e494250cd9ee2efcc29559388 Mon Sep 17 00:00:00 2001 From: Tan Li Hau Date: Wed, 20 Mar 2019 08:11:41 +0800 Subject: [PATCH 1/4] ! added test case --- .../types/literal-string-1/input.js | 1 + .../types/literal-string-1/output.json | 136 ++++++++++++++++++ .../types/literal-string-2/input.js | 1 + 3 files changed, 138 insertions(+) create mode 100644 packages/babel-parser/test/fixtures/typescript/types/literal-string-1/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/types/literal-string-1/output.json create mode 100644 packages/babel-parser/test/fixtures/typescript/types/literal-string-2/input.js diff --git a/packages/babel-parser/test/fixtures/typescript/types/literal-string-1/input.js b/packages/babel-parser/test/fixtures/typescript/types/literal-string-1/input.js new file mode 100644 index 000000000000..089ff63ba1d8 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/types/literal-string-1/input.js @@ -0,0 +1 @@ +let x: `foo`; diff --git a/packages/babel-parser/test/fixtures/typescript/types/literal-string-1/output.json b/packages/babel-parser/test/fixtures/typescript/types/literal-string-1/output.json new file mode 100644 index 000000000000..c9cbc53ac2f8 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/types/literal-string-1/output.json @@ -0,0 +1,136 @@ +{ + "type": "File", + "start": 0, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 12 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start": 5, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "typeAnnotation": { + "type": "TSLiteralType", + "start": 7, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "literal": { + "type": "StringLiteral", + "start": 7, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "extra": { + "rawValue": "foo", + "raw": "\"foo\"" + }, + "value": "foo" + } + } + } + }, + "init": null + } + ], + "kind": "let" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/types/literal-string-2/input.js b/packages/babel-parser/test/fixtures/typescript/types/literal-string-2/input.js new file mode 100644 index 000000000000..e21e6a4d4b08 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/types/literal-string-2/input.js @@ -0,0 +1 @@ +let x: `foo-${bar}`; From be7d36e88bad6540a281964e8ff8d1d88f43f90d Mon Sep 17 00:00:00 2001 From: Li Hau Tan Date: Fri, 22 Mar 2019 19:58:20 +0800 Subject: [PATCH 2/4] template literal as type --- .../babel-parser/src/plugins/typescript.js | 29 +++++++++++++++++++ .../types/literal-string-1/output.json | 14 ++++----- .../types/literal-string-2/options.json | 7 +++++ 3 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 packages/babel-parser/test/fixtures/typescript/types/literal-string-2/options.json diff --git a/packages/babel-parser/src/plugins/typescript.js b/packages/babel-parser/src/plugins/typescript.js index 07c560707dc2..6cbad32678ab 100644 --- a/packages/babel-parser/src/plugins/typescript.js +++ b/packages/babel-parser/src/plugins/typescript.js @@ -659,6 +659,33 @@ export default (superClass: Class): Class => return this.finishNode(node, "TSLiteralType"); } + tsParseTemplateLiteralType(): N.TsType { + const node: N.TsLiteralType = this.startNode(); + const startPosition = this.state.start; + const startLocation = this.state.startLoc; + const templateNode = this.parseTemplate(false); + if ( + templateNode.expressions.length === 0 && + templateNode.quasis.length === 1 + ) { + const value = templateNode.quasis[0].value.raw; + const noSubstitutionTemplateLiteralNode = this.startNodeAt( + startPosition, + startLocation, + ); + this.addExtra(node, "rawValue", value); + this.addExtra(node, "raw", value); + node.value = value; + node.literal = this.finishNode( + noSubstitutionTemplateLiteralNode, + "NoSubstitutionTemplateLiteral", + ); + } else { + throw this.unexpected(startPosition); + } + return this.finishNode(node, "TSLiteralType"); + } + tsParseNonArrayType(): N.TsType { switch (this.state.type) { case tt.name: @@ -717,6 +744,8 @@ export default (superClass: Class): Class => return this.tsParseTupleType(); case tt.parenL: return this.tsParseParenthesizedType(); + case tt.backQuote: + return this.tsParseTemplateLiteralType(); } throw this.unexpected(); diff --git a/packages/babel-parser/test/fixtures/typescript/types/literal-string-1/output.json b/packages/babel-parser/test/fixtures/typescript/types/literal-string-1/output.json index c9cbc53ac2f8..b6fa9dedca56 100644 --- a/packages/babel-parser/test/fixtures/typescript/types/literal-string-1/output.json +++ b/packages/babel-parser/test/fixtures/typescript/types/literal-string-1/output.json @@ -102,8 +102,13 @@ "column": 12 } }, + "extra": { + "rawValue": "foo", + "raw": "foo" + }, + "value": "foo", "literal": { - "type": "StringLiteral", + "type": "NoSubstitutionTemplateLiteral", "start": 7, "end": 12, "loc": { @@ -115,12 +120,7 @@ "line": 1, "column": 12 } - }, - "extra": { - "rawValue": "foo", - "raw": "\"foo\"" - }, - "value": "foo" + } } } } diff --git a/packages/babel-parser/test/fixtures/typescript/types/literal-string-2/options.json b/packages/babel-parser/test/fixtures/typescript/types/literal-string-2/options.json new file mode 100644 index 000000000000..bb06dda35ca1 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/types/literal-string-2/options.json @@ -0,0 +1,7 @@ +{ + "sourceType": "module", + "plugins": [ + "typescript" + ], + "throws": "Unexpected token (1:7)" +} \ No newline at end of file From 074450b2a7768b35ceaa82c769b41630b646877d Mon Sep 17 00:00:00 2001 From: Tan Li Hau Date: Sun, 24 Mar 2019 10:28:08 +0800 Subject: [PATCH 3/4] fix code review --- .../babel-parser/src/plugins/typescript.js | 24 +++----------- .../types/literal-string-1/output.json | 32 +++++++++++++++---- .../types/literal-string-2/options.json | 2 +- 3 files changed, 31 insertions(+), 27 deletions(-) diff --git a/packages/babel-parser/src/plugins/typescript.js b/packages/babel-parser/src/plugins/typescript.js index 6cbad32678ab..0b6259ba81bc 100644 --- a/packages/babel-parser/src/plugins/typescript.js +++ b/packages/babel-parser/src/plugins/typescript.js @@ -661,28 +661,14 @@ export default (superClass: Class): Class => tsParseTemplateLiteralType(): N.TsType { const node: N.TsLiteralType = this.startNode(); - const startPosition = this.state.start; - const startLocation = this.state.startLoc; const templateNode = this.parseTemplate(false); - if ( - templateNode.expressions.length === 0 && - templateNode.quasis.length === 1 - ) { - const value = templateNode.quasis[0].value.raw; - const noSubstitutionTemplateLiteralNode = this.startNodeAt( - startPosition, - startLocation, + if (templateNode.expressions.length > 0) { + throw this.raise( + templateNode.expressions[0].start, + "Template literal types cannot have any substitution", ); - this.addExtra(node, "rawValue", value); - this.addExtra(node, "raw", value); - node.value = value; - node.literal = this.finishNode( - noSubstitutionTemplateLiteralNode, - "NoSubstitutionTemplateLiteral", - ); - } else { - throw this.unexpected(startPosition); } + node.literal = templateNode; return this.finishNode(node, "TSLiteralType"); } diff --git a/packages/babel-parser/test/fixtures/typescript/types/literal-string-1/output.json b/packages/babel-parser/test/fixtures/typescript/types/literal-string-1/output.json index b6fa9dedca56..a153fb364a09 100644 --- a/packages/babel-parser/test/fixtures/typescript/types/literal-string-1/output.json +++ b/packages/babel-parser/test/fixtures/typescript/types/literal-string-1/output.json @@ -102,13 +102,8 @@ "column": 12 } }, - "extra": { - "rawValue": "foo", - "raw": "foo" - }, - "value": "foo", "literal": { - "type": "NoSubstitutionTemplateLiteral", + "type": "TemplateLiteral", "start": 7, "end": 12, "loc": { @@ -120,7 +115,30 @@ "line": 1, "column": 12 } - } + }, + "expressions": [], + "quasis": [ + { + "type": "TemplateElement", + "start": 8, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "value": { + "raw": "foo", + "cooked": "foo" + }, + "tail": true + } + ] } } } diff --git a/packages/babel-parser/test/fixtures/typescript/types/literal-string-2/options.json b/packages/babel-parser/test/fixtures/typescript/types/literal-string-2/options.json index bb06dda35ca1..3c001f6afda6 100644 --- a/packages/babel-parser/test/fixtures/typescript/types/literal-string-2/options.json +++ b/packages/babel-parser/test/fixtures/typescript/types/literal-string-2/options.json @@ -3,5 +3,5 @@ "plugins": [ "typescript" ], - "throws": "Unexpected token (1:7)" + "throws": "Template literal types cannot have any substitution (1:14)" } \ No newline at end of file From 3cc031085b03ca412c554e015051fef243e8c61f Mon Sep 17 00:00:00 2001 From: Tan Li Hau Date: Sun, 24 Mar 2019 10:29:25 +0800 Subject: [PATCH 4/4] fix flow --- packages/babel-parser/src/types.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/babel-parser/src/types.js b/packages/babel-parser/src/types.js index 5c49be447227..22ceb9e2dfa8 100644 --- a/packages/babel-parser/src/types.js +++ b/packages/babel-parser/src/types.js @@ -1308,7 +1308,7 @@ export type TsMappedType = TsTypeBase & { export type TsLiteralType = TsTypeBase & { type: "TSLiteralType", - literal: NumericLiteral | StringLiteral | BooleanLiteral, + literal: NumericLiteral | StringLiteral | BooleanLiteral | TemplateLiteral, }; export type TsImportType = TsTypeBase & {