make globalThis have an empty declarations (#34561)

Fixes #33860 by making it an error.  This is an improvement, but sounds
like it would be better to make it work later.
This commit is contained in:
Eli Barzilay 2019-10-25 22:49:31 -04:00 committed by GitHub
parent d8840f8a18
commit eb0208c589
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 55 additions and 5 deletions

View File

@ -319,6 +319,7 @@ namespace ts {
const globalThisSymbol = createSymbol(SymbolFlags.Module, "globalThis" as __String, CheckFlags.Readonly);
globalThisSymbol.exports = globals;
globalThisSymbol.declarations = [];
globals.set(globalThisSymbol.escapedName, globalThisSymbol);
const argumentsSymbol = createSymbol(SymbolFlags.Property, "arguments" as __String);

View File

@ -3,7 +3,7 @@ declare global {
>global : Symbol(global, Decl(extension.d.ts, 0, 0))
namespace globalThis {
>globalThis : Symbol(globalThis)
>globalThis : Symbol(globalThis, Decl(extension.d.ts, 0, 16))
var test: string;
>test : Symbol(test, Decl(extension.d.ts, 2, 11))
@ -16,7 +16,7 @@ export {}
import "./extention";
globalThis.tests = "a-b";
>globalThis : Symbol(globalThis)
>globalThis : Symbol(globalThis, Decl(extension.d.ts, 0, 16))
console.log(globalThis.test.split("-"));
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
@ -24,7 +24,7 @@ console.log(globalThis.test.split("-"));
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>globalThis.test.split : Symbol(String.split, Decl(lib.es5.d.ts, --, --))
>globalThis.test : Symbol(test, Decl(extension.d.ts, 2, 11))
>globalThis : Symbol(globalThis)
>globalThis : Symbol(globalThis, Decl(extension.d.ts, 0, 16))
>test : Symbol(test, Decl(extension.d.ts, 2, 11))
>split : Symbol(String.split, Decl(lib.es5.d.ts, --, --))

View File

@ -0,0 +1,10 @@
tests/cases/compiler/extendGlobalThis2.ts(1,11): error TS2397: Declaration name conflicts with built-in global identifier 'globalThis'.
==== tests/cases/compiler/extendGlobalThis2.ts (1 errors) ====
namespace globalThis {
~~~~~~~~~~
!!! error TS2397: Declaration name conflicts with built-in global identifier 'globalThis'.
export function foo() { console.log("x"); }
}

View File

@ -0,0 +1,12 @@
//// [extendGlobalThis2.ts]
namespace globalThis {
export function foo() { console.log("x"); }
}
//// [extendGlobalThis2.js]
var globalThis;
(function (globalThis) {
function foo() { console.log("x"); }
globalThis.foo = foo;
})(globalThis || (globalThis = {}));

View File

@ -0,0 +1,11 @@
=== tests/cases/compiler/extendGlobalThis2.ts ===
namespace globalThis {
>globalThis : Symbol(globalThis, Decl(extendGlobalThis2.ts, 0, 0))
export function foo() { console.log("x"); }
>foo : Symbol(foo, Decl(extendGlobalThis2.ts, 0, 22))
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
}

View File

@ -0,0 +1,13 @@
=== tests/cases/compiler/extendGlobalThis2.ts ===
namespace globalThis {
>globalThis : typeof globalThis
export function foo() { console.log("x"); }
>foo : () => void
>console.log("x") : void
>console.log : (message?: any, ...optionalParams: any[]) => void
>console : Console
>log : (message?: any, ...optionalParams: any[]) => void
>"x" : "x"
}

View File

@ -1,7 +1,7 @@
=== tests/cases/conformance/es2019/globalThisPropertyAssignment.js ===
this.x = 1
>this.x : Symbol(x, Decl(globalThisPropertyAssignment.js, 0, 0))
>this : Symbol(globalThis)
>this : Symbol(globalThis, Decl(globalThisPropertyAssignment.js, 3, 12))
>x : Symbol(x, Decl(globalThisPropertyAssignment.js, 0, 0))
var y = 2
@ -14,6 +14,6 @@ window.z = 3
// should work in JS (even though it's a secondary declaration)
globalThis.alpha = 4
>globalThis.alpha : Symbol(alpha, Decl(globalThisPropertyAssignment.js, 3, 12))
>globalThis : Symbol(globalThis)
>globalThis : Symbol(globalThis, Decl(globalThisPropertyAssignment.js, 3, 12))
>alpha : Symbol(alpha, Decl(globalThisPropertyAssignment.js, 3, 12))

View File

@ -0,0 +1,3 @@
namespace globalThis {
export function foo() { console.log("x"); }
}