Allow Infinity and NaN to be used as an Enum property identifier

This commit is contained in:
Fabian Cook 2016-09-08 01:46:10 +12:00
parent 4685646281
commit 088da9ea9d
9 changed files with 67 additions and 1 deletions

View File

@ -10033,6 +10033,10 @@ namespace ts {
return isTypeAny(type) || isTypeOfKind(type, kind);
}
function isInfinityOrNaNString(name: string) : boolean {
return name === 'Infinity' || name === 'NaN';
}
function isNumericLiteralName(name: string) {
// The intent of numeric names is that
// - they are names with text in a numeric form, and that
@ -16848,7 +16852,7 @@ namespace ts {
}
else {
const text = getTextOfPropertyName(<PropertyName>member.name);
if (isNumericLiteralName(text)) {
if (isNumericLiteralName(text) && !isInfinityOrNaNString(text)) {
error(member.name, Diagnostics.An_enum_member_cannot_have_a_numeric_name);
}
}

View File

@ -0,0 +1,11 @@
//// [enumWithInfinityProperty.ts]
enum A {
Infinity = 1
}
//// [enumWithInfinityProperty.js]
var A;
(function (A) {
A[A["Infinity"] = 1] = "Infinity";
})(A || (A = {}));

View File

@ -0,0 +1,8 @@
=== tests/cases/compiler/enumWithInfinityProperty.ts ===
enum A {
>A : Symbol(A, Decl(enumWithInfinityProperty.ts, 0, 0))
Infinity = 1
>Infinity : Symbol(A.Infinity, Decl(enumWithInfinityProperty.ts, 0, 8))
}

View File

@ -0,0 +1,9 @@
=== tests/cases/compiler/enumWithInfinityProperty.ts ===
enum A {
>A : A
Infinity = 1
>Infinity : A
>1 : number
}

View File

@ -0,0 +1,11 @@
//// [enumWithNaNProperty.ts]
enum A {
NaN = 1
}
//// [enumWithNaNProperty.js]
var A;
(function (A) {
A[A["NaN"] = 1] = "NaN";
})(A || (A = {}));

View File

@ -0,0 +1,8 @@
=== tests/cases/compiler/enumWithNaNProperty.ts ===
enum A {
>A : Symbol(A, Decl(enumWithNaNProperty.ts, 0, 0))
NaN = 1
>NaN : Symbol(A.NaN, Decl(enumWithNaNProperty.ts, 0, 8))
}

View File

@ -0,0 +1,9 @@
=== tests/cases/compiler/enumWithNaNProperty.ts ===
enum A {
>A : A
NaN = 1
>NaN : A
>1 : number
}

View File

@ -0,0 +1,3 @@
enum A {
Infinity = 1
}

View File

@ -0,0 +1,3 @@
enum A {
NaN = 1
}