Skip to content

Commit 874846a

Browse files
authored
Merge pull request #10749 from fabiancook/master
Allow Infinity and NaN to be used as an Enum property identifier
2 parents 890c793 + 5714440 commit 874846a

13 files changed

+96
-1
lines changed

src/compiler/checker.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -10033,6 +10033,10 @@ namespace ts {
1003310033
return isTypeAny(type) || isTypeOfKind(type, kind);
1003410034
}
1003510035

10036+
function isInfinityOrNaNString(name: string): boolean {
10037+
return name === "Infinity" || name === "-Infinity" || name === "NaN";
10038+
}
10039+
1003610040
function isNumericLiteralName(name: string) {
1003710041
// The intent of numeric names is that
1003810042
// - they are names with text in a numeric form, and that
@@ -16848,7 +16852,7 @@ namespace ts {
1684816852
}
1684916853
else {
1685016854
const text = getTextOfPropertyName(<PropertyName>member.name);
16851-
if (isNumericLiteralName(text)) {
16855+
if (isNumericLiteralName(text) && !isInfinityOrNaNString(text)) {
1685216856
error(member.name, Diagnostics.An_enum_member_cannot_have_a_numeric_name);
1685316857
}
1685416858
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//// [enumWithInfinityProperty.ts]
2+
enum A {
3+
Infinity = 1
4+
}
5+
6+
7+
//// [enumWithInfinityProperty.js]
8+
var A;
9+
(function (A) {
10+
A[A["Infinity"] = 1] = "Infinity";
11+
})(A || (A = {}));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/compiler/enumWithInfinityProperty.ts ===
2+
enum A {
3+
>A : Symbol(A, Decl(enumWithInfinityProperty.ts, 0, 0))
4+
5+
Infinity = 1
6+
>Infinity : Symbol(A.Infinity, Decl(enumWithInfinityProperty.ts, 0, 8))
7+
}
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
=== tests/cases/compiler/enumWithInfinityProperty.ts ===
2+
enum A {
3+
>A : A
4+
5+
Infinity = 1
6+
>Infinity : A
7+
>1 : number
8+
}
9+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//// [enumWithNaNProperty.ts]
2+
enum A {
3+
NaN = 1
4+
}
5+
6+
7+
//// [enumWithNaNProperty.js]
8+
var A;
9+
(function (A) {
10+
A[A["NaN"] = 1] = "NaN";
11+
})(A || (A = {}));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/compiler/enumWithNaNProperty.ts ===
2+
enum A {
3+
>A : Symbol(A, Decl(enumWithNaNProperty.ts, 0, 0))
4+
5+
NaN = 1
6+
>NaN : Symbol(A.NaN, Decl(enumWithNaNProperty.ts, 0, 8))
7+
}
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
=== tests/cases/compiler/enumWithNaNProperty.ts ===
2+
enum A {
3+
>A : A
4+
5+
NaN = 1
6+
>NaN : A
7+
>1 : number
8+
}
9+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//// [enumWithNegativeInfinityProperty.ts]
2+
enum A {
3+
"-Infinity" = 1
4+
}
5+
6+
7+
//// [enumWithNegativeInfinityProperty.js]
8+
var A;
9+
(function (A) {
10+
A[A["-Infinity"] = 1] = "-Infinity";
11+
})(A || (A = {}));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=== tests/cases/compiler/enumWithNegativeInfinityProperty.ts ===
2+
enum A {
3+
>A : Symbol(A, Decl(enumWithNegativeInfinityProperty.ts, 0, 0))
4+
5+
"-Infinity" = 1
6+
}
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/compiler/enumWithNegativeInfinityProperty.ts ===
2+
enum A {
3+
>A : A
4+
5+
"-Infinity" = 1
6+
>1 : number
7+
}
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
enum A {
2+
Infinity = 1
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
enum A {
2+
NaN = 1
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
enum A {
2+
"-Infinity" = 1
3+
}

0 commit comments

Comments
 (0)