Skip to content

Commit 8bac5aa

Browse files
committed
Support template literals in ambient module declaration
1 parent bf4b5cc commit 8bac5aa

8 files changed

+192
-7
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33156,7 +33156,7 @@ namespace ts {
3315633156
}
3315733157

3315833158
function isStringOrNumberLiteralExpression(expr: Expression) {
33159-
return expr.kind === SyntaxKind.StringLiteral || expr.kind === SyntaxKind.NumericLiteral ||
33159+
return isStringOrNumericLiteralLike(expr) ||
3316033160
expr.kind === SyntaxKind.PrefixUnaryExpression && (<PrefixUnaryExpression>expr).operator === SyntaxKind.MinusToken &&
3316133161
(<PrefixUnaryExpression>expr).operand.kind === SyntaxKind.NumericLiteral;
3316233162
}
@@ -33186,7 +33186,7 @@ namespace ts {
3318633186
const isConstOrReadonly = isDeclarationReadonly(node) || isVariableDeclaration(node) && isVarConst(node);
3318733187
if (isConstOrReadonly && !node.type) {
3318833188
if (isInvalidInitializer) {
33189-
return grammarErrorOnNode(initializer, Diagnostics.A_const_initializer_in_an_ambient_context_must_be_a_string_or_numeric_literal_or_literal_enum_reference);
33189+
return grammarErrorOnNode(initializer, Diagnostics.A_const_initializer_in_an_ambient_context_must_be_a_string_or_numeric_literal_or_template_literal_or_literal_enum_reference);
3319033190
}
3319133191
}
3319233192
else {

src/compiler/diagnosticMessages.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@
831831
"category": "Error",
832832
"code": 1253
833833
},
834-
"A 'const' initializer in an ambient context must be a string or numeric literal or literal enum reference.": {
834+
"A 'const' initializer in an ambient context must be a string or numeric literal or template literal or literal enum reference.": {
835835
"category": "Error",
836836
"code": 1254
837837
},
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//// [ambientModuleWithTemplateLiterals.ts]
2+
declare module Foo {
3+
enum Bar {
4+
a = `1`,
5+
b = '2',
6+
c = '3'
7+
}
8+
9+
export const a = 'string';
10+
export const b = `template`;
11+
12+
export const c = Bar.a;
13+
export const d = Bar['b'];
14+
export const e = Bar[`c`];
15+
}
16+
17+
Foo.a;
18+
Foo.b;
19+
Foo.c;
20+
Foo.d;
21+
Foo.e;
22+
23+
//// [ambientModuleWithTemplateLiterals.js]
24+
Foo.a;
25+
Foo.b;
26+
Foo.c;
27+
Foo.d;
28+
Foo.e;
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
=== tests/cases/compiler/ambientModuleWithTemplateLiterals.ts ===
2+
declare module Foo {
3+
>Foo : Symbol(Foo, Decl(ambientModuleWithTemplateLiterals.ts, 0, 0))
4+
5+
enum Bar {
6+
>Bar : Symbol(Bar, Decl(ambientModuleWithTemplateLiterals.ts, 0, 20))
7+
8+
a = `1`,
9+
>a : Symbol(Bar.a, Decl(ambientModuleWithTemplateLiterals.ts, 1, 14))
10+
11+
b = '2',
12+
>b : Symbol(Bar.b, Decl(ambientModuleWithTemplateLiterals.ts, 2, 16))
13+
14+
c = '3'
15+
>c : Symbol(Bar.c, Decl(ambientModuleWithTemplateLiterals.ts, 3, 16))
16+
}
17+
18+
export const a = 'string';
19+
>a : Symbol(a, Decl(ambientModuleWithTemplateLiterals.ts, 7, 16))
20+
21+
export const b = `template`;
22+
>b : Symbol(b, Decl(ambientModuleWithTemplateLiterals.ts, 8, 16))
23+
24+
export const c = Bar.a;
25+
>c : Symbol(c, Decl(ambientModuleWithTemplateLiterals.ts, 10, 16))
26+
>Bar.a : Symbol(Bar.a, Decl(ambientModuleWithTemplateLiterals.ts, 1, 14))
27+
>Bar : Symbol(Bar, Decl(ambientModuleWithTemplateLiterals.ts, 0, 20))
28+
>a : Symbol(Bar.a, Decl(ambientModuleWithTemplateLiterals.ts, 1, 14))
29+
30+
export const d = Bar['b'];
31+
>d : Symbol(d, Decl(ambientModuleWithTemplateLiterals.ts, 11, 16))
32+
>Bar : Symbol(Bar, Decl(ambientModuleWithTemplateLiterals.ts, 0, 20))
33+
>'b' : Symbol(Bar.b, Decl(ambientModuleWithTemplateLiterals.ts, 2, 16))
34+
35+
export const e = Bar[`c`];
36+
>e : Symbol(e, Decl(ambientModuleWithTemplateLiterals.ts, 12, 16))
37+
>Bar : Symbol(Bar, Decl(ambientModuleWithTemplateLiterals.ts, 0, 20))
38+
>`c` : Symbol(Bar.c, Decl(ambientModuleWithTemplateLiterals.ts, 3, 16))
39+
}
40+
41+
Foo.a;
42+
>Foo.a : Symbol(Foo.a, Decl(ambientModuleWithTemplateLiterals.ts, 7, 16))
43+
>Foo : Symbol(Foo, Decl(ambientModuleWithTemplateLiterals.ts, 0, 0))
44+
>a : Symbol(Foo.a, Decl(ambientModuleWithTemplateLiterals.ts, 7, 16))
45+
46+
Foo.b;
47+
>Foo.b : Symbol(Foo.b, Decl(ambientModuleWithTemplateLiterals.ts, 8, 16))
48+
>Foo : Symbol(Foo, Decl(ambientModuleWithTemplateLiterals.ts, 0, 0))
49+
>b : Symbol(Foo.b, Decl(ambientModuleWithTemplateLiterals.ts, 8, 16))
50+
51+
Foo.c;
52+
>Foo.c : Symbol(Foo.c, Decl(ambientModuleWithTemplateLiterals.ts, 10, 16))
53+
>Foo : Symbol(Foo, Decl(ambientModuleWithTemplateLiterals.ts, 0, 0))
54+
>c : Symbol(Foo.c, Decl(ambientModuleWithTemplateLiterals.ts, 10, 16))
55+
56+
Foo.d;
57+
>Foo.d : Symbol(Foo.d, Decl(ambientModuleWithTemplateLiterals.ts, 11, 16))
58+
>Foo : Symbol(Foo, Decl(ambientModuleWithTemplateLiterals.ts, 0, 0))
59+
>d : Symbol(Foo.d, Decl(ambientModuleWithTemplateLiterals.ts, 11, 16))
60+
61+
Foo.e;
62+
>Foo.e : Symbol(Foo.e, Decl(ambientModuleWithTemplateLiterals.ts, 12, 16))
63+
>Foo : Symbol(Foo, Decl(ambientModuleWithTemplateLiterals.ts, 0, 0))
64+
>e : Symbol(Foo.e, Decl(ambientModuleWithTemplateLiterals.ts, 12, 16))
65+
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
=== tests/cases/compiler/ambientModuleWithTemplateLiterals.ts ===
2+
declare module Foo {
3+
>Foo : typeof Foo
4+
5+
enum Bar {
6+
>Bar : Bar
7+
8+
a = `1`,
9+
>a : Bar.a
10+
>`1` : "1"
11+
12+
b = '2',
13+
>b : Bar.b
14+
>'2' : "2"
15+
16+
c = '3'
17+
>c : Bar.c
18+
>'3' : "3"
19+
}
20+
21+
export const a = 'string';
22+
>a : "string"
23+
>'string' : "string"
24+
25+
export const b = `template`;
26+
>b : "template"
27+
>`template` : "template"
28+
29+
export const c = Bar.a;
30+
>c : Bar.a
31+
>Bar.a : Bar.a
32+
>Bar : typeof Bar
33+
>a : Bar.a
34+
35+
export const d = Bar['b'];
36+
>d : Bar.b
37+
>Bar['b'] : Bar.b
38+
>Bar : typeof Bar
39+
>'b' : "b"
40+
41+
export const e = Bar[`c`];
42+
>e : Bar.c
43+
>Bar[`c`] : Bar.c
44+
>Bar : typeof Bar
45+
>`c` : "c"
46+
}
47+
48+
Foo.a;
49+
>Foo.a : "string"
50+
>Foo : typeof Foo
51+
>a : "string"
52+
53+
Foo.b;
54+
>Foo.b : "template"
55+
>Foo : typeof Foo
56+
>b : "template"
57+
58+
Foo.c;
59+
>Foo.c : Foo.Bar.a
60+
>Foo : typeof Foo
61+
>c : Foo.Bar.a
62+
63+
Foo.d;
64+
>Foo.d : Foo.Bar.b
65+
>Foo : typeof Foo
66+
>d : Foo.Bar.b
67+
68+
Foo.e;
69+
>Foo.e : Foo.Bar.c
70+
>Foo : typeof Foo
71+
>e : Foo.Bar.c
72+

tests/baselines/reference/constDeclarations-ambient-errors.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
tests/cases/compiler/constDeclarations-ambient-errors.ts(2,29): error TS1039: Initializers are not allowed in ambient contexts.
22
tests/cases/compiler/constDeclarations-ambient-errors.ts(3,28): error TS1039: Initializers are not allowed in ambient contexts.
3-
tests/cases/compiler/constDeclarations-ambient-errors.ts(4,20): error TS1254: A 'const' initializer in an ambient context must be a string or numeric literal or literal enum reference.
3+
tests/cases/compiler/constDeclarations-ambient-errors.ts(4,20): error TS1254: A 'const' initializer in an ambient context must be a string or numeric literal or template literal or literal enum reference.
44
tests/cases/compiler/constDeclarations-ambient-errors.ts(4,39): error TS1039: Initializers are not allowed in ambient contexts.
55
tests/cases/compiler/constDeclarations-ambient-errors.ts(4,53): error TS1039: Initializers are not allowed in ambient contexts.
66
tests/cases/compiler/constDeclarations-ambient-errors.ts(8,24): error TS1039: Initializers are not allowed in ambient contexts.
@@ -16,7 +16,7 @@ tests/cases/compiler/constDeclarations-ambient-errors.ts(8,24): error TS1039: In
1616
!!! error TS1039: Initializers are not allowed in ambient contexts.
1717
declare const c3 = null, c4 :string = "", c5: any = 0;
1818
~~~~
19-
!!! error TS1254: A 'const' initializer in an ambient context must be a string or numeric literal or literal enum reference.
19+
!!! error TS1254: A 'const' initializer in an ambient context must be a string or numeric literal or template literal or literal enum reference.
2020
~~
2121
!!! error TS1039: Initializers are not allowed in ambient contexts.
2222
~

tests/baselines/reference/duplicatePackage_withErrors.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/node_modules/a/node_modules/x/index.d.ts(1,18): error TS1254: A 'const' initializer in an ambient context must be a string or numeric literal or literal enum reference.
1+
/node_modules/a/node_modules/x/index.d.ts(1,18): error TS1254: A 'const' initializer in an ambient context must be a string or numeric literal or template literal or literal enum reference.
22

33

44
==== /src/a.ts (0 errors) ====
@@ -11,7 +11,7 @@
1111
==== /node_modules/a/node_modules/x/index.d.ts (1 errors) ====
1212
export const x = 1 + 1;
1313
~~~~~
14-
!!! error TS1254: A 'const' initializer in an ambient context must be a string or numeric literal or literal enum reference.
14+
!!! error TS1254: A 'const' initializer in an ambient context must be a string or numeric literal or template literal or literal enum reference.
1515

1616
==== /node_modules/a/node_modules/x/package.json (0 errors) ====
1717
{ "name": "x", "version": "1.2.3" }
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
declare module Foo {
2+
enum Bar {
3+
a = `1`,
4+
b = '2',
5+
c = '3'
6+
}
7+
8+
export const a = 'string';
9+
export const b = `template`;
10+
11+
export const c = Bar.a;
12+
export const d = Bar['b'];
13+
export const e = Bar[`c`];
14+
}
15+
16+
Foo.a;
17+
Foo.b;
18+
Foo.c;
19+
Foo.d;
20+
Foo.e;

0 commit comments

Comments
 (0)