Merge pull request #5420 from weswigham/undefined-unique

Error on redeclarations of undefined
This commit is contained in:
Wesley Wigham
2015-12-04 17:43:12 -08:00
14 changed files with 148 additions and 1 deletions

View File

@@ -51,6 +51,7 @@ namespace ts {
const emitResolver = createResolver();
const undefinedSymbol = createSymbol(SymbolFlags.Property | SymbolFlags.Transient, "undefined");
undefinedSymbol.declarations = [];
const argumentsSymbol = createSymbol(SymbolFlags.Property | SymbolFlags.Transient, "arguments");
const checker: TypeChecker = {
@@ -234,6 +235,10 @@ namespace ts {
ResolvedReturnType
}
const builtinGlobals: SymbolTable = {
[undefinedSymbol.name]: undefinedSymbol
};
initializeTypeChecker();
return checker;
@@ -360,6 +365,24 @@ namespace ts {
}
}
function addToSymbolTable(target: SymbolTable, source: SymbolTable, message: DiagnosticMessage) {
for (const id in source) {
if (hasProperty(source, id)) {
if (hasProperty(target, id)) {
// Error on redeclarations
forEach(target[id].declarations, addDeclarationDiagnostic(id, message));
}
else {
target[id] = source[id];
}
}
}
function addDeclarationDiagnostic(id: string, message: DiagnosticMessage) {
return (declaration: Declaration) => diagnostics.add(createDiagnosticForNode(declaration, message, id));
}
}
function getSymbolLinks(symbol: Symbol): SymbolLinks {
if (symbol.flags & SymbolFlags.Transient) return <TransientSymbol>symbol;
const id = getSymbolId(symbol);
@@ -15327,10 +15350,12 @@ namespace ts {
}
});
// Setup global builtins
addToSymbolTable(globals, builtinGlobals, Diagnostics.Declaration_name_conflicts_with_built_in_global_identifier_0);
getSymbolLinks(undefinedSymbol).type = undefinedType;
getSymbolLinks(argumentsSymbol).type = getGlobalType("IArguments");
getSymbolLinks(unknownSymbol).type = unknownType;
globals[undefinedSymbol.name] = undefinedSymbol;
// Initialize special types
globalArrayType = <GenericType>getGlobalType("Array", /*arity*/ 1);

View File

@@ -1188,6 +1188,10 @@
"category": "Error",
"code": 2396
},
"Declaration name conflicts with built-in global identifier '{0}'.": {
"category": "Error",
"code": 2397
},
"Duplicate identifier '_this'. Compiler uses variable declaration '_this' to capture 'this' reference.": {
"category": "Error",
"code": 2399

View File

@@ -0,0 +1,11 @@
tests/cases/compiler/undefinedTypeAssignment1.ts(1,1): error TS2397: Declaration name conflicts with built-in global identifier 'undefined'.
==== tests/cases/compiler/undefinedTypeAssignment1.ts (1 errors) ====
type undefined = string;
~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2397: Declaration name conflicts with built-in global identifier 'undefined'.
function p(undefined = "wat") {
return undefined;
}

View File

@@ -0,0 +1,12 @@
//// [undefinedTypeAssignment1.ts]
type undefined = string;
function p(undefined = "wat") {
return undefined;
}
//// [undefinedTypeAssignment1.js]
function p(undefined) {
if (undefined === void 0) { undefined = "wat"; }
return undefined;
}

View File

@@ -0,0 +1,8 @@
tests/cases/compiler/undefinedTypeAssignment2.ts(1,5): error TS2397: Declaration name conflicts with built-in global identifier 'undefined'.
==== tests/cases/compiler/undefinedTypeAssignment2.ts (1 errors) ====
var undefined = void 0;
~~~~~~~~~
!!! error TS2397: Declaration name conflicts with built-in global identifier 'undefined'.

View File

@@ -0,0 +1,6 @@
//// [undefinedTypeAssignment2.ts]
var undefined = void 0;
//// [undefinedTypeAssignment2.js]
var undefined = void 0;

View File

@@ -0,0 +1,8 @@
tests/cases/compiler/undefinedTypeAssignment3.ts(1,5): error TS2397: Declaration name conflicts with built-in global identifier 'undefined'.
==== tests/cases/compiler/undefinedTypeAssignment3.ts (1 errors) ====
var undefined = null;
~~~~~~~~~
!!! error TS2397: Declaration name conflicts with built-in global identifier 'undefined'.

View File

@@ -0,0 +1,6 @@
//// [undefinedTypeAssignment3.ts]
var undefined = null;
//// [undefinedTypeAssignment3.js]
var undefined = null;

View File

@@ -0,0 +1,24 @@
tests/cases/compiler/undefinedTypeAssignment4.ts(1,7): error TS2397: Declaration name conflicts with built-in global identifier 'undefined'.
tests/cases/compiler/undefinedTypeAssignment4.ts(4,11): error TS2397: Declaration name conflicts with built-in global identifier 'undefined'.
tests/cases/compiler/undefinedTypeAssignment4.ts(7,11): error TS2397: Declaration name conflicts with built-in global identifier 'undefined'.
==== tests/cases/compiler/undefinedTypeAssignment4.ts (3 errors) ====
class undefined {
~~~~~~~~~
!!! error TS2397: Declaration name conflicts with built-in global identifier 'undefined'.
foo: string;
}
interface undefined {
~~~~~~~~~
!!! error TS2397: Declaration name conflicts with built-in global identifier 'undefined'.
member: number;
}
namespace undefined {
~~~~~~~~~
!!! error TS2397: Declaration name conflicts with built-in global identifier 'undefined'.
export var x = 42;
}
var x: undefined;
var y: typeof undefined;

View File

@@ -0,0 +1,26 @@
//// [undefinedTypeAssignment4.ts]
class undefined {
foo: string;
}
interface undefined {
member: number;
}
namespace undefined {
export var x = 42;
}
var x: undefined;
var y: typeof undefined;
//// [undefinedTypeAssignment4.js]
var undefined = (function () {
function undefined() {
}
return undefined;
})();
var undefined;
(function (undefined) {
undefined.x = 42;
})(undefined || (undefined = {}));
var x;
var y;

View File

@@ -0,0 +1,4 @@
type undefined = string;
function p(undefined = "wat") {
return undefined;
}

View File

@@ -0,0 +1 @@
var undefined = void 0;

View File

@@ -0,0 +1 @@
var undefined = null;

View File

@@ -0,0 +1,11 @@
class undefined {
foo: string;
}
interface undefined {
member: number;
}
namespace undefined {
export var x = 42;
}
var x: undefined;
var y: typeof undefined;