Handle numeric signs in 'isNumericName'.

To be fair, I don't think humanity knew about negative numbers until like 200 B.C.E.
This commit is contained in:
Daniel Rosenwasser
2014-10-09 00:33:35 -07:00
parent 92a2c7ff3c
commit 5a49bb610a
4 changed files with 91 additions and 3 deletions

View File

@@ -4097,9 +4097,15 @@ module ts {
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;
// 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();
if (token === SyntaxKind.MinusToken || token === SyntaxKind.PlusToken) {
token = numericScanner.scan();
}
return token === SyntaxKind.NumericLiteral && numericScanner.getTextPos() === name.length;
}
function checkObjectLiteral(node: ObjectLiteral, contextualMapper?: TypeMapper): Type {

View File

@@ -0,0 +1,29 @@
tests/cases/compiler/propertiesAndIndexersForNumericNames.ts(3,3): error TS2412: Property '"1"' of type 'string' is not assignable to numeric index type 'number'.
tests/cases/compiler/propertiesAndIndexersForNumericNames.ts(4,3): error TS2412: Property '"-1"' of type 'string' is not assignable to numeric index type 'number'.
tests/cases/compiler/propertiesAndIndexersForNumericNames.ts(5,3): error TS2412: Property '"+1"' of type 'string' is not assignable to numeric index type 'number'.
==== tests/cases/compiler/propertiesAndIndexersForNumericNames.ts (3 errors) ====
class C {
[i: number]: number;
public "1": string = "number"; // Error
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2412: Property '"1"' of type 'string' is not assignable to numeric index type 'number'.
public "-1": string = "negative number"; // Error
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2412: Property '"-1"' of type 'string' is not assignable to numeric index type 'number'.
public "+1": string = "positive number (for the paranoid)"; // Error
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2412: Property '"+1"' of type 'string' is not assignable to numeric index type 'number'.
public " 1": string = "leading space"; // No error
public "1 ": string = "trailing space"; // No error
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 "+Infinity": string = "A gillion"; // No error
public "-Infinity": string = "Negative-a-gillion"; // No error
}

View File

@@ -0,0 +1,37 @@
//// [propertiesAndIndexersForNumericNames.ts]
class C {
[i: number]: number;
public "1": string = "number"; // Error
public "-1": string = "negative number"; // Error
public "+1": string = "positive number (for the paranoid)"; // Error
public " 1": string = "leading space"; // No error
public "1 ": string = "trailing space"; // No error
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 "+Infinity": string = "A gillion"; // No error
public "-Infinity": string = "Negative-a-gillion"; // No error
}
//// [propertiesAndIndexersForNumericNames.js]
var C = (function () {
function C() {
this["1"] = "number"; // Error
this["-1"] = "negative number"; // Error
this["+1"] = "positive number (for the paranoid)"; // Error
this[" 1"] = "leading space"; // No error
this["1 "] = "trailing space"; // No error
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["+Infinity"] = "A gillion"; // No error
this["-Infinity"] = "Negative-a-gillion"; // No error
}
return C;
})();

View File

@@ -0,0 +1,16 @@
class C {
[i: number]: number;
public "1": string = "number"; // Error
public "-1": string = "negative number"; // Error
public "+1": string = "positive number (for the paranoid)"; // Error
public " 1": string = "leading space"; // No error
public "1 ": string = "trailing space"; // No error
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 "+Infinity": string = "A gillion"; // No error
public "-Infinity": string = "Negative-a-gillion"; // No error
}