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

[ts] Add support for template interpolations in types #12131

Merged
merged 5 commits into from Oct 14, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 6 additions & 1 deletion packages/babel-parser/src/parser/expression.js
Expand Up @@ -1605,14 +1605,19 @@ export default class ExpressionParser extends LValParser {
node.quasis = [curElt];
while (!curElt.tail) {
this.expect(tt.dollarBraceL);
node.expressions.push(this.parseExpression());
node.expressions.push(this.parseTemplateInterpolation());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we extend parseTemplate in typescript plugin? since it seems to me that TemplateInterpolation is a TS-only concept.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it the same concept as for js? i.e. a${x}b is valid both in TS and in JS.
Maybe if I call it parseTemplateSubstitution, which is the language used in the spec?

this.expect(tt.braceR);
node.quasis.push((curElt = this.parseTemplateElement(isTagged)));
}
this.next();
return this.finishNode(node, "TemplateLiteral");
}

// This is overwritten by the TypeScript plugin to parse tepmlate types
nicolo-ribaudo marked this conversation as resolved.
Show resolved Hide resolved
parseTemplateInterpolation(): N.Expression {
return this.parseExpression();
}

// Parse an object literal, binding pattern, or record.

parseObjectLike<T: N.ObjectPattern | N.ObjectExpression>(
Expand Down
16 changes: 6 additions & 10 deletions packages/babel-parser/src/plugins/typescript/index.js
Expand Up @@ -94,8 +94,6 @@ const TSErrors = Object.freeze({
"Private elements cannot have the 'abstract' modifier.",
PrivateElementHasAccessibility:
"Private elements cannot have an accessibility modifier ('%0')",
TemplateTypeHasSubstitution:
"Template literal types cannot have any substitution",
TypeAnnotationAfterAssign:
"Type annotations must come before default assignments, e.g. instead of `age = 25: number` use `age: number = 25`",
UnexpectedParameterModifier:
Expand Down Expand Up @@ -773,17 +771,15 @@ export default (superClass: Class<Parser>): Class<Parser> =>

tsParseTemplateLiteralType(): N.TsType {
const node: N.TsLiteralType = this.startNode();
const templateNode = this.parseTemplate(false);
if (templateNode.expressions.length > 0) {
this.raise(
templateNode.expressions[0].start,
TSErrors.TemplateTypeHasSubstitution,
);
}
node.literal = templateNode;
node.literal = this.parseTemplate(false);
return this.finishNode(node, "TSLiteralType");
}

parseTemplateInterpolation(): N.TsType {
if (this.state.inType) return this.tsParseType();
return super.parseTemplateInterpolation();
}

tsParseThisTypeOrThisTypePredicate(): N.TsThisType | N.TsTypePredicate {
const thisKeyword = this.tsParseThisTypeNode();
if (this.isContextual("is") && !this.hasPrecedingLineBreak()) {
Expand Down
@@ -1,9 +1,6 @@
{
"type": "File",
"start":0,"end":20,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":20}},
"errors": [
"SyntaxError: Template literal types cannot have any substitution (1:14)"
],
"program": {
"type": "Program",
"start":0,"end":20,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":20}},
Expand Down Expand Up @@ -32,9 +29,13 @@
"start":7,"end":19,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":19}},
"expressions": [
{
"type": "Identifier",
"start":14,"end":17,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":17},"identifierName":"bar"},
"name": "bar"
"type": "TSTypeReference",
"start":14,"end":17,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":17}},
"typeName": {
"type": "Identifier",
"start":14,"end":17,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":17},"identifierName":"bar"},
"name": "bar"
}
}
],
"quasis": [
Expand Down
@@ -0,0 +1 @@
let x: `foo-${bar + baz}`;
@@ -0,0 +1,3 @@
{
"throws": "Unexpected token, expected \"}\" (1:18)"
}
@@ -0,0 +1 @@
let x: `foo-${infer bar}`;
@@ -0,0 +1,73 @@
{
"type": "File",
"start":0,"end":26,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":26}},
"program": {
"type": "Program",
"start":0,"end":26,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":26}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "VariableDeclaration",
"start":0,"end":26,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":26}},
"declarations": [
{
"type": "VariableDeclarator",
"start":4,"end":25,"loc":{"start":{"line":1,"column":4},"end":{"line":1,"column":25}},
"id": {
"type": "Identifier",
"start":4,"end":25,"loc":{"start":{"line":1,"column":4},"end":{"line":1,"column":25},"identifierName":"x"},
"name": "x",
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":5,"end":25,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":25}},
"typeAnnotation": {
"type": "TSLiteralType",
"start":7,"end":25,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":25}},
"literal": {
"type": "TemplateLiteral",
"start":7,"end":25,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":25}},
"expressions": [
{
"type": "TSInferType",
"start":14,"end":23,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":23}},
"typeParameter": {
"type": "TSTypeParameter",
"start":20,"end":23,"loc":{"start":{"line":1,"column":20},"end":{"line":1,"column":23}},
"name": "bar"
}
}
],
"quasis": [
{
"type": "TemplateElement",
"start":8,"end":12,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":12}},
"value": {
"raw": "foo-",
"cooked": "foo-"
},
"tail": false
},
{
"type": "TemplateElement",
"start":24,"end":24,"loc":{"start":{"line":1,"column":24},"end":{"line":1,"column":24}},
"value": {
"raw": "",
"cooked": ""
},
"tail": true
}
]
}
}
}
},
"init": null
}
],
"kind": "let"
}
],
"directives": []
}
}