Skip to content

Commit

Permalink
make globalThis have an empty declarations (microsoft#34561)
Browse files Browse the repository at this point in the history
Fixes microsoft#33860 by making it an error.  This is an improvement, but sounds
like it would be better to make it work later.
  • Loading branch information
elibarzilay committed Oct 26, 2019
1 parent d8840f8 commit eb0208c
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/compiler/checker.ts
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions tests/baselines/reference/extendGlobalThis.symbols
Expand Up @@ -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))
Expand All @@ -16,15 +16,15 @@ 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, --, --))
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
>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, --, --))

10 changes: 10 additions & 0 deletions tests/baselines/reference/extendGlobalThis2.errors.txt
@@ -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"); }
}

12 changes: 12 additions & 0 deletions tests/baselines/reference/extendGlobalThis2.js
@@ -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 = {}));
11 changes: 11 additions & 0 deletions tests/baselines/reference/extendGlobalThis2.symbols
@@ -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, --, --))
}

13 changes: 13 additions & 0 deletions tests/baselines/reference/extendGlobalThis2.types
@@ -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"
}

@@ -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
Expand All @@ -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))

3 changes: 3 additions & 0 deletions tests/cases/compiler/extendGlobalThis2.ts
@@ -0,0 +1,3 @@
namespace globalThis {
export function foo() { console.log("x"); }
}

0 comments on commit eb0208c

Please sign in to comment.