Error on redeclarations of undefined

This commit is contained in:
Wesley Wigham
2015-10-27 13:24:03 -07:00
parent 6e78b9c34d
commit c4db5cc6b5
5 changed files with 127 additions and 1 deletions
+19 -1
View File
@@ -50,6 +50,7 @@ namespace ts {
let emitResolver = createResolver();
let undefinedSymbol = createSymbol(SymbolFlags.Property | SymbolFlags.Transient, "undefined");
undefinedSymbol.declarations = [];
let argumentsSymbol = createSymbol(SymbolFlags.Property | SymbolFlags.Transient, "arguments");
let checker: TypeChecker = {
@@ -225,6 +226,10 @@ namespace ts {
ResolvedReturnType
}
const builtinGlobals: SymbolTable = {
[undefinedSymbol.name]: undefinedSymbol
};
initializeTypeChecker();
return checker;
@@ -336,6 +341,13 @@ namespace ts {
target[id] = source[id];
}
else {
if (target === globals && source !== builtinGlobals) {
if (hasProperty(builtinGlobals, id) && source[id].declarations && source[id].declarations.length) {
// Error on builtin redeclarations
forEach(source[id].declarations, addDeclarationDiagnostic.bind(undefined, id));
continue;
}
}
let symbol = target[id];
if (!(symbol.flags & SymbolFlags.Merged)) {
target[id] = symbol = cloneSymbol(symbol);
@@ -344,6 +356,10 @@ namespace ts {
}
}
}
function addDeclarationDiagnostic(id: string, declaration: Declaration) {
diagnostics.add(createDiagnosticForNode(declaration, Diagnostics.Declaration_duplicates_builtin_global_identifier_0, id));
}
}
function getSymbolLinks(symbol: Symbol): SymbolLinks {
@@ -14909,6 +14925,9 @@ namespace ts {
bindSourceFile(file);
});
// Setup global builtins
mergeSymbolTable(globals, builtinGlobals);
// Initialize global symbol table
forEach(host.getSourceFiles(), file => {
if (!isExternalModule(file)) {
@@ -14920,7 +14939,6 @@ namespace ts {
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);
globalObjectType = getGlobalType("Object");
+4
View File
@@ -1168,6 +1168,10 @@
"category": "Error",
"code": 2396
},
"Declaration duplicates builtin global identifier '{0}'.": {
"category": "Error",
"code": 2397
},
"Duplicate identifier '_this'. Compiler uses variable declaration '_this' to capture 'this' reference.": {
"category": "Error",
"code": 2399
@@ -0,0 +1,43 @@
tests/cases/compiler/a.ts(1,1): error TS2397: Declaration duplicates builtin global identifier 'undefined'.
tests/cases/compiler/a.ts(2,5): error TS2397: Declaration duplicates builtin global identifier 'undefined'.
tests/cases/compiler/a.ts(3,5): error TS2397: Declaration duplicates builtin global identifier 'undefined'.
tests/cases/compiler/b.ts(1,7): error TS2397: Declaration duplicates builtin global identifier 'undefined'.
tests/cases/compiler/b.ts(4,11): error TS2397: Declaration duplicates builtin global identifier 'undefined'.
tests/cases/compiler/b.ts(7,11): error TS2397: Declaration duplicates builtin global identifier 'undefined'.
tests/cases/compiler/b.ts(10,8): error TS2304: Cannot find name 'undefined'.
==== tests/cases/compiler/a.ts (3 errors) ====
type undefined = string;
~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2397: Declaration duplicates builtin global identifier 'undefined'.
var undefined = void 0;
~~~~~~~~~
!!! error TS2397: Declaration duplicates builtin global identifier 'undefined'.
var undefined = null;
~~~~~~~~~
!!! error TS2397: Declaration duplicates builtin global identifier 'undefined'.
function p(undefined = 42) {
return undefined;
}
==== tests/cases/compiler/b.ts (4 errors) ====
class undefined {
~~~~~~~~~
!!! error TS2397: Declaration duplicates builtin global identifier 'undefined'.
foo: string;
}
interface undefined {
~~~~~~~~~
!!! error TS2397: Declaration duplicates builtin global identifier 'undefined'.
member: number;
}
namespace undefined {
~~~~~~~~~
!!! error TS2397: Declaration duplicates builtin global identifier 'undefined'.
export var x = 42;
}
var x: undefined;
~~~~~~~~~
!!! error TS2304: Cannot find name 'undefined'.
var x: typeof undefined;
@@ -0,0 +1,42 @@
//// [tests/cases/compiler/undefinedTypeAssignment.ts] ////
//// [a.ts]
type undefined = string;
var undefined = void 0;
var undefined = null;
function p(undefined = 42) {
return undefined;
}
//// [b.ts]
class undefined {
foo: string;
}
interface undefined {
member: number;
}
namespace undefined {
export var x = 42;
}
var x: undefined;
var x: typeof undefined;
//// [a.js]
var undefined = void 0;
var undefined = null;
function p(undefined) {
if (undefined === void 0) { undefined = 42; }
return undefined;
}
//// [b.js]
var undefined = (function () {
function undefined() {
}
return undefined;
})();
var undefined;
(function (undefined) {
undefined.x = 42;
})(undefined || (undefined = {}));
var x;
var x;
@@ -0,0 +1,19 @@
// @filename: a.ts
type undefined = string;
var undefined = void 0;
var undefined = null;
function p(undefined = 42) {
return undefined;
}
// @filename: b.ts
class undefined {
foo: string;
}
interface undefined {
member: number;
}
namespace undefined {
export var x = 42;
}
var x: undefined;
var x: typeof undefined;