Skip to content

Commit d97f02a

Browse files
author
Andy Hanson
committed
Allow to use a const in the initializer of a const enum
1 parent 7aee3a1 commit d97f02a

10 files changed

+162
-4
lines changed

src/compiler/checker.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21859,8 +21859,10 @@ namespace ts {
2185921859
return +(<NumericLiteral>expr).text;
2186021860
case SyntaxKind.ParenthesizedExpression:
2186121861
return evaluate((<ParenthesizedExpression>expr).expression);
21862-
case SyntaxKind.Identifier:
21863-
return nodeIsMissing(expr) ? 0 : evaluateEnumMember(expr, getSymbolOfNode(member.parent), (<Identifier>expr).escapedText);
21862+
case SyntaxKind.Identifier: {
21863+
const id = expr as Identifier;
21864+
return nodeIsMissing(expr) ? 0 : evaluateEnumMember(id, getSymbolOfNode(member.parent), id.escapedText);
21865+
}
2186421866
case SyntaxKind.ElementAccessExpression:
2186521867
case SyntaxKind.PropertyAccessExpression:
2186621868
const ex = <PropertyAccessExpression | ElementAccessExpression>expr;
@@ -21876,15 +21878,15 @@ namespace ts {
2187621878
Debug.assert(isLiteralExpression(argument));
2187721879
name = escapeLeadingUnderscores((argument as LiteralExpression).text);
2187821880
}
21879-
return evaluateEnumMember(expr, type.symbol, name);
21881+
return evaluateEnumMember(ex, type.symbol, name);
2188021882
}
2188121883
}
2188221884
break;
2188321885
}
2188421886
return undefined;
2188521887
}
2188621888

21887-
function evaluateEnumMember(expr: Expression, enumSymbol: Symbol, name: __String) {
21889+
function evaluateEnumMember(expr: Identifier | ElementAccessExpression | PropertyAccessExpression, enumSymbol: Symbol, name: __String): string | number {
2188821890
const memberSymbol = enumSymbol.exports.get(name);
2188921891
if (memberSymbol) {
2189021892
const declaration = memberSymbol.valueDeclaration;
@@ -21896,6 +21898,12 @@ namespace ts {
2189621898
return 0;
2189721899
}
2189821900
}
21901+
else {
21902+
const type = checkExpression(expr);
21903+
if (type.flags & TypeFlags.StringOrNumberLiteral) {
21904+
return (type as LiteralType).value;
21905+
}
21906+
}
2189921907
return undefined;
2190021908
}
2190121909
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//// [enumInitializers_const.ts]
2+
const stride = 5;
3+
const enum E {
4+
x = stride * 2,
5+
}
6+
E.x;
7+
8+
const s = "abc";
9+
const enum S {
10+
abc = s,
11+
}
12+
S.abc;
13+
14+
15+
//// [enumInitializers_const.js]
16+
var stride = 5;
17+
10 /* x */;
18+
var s = "abc";
19+
"abc" /* abc */;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
=== tests/cases/compiler/enumInitializers_const.ts ===
2+
const stride = 5;
3+
>stride : Symbol(stride, Decl(enumInitializers_const.ts, 0, 5))
4+
5+
const enum E {
6+
>E : Symbol(E, Decl(enumInitializers_const.ts, 0, 17))
7+
8+
x = stride * 2,
9+
>x : Symbol(E.x, Decl(enumInitializers_const.ts, 1, 14))
10+
>stride : Symbol(stride, Decl(enumInitializers_const.ts, 0, 5))
11+
}
12+
E.x;
13+
>E.x : Symbol(E.x, Decl(enumInitializers_const.ts, 1, 14))
14+
>E : Symbol(E, Decl(enumInitializers_const.ts, 0, 17))
15+
>x : Symbol(E.x, Decl(enumInitializers_const.ts, 1, 14))
16+
17+
const s = "abc";
18+
>s : Symbol(s, Decl(enumInitializers_const.ts, 6, 5))
19+
20+
const enum S {
21+
>S : Symbol(S, Decl(enumInitializers_const.ts, 6, 16))
22+
23+
abc = s,
24+
>abc : Symbol(S.abc, Decl(enumInitializers_const.ts, 7, 14))
25+
>s : Symbol(s, Decl(enumInitializers_const.ts, 6, 5))
26+
}
27+
S.abc;
28+
>S.abc : Symbol(S.abc, Decl(enumInitializers_const.ts, 7, 14))
29+
>S : Symbol(S, Decl(enumInitializers_const.ts, 6, 16))
30+
>abc : Symbol(S.abc, Decl(enumInitializers_const.ts, 7, 14))
31+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
=== tests/cases/compiler/enumInitializers_const.ts ===
2+
const stride = 5;
3+
>stride : 5
4+
>5 : 5
5+
6+
const enum E {
7+
>E : E
8+
9+
x = stride * 2,
10+
>x : E
11+
>stride * 2 : number
12+
>stride : 5
13+
>2 : 2
14+
}
15+
E.x;
16+
>E.x : E
17+
>E : typeof E
18+
>x : E
19+
20+
const s = "abc";
21+
>s : "abc"
22+
>"abc" : "abc"
23+
24+
const enum S {
25+
>S : S
26+
27+
abc = s,
28+
>abc : S
29+
>s : "abc"
30+
}
31+
S.abc;
32+
>S.abc : S
33+
>S : typeof S
34+
>abc : S
35+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
tests/cases/compiler/enumInitilizers_const_circular.ts(3,9): error TS2474: In 'const' enum declarations member initializer must be constant expression.
2+
3+
4+
==== tests/cases/compiler/enumInitilizers_const_circular.ts (1 errors) ====
5+
const x = E.y;
6+
const enum E {
7+
y = x,
8+
~
9+
!!! error TS2474: In 'const' enum declarations member initializer must be constant expression.
10+
}
11+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//// [enumInitilizers_const_circular.ts]
2+
const x = E.y;
3+
const enum E {
4+
y = x,
5+
}
6+
7+
8+
//// [enumInitilizers_const_circular.js]
9+
var x = E.y;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
=== tests/cases/compiler/enumInitilizers_const_circular.ts ===
2+
const x = E.y;
3+
>x : Symbol(x, Decl(enumInitilizers_const_circular.ts, 0, 5))
4+
>E.y : Symbol(E.y, Decl(enumInitilizers_const_circular.ts, 1, 14))
5+
>E : Symbol(E, Decl(enumInitilizers_const_circular.ts, 0, 14))
6+
>y : Symbol(E.y, Decl(enumInitilizers_const_circular.ts, 1, 14))
7+
8+
const enum E {
9+
>E : Symbol(E, Decl(enumInitilizers_const_circular.ts, 0, 14))
10+
11+
y = x,
12+
>y : Symbol(E.y, Decl(enumInitilizers_const_circular.ts, 1, 14))
13+
>x : Symbol(x, Decl(enumInitilizers_const_circular.ts, 0, 5))
14+
}
15+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
=== tests/cases/compiler/enumInitilizers_const_circular.ts ===
2+
const x = E.y;
3+
>x : E
4+
>E.y : E
5+
>E : typeof E
6+
>y : E
7+
8+
const enum E {
9+
>E : E
10+
11+
y = x,
12+
>y : E
13+
>x : E
14+
}
15+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const stride = 5;
2+
const enum E {
3+
x = stride * 2,
4+
}
5+
E.x;
6+
7+
const s = "abc";
8+
const enum S {
9+
abc = s,
10+
}
11+
S.abc;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const x = E.y;
2+
const enum E {
3+
y = x,
4+
}

0 commit comments

Comments
 (0)