Skip to content

Commit 7a167f2

Browse files
committed
Merge pull request microsoft#2792 from Microsoft/conformanceConstEnum
Conformance for spec update section 9.4, 12.1.4 const enum
2 parents 80103b0 + e8a9254 commit 7a167f2

21 files changed

+705
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
tests/cases/conformance/ambient/ambientEnumDeclaration1.ts(5,5): error TS1066: Ambient enum elements can only have integer literal initializers.
2+
tests/cases/conformance/ambient/ambientEnumDeclaration1.ts(6,5): error TS1066: Ambient enum elements can only have integer literal initializers.
3+
tests/cases/conformance/ambient/ambientEnumDeclaration1.ts(7,5): error TS1066: Ambient enum elements can only have integer literal initializers.
4+
tests/cases/conformance/ambient/ambientEnumDeclaration1.ts(8,5): error TS1066: Ambient enum elements can only have integer literal initializers.
5+
6+
7+
==== tests/cases/conformance/ambient/ambientEnumDeclaration1.ts (4 errors) ====
8+
// In ambient enum declarations, all values specified in enum member declarations must be classified as constant enum expressions.
9+
10+
declare enum E {
11+
a = 10,
12+
b = 10 + 1,
13+
~
14+
!!! error TS1066: Ambient enum elements can only have integer literal initializers.
15+
c = b,
16+
~
17+
!!! error TS1066: Ambient enum elements can only have integer literal initializers.
18+
d = (c) + 1,
19+
~
20+
!!! error TS1066: Ambient enum elements can only have integer literal initializers.
21+
e = 10 << 2 * 8,
22+
~
23+
!!! error TS1066: Ambient enum elements can only have integer literal initializers.
24+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//// [ambientEnumDeclaration1.ts]
2+
// In ambient enum declarations, all values specified in enum member declarations must be classified as constant enum expressions.
3+
4+
declare enum E {
5+
a = 10,
6+
b = 10 + 1,
7+
c = b,
8+
d = (c) + 1,
9+
e = 10 << 2 * 8,
10+
}
11+
12+
//// [ambientEnumDeclaration1.js]
13+
// In ambient enum declarations, all values specified in enum member declarations must be classified as constant enum expressions.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//// [ambientEnumDeclaration2.ts]
2+
// In ambient enum declarations that specify no const modifier, enum member declarations
3+
// that omit a value are considered computed members (as opposed to having auto- incremented values assigned).
4+
5+
declare enum E {
6+
a, // E.a
7+
b, // E.b
8+
}
9+
10+
declare const enum E1 {
11+
a, // E.a = 0
12+
b, // E.b = 1
13+
}
14+
15+
//// [ambientEnumDeclaration2.js]
16+
// In ambient enum declarations that specify no const modifier, enum member declarations
17+
// that omit a value are considered computed members (as opposed to having auto- incremented values assigned).
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
=== tests/cases/conformance/ambient/ambientEnumDeclaration2.ts ===
2+
// In ambient enum declarations that specify no const modifier, enum member declarations
3+
// that omit a value are considered computed members (as opposed to having auto- incremented values assigned).
4+
5+
declare enum E {
6+
>E : Symbol(E, Decl(ambientEnumDeclaration2.ts, 0, 0))
7+
8+
a, // E.a
9+
>a : Symbol(E.a, Decl(ambientEnumDeclaration2.ts, 3, 16))
10+
11+
b, // E.b
12+
>b : Symbol(E.b, Decl(ambientEnumDeclaration2.ts, 4, 6))
13+
}
14+
15+
declare const enum E1 {
16+
>E1 : Symbol(E1, Decl(ambientEnumDeclaration2.ts, 6, 1))
17+
18+
a, // E.a = 0
19+
>a : Symbol(E1.a, Decl(ambientEnumDeclaration2.ts, 8, 23))
20+
21+
b, // E.b = 1
22+
>b : Symbol(E1.b, Decl(ambientEnumDeclaration2.ts, 9, 6))
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
=== tests/cases/conformance/ambient/ambientEnumDeclaration2.ts ===
2+
// In ambient enum declarations that specify no const modifier, enum member declarations
3+
// that omit a value are considered computed members (as opposed to having auto- incremented values assigned).
4+
5+
declare enum E {
6+
>E : E
7+
8+
a, // E.a
9+
>a : E
10+
11+
b, // E.b
12+
>b : E
13+
}
14+
15+
declare const enum E1 {
16+
>E1 : E1
17+
18+
a, // E.a = 0
19+
>a : E1
20+
21+
b, // E.b = 1
22+
>b : E1
23+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//// [constEnum1.ts]
2+
3+
// An enum declaration that specifies a const modifier is a constant enum declaration.
4+
// In a constant enum declaration, all members must have constant values and
5+
// it is an error for a member declaration to specify an expression that isn't classified as a constant enum expression.
6+
7+
const enum E {
8+
a = 10,
9+
b = a,
10+
c = (a+1),
11+
e,
12+
d = ~e,
13+
f = a << 2 >> 1,
14+
g = a << 2 >>> 1,
15+
h = a | b
16+
}
17+
18+
//// [constEnum1.js]
19+
// An enum declaration that specifies a const modifier is a constant enum declaration.
20+
// In a constant enum declaration, all members must have constant values and
21+
// it is an error for a member declaration to specify an expression that isn't classified as a constant enum expression.
22+
23+
24+
//// [constEnum1.d.ts]
25+
declare const enum E {
26+
a = 10,
27+
b = 10,
28+
c = 11,
29+
e = 12,
30+
d = -13,
31+
f = 20,
32+
g = 20,
33+
h = 10,
34+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
=== tests/cases/conformance/constEnums/constEnum1.ts ===
2+
3+
// An enum declaration that specifies a const modifier is a constant enum declaration.
4+
// In a constant enum declaration, all members must have constant values and
5+
// it is an error for a member declaration to specify an expression that isn't classified as a constant enum expression.
6+
7+
const enum E {
8+
>E : Symbol(E, Decl(constEnum1.ts, 0, 0))
9+
10+
a = 10,
11+
>a : Symbol(E.a, Decl(constEnum1.ts, 5, 14))
12+
13+
b = a,
14+
>b : Symbol(E.b, Decl(constEnum1.ts, 6, 11))
15+
>a : Symbol(E.a, Decl(constEnum1.ts, 5, 14))
16+
17+
c = (a+1),
18+
>c : Symbol(E.c, Decl(constEnum1.ts, 7, 10))
19+
>a : Symbol(E.a, Decl(constEnum1.ts, 5, 14))
20+
21+
e,
22+
>e : Symbol(E.e, Decl(constEnum1.ts, 8, 14))
23+
24+
d = ~e,
25+
>d : Symbol(E.d, Decl(constEnum1.ts, 9, 6))
26+
>e : Symbol(E.e, Decl(constEnum1.ts, 8, 14))
27+
28+
f = a << 2 >> 1,
29+
>f : Symbol(E.f, Decl(constEnum1.ts, 10, 11))
30+
>a : Symbol(E.a, Decl(constEnum1.ts, 5, 14))
31+
32+
g = a << 2 >>> 1,
33+
>g : Symbol(E.g, Decl(constEnum1.ts, 11, 20))
34+
>a : Symbol(E.a, Decl(constEnum1.ts, 5, 14))
35+
36+
h = a | b
37+
>h : Symbol(E.h, Decl(constEnum1.ts, 12, 21))
38+
>a : Symbol(E.a, Decl(constEnum1.ts, 5, 14))
39+
>b : Symbol(E.b, Decl(constEnum1.ts, 6, 11))
40+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
=== tests/cases/conformance/constEnums/constEnum1.ts ===
2+
3+
// An enum declaration that specifies a const modifier is a constant enum declaration.
4+
// In a constant enum declaration, all members must have constant values and
5+
// it is an error for a member declaration to specify an expression that isn't classified as a constant enum expression.
6+
7+
const enum E {
8+
>E : E
9+
10+
a = 10,
11+
>a : E
12+
>10 : number
13+
14+
b = a,
15+
>b : E
16+
>a : E
17+
18+
c = (a+1),
19+
>c : E
20+
>(a+1) : number
21+
>a+1 : number
22+
>a : E
23+
>1 : number
24+
25+
e,
26+
>e : E
27+
28+
d = ~e,
29+
>d : E
30+
>~e : number
31+
>e : E
32+
33+
f = a << 2 >> 1,
34+
>f : E
35+
>a << 2 >> 1 : number
36+
>a << 2 : number
37+
>a : E
38+
>2 : number
39+
>1 : number
40+
41+
g = a << 2 >>> 1,
42+
>g : E
43+
>a << 2 >>> 1 : number
44+
>a << 2 : number
45+
>a : E
46+
>2 : number
47+
>1 : number
48+
49+
h = a | b
50+
>h : E
51+
>a | b : number
52+
>a : E
53+
>b : E
54+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
tests/cases/conformance/constEnums/constEnum2.ts(11,9): error TS2474: In 'const' enum declarations member initializer must be constant expression.
2+
tests/cases/conformance/constEnums/constEnum2.ts(12,9): error TS2474: In 'const' enum declarations member initializer must be constant expression.
3+
tests/cases/conformance/constEnums/constEnum2.ts(13,5): error TS1005: ',' expected.
4+
tests/cases/conformance/constEnums/constEnum2.ts(13,9): error TS2474: In 'const' enum declarations member initializer must be constant expression.
5+
6+
7+
==== tests/cases/conformance/constEnums/constEnum2.ts (4 errors) ====
8+
9+
// An enum declaration that specifies a const modifier is a constant enum declaration.
10+
// In a constant enum declaration, all members must have constant values and
11+
// it is an error for a member declaration to specify an expression that isn't classified as a constant enum expression.
12+
13+
// Error : not a constant enum expression
14+
15+
const CONST = 9000 % 2;
16+
const enum D {
17+
d = 10,
18+
e = 199 * Math.floor(Math.random() * 1000),
19+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20+
!!! error TS2474: In 'const' enum declarations member initializer must be constant expression.
21+
f = d - (100 * Math.floor(Math.random() % 8))
22+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23+
!!! error TS2474: In 'const' enum declarations member initializer must be constant expression.
24+
g = CONST,
25+
~
26+
!!! error TS1005: ',' expected.
27+
~~~~~
28+
!!! error TS2474: In 'const' enum declarations member initializer must be constant expression.
29+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//// [constEnum2.ts]
2+
3+
// An enum declaration that specifies a const modifier is a constant enum declaration.
4+
// In a constant enum declaration, all members must have constant values and
5+
// it is an error for a member declaration to specify an expression that isn't classified as a constant enum expression.
6+
7+
// Error : not a constant enum expression
8+
9+
const CONST = 9000 % 2;
10+
const enum D {
11+
d = 10,
12+
e = 199 * Math.floor(Math.random() * 1000),
13+
f = d - (100 * Math.floor(Math.random() % 8))
14+
g = CONST,
15+
}
16+
17+
//// [constEnum2.js]
18+
// An enum declaration that specifies a const modifier is a constant enum declaration.
19+
// In a constant enum declaration, all members must have constant values and
20+
// it is an error for a member declaration to specify an expression that isn't classified as a constant enum expression.
21+
// Error : not a constant enum expression
22+
var CONST = 9000 % 2;
23+
24+
25+
//// [constEnum2.d.ts]
26+
declare const CONST: number;
27+
declare const enum D {
28+
d = 10,
29+
e,
30+
f,
31+
g,
32+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//// [constEnumPropertyAccess1.ts]
2+
3+
// constant enum declarations are completely erased in the emitted JavaScript code.
4+
// it is an error to reference a constant enum object in any other context
5+
// than a property access that selects one of the enum's members
6+
7+
const enum G {
8+
A = 1,
9+
B = 2,
10+
C = A + B,
11+
D = A * 2
12+
}
13+
14+
var o: {
15+
[idx: number]: boolean
16+
} = {
17+
1: true
18+
};
19+
20+
var a = G.A;
21+
var a1 = G["A"];
22+
var g = o[G.A];
23+
24+
class C {
25+
[G.A]() { }
26+
get [G.B]() {
27+
return true;
28+
}
29+
set [G.B](x: number) { }
30+
}
31+
32+
33+
34+
//// [constEnumPropertyAccess1.js]
35+
// constant enum declarations are completely erased in the emitted JavaScript code.
36+
// it is an error to reference a constant enum object in any other context
37+
// than a property access that selects one of the enum's members
38+
var o = {
39+
1: true
40+
};
41+
var a = 1 /* A */;
42+
var a1 = 1 /* "A" */;
43+
var g = o[1 /* A */];
44+
class C {
45+
[1 /* A */]() { }
46+
get [2 /* B */]() {
47+
return true;
48+
}
49+
set [2 /* B */](x) { }
50+
}
51+
52+
53+
//// [constEnumPropertyAccess1.d.ts]
54+
declare const enum G {
55+
A = 1,
56+
B = 2,
57+
C = 3,
58+
D = 2,
59+
}
60+
declare var o: {
61+
[idx: number]: boolean;
62+
};
63+
declare var a: G;
64+
declare var a1: G;
65+
declare var g: boolean;
66+
declare class C {
67+
}

0 commit comments

Comments
 (0)