Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[typescript] parsing template literal as type #9748

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions packages/babel-parser/src/plugins/typescript.js
Expand Up @@ -659,6 +659,19 @@ export default (superClass: Class<Parser>): Class<Parser> =>
return this.finishNode(node, "TSLiteralType");
}

tsParseTemplateLiteralType(): N.TsType {
const node: N.TsLiteralType = this.startNode();
const templateNode = this.parseTemplate(false);
if (templateNode.expressions.length > 0) {
throw this.raise(
templateNode.expressions[0].start,
"Template literal types cannot have any substitution",
);
}
node.literal = templateNode;
return this.finishNode(node, "TSLiteralType");
}

tsParseNonArrayType(): N.TsType {
switch (this.state.type) {
case tt.name:
Expand Down Expand Up @@ -717,6 +730,8 @@ export default (superClass: Class<Parser>): Class<Parser> =>
return this.tsParseTupleType();
case tt.parenL:
return this.tsParseParenthesizedType();
case tt.backQuote:
return this.tsParseTemplateLiteralType();
}

throw this.unexpected();
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-parser/src/types.js
Expand Up @@ -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 & {
Expand Down
@@ -0,0 +1 @@
let x: `foo`;
@@ -0,0 +1,154 @@
{
"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": "TemplateLiteral",
"start": 7,
"end": 12,
"loc": {
"start": {
"line": 1,
"column": 7
},
"end": {
"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
}
]
}
}
}
},
"init": null
}
],
"kind": "let"
}
],
"directives": []
}
}
@@ -0,0 +1 @@
let x: `foo-${bar}`;
@@ -0,0 +1,7 @@
{
"sourceType": "module",
"plugins": [
"typescript"
],
"throws": "Template literal types cannot have any substitution (1:14)"
}