Make things more conformant with 9.8.1 of EC-262 spec.

Specifically, a numeric property is simply now any property whose name N is equal to ToString(ToNumber(N)).
This commit is contained in:
Daniel Rosenwasser 2014-10-10 14:12:21 -07:00
parent 291982007a
commit 9f32f64a48
6 changed files with 51 additions and 35 deletions

View file

@ -4083,27 +4083,8 @@ module ts {
return createArrayType(elementType);
}
var numericScanner: Scanner;
function isNumericName(name: string) {
// First see if the name is in canonical string representation.
// We can't simply rely on this because this permits various forms of "NaN" and "Infinity".
if ((+name).toString() !== name) {
return false;
}
numericScanner = numericScanner || createScanner(compilerOptions.target || ScriptTarget.ES5, /*skipTrivia*/ false);
numericScanner.setText(name);
// Ensure that the name is nothing more than an optional sign ('-') and a numeric literal
// (i.e. it is preceded by nothing and scanning leaves us at the very end of the string).
var token = numericScanner.scan();
// '+' will never be in front of a number in its printed form.
if (token === SyntaxKind.MinusToken) {
token = numericScanner.scan();
}
return token === SyntaxKind.NumericLiteral && numericScanner.getTextPos() === name.length;
return (+name).toString() === name;
}
function checkObjectLiteral(node: ObjectLiteral, contextualMapper?: TypeMapper): Type {

View file

@ -6,7 +6,7 @@ tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerCo
tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations.ts(18,5): error TS2412: Property '2.0' of type 'number' is not assignable to numeric index type 'string'.
tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations.ts(21,5): error TS2412: Property '3.0' of type 'MyNumber' is not assignable to numeric index type 'string'.
tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations.ts(50,5): error TS2412: Property '2.0' of type 'number' is not assignable to numeric index type 'string'.
tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations.ts(68,5): error TS2412: Property '2.0' of type 'number' is not assignable to numeric index type 'string'.
tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations.ts(68,5): error TS2412: Property '2.0' of type 'number' is not assignable to numeric index type 'string'.
tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexerConstrainsPropertyDeclarations.ts(78,5): error TS2322: Type '{ [x: number]: {}; 1.0: string; 2.0: number; a: string; b: number; c: () => void; "d": string; "e": number; "3.0": string; "4.0": number; f: any; X: string; foo: () => string; }' is not assignable to type '{ [x: number]: string; }':
Index signatures are incompatible:
Type '{}' is not assignable to type 'string'.

View file

@ -13,12 +13,13 @@ tests/cases/compiler/propertiesAndIndexers.ts(33,11): error TS2411: Property '6'
tests/cases/compiler/propertiesAndIndexers.ts(33,11): error TS2413: Numeric index type 'string' is not assignable to string index type 'number'.
tests/cases/compiler/propertiesAndIndexers.ts(34,5): error TS2411: Property '2' of type 'Z' is not assignable to string index type 'number'.
tests/cases/compiler/propertiesAndIndexers.ts(34,5): error TS2412: Property '2' of type 'Z' is not assignable to numeric index type 'string'.
tests/cases/compiler/propertiesAndIndexers.ts(35,5): error TS2412: Property 'Infinity' of type 'number' is not assignable to numeric index type 'string'.
tests/cases/compiler/propertiesAndIndexers.ts(36,5): error TS2411: Property 'zoo' of type 'string' is not assignable to string index type 'number'.
tests/cases/compiler/propertiesAndIndexers.ts(44,5): error TS2411: Property 't' of type 'number' is not assignable to string index type 'string'.
tests/cases/compiler/propertiesAndIndexers.ts(50,5): error TS2412: Property '3' of type 'boolean' is not assignable to numeric index type 'string'.
==== tests/cases/compiler/propertiesAndIndexers.ts (18 errors) ====
==== tests/cases/compiler/propertiesAndIndexers.ts (19 errors) ====
interface X { }
interface Y {
n: number;
@ -84,6 +85,8 @@ tests/cases/compiler/propertiesAndIndexers.ts(50,5): error TS2412: Property '3'
~~~~~
!!! error TS2412: Property '2' of type 'Z' is not assignable to numeric index type 'string'.
Infinity: number;
~~~~~~~~~~~~~~~~~
!!! error TS2412: Property 'Infinity' of type 'number' is not assignable to numeric index type 'string'.
zoo: string;
~~~~~~~~~~~~
!!! error TS2411: Property 'zoo' of type 'string' is not assignable to string index type 'number'.

View file

@ -3,9 +3,12 @@ tests/cases/compiler/propertiesAndIndexersForNumericNames.ts(7,5): error TS2412:
tests/cases/compiler/propertiesAndIndexersForNumericNames.ts(8,5): error TS2412: Property '"-2.5"' of type 'string' is not assignable to numeric index type 'number'.
tests/cases/compiler/propertiesAndIndexersForNumericNames.ts(9,5): error TS2412: Property '"3.141592"' of type 'string' is not assignable to numeric index type 'number'.
tests/cases/compiler/propertiesAndIndexersForNumericNames.ts(10,5): error TS2412: Property '"1.2e-20"' of type 'string' is not assignable to numeric index type 'number'.
tests/cases/compiler/propertiesAndIndexersForNumericNames.ts(11,5): error TS2412: Property '"Infinity"' of type 'string' is not assignable to numeric index type 'number'.
tests/cases/compiler/propertiesAndIndexersForNumericNames.ts(12,5): error TS2412: Property '"-Infinity"' of type 'string' is not assignable to numeric index type 'number'.
tests/cases/compiler/propertiesAndIndexersForNumericNames.ts(13,5): error TS2412: Property '"NaN"' of type 'string' is not assignable to numeric index type 'number'.
==== tests/cases/compiler/propertiesAndIndexersForNumericNames.ts (5 errors) ====
==== tests/cases/compiler/propertiesAndIndexersForNumericNames.ts (8 errors) ====
class C {
[i: number]: number;
@ -26,6 +29,15 @@ tests/cases/compiler/propertiesAndIndexersForNumericNames.ts(10,5): error TS2412
public "1.2e-20": string = "really small number"; // Error
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2412: Property '"1.2e-20"' of type 'string' is not assignable to numeric index type 'number'.
public "Infinity": string = "A gillion"; // Error
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2412: Property '"Infinity"' of type 'string' is not assignable to numeric index type 'number'.
public "-Infinity": string = "Negative-a-gillion"; // Error
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2412: Property '"-Infinity"' of type 'string' is not assignable to numeric index type 'number'.
public "NaN": string = "not a number"; // Error
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2412: Property '"NaN"' of type 'string' is not assignable to numeric index type 'number'.
// These all have *partially* numeric names,
// but should really be treated as plain string literals.
@ -34,21 +46,24 @@ tests/cases/compiler/propertiesAndIndexersForNumericNames.ts(10,5): error TS2412
public "": string = "no nothing"; // No error
public " ": string = "just space"; // No error
public "1 0 1": string = "several numbers and spaces"; // No error
public "NaN": string = "not a number"; // No error
public "-NaN": string = "not a negative number"; // No error
public "hunter2": string = "not a password"; // No error
public "+Infinity": string = "A gillion"; // No error
public "-Infinity": string = "Negative-a-gillion"; // No error
public "+NaN": string = "not a positive number"; // No error
public "-NaN": string = "not a negative number"; // No error
// These fall into the above category, however, they are "trickier";
// these all are *scanned* as numeric literals, but they are not written in
// "canonical" numeric representations.
public "+1": string = "positive number (for the paranoid)"; // No error
public "1e0": string = "just one"; // No error
public "-0": string = "just zero"; // No error
public "-0e0": string = "just zero"; // No error
public "0xF00D": string = "hex food"; // No error
public "0xBEEF": string = "hex beef"; // No error
public "0123": string = "oct 83"; // No error
public "0o123": string = "explicit oct 83"; // No error
public "0b101101001010": string = "explicit binary"; // No error
public "0.000000000000000000012": string = "should've been in exponential form"; // No error
}

View file

@ -9,6 +9,9 @@ class C {
public "-2.5": string = "negative number"; // Error
public "3.141592": string = "pi-sitive number"; // Error
public "1.2e-20": string = "really small number"; // Error
public "Infinity": string = "A gillion"; // Error
public "-Infinity": string = "Negative-a-gillion"; // Error
public "NaN": string = "not a number"; // Error
// These all have *partially* numeric names,
// but should really be treated as plain string literals.
@ -17,21 +20,24 @@ class C {
public "": string = "no nothing"; // No error
public " ": string = "just space"; // No error
public "1 0 1": string = "several numbers and spaces"; // No error
public "NaN": string = "not a number"; // No error
public "-NaN": string = "not a negative number"; // No error
public "hunter2": string = "not a password"; // No error
public "+Infinity": string = "A gillion"; // No error
public "-Infinity": string = "Negative-a-gillion"; // No error
public "+NaN": string = "not a positive number"; // No error
public "-NaN": string = "not a negative number"; // No error
// These fall into the above category, however, they are "trickier";
// these all are *scanned* as numeric literals, but they are not written in
// "canonical" numeric representations.
public "+1": string = "positive number (for the paranoid)"; // No error
public "1e0": string = "just one"; // No error
public "-0": string = "just zero"; // No error
public "-0e0": string = "just zero"; // No error
public "0xF00D": string = "hex food"; // No error
public "0xBEEF": string = "hex beef"; // No error
public "0123": string = "oct 83"; // No error
public "0o123": string = "explicit oct 83"; // No error
public "0b101101001010": string = "explicit binary"; // No error
public "0.000000000000000000012": string = "should've been in exponential form"; // No error
}
@ -46,6 +52,9 @@ var C = (function () {
this["-2.5"] = "negative number"; // Error
this["3.141592"] = "pi-sitive number"; // Error
this["1.2e-20"] = "really small number"; // Error
this["Infinity"] = "A gillion"; // Error
this["-Infinity"] = "Negative-a-gillion"; // Error
this["NaN"] = "not a number"; // Error
// These all have *partially* numeric names,
// but should really be treated as plain string literals.
this[" 1"] = "leading space"; // No error
@ -53,20 +62,22 @@ var C = (function () {
this[""] = "no nothing"; // No error
this[" "] = "just space"; // No error
this["1 0 1"] = "several numbers and spaces"; // No error
this["NaN"] = "not a number"; // No error
this["-NaN"] = "not a negative number"; // No error
this["hunter2"] = "not a password"; // No error
this["+Infinity"] = "A gillion"; // No error
this["-Infinity"] = "Negative-a-gillion"; // No error
this["+NaN"] = "not a positive number"; // No error
this["-NaN"] = "not a negative number"; // No error
// These fall into the above category, however, they are "trickier";
// these all are *scanned* as numeric literals, but they are not written in
// "canonical" numeric representations.
this["+1"] = "positive number (for the paranoid)"; // No error
this["1e0"] = "just one"; // No error
this["-0"] = "just zero"; // No error
this["-0e0"] = "just zero"; // No error
this["0xF00D"] = "hex food"; // No error
this["0xBEEF"] = "hex beef"; // No error
this["0123"] = "oct 83"; // No error
this["0o123"] = "explicit oct 83"; // No error
this["0b101101001010"] = "explicit binary"; // No error
this["0.000000000000000000012"] = "should've been in exponential form"; // No error
}
return C;

View file

@ -8,6 +8,9 @@ class C {
public "-2.5": string = "negative number"; // Error
public "3.141592": string = "pi-sitive number"; // Error
public "1.2e-20": string = "really small number"; // Error
public "Infinity": string = "A gillion"; // Error
public "-Infinity": string = "Negative-a-gillion"; // Error
public "NaN": string = "not a number"; // Error
// These all have *partially* numeric names,
// but should really be treated as plain string literals.
@ -16,20 +19,23 @@ class C {
public "": string = "no nothing"; // No error
public " ": string = "just space"; // No error
public "1 0 1": string = "several numbers and spaces"; // No error
public "NaN": string = "not a number"; // No error
public "-NaN": string = "not a negative number"; // No error
public "hunter2": string = "not a password"; // No error
public "+Infinity": string = "A gillion"; // No error
public "-Infinity": string = "Negative-a-gillion"; // No error
public "+NaN": string = "not a positive number"; // No error
public "-NaN": string = "not a negative number"; // No error
// These fall into the above category, however, they are "trickier";
// these all are *scanned* as numeric literals, but they are not written in
// "canonical" numeric representations.
public "+1": string = "positive number (for the paranoid)"; // No error
public "1e0": string = "just one"; // No error
public "-0": string = "just zero"; // No error
public "-0e0": string = "just zero"; // No error
public "0xF00D": string = "hex food"; // No error
public "0xBEEF": string = "hex beef"; // No error
public "0123": string = "oct 83"; // No error
public "0o123": string = "explicit oct 83"; // No error
public "0b101101001010": string = "explicit binary"; // No error
public "0.000000000000000000012": string = "should've been in exponential form"; // No error
}