Skip to content

make globalThis have an empty declarations #34561

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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"
}

Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace globalThis {
export function foo() { console.log("x"); }
}