Added error when Enum member initaliser references itself (#34655)

Fixes #34606
This commit is contained in:
Ashley Claymore 2020-04-14 20:20:19 +01:00 committed by GitHub
parent edd4e0a42b
commit 4538640d8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 99 additions and 0 deletions

View File

@ -33764,6 +33764,9 @@ namespace ts {
error(expr, Diagnostics.A_member_initializer_in_a_enum_declaration_cannot_reference_members_declared_after_it_including_members_defined_in_other_enums);
return 0;
}
else {
error(expr, Diagnostics.Property_0_is_used_before_being_assigned, symbolToString(memberSymbol));
}
}
return undefined;
}

View File

@ -0,0 +1,22 @@
tests/cases/compiler/enumPropertyAccessBeforeInitalisation.ts(2,9): error TS2565: Property 'A' is used before being assigned.
tests/cases/compiler/enumPropertyAccessBeforeInitalisation.ts(3,9): error TS2565: Property 'B' is used before being assigned.
tests/cases/compiler/enumPropertyAccessBeforeInitalisation.ts(4,9): error TS2565: Property 'C' is used before being assigned.
tests/cases/compiler/enumPropertyAccessBeforeInitalisation.ts(5,13): error TS2565: Property 'D' is used before being assigned.
==== tests/cases/compiler/enumPropertyAccessBeforeInitalisation.ts (4 errors) ====
enum E {
A = A,
~
!!! error TS2565: Property 'A' is used before being assigned.
B = E.B,
~~~
!!! error TS2565: Property 'B' is used before being assigned.
C = E["C"],
~~~~~~
!!! error TS2565: Property 'C' is used before being assigned.
D = 1 + D
~
!!! error TS2565: Property 'D' is used before being assigned.
}

View File

@ -0,0 +1,17 @@
//// [enumPropertyAccessBeforeInitalisation.ts]
enum E {
A = A,
B = E.B,
C = E["C"],
D = 1 + D
}
//// [enumPropertyAccessBeforeInitalisation.js]
var E;
(function (E) {
E[E["A"] = E.A] = "A";
E[E["B"] = E.B] = "B";
E[E["C"] = E["C"]] = "C";
E[E["D"] = 1 + E.D] = "D";
})(E || (E = {}));

View File

@ -0,0 +1,24 @@
=== tests/cases/compiler/enumPropertyAccessBeforeInitalisation.ts ===
enum E {
>E : Symbol(E, Decl(enumPropertyAccessBeforeInitalisation.ts, 0, 0))
A = A,
>A : Symbol(E.A, Decl(enumPropertyAccessBeforeInitalisation.ts, 0, 8))
>A : Symbol(E.A, Decl(enumPropertyAccessBeforeInitalisation.ts, 0, 8))
B = E.B,
>B : Symbol(E.B, Decl(enumPropertyAccessBeforeInitalisation.ts, 1, 10))
>E.B : Symbol(E.B, Decl(enumPropertyAccessBeforeInitalisation.ts, 1, 10))
>E : Symbol(E, Decl(enumPropertyAccessBeforeInitalisation.ts, 0, 0))
>B : Symbol(E.B, Decl(enumPropertyAccessBeforeInitalisation.ts, 1, 10))
C = E["C"],
>C : Symbol(E.C, Decl(enumPropertyAccessBeforeInitalisation.ts, 2, 12))
>E : Symbol(E, Decl(enumPropertyAccessBeforeInitalisation.ts, 0, 0))
>"C" : Symbol(E.C, Decl(enumPropertyAccessBeforeInitalisation.ts, 2, 12))
D = 1 + D
>D : Symbol(E.D, Decl(enumPropertyAccessBeforeInitalisation.ts, 3, 15))
>D : Symbol(E.D, Decl(enumPropertyAccessBeforeInitalisation.ts, 3, 15))
}

View File

@ -0,0 +1,27 @@
=== tests/cases/compiler/enumPropertyAccessBeforeInitalisation.ts ===
enum E {
>E : E
A = A,
>A : E
>A : E
B = E.B,
>B : E
>E.B : E
>E : typeof E
>B : E
C = E["C"],
>C : E
>E["C"] : E
>E : typeof E
>"C" : "C"
D = 1 + D
>D : E
>1 + D : number
>1 : 1
>D : E
}

View File

@ -0,0 +1,6 @@
enum E {
A = A,
B = E.B,
C = E["C"],
D = 1 + D
}