Skip to content

Commit 4538640

Browse files
authored
Added error when Enum member initaliser references itself (microsoft#34655)
Fixes microsoft#34606
1 parent edd4e0a commit 4538640

6 files changed

+99
-0
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33764,6 +33764,9 @@ namespace ts {
3376433764
error(expr, Diagnostics.A_member_initializer_in_a_enum_declaration_cannot_reference_members_declared_after_it_including_members_defined_in_other_enums);
3376533765
return 0;
3376633766
}
33767+
else {
33768+
error(expr, Diagnostics.Property_0_is_used_before_being_assigned, symbolToString(memberSymbol));
33769+
}
3376733770
}
3376833771
return undefined;
3376933772
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
tests/cases/compiler/enumPropertyAccessBeforeInitalisation.ts(2,9): error TS2565: Property 'A' is used before being assigned.
2+
tests/cases/compiler/enumPropertyAccessBeforeInitalisation.ts(3,9): error TS2565: Property 'B' is used before being assigned.
3+
tests/cases/compiler/enumPropertyAccessBeforeInitalisation.ts(4,9): error TS2565: Property 'C' is used before being assigned.
4+
tests/cases/compiler/enumPropertyAccessBeforeInitalisation.ts(5,13): error TS2565: Property 'D' is used before being assigned.
5+
6+
7+
==== tests/cases/compiler/enumPropertyAccessBeforeInitalisation.ts (4 errors) ====
8+
enum E {
9+
A = A,
10+
~
11+
!!! error TS2565: Property 'A' is used before being assigned.
12+
B = E.B,
13+
~~~
14+
!!! error TS2565: Property 'B' is used before being assigned.
15+
C = E["C"],
16+
~~~~~~
17+
!!! error TS2565: Property 'C' is used before being assigned.
18+
D = 1 + D
19+
~
20+
!!! error TS2565: Property 'D' is used before being assigned.
21+
}
22+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//// [enumPropertyAccessBeforeInitalisation.ts]
2+
enum E {
3+
A = A,
4+
B = E.B,
5+
C = E["C"],
6+
D = 1 + D
7+
}
8+
9+
10+
//// [enumPropertyAccessBeforeInitalisation.js]
11+
var E;
12+
(function (E) {
13+
E[E["A"] = E.A] = "A";
14+
E[E["B"] = E.B] = "B";
15+
E[E["C"] = E["C"]] = "C";
16+
E[E["D"] = 1 + E.D] = "D";
17+
})(E || (E = {}));
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
=== tests/cases/compiler/enumPropertyAccessBeforeInitalisation.ts ===
2+
enum E {
3+
>E : Symbol(E, Decl(enumPropertyAccessBeforeInitalisation.ts, 0, 0))
4+
5+
A = A,
6+
>A : Symbol(E.A, Decl(enumPropertyAccessBeforeInitalisation.ts, 0, 8))
7+
>A : Symbol(E.A, Decl(enumPropertyAccessBeforeInitalisation.ts, 0, 8))
8+
9+
B = E.B,
10+
>B : Symbol(E.B, Decl(enumPropertyAccessBeforeInitalisation.ts, 1, 10))
11+
>E.B : Symbol(E.B, Decl(enumPropertyAccessBeforeInitalisation.ts, 1, 10))
12+
>E : Symbol(E, Decl(enumPropertyAccessBeforeInitalisation.ts, 0, 0))
13+
>B : Symbol(E.B, Decl(enumPropertyAccessBeforeInitalisation.ts, 1, 10))
14+
15+
C = E["C"],
16+
>C : Symbol(E.C, Decl(enumPropertyAccessBeforeInitalisation.ts, 2, 12))
17+
>E : Symbol(E, Decl(enumPropertyAccessBeforeInitalisation.ts, 0, 0))
18+
>"C" : Symbol(E.C, Decl(enumPropertyAccessBeforeInitalisation.ts, 2, 12))
19+
20+
D = 1 + D
21+
>D : Symbol(E.D, Decl(enumPropertyAccessBeforeInitalisation.ts, 3, 15))
22+
>D : Symbol(E.D, Decl(enumPropertyAccessBeforeInitalisation.ts, 3, 15))
23+
}
24+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
=== tests/cases/compiler/enumPropertyAccessBeforeInitalisation.ts ===
2+
enum E {
3+
>E : E
4+
5+
A = A,
6+
>A : E
7+
>A : E
8+
9+
B = E.B,
10+
>B : E
11+
>E.B : E
12+
>E : typeof E
13+
>B : E
14+
15+
C = E["C"],
16+
>C : E
17+
>E["C"] : E
18+
>E : typeof E
19+
>"C" : "C"
20+
21+
D = 1 + D
22+
>D : E
23+
>1 + D : number
24+
>1 : 1
25+
>D : E
26+
}
27+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
enum E {
2+
A = A,
3+
B = E.B,
4+
C = E["C"],
5+
D = 1 + D
6+
}

0 commit comments

Comments
 (0)