Skip to content

Commit 7a5c360

Browse files
Merge pull request #1072 from Microsoft/taggedTemplates
Type checking for tagged template expressions
2 parents ce4dac3 + 3113429 commit 7a5c360

File tree

64 files changed

+2086
-187
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+2086
-187
lines changed

src/compiler/checker.ts

Lines changed: 224 additions & 72 deletions
Large diffs are not rendered by default.

src/compiler/core.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,17 @@ module ts {
122122
return result;
123123
}
124124

125+
/**
126+
* Returns the last element of an array if non-empty, undefined otherwise.
127+
*/
128+
export function lastOrUndefined<T>(array: T[]): T {
129+
if (array.length === 0) {
130+
return undefined;
131+
}
132+
133+
return array[array.length - 1];
134+
}
135+
125136
export function binarySearch(array: number[], value: number): number {
126137
var low = 0;
127138
var high = array.length - 1;

src/compiler/parser.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,6 @@ module ts {
567567
return false;
568568
}
569569

570-
571570
export function isDeclaration(node: Node): boolean {
572571
switch (node.kind) {
573572
case SyntaxKind.TypeParameter:
@@ -796,6 +795,20 @@ module ts {
796795
return SyntaxKind.FirstTriviaToken <= token && token <= SyntaxKind.LastTriviaToken;
797796
}
798797

798+
export function isUnterminatedTemplateEnd(node: LiteralExpression) {
799+
Debug.assert(node.kind === SyntaxKind.NoSubstitutionTemplateLiteral || node.kind === SyntaxKind.TemplateTail);
800+
var sourceText = getSourceFileOfNode(node).text;
801+
802+
// If we're not at the EOF, we know we must be terminated.
803+
if (node.end !== sourceText.length) {
804+
return false;
805+
}
806+
807+
// If we didn't end in a backtick, we must still be in the middle of a template.
808+
// If we did, make sure that it's not the *initial* backtick.
809+
return sourceText.charCodeAt(node.end - 1) !== CharacterCodes.backtick || node.text.length === 0;
810+
}
811+
799812
export function isModifier(token: SyntaxKind): boolean {
800813
switch (token) {
801814
case SyntaxKind.PublicKeyword:

src/compiler/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,8 @@ module ts {
477477
template: LiteralExpression | TemplateExpression;
478478
}
479479

480+
export type CallLikeExpression = CallExpression | NewExpression | TaggedTemplateExpression;
481+
480482
export interface TypeAssertion extends Expression {
481483
type: TypeNode;
482484
operand: Expression;

src/lib/core.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,10 @@ declare var Number: {
484484
POSITIVE_INFINITY: number;
485485
}
486486

487+
interface TemplateStringsArray extends Array<string> {
488+
raw: string[];
489+
}
490+
487491
interface Math {
488492
/** The mathematical constant e. This is Euler's number, the base of natural logarithms. */
489493
E: number;

tests/baselines/reference/noDefaultLib.errors.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
error TS2318: Cannot find global type 'Boolean'.
22
error TS2318: Cannot find global type 'IArguments'.
3+
error TS2318: Cannot find global type 'TemplateStringsArray'.
34
tests/cases/compiler/noDefaultLib.ts(4,11): error TS2317: Global type 'Array' must have 1 type parameter(s).
45

56

67
!!! error TS2318: Cannot find global type 'Boolean'.
78
!!! error TS2318: Cannot find global type 'IArguments'.
9+
!!! error TS2318: Cannot find global type 'TemplateStringsArray'.
810
==== tests/cases/compiler/noDefaultLib.ts (1 errors) ====
911
/// <reference no-default-lib="true"/>
1012
var x;

tests/baselines/reference/parser509698.errors.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ error TS2318: Cannot find global type 'Number'.
66
error TS2318: Cannot find global type 'Object'.
77
error TS2318: Cannot find global type 'RegExp'.
88
error TS2318: Cannot find global type 'String'.
9+
error TS2318: Cannot find global type 'TemplateStringsArray'.
910

1011

1112
!!! error TS2318: Cannot find global type 'Array'.
@@ -16,6 +17,7 @@ error TS2318: Cannot find global type 'String'.
1617
!!! error TS2318: Cannot find global type 'Object'.
1718
!!! error TS2318: Cannot find global type 'RegExp'.
1819
!!! error TS2318: Cannot find global type 'String'.
20+
!!! error TS2318: Cannot find global type 'TemplateStringsArray'.
1921
==== tests/cases/conformance/parser/ecmascript5/RegressionTests/parser509698.ts (0 errors) ====
2022
/// <style requireSemi="on" />
2123
/// <reference no-default-lib="true"/>

tests/baselines/reference/project/noDefaultLib/amd/noDefaultLib.errors.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ error TS2318: Cannot find global type 'Number'.
66
error TS2318: Cannot find global type 'Object'.
77
error TS2318: Cannot find global type 'RegExp'.
88
error TS2318: Cannot find global type 'String'.
9+
error TS2318: Cannot find global type 'TemplateStringsArray'.
910
test.ts(3,8): error TS2304: Cannot find name 'Array'.
1011

1112

@@ -17,6 +18,7 @@ test.ts(3,8): error TS2304: Cannot find name 'Array'.
1718
!!! error TS2318: Cannot find global type 'Object'.
1819
!!! error TS2318: Cannot find global type 'RegExp'.
1920
!!! error TS2318: Cannot find global type 'String'.
21+
!!! error TS2318: Cannot find global type 'TemplateStringsArray'.
2022
==== test.ts (1 errors) ====
2123
/// <reference no-default-lib="true"/>
2224

tests/baselines/reference/project/noDefaultLib/node/noDefaultLib.errors.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ error TS2318: Cannot find global type 'Number'.
66
error TS2318: Cannot find global type 'Object'.
77
error TS2318: Cannot find global type 'RegExp'.
88
error TS2318: Cannot find global type 'String'.
9+
error TS2318: Cannot find global type 'TemplateStringsArray'.
910
test.ts(3,8): error TS2304: Cannot find name 'Array'.
1011

1112

@@ -17,6 +18,7 @@ test.ts(3,8): error TS2304: Cannot find name 'Array'.
1718
!!! error TS2318: Cannot find global type 'Object'.
1819
!!! error TS2318: Cannot find global type 'RegExp'.
1920
!!! error TS2318: Cannot find global type 'String'.
21+
!!! error TS2318: Cannot find global type 'TemplateStringsArray'.
2022
==== test.ts (1 errors) ====
2123
/// <reference no-default-lib="true"/>
2224

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(5,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
2+
tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(9,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
3+
tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(13,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
4+
tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(16,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
5+
tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(20,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
6+
tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(23,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
7+
tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(27,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
8+
tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(28,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
9+
tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(29,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
10+
tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(33,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
11+
tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(34,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
12+
tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(35,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
13+
tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(39,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
14+
tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(40,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
15+
tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(41,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
16+
tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(45,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
17+
tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(46,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
18+
tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(47,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
19+
tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(51,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
20+
tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(52,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
21+
tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(53,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
22+
tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(57,9): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
23+
tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(58,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
24+
tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(64,11): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
25+
tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(77,11): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
26+
tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(81,11): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
27+
tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(86,9): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
28+
tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(90,11): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
29+
tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(64,11): error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
30+
Type argument candidate 'string' is not a valid type argument because it is not a supertype of candidate 'number'.
31+
tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts(77,11): error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
32+
Type argument candidate '{ x: number; z: Date; }' is not a valid type argument because it is not a supertype of candidate '{ x: number; y: string; }'.
33+
34+
35+
==== tests/cases/conformance/es6/templates/taggedTemplateStringsTypeArgumentInference.ts (30 errors) ====
36+
37+
38+
// Generic tag with one parameter
39+
function noParams<T>(n: T) { }
40+
noParams ``;
41+
~~~~~~~~~~~
42+
!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
43+
44+
// Generic tag with parameter which does not use type parameter
45+
function noGenericParams<T>(n: string[]) { }
46+
noGenericParams ``;
47+
~~~~~~~~~~~~~~~~~~
48+
!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
49+
50+
// Generic tag with multiple type parameters and only one used in parameter type annotation
51+
function someGenerics1a<T, U>(n: T, m: number) { }
52+
someGenerics1a `${3}`;
53+
~~~~~~~~~~~~~~~~~~~~~
54+
!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
55+
56+
function someGenerics1b<T, U>(n: string[], m: U) { }
57+
someGenerics1b `${3}`;
58+
~~~~~~~~~~~~~~~~~~~~~
59+
!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
60+
61+
// Generic tag with argument of function type whose parameter is of type parameter type
62+
function someGenerics2a<T>(strs: string[], n: (x: T) => void) { }
63+
someGenerics2a `${(n: string) => n}`;
64+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
65+
!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
66+
67+
function someGenerics2b<T, U>(strs: string[], n: (x: T, y: U) => void) { }
68+
someGenerics2b `${ (n: string, x: number) => n }`;
69+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
70+
!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
71+
72+
// Generic tag with argument of function type whose parameter is not of type parameter type but body/return type uses type parameter
73+
function someGenerics3<T>(strs: string[], producer: () => T) { }
74+
someGenerics3 `${() => ''}`;
75+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
76+
!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
77+
someGenerics3 `${() => undefined}`;
78+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
79+
!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
80+
someGenerics3 `${() => 3}`;
81+
~~~~~~~~~~~~~~~~~~~~~~~~~~
82+
!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
83+
84+
// 2 parameter generic tag with argument 1 of type parameter type and argument 2 of function type whose parameter is of type parameter type
85+
function someGenerics4<T, U>(strs: string[], n: T, f: (x: U) => void) { }
86+
someGenerics4 `${4}${ () => null }`;
87+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
88+
!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
89+
someGenerics4 `${''}${ () => 3 }`;
90+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
91+
!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
92+
someGenerics4 `${ null }${ null }`;
93+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
94+
!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
95+
96+
// 2 parameter generic tag with argument 2 of type parameter type and argument 1 of function type whose parameter is of type parameter type
97+
function someGenerics5<U, T>(strs: string[], n: T, f: (x: U) => void) { }
98+
someGenerics5 `${ 4 } ${ () => null }`;
99+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
100+
!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
101+
someGenerics5 `${ '' }${ () => 3 }`;
102+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
103+
!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
104+
someGenerics5 `${null}${null}`;
105+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
106+
!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
107+
108+
// Generic tag with multiple arguments of function types that each have parameters of the same generic type
109+
function someGenerics6<A>(strs: string[], a: (a: A) => A, b: (b: A) => A, c: (c: A) => A) { }
110+
someGenerics6 `${ n => n }${ n => n}${ n => n}`;
111+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
112+
!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
113+
someGenerics6 `${ n => n }${ n => n}${ n => n}`;
114+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
115+
!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
116+
someGenerics6 `${ (n: number) => n }${ (n: number) => n }${ (n: number) => n }`;
117+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
118+
!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
119+
120+
// Generic tag with multiple arguments of function types that each have parameters of different generic type
121+
function someGenerics7<A, B, C>(strs: string[], a: (a: A) => A, b: (b: B) => B, c: (c: C) => C) { }
122+
someGenerics7 `${ n => n }${ n => n }${ n => n }`;
123+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
124+
!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
125+
someGenerics7 `${ n => n }${ n => n }${ n => n }`;
126+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
127+
!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
128+
someGenerics7 `${(n: number) => n}${ (n: string) => n}${ (n: number) => n}`;
129+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
130+
!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
131+
132+
// Generic tag with argument of generic function type
133+
function someGenerics8<T>(strs: string[], n: T): T { return n; }
134+
var x = someGenerics8 `${ someGenerics7 }`;
135+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
136+
!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
137+
x `${null}${null}${null}`;
138+
~~~~~~~~~~~~~~~~~~~~~~~~~
139+
!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
140+
141+
// Generic tag with multiple parameters of generic type passed arguments with no best common type
142+
function someGenerics9<T>(strs: string[], a: T, b: T, c: T): T {
143+
return null;
144+
}
145+
var a9a = someGenerics9 `${ '' }${ 0 }${ [] }`;
146+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
147+
!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
148+
~~~~~~~~~~~~~
149+
!!! error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
150+
!!! error TS2453: Type argument candidate 'string' is not a valid type argument because it is not a supertype of candidate 'number'.
151+
var a9a: {};
152+
153+
// Generic tag with multiple parameters of generic type passed arguments with multiple best common types
154+
interface A91 {
155+
x: number;
156+
y?: string;
157+
}
158+
interface A92 {
159+
x: number;
160+
z?: Date;
161+
}
162+
163+
var a9e = someGenerics9 `${ undefined }${ { x: 6, z: new Date() } }${ { x: 6, y: '' } }`;
164+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
165+
!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
166+
~~~~~~~~~~~~~
167+
!!! error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
168+
!!! error TS2453: Type argument candidate '{ x: number; z: Date; }' is not a valid type argument because it is not a supertype of candidate '{ x: number; y: string; }'.
169+
var a9e: {};
170+
171+
// Generic tag with multiple parameters of generic type passed arguments with a single best common type
172+
var a9d = someGenerics9 `${ { x: 3 }}${ { x: 6 }}${ { x: 6 } }`;
173+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
174+
!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
175+
var a9d: { x: number; };
176+
177+
// Generic tag with multiple parameters of generic type where one argument is of type 'any'
178+
var anyVar: any;
179+
var a = someGenerics9 `${ 7 }${ anyVar }${ 4 }`;
180+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
181+
!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
182+
var a: any;
183+
184+
// Generic tag with multiple parameters of generic type where one argument is [] and the other is not 'any'
185+
var arr = someGenerics9 `${ [] }${ null }${ undefined }`;
186+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
187+
!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher.
188+
var arr: any[];
189+
190+

0 commit comments

Comments
 (0)