From 92a2c7ff3cd66e541efc7e3b49f7ba95e3b2f42c Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Wed, 8 Oct 2014 17:05:04 -0700 Subject: [PATCH] Use our own scanner for 'isNumericName'. --- src/compiler/checker.ts | 8 +++- .../propertiesAndIndexers.errors.txt | 5 +- .../propertiesAndIndexers2.errors.txt | 46 +++++++++++++++++++ .../reference/propertiesAndIndexers2.js | 19 ++++++++ .../cases/compiler/propertiesAndIndexers2.ts | 15 ++++++ 5 files changed, 88 insertions(+), 5 deletions(-) create mode 100644 tests/baselines/reference/propertiesAndIndexers2.errors.txt create mode 100644 tests/baselines/reference/propertiesAndIndexers2.js create mode 100644 tests/cases/compiler/propertiesAndIndexers2.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 33d4008885..d7fe22ffdb 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -4092,8 +4092,14 @@ module ts { return createArrayType(elementType); } + var numericScanner: Scanner; function isNumericName(name: string) { - return !isNaN(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 { diff --git a/tests/baselines/reference/propertiesAndIndexers.errors.txt b/tests/baselines/reference/propertiesAndIndexers.errors.txt index 4ab8bba644..bfe9c4219e 100644 --- a/tests/baselines/reference/propertiesAndIndexers.errors.txt +++ b/tests/baselines/reference/propertiesAndIndexers.errors.txt @@ -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'. diff --git a/tests/baselines/reference/propertiesAndIndexers2.errors.txt b/tests/baselines/reference/propertiesAndIndexers2.errors.txt new file mode 100644 index 0000000000..e1ab8c9d13 --- /dev/null +++ b/tests/baselines/reference/propertiesAndIndexers2.errors.txt @@ -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'. + } + \ No newline at end of file diff --git a/tests/baselines/reference/propertiesAndIndexers2.js b/tests/baselines/reference/propertiesAndIndexers2.js new file mode 100644 index 0000000000..74485a8924 --- /dev/null +++ b/tests/baselines/reference/propertiesAndIndexers2.js @@ -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] diff --git a/tests/cases/compiler/propertiesAndIndexers2.ts b/tests/cases/compiler/propertiesAndIndexers2.ts new file mode 100644 index 0000000000..188635bf1d --- /dev/null +++ b/tests/cases/compiler/propertiesAndIndexers2.ts @@ -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; +}