Skip to content

Commit eb0208c

Browse files
authored
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.
1 parent d8840f8 commit eb0208c

8 files changed

+55
-5
lines changed

src/compiler/checker.ts

+1
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ namespace ts {
319319

320320
const globalThisSymbol = createSymbol(SymbolFlags.Module, "globalThis" as __String, CheckFlags.Readonly);
321321
globalThisSymbol.exports = globals;
322+
globalThisSymbol.declarations = [];
322323
globals.set(globalThisSymbol.escapedName, globalThisSymbol);
323324

324325
const argumentsSymbol = createSymbol(SymbolFlags.Property, "arguments" as __String);

tests/baselines/reference/extendGlobalThis.symbols

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ declare global {
33
>global : Symbol(global, Decl(extension.d.ts, 0, 0))
44

55
namespace globalThis {
6-
>globalThis : Symbol(globalThis)
6+
>globalThis : Symbol(globalThis, Decl(extension.d.ts, 0, 16))
77

88
var test: string;
99
>test : Symbol(test, Decl(extension.d.ts, 2, 11))
@@ -16,15 +16,15 @@ export {}
1616
import "./extention";
1717

1818
globalThis.tests = "a-b";
19-
>globalThis : Symbol(globalThis)
19+
>globalThis : Symbol(globalThis, Decl(extension.d.ts, 0, 16))
2020

2121
console.log(globalThis.test.split("-"));
2222
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
2323
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
2424
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
2525
>globalThis.test.split : Symbol(String.split, Decl(lib.es5.d.ts, --, --))
2626
>globalThis.test : Symbol(test, Decl(extension.d.ts, 2, 11))
27-
>globalThis : Symbol(globalThis)
27+
>globalThis : Symbol(globalThis, Decl(extension.d.ts, 0, 16))
2828
>test : Symbol(test, Decl(extension.d.ts, 2, 11))
2929
>split : Symbol(String.split, Decl(lib.es5.d.ts, --, --))
3030

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
tests/cases/compiler/extendGlobalThis2.ts(1,11): error TS2397: Declaration name conflicts with built-in global identifier 'globalThis'.
2+
3+
4+
==== tests/cases/compiler/extendGlobalThis2.ts (1 errors) ====
5+
namespace globalThis {
6+
~~~~~~~~~~
7+
!!! error TS2397: Declaration name conflicts with built-in global identifier 'globalThis'.
8+
export function foo() { console.log("x"); }
9+
}
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//// [extendGlobalThis2.ts]
2+
namespace globalThis {
3+
export function foo() { console.log("x"); }
4+
}
5+
6+
7+
//// [extendGlobalThis2.js]
8+
var globalThis;
9+
(function (globalThis) {
10+
function foo() { console.log("x"); }
11+
globalThis.foo = foo;
12+
})(globalThis || (globalThis = {}));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
=== tests/cases/compiler/extendGlobalThis2.ts ===
2+
namespace globalThis {
3+
>globalThis : Symbol(globalThis, Decl(extendGlobalThis2.ts, 0, 0))
4+
5+
export function foo() { console.log("x"); }
6+
>foo : Symbol(foo, Decl(extendGlobalThis2.ts, 0, 22))
7+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
8+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
9+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
10+
}
11+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/extendGlobalThis2.ts ===
2+
namespace globalThis {
3+
>globalThis : typeof globalThis
4+
5+
export function foo() { console.log("x"); }
6+
>foo : () => void
7+
>console.log("x") : void
8+
>console.log : (message?: any, ...optionalParams: any[]) => void
9+
>console : Console
10+
>log : (message?: any, ...optionalParams: any[]) => void
11+
>"x" : "x"
12+
}
13+

tests/baselines/reference/globalThisPropertyAssignment.symbols

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
=== tests/cases/conformance/es2019/globalThisPropertyAssignment.js ===
22
this.x = 1
33
>this.x : Symbol(x, Decl(globalThisPropertyAssignment.js, 0, 0))
4-
>this : Symbol(globalThis)
4+
>this : Symbol(globalThis, Decl(globalThisPropertyAssignment.js, 3, 12))
55
>x : Symbol(x, Decl(globalThisPropertyAssignment.js, 0, 0))
66

77
var y = 2
@@ -14,6 +14,6 @@ window.z = 3
1414
// should work in JS (even though it's a secondary declaration)
1515
globalThis.alpha = 4
1616
>globalThis.alpha : Symbol(alpha, Decl(globalThisPropertyAssignment.js, 3, 12))
17-
>globalThis : Symbol(globalThis)
17+
>globalThis : Symbol(globalThis, Decl(globalThisPropertyAssignment.js, 3, 12))
1818
>alpha : Symbol(alpha, Decl(globalThisPropertyAssignment.js, 3, 12))
1919

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
namespace globalThis {
2+
export function foo() { console.log("x"); }
3+
}

0 commit comments

Comments
 (0)