From c4db5cc6b590bac31d0379b6db0caf3700b76d00 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Tue, 27 Oct 2015 12:47:35 -0700 Subject: [PATCH] Error on redeclarations of undefined --- src/compiler/checker.ts | 20 ++++++++- src/compiler/diagnosticMessages.json | 4 ++ .../undefinedTypeAssignment.errors.txt | 43 +++++++++++++++++++ .../reference/undefinedTypeAssignment.js | 42 ++++++++++++++++++ .../cases/compiler/undefinedTypeAssignment.ts | 19 ++++++++ 5 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/undefinedTypeAssignment.errors.txt create mode 100644 tests/baselines/reference/undefinedTypeAssignment.js create mode 100644 tests/cases/compiler/undefinedTypeAssignment.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 5370e7c4f02..ba0342596c1 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -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 = getGlobalType("Array", /*arity*/ 1); globalObjectType = getGlobalType("Object"); diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index b799c2addba..d6e7d8dd592 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -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 diff --git a/tests/baselines/reference/undefinedTypeAssignment.errors.txt b/tests/baselines/reference/undefinedTypeAssignment.errors.txt new file mode 100644 index 00000000000..a14b1191255 --- /dev/null +++ b/tests/baselines/reference/undefinedTypeAssignment.errors.txt @@ -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; + \ No newline at end of file diff --git a/tests/baselines/reference/undefinedTypeAssignment.js b/tests/baselines/reference/undefinedTypeAssignment.js new file mode 100644 index 00000000000..9b4106c18ea --- /dev/null +++ b/tests/baselines/reference/undefinedTypeAssignment.js @@ -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; diff --git a/tests/cases/compiler/undefinedTypeAssignment.ts b/tests/cases/compiler/undefinedTypeAssignment.ts new file mode 100644 index 00000000000..d8a128a49e7 --- /dev/null +++ b/tests/cases/compiler/undefinedTypeAssignment.ts @@ -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;