From 39396b21b14372b9afa5a9cf7c5c79bb062e6e5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Wed, 18 Jun 2025 18:10:28 +0200 Subject: [PATCH] Don't change `global` keyword to `namespace` in `transformModuleDeclaration` --- .../transformers/declarations/transform.go | 2 +- .../declarationEmitRetainsJsdocyComments.js | 3 +- ...clarationEmitRetainsJsdocyComments.js.diff | 28 ++++++++----------- .../compiler/moduleAugmentationGlobal1.js | 3 +- .../moduleAugmentationGlobal1.js.diff | 8 ++---- .../compiler/moduleAugmentationGlobal2.js | 2 +- .../moduleAugmentationGlobal2.js.diff | 10 +------ .../compiler/moduleAugmentationGlobal3.js | 2 +- .../moduleAugmentationGlobal3.js.diff | 10 +------ .../compiler/moduleAugmentationGlobal4.js | 4 +-- .../moduleAugmentationGlobal4.js.diff | 19 ------------- ...tionExportStarShadowingGlobalIsNameable.js | 3 +- ...xportStarShadowingGlobalIsNameable.js.diff | 15 ---------- .../leaveOptionalParameterAsWritten.js | 3 +- .../leaveOptionalParameterAsWritten.js.diff | 16 ++--------- .../submodule/conformance/symbolProperty61.js | 3 +- .../conformance/symbolProperty61.js.diff | 10 ++----- 17 files changed, 31 insertions(+), 110 deletions(-) delete mode 100644 testdata/baselines/reference/submodule/compiler/moduleAugmentationGlobal4.js.diff diff --git a/internal/transformers/declarations/transform.go b/internal/transformers/declarations/transform.go index 647f516357..075c3d6644 100644 --- a/internal/transformers/declarations/transform.go +++ b/internal/transformers/declarations/transform.go @@ -1167,7 +1167,7 @@ func (tx *DeclarationTransformer) transformModuleDeclaration(input *ast.ModuleDe tx.needsDeclare = false inner := input.Body keyword := input.Keyword - if input.Name() == nil || !ast.IsStringLiteral(input.Name()) { + if keyword != ast.KindGlobalKeyword && (input.Name() == nil || !ast.IsStringLiteral(input.Name())) { keyword = ast.KindNamespaceKeyword } diff --git a/testdata/baselines/reference/submodule/compiler/declarationEmitRetainsJsdocyComments.js b/testdata/baselines/reference/submodule/compiler/declarationEmitRetainsJsdocyComments.js index a1bcf4f1ac..6211ab2063 100644 --- a/testdata/baselines/reference/submodule/compiler/declarationEmitRetainsJsdocyComments.js +++ b/testdata/baselines/reference/submodule/compiler/declarationEmitRetainsJsdocyComments.js @@ -113,7 +113,7 @@ export declare class Foo { */ bar(s: number): void; } -declare namespace global { +declare global { interface ExtFunc { /** * comment6 @@ -121,4 +121,3 @@ declare namespace global { someMethod(collection: any[]): boolean; } } -export {}; diff --git a/testdata/baselines/reference/submodule/compiler/declarationEmitRetainsJsdocyComments.js.diff b/testdata/baselines/reference/submodule/compiler/declarationEmitRetainsJsdocyComments.js.diff index e2a02857b7..291c116a53 100644 --- a/testdata/baselines/reference/submodule/compiler/declarationEmitRetainsJsdocyComments.js.diff +++ b/testdata/baselines/reference/submodule/compiler/declarationEmitRetainsJsdocyComments.js.diff @@ -34,18 +34,21 @@ exports.foo = foo; -var Foo = /** @class */ (function () { - function Foo() { -- } +class Foo { - /** - * comment4 - * @param s - */ ++ /** ++ * comment4 ++ * @param s ++ */ ++ bar(s) { + } +- /** +- * comment4 +- * @param s +- */ - Foo.prototype.bar = function (s) { - }; - return Foo; -}()); -+ bar(s) { -+ } +} exports.Foo = Foo; -/** @@ -69,13 +72,6 @@ -* comment5 -*/ -someMethod: any; --declare global { -+declare namespace global { + declare global { interface ExtFunc { - /** - * comment6 -@@= skipped -13, +8 lines =@@ - someMethod(collection: any[]): boolean; - } - } -+export {}; \ No newline at end of file + /** \ No newline at end of file diff --git a/testdata/baselines/reference/submodule/compiler/moduleAugmentationGlobal1.js b/testdata/baselines/reference/submodule/compiler/moduleAugmentationGlobal1.js index 6640282317..32336d5912 100644 --- a/testdata/baselines/reference/submodule/compiler/moduleAugmentationGlobal1.js +++ b/testdata/baselines/reference/submodule/compiler/moduleAugmentationGlobal1.js @@ -39,9 +39,8 @@ export declare class A { //// [f2.d.ts] import { A } from "./f1"; // change the shape of Array -declare namespace global { +declare global { interface Array { getA(): A; } } -export {}; diff --git a/testdata/baselines/reference/submodule/compiler/moduleAugmentationGlobal1.js.diff b/testdata/baselines/reference/submodule/compiler/moduleAugmentationGlobal1.js.diff index f7b1a89180..21c1d86cba 100644 --- a/testdata/baselines/reference/submodule/compiler/moduleAugmentationGlobal1.js.diff +++ b/testdata/baselines/reference/submodule/compiler/moduleAugmentationGlobal1.js.diff @@ -27,11 +27,7 @@ } //// [f2.d.ts] import { A } from "./f1"; --declare global { +// change the shape of Array -+declare namespace global { + declare global { interface Array { - getA(): A; - } - } -+export {}; \ No newline at end of file + getA(): A; \ No newline at end of file diff --git a/testdata/baselines/reference/submodule/compiler/moduleAugmentationGlobal2.js b/testdata/baselines/reference/submodule/compiler/moduleAugmentationGlobal2.js index a1fee14aaf..35074ca47e 100644 --- a/testdata/baselines/reference/submodule/compiler/moduleAugmentationGlobal2.js +++ b/testdata/baselines/reference/submodule/compiler/moduleAugmentationGlobal2.js @@ -35,7 +35,7 @@ let y = x.getCountAsString().toLowerCase(); export declare class A { } //// [f2.d.ts] -declare namespace global { +declare global { interface Array { getCountAsString(): string; } diff --git a/testdata/baselines/reference/submodule/compiler/moduleAugmentationGlobal2.js.diff b/testdata/baselines/reference/submodule/compiler/moduleAugmentationGlobal2.js.diff index a3716ab949..cc57846832 100644 --- a/testdata/baselines/reference/submodule/compiler/moduleAugmentationGlobal2.js.diff +++ b/testdata/baselines/reference/submodule/compiler/moduleAugmentationGlobal2.js.diff @@ -22,12 +22,4 @@ +let y = x.getCountAsString().toLowerCase(); - //// [f1.d.ts] - export declare class A { - } - //// [f2.d.ts] --declare global { -+declare namespace global { - interface Array { - getCountAsString(): string; - } \ No newline at end of file + //// [f1.d.ts] \ No newline at end of file diff --git a/testdata/baselines/reference/submodule/compiler/moduleAugmentationGlobal3.js b/testdata/baselines/reference/submodule/compiler/moduleAugmentationGlobal3.js index 01fdfd85cd..1f2b4c1596 100644 --- a/testdata/baselines/reference/submodule/compiler/moduleAugmentationGlobal3.js +++ b/testdata/baselines/reference/submodule/compiler/moduleAugmentationGlobal3.js @@ -42,7 +42,7 @@ let y = x.getCountAsString().toLowerCase(); export declare class A { } //// [f2.d.ts] -declare namespace global { +declare global { interface Array { getCountAsString(): string; } diff --git a/testdata/baselines/reference/submodule/compiler/moduleAugmentationGlobal3.js.diff b/testdata/baselines/reference/submodule/compiler/moduleAugmentationGlobal3.js.diff index d82b8dddf3..c5d012c82f 100644 --- a/testdata/baselines/reference/submodule/compiler/moduleAugmentationGlobal3.js.diff +++ b/testdata/baselines/reference/submodule/compiler/moduleAugmentationGlobal3.js.diff @@ -24,12 +24,4 @@ +let y = x.getCountAsString().toLowerCase(); - //// [f1.d.ts] - export declare class A { - } - //// [f2.d.ts] --declare global { -+declare namespace global { - interface Array { - getCountAsString(): string; - } \ No newline at end of file + //// [f1.d.ts] \ No newline at end of file diff --git a/testdata/baselines/reference/submodule/compiler/moduleAugmentationGlobal4.js b/testdata/baselines/reference/submodule/compiler/moduleAugmentationGlobal4.js index 96951df31d..4124a7e4f3 100644 --- a/testdata/baselines/reference/submodule/compiler/moduleAugmentationGlobal4.js +++ b/testdata/baselines/reference/submodule/compiler/moduleAugmentationGlobal4.js @@ -30,14 +30,14 @@ require("./f2"); //// [f1.d.ts] -declare namespace global { +declare global { interface Something { x: any; } } export {}; //// [f2.d.ts] -declare namespace global { +declare global { interface Something { y: any; } diff --git a/testdata/baselines/reference/submodule/compiler/moduleAugmentationGlobal4.js.diff b/testdata/baselines/reference/submodule/compiler/moduleAugmentationGlobal4.js.diff deleted file mode 100644 index 4e3a3c9e63..0000000000 --- a/testdata/baselines/reference/submodule/compiler/moduleAugmentationGlobal4.js.diff +++ /dev/null @@ -1,19 +0,0 @@ ---- old.moduleAugmentationGlobal4.js -+++ new.moduleAugmentationGlobal4.js -@@= skipped -29, +29 lines =@@ - - - //// [f1.d.ts] --declare global { -+declare namespace global { - interface Something { - x: any; - } - } - export {}; - //// [f2.d.ts] --declare global { -+declare namespace global { - interface Something { - y: any; - } \ No newline at end of file diff --git a/testdata/baselines/reference/submodule/compiler/moduleDeclarationExportStarShadowingGlobalIsNameable.js b/testdata/baselines/reference/submodule/compiler/moduleDeclarationExportStarShadowingGlobalIsNameable.js index 407de174f9..05a55dc77c 100644 --- a/testdata/baselines/reference/submodule/compiler/moduleDeclarationExportStarShadowingGlobalIsNameable.js +++ b/testdata/baselines/reference/submodule/compiler/moduleDeclarationExportStarShadowingGlobalIsNameable.js @@ -65,7 +65,7 @@ export { Account2 as Acc }; //// [index.d.ts] export * from "./account"; //// [index.d.ts] -declare namespace global { +declare global { interface Account { someProp: number; } @@ -75,4 +75,3 @@ declare namespace global { } import * as model from "./model"; export declare const func: (account: model.Account, acc2: model.Acc) => void; -export {}; diff --git a/testdata/baselines/reference/submodule/compiler/moduleDeclarationExportStarShadowingGlobalIsNameable.js.diff b/testdata/baselines/reference/submodule/compiler/moduleDeclarationExportStarShadowingGlobalIsNameable.js.diff index 358a6e86da..322ae11ab1 100644 --- a/testdata/baselines/reference/submodule/compiler/moduleDeclarationExportStarShadowingGlobalIsNameable.js.diff +++ b/testdata/baselines/reference/submodule/compiler/moduleDeclarationExportStarShadowingGlobalIsNameable.js.diff @@ -8,18 +8,3 @@ +const func = (account, acc2) => { }; exports.func = func; - -@@= skipped -15, +15 lines =@@ - //// [index.d.ts] - export * from "./account"; - //// [index.d.ts] --declare global { -+declare namespace global { - interface Account { - someProp: number; - } -@@= skipped -10, +10 lines =@@ - } - import * as model from "./model"; - export declare const func: (account: model.Account, acc2: model.Acc) => void; -+export {}; \ No newline at end of file diff --git a/testdata/baselines/reference/submodule/conformance/leaveOptionalParameterAsWritten.js b/testdata/baselines/reference/submodule/conformance/leaveOptionalParameterAsWritten.js index 40858d9de3..aa0c0ca22d 100644 --- a/testdata/baselines/reference/submodule/conformance/leaveOptionalParameterAsWritten.js +++ b/testdata/baselines/reference/submodule/conformance/leaveOptionalParameterAsWritten.js @@ -24,13 +24,12 @@ export interface Foo { } //// [b.d.ts] import * as a from "./a"; -declare namespace global { +declare global { namespace teams { namespace calling { export import Foo = a.Foo; } } } -export {}; //// [c.d.ts] export declare const bar: (p?: import("./a").Foo | undefined) => void; diff --git a/testdata/baselines/reference/submodule/conformance/leaveOptionalParameterAsWritten.js.diff b/testdata/baselines/reference/submodule/conformance/leaveOptionalParameterAsWritten.js.diff index 4e943d44ef..9b5671baa5 100644 --- a/testdata/baselines/reference/submodule/conformance/leaveOptionalParameterAsWritten.js.diff +++ b/testdata/baselines/reference/submodule/conformance/leaveOptionalParameterAsWritten.js.diff @@ -1,20 +1,10 @@ --- old.leaveOptionalParameterAsWritten.js +++ new.leaveOptionalParameterAsWritten.js -@@= skipped -23, +23 lines =@@ - } - //// [b.d.ts] - import * as a from "./a"; --declare global { -+declare namespace global { - namespace teams { - namespace calling { - export import Foo = a.Foo; - } +@@= skipped -31, +31 lines =@@ } } --//// [c.d.ts] + //// [c.d.ts] -type Foo = teams.calling.Foo; -export declare const bar: (p?: Foo) => void; - export {}; -+//// [c.d.ts] +-export {}; +export declare const bar: (p?: import("./a").Foo | undefined) => void; \ No newline at end of file diff --git a/testdata/baselines/reference/submodule/conformance/symbolProperty61.js b/testdata/baselines/reference/submodule/conformance/symbolProperty61.js index 4659f8ec8c..3ed2223c95 100644 --- a/testdata/baselines/reference/submodule/conformance/symbolProperty61.js +++ b/testdata/baselines/reference/submodule/conformance/symbolProperty61.js @@ -53,7 +53,7 @@ from(new MyObservable(42)); //// [symbolProperty61.d.ts] -declare namespace global { +declare global { interface SymbolConstructor { readonly obs: symbol; } @@ -64,4 +64,3 @@ export declare class MyObservable { constructor(_val: T); subscribe(next: (val: T) => void): void; } -export {}; diff --git a/testdata/baselines/reference/submodule/conformance/symbolProperty61.js.diff b/testdata/baselines/reference/submodule/conformance/symbolProperty61.js.diff index 5f2bd9e531..d108f7ff87 100644 --- a/testdata/baselines/reference/submodule/conformance/symbolProperty61.js.diff +++ b/testdata/baselines/reference/submodule/conformance/symbolProperty61.js.diff @@ -8,13 +8,7 @@ constructor(_val) { this._val = _val; } -@@= skipped -17, +18 lines =@@ - - - //// [symbolProperty61.d.ts] --declare global { -+declare namespace global { - interface SymbolConstructor { +@@= skipped -22, +23 lines =@@ readonly obs: symbol; } } @@ -26,4 +20,4 @@ subscribe(next: (val: T) => void): void; - [observable](): this; } - export {}; \ No newline at end of file +-export {}; \ No newline at end of file