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
Changes from 2 commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -659,6 +659,33 @@ export default (superClass: Class<Parser>): Class<Parser> =>
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",
);

This comment has been minimized.

Copy link
@nicolo-ribaudo

nicolo-ribaudo Mar 23, 2019

Member

I don't see why we need a new node type here. We can just re-use TemplateLiteral: if node.expressions.length === 0, then it doesn't have any substitution.

} else {
throw this.unexpected(startPosition);

This comment has been minimized.

Copy link
@nicolo-ribaudo

nicolo-ribaudo Mar 23, 2019

Member
Suggested change
throw this.unexpected(startPosition);
throw this.raise(templateNode.expressions[0].start, "Template literal types cannot have any substitution");
}
return this.finishNode(node, "TSLiteralType");
}

tsParseNonArrayType(): N.TsType {
switch (this.state.type) {
case tt.name:
@@ -717,6 +744,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();
@@ -0,0 +1 @@
let x: `foo`;
@@ -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
}
},
"extra": {
"rawValue": "foo",
"raw": "foo"
},
"value": "foo",
"literal": {
"type": "NoSubstitutionTemplateLiteral",
"start": 7,
"end": 12,
"loc": {
"start": {
"line": 1,
"column": 7
},
"end": {
"line": 1,
"column": 12
}
}
}
}
}
},
"init": null
}
],
"kind": "let"
}
],
"directives": []
}
}
@@ -0,0 +1 @@
let x: `foo-${bar}`;
@@ -0,0 +1,7 @@
{
"sourceType": "module",
"plugins": [
"typescript"
],
"throws": "Unexpected token (1:7)"
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.