Use our own scanner for 'isNumericName'.

This commit is contained in:
Daniel Rosenwasser 2014-10-08 17:05:04 -07:00
parent e5b6bfbc46
commit 92a2c7ff3c
5 changed files with 88 additions and 5 deletions

View file

@ -4092,8 +4092,14 @@ module ts {
return createArrayType(elementType);
}
var numericScanner: Scanner;
function isNumericName(name: string) {
return !isNaN(<number><any>name);
numericScanner = numericScanner || createScanner(compilerOptions.target || ScriptTarget.ES5, /*skipTrivia*/ false);
numericScanner.setText(name);
// Ensure that the name is nothing more than a numeric literal
// (i.e. it is preceded by nothing (whitespace) and scanning leaves us at the very end of the string).
return numericScanner.scan() === SyntaxKind.NumericLiteral && numericScanner.getTextPos() === name.length;
}
function checkObjectLiteral(node: ObjectLiteral, contextualMapper?: TypeMapper): Type {

View file

@ -13,13 +13,12 @@ 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 (19 errors) ====
==== tests/cases/compiler/propertiesAndIndexers.ts (18 errors) ====
interface X { }
interface Y {
n: number;
@ -85,8 +84,6 @@ 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

@ -0,0 +1,46 @@
tests/cases/compiler/propertiesAndIndexers2.ts(2,5): error TS2413: Numeric index type 'string' is not assignable to string index type 'number'.
tests/cases/compiler/propertiesAndIndexers2.ts(8,5): error TS2411: Property 'c' of type 'string' is not assignable to string index type 'number'.
tests/cases/compiler/propertiesAndIndexers2.ts(9,5): error TS2411: Property '3' of type 'string' is not assignable to string index type 'number'.
tests/cases/compiler/propertiesAndIndexers2.ts(10,5): error TS2411: Property 'Infinity' of type 'string' is not assignable to string index type 'number'.
tests/cases/compiler/propertiesAndIndexers2.ts(11,5): error TS2411: Property '"-Infinity"' of type 'string' is not assignable to string index type 'number'.
tests/cases/compiler/propertiesAndIndexers2.ts(12,5): error TS2411: Property 'NaN' of type 'string' is not assignable to string index type 'number'.
tests/cases/compiler/propertiesAndIndexers2.ts(13,5): error TS2411: Property '"-NaN"' of type 'string' is not assignable to string index type 'number'.
tests/cases/compiler/propertiesAndIndexers2.ts(14,5): error TS2411: Property '6' of type '() => string' is not assignable to string index type 'number'.
tests/cases/compiler/propertiesAndIndexers2.ts(14,5): error TS2412: Property '6' of type '() => string' is not assignable to numeric index type 'string'.
==== tests/cases/compiler/propertiesAndIndexers2.ts (9 errors) ====
interface A {
[n: number]: string;
~~~~~~~~~~~~~~~~~~~~
!!! error TS2413: Numeric index type 'string' is not assignable to string index type 'number'.
[s: string]: number;
}
// All of these should fail.
interface B extends A {
c: string;
~~~~~~~~~~
!!! error TS2411: Property 'c' of type 'string' is not assignable to string index type 'number'.
3: string;
~~~~~~~~~~
!!! error TS2411: Property '3' of type 'string' is not assignable to string index type 'number'.
Infinity: string;
~~~~~~~~~~~~~~~~~
!!! error TS2411: Property 'Infinity' of type 'string' is not assignable to string index type 'number'.
"-Infinity": string;
~~~~~~~~~~~~~~~~~~~~
!!! error TS2411: Property '"-Infinity"' of type 'string' is not assignable to string index type 'number'.
NaN: string;
~~~~~~~~~~~~
!!! error TS2411: Property 'NaN' of type 'string' is not assignable to string index type 'number'.
"-NaN": string;
~~~~~~~~~~~~~~~
!!! error TS2411: Property '"-NaN"' of type 'string' is not assignable to string index type 'number'.
6(): string;
~~~~~~~~~~~~
!!! error TS2411: Property '6' of type '() => string' is not assignable to string index type 'number'.
~~~~~~~~~~~~
!!! error TS2412: Property '6' of type '() => string' is not assignable to numeric index type 'string'.
}

View file

@ -0,0 +1,19 @@
//// [propertiesAndIndexers2.ts]
interface A {
[n: number]: string;
[s: string]: number;
}
// All of these should fail.
interface B extends A {
c: string;
3: string;
Infinity: string;
"-Infinity": string;
NaN: string;
"-NaN": string;
6(): string;
}
//// [propertiesAndIndexers2.js]

View file

@ -0,0 +1,15 @@
interface A {
[n: number]: string;
[s: string]: number;
}
// All of these should fail.
interface B extends A {
c: string;
3: string;
Infinity: string;
"-Infinity": string;
NaN: string;
"-NaN": string;
6(): string;
}