Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ae42979

Browse filesBrowse files
author
Yui T
committed
Add conformance tests for const enum
1 parent b1acce0 commit ae42979
Copy full SHA for ae42979

12 files changed

+468Lines changed: 468 additions & 0 deletions
Expand file treeCollapse file tree
Open diff view settings
Collapse file
+34Lines 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+
}
Collapse file
+54Lines 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, Symbol(E, Decl(constEnum1.ts, 0, 0))
9+
10+
a = 10,
11+
>a : E, Symbol(E.a, Decl(constEnum1.ts, 5, 14))
12+
>10 : number
13+
14+
b = a,
15+
>b : E, Symbol(E.b, Decl(constEnum1.ts, 6, 11))
16+
>a : E, Symbol(E.a, Decl(constEnum1.ts, 5, 14))
17+
18+
c = (a+1),
19+
>c : E, Symbol(E.c, Decl(constEnum1.ts, 7, 10))
20+
>(a+1) : number
21+
>a+1 : number
22+
>a : E, Symbol(E.a, Decl(constEnum1.ts, 5, 14))
23+
>1 : number
24+
25+
e,
26+
>e : E, Symbol(E.e, Decl(constEnum1.ts, 8, 14))
27+
28+
d = ~e,
29+
>d : E, Symbol(E.d, Decl(constEnum1.ts, 9, 6))
30+
>~e : number
31+
>e : E, Symbol(E.e, Decl(constEnum1.ts, 8, 14))
32+
33+
f = a << 2 >> 1,
34+
>f : E, Symbol(E.f, Decl(constEnum1.ts, 10, 11))
35+
>a << 2 >> 1 : number
36+
>a << 2 : number
37+
>a : E, Symbol(E.a, Decl(constEnum1.ts, 5, 14))
38+
>2 : number
39+
>1 : number
40+
41+
g = a << 2 >>> 1,
42+
>g : E, Symbol(E.g, Decl(constEnum1.ts, 11, 20))
43+
>a << 2 >>> 1 : number
44+
>a << 2 : number
45+
>a : E, Symbol(E.a, Decl(constEnum1.ts, 5, 14))
46+
>2 : number
47+
>1 : number
48+
49+
h = a | b
50+
>h : E, Symbol(E.h, Decl(constEnum1.ts, 12, 21))
51+
>a | b : number
52+
>a : E, Symbol(E.a, Decl(constEnum1.ts, 5, 14))
53+
>b : E, Symbol(E.b, Decl(constEnum1.ts, 6, 11))
54+
}
Collapse file
+29Lines 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+
}
Collapse file
+32Lines 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+
}
Collapse file
+67Lines 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+
}
Collapse file
+88Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
=== tests/cases/conformance/constEnums/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+
>G : G, Symbol(G, Decl(constEnumPropertyAccess1.ts, 0, 0))
9+
10+
A = 1,
11+
>A : G, Symbol(G.A, Decl(constEnumPropertyAccess1.ts, 5, 14))
12+
>1 : number
13+
14+
B = 2,
15+
>B : G, Symbol(G.B, Decl(constEnumPropertyAccess1.ts, 6, 10))
16+
>2 : number
17+
18+
C = A + B,
19+
>C : G, Symbol(G.C, Decl(constEnumPropertyAccess1.ts, 7, 10))
20+
>A + B : number
21+
>A : G, Symbol(G.A, Decl(constEnumPropertyAccess1.ts, 5, 14))
22+
>B : G, Symbol(G.B, Decl(constEnumPropertyAccess1.ts, 6, 10))
23+
24+
D = A * 2
25+
>D : G, Symbol(G.D, Decl(constEnumPropertyAccess1.ts, 8, 14))
26+
>A * 2 : number
27+
>A : G, Symbol(G.A, Decl(constEnumPropertyAccess1.ts, 5, 14))
28+
>2 : number
29+
}
30+
31+
var o: {
32+
>o : { [idx: number]: boolean; }, Symbol(o, Decl(constEnumPropertyAccess1.ts, 12, 3))
33+
34+
[idx: number]: boolean
35+
>idx : number, Symbol(idx, Decl(constEnumPropertyAccess1.ts, 13, 5))
36+
37+
} = {
38+
>{ 1: true } : { [x: number]: boolean; 1: boolean; }
39+
40+
1: true
41+
>true : boolean
42+
43+
};
44+
45+
var a = G.A;
46+
>a : G, Symbol(a, Decl(constEnumPropertyAccess1.ts, 18, 3))
47+
>G.A : G, Symbol(G.A, Decl(constEnumPropertyAccess1.ts, 5, 14))
48+
>G : typeof G, Symbol(G, Decl(constEnumPropertyAccess1.ts, 0, 0))
49+
>A : G, Symbol(G.A, Decl(constEnumPropertyAccess1.ts, 5, 14))
50+
51+
var a1 = G["A"];
52+
>a1 : G, Symbol(a1, Decl(constEnumPropertyAccess1.ts, 19, 3))
53+
>G["A"] : G
54+
>G : typeof G, Symbol(G, Decl(constEnumPropertyAccess1.ts, 0, 0))
55+
>"A" : string, Symbol(G.A, Decl(constEnumPropertyAccess1.ts, 5, 14))
56+
57+
var g = o[G.A];
58+
>g : boolean, Symbol(g, Decl(constEnumPropertyAccess1.ts, 20, 3))
59+
>o[G.A] : boolean
60+
>o : { [idx: number]: boolean; }, Symbol(o, Decl(constEnumPropertyAccess1.ts, 12, 3))
61+
>G.A : G, Symbol(G.A, Decl(constEnumPropertyAccess1.ts, 5, 14))
62+
>G : typeof G, Symbol(G, Decl(constEnumPropertyAccess1.ts, 0, 0))
63+
>A : G, Symbol(G.A, Decl(constEnumPropertyAccess1.ts, 5, 14))
64+
65+
class C {
66+
>C : C, Symbol(C, Decl(constEnumPropertyAccess1.ts, 20, 15))
67+
68+
[G.A]() { }
69+
>G.A : G, Symbol(G.A, Decl(constEnumPropertyAccess1.ts, 5, 14))
70+
>G : typeof G, Symbol(G, Decl(constEnumPropertyAccess1.ts, 0, 0))
71+
>A : G, Symbol(G.A, Decl(constEnumPropertyAccess1.ts, 5, 14))
72+
73+
get [G.B]() {
74+
>G.B : G, Symbol(G.B, Decl(constEnumPropertyAccess1.ts, 6, 10))
75+
>G : typeof G, Symbol(G, Decl(constEnumPropertyAccess1.ts, 0, 0))
76+
>B : G, Symbol(G.B, Decl(constEnumPropertyAccess1.ts, 6, 10))
77+
78+
return true;
79+
>true : boolean
80+
}
81+
set [G.B](x: number) { }
82+
>G.B : G, Symbol(G.B, Decl(constEnumPropertyAccess1.ts, 6, 10))
83+
>G : typeof G, Symbol(G, Decl(constEnumPropertyAccess1.ts, 0, 0))
84+
>B : G, Symbol(G.B, Decl(constEnumPropertyAccess1.ts, 6, 10))
85+
>x : number, Symbol(x, Decl(constEnumPropertyAccess1.ts, 27, 14))
86+
}
87+
88+
Collapse file
+35Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
tests/cases/conformance/constEnums/constEnumPropertyAccess2.ts(14,9): error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment.
2+
tests/cases/conformance/constEnums/constEnumPropertyAccess2.ts(15,12): error TS2476: A const enum member can only be accessed using a string literal.
3+
tests/cases/conformance/constEnums/constEnumPropertyAccess2.ts(17,1): error TS2322: Type 'string' is not assignable to type 'G'.
4+
tests/cases/conformance/constEnums/constEnumPropertyAccess2.ts(19,1): error TS2364: Invalid left-hand side of assignment expression.
5+
6+
7+
==== tests/cases/conformance/constEnums/constEnumPropertyAccess2.ts (4 errors) ====
8+
9+
// constant enum declarations are completely erased in the emitted JavaScript code.
10+
// it is an error to reference a constant enum object in any other context
11+
// than a property access that selects one of the enum's members
12+
13+
const enum G {
14+
A = 1,
15+
B = 2,
16+
C = A + B,
17+
D = A * 2
18+
}
19+
20+
// Error from referring constant enum in any other context than a property access
21+
var z = G;
22+
~
23+
!!! error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment.
24+
var z1 = G[G.A];
25+
~~~
26+
!!! error TS2476: A const enum member can only be accessed using a string literal.
27+
var g: G;
28+
g = "string";
29+
~
30+
!!! error TS2322: Type 'string' is not assignable to type 'G'.
31+
function foo(x: G) { }
32+
G.B = 3;
33+
~~~
34+
!!! error TS2364: Invalid left-hand side of assignment expression.
35+
Collapse file
+46Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//// [constEnumPropertyAccess2.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+
// Error from referring constant enum in any other context than a property access
15+
var z = G;
16+
var z1 = G[G.A];
17+
var g: G;
18+
g = "string";
19+
function foo(x: G) { }
20+
G.B = 3;
21+
22+
23+
//// [constEnumPropertyAccess2.js]
24+
// constant enum declarations are completely erased in the emitted JavaScript code.
25+
// it is an error to reference a constant enum object in any other context
26+
// than a property access that selects one of the enum's members
27+
// Error from referring constant enum in any other context than a property access
28+
var z = G;
29+
var z1 = G[1 /* A */];
30+
var g;
31+
g = "string";
32+
function foo(x) { }
33+
2 /* B */ = 3;
34+
35+
36+
//// [constEnumPropertyAccess2.d.ts]
37+
declare const enum G {
38+
A = 1,
39+
B = 2,
40+
C = 3,
41+
D = 2,
42+
}
43+
declare var z: typeof G;
44+
declare var z1: any;
45+
declare var g: G;
46+
declare function foo(x: G): void;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.