Skip to content

Commit c03dc10

Browse files
Added syntactic classification for templates; also made 'spans' a NodeArray.
1 parent 799609c commit c03dc10

File tree

6 files changed

+58
-3
lines changed

6 files changed

+58
-3
lines changed

src/compiler/parser.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,13 +1406,17 @@ module ts {
14061406
template.head = parseLiteralNode();
14071407
Debug.assert(template.head.kind === SyntaxKind.TemplateHead, "Template head has wrong token kind");
14081408

1409-
var templateSpans: TemplateSpan[] = [];
1409+
var templateSpans = <NodeArray<TemplateSpan>>[];
1410+
templateSpans.pos = getNodePos();
1411+
14101412
do {
14111413
templateSpans.push(parseTemplateSpan());
14121414
}
14131415
while (templateSpans[templateSpans.length - 1].literal.kind === SyntaxKind.TemplateMiddle)
1414-
1416+
1417+
templateSpans.end = getNodeEnd();
14151418
template.templateSpans = templateSpans;
1419+
14161420
return finishNode(template);
14171421
}
14181422

src/compiler/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ module ts {
400400

401401
export interface TemplateExpression extends Expression {
402402
head: LiteralExpression;
403-
templateSpans: TemplateSpan[]
403+
templateSpans: NodeArray<TemplateSpan>;
404404
}
405405

406406
export interface TemplateSpan extends Node {

src/services/services.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4901,6 +4901,10 @@ module ts {
49014901
// TODO: we should get another classification type for these literals.
49024902
return ClassificationTypeNames.stringLiteral;
49034903
}
4904+
else if (isTemplateLiteralKind(tokenKind)) {
4905+
// TODO (drosen): we should *also* get another classification type for these literals.
4906+
return ClassificationTypeNames.stringLiteral;
4907+
}
49044908
else if (tokenKind === SyntaxKind.Identifier) {
49054909
switch (token.parent.kind) {
49064910
case SyntaxKind.ClassDeclaration:
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/// <reference path="fourslash.ts"/>
2+
3+
////module /*0*/M {
4+
//// export class /*1*/C {
5+
//// static x;
6+
//// }
7+
//// export enum /*2*/E {
8+
//// E1 = 0
9+
//// }
10+
////}
11+
////`abcd${ /*3*/M./*4*/C.x + /*5*/M./*6*/E.E1}efg`
12+
13+
var c = classification;
14+
verify.semanticClassificationsAre(
15+
c.moduleName("M", test.marker("0").position),
16+
c.className("C", test.marker("1").position),
17+
c.enumName("E", test.marker("2").position),
18+
c.moduleName("M", test.marker("3").position),
19+
c.className("C", test.marker("4").position),
20+
c.moduleName("M", test.marker("5").position),
21+
c.enumName("E", test.marker("6").position));
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/// <reference path="fourslash.ts"/>
2+
3+
////var v = 10e0;
4+
////var x = {
5+
//// p1: `hello world`,
6+
//// p2: `goodbye ${0} cruel ${0} world`,
7+
////};
8+
9+
var c = classification;
10+
verify.syntacticClassificationsAre(
11+
c.keyword("var"), c.text("v"), c.operator("="), c.numericLiteral("10e0"), c.punctuation(";"),
12+
c.keyword("var"), c.text("x"), c.operator("="), c.punctuation("{"),
13+
c.text("p1"), c.punctuation(":"), c.stringLiteral("`hello world`"), c.punctuation(","),
14+
c.text("p2"), c.punctuation(":"), c.stringLiteral("`goodbye ${"), c.numericLiteral("0"), c.stringLiteral("} cruel ${"), c.numericLiteral("0"), c.stringLiteral("} world`"), c.punctuation(","),
15+
c.punctuation("}"), c.punctuation(";"));
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// <reference path="fourslash.ts"/>
2+
3+
////var tiredOfCanonicalExamples =
4+
////`goodbye "${ `hello world` }"
5+
////and ${ `good${ " " }riddance` }`;
6+
7+
var c = classification;
8+
verify.syntacticClassificationsAre(
9+
c.keyword("var"), c.text("tiredOfCanonicalExamples"), c.operator("="),
10+
c.stringLiteral("`goodbye \"${"), c.stringLiteral("`hello world`"),
11+
c.stringLiteral("}\" \nand ${"), c.stringLiteral("`good${"), c.stringLiteral("\" \""), c.stringLiteral("}riddance`"), c.stringLiteral("}`"), c.punctuation(";"));

0 commit comments

Comments
 (0)