Skip to content

Commit a94eb31

Browse files
Transform namespaces declared with module keyword to namespace keyword in d.ts files (#54134)
Signed-off-by: Chi Leung <chiguan.leung@gmail.com> Co-authored-by: Titian Cernicova-Dragomir <dragomirtitian@yahoo.com> Co-authored-by: Titian Cernicova-Dragomir <tcernicovad1@bloomberg.net>
1 parent 7dd7caa commit a94eb31

File tree

174 files changed

+547
-518
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

174 files changed

+547
-518
lines changed

src/compiler/transformers/declarations.ts

+31-2
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ import {
9191
ImportTypeNode,
9292
IndexSignatureDeclaration,
9393
InterfaceDeclaration,
94+
isAmbientModule,
9495
isAnyImportSyntax,
9596
isArray,
9697
isArrayBindingElement,
@@ -163,8 +164,10 @@ import {
163164
MethodSignature,
164165
Modifier,
165166
ModifierFlags,
167+
ModifierLike,
166168
ModuleBody,
167169
ModuleDeclaration,
170+
ModuleName,
168171
NamedDeclaration,
169172
NamespaceDeclaration,
170173
needsScopeMarker,
@@ -1422,6 +1425,31 @@ export function transformDeclarations(context: TransformationContext) {
14221425
return factory.updateModifiers(statement, modifiers);
14231426
}
14241427

1428+
function updateModuleDeclarationAndKeyword(
1429+
node: ModuleDeclaration,
1430+
modifiers: readonly ModifierLike[] | undefined,
1431+
name: ModuleName,
1432+
body: ModuleBody | undefined
1433+
) {
1434+
const updated = factory.updateModuleDeclaration(node, modifiers, name, body);
1435+
1436+
if (isAmbientModule(updated) || updated.flags & NodeFlags.Namespace) {
1437+
return updated;
1438+
}
1439+
1440+
const fixed = factory.createModuleDeclaration(
1441+
updated.modifiers,
1442+
updated.name,
1443+
updated.body,
1444+
updated.flags | NodeFlags.Namespace
1445+
);
1446+
1447+
setOriginalNode(fixed, updated);
1448+
setTextRange(fixed, updated);
1449+
1450+
return fixed;
1451+
}
1452+
14251453
function transformTopLevelDeclaration(input: LateVisibilityPaintedStatement) {
14261454
if (lateMarkedStatements) {
14271455
while (orderedRemoveItem(lateMarkedStatements, input));
@@ -1598,7 +1626,8 @@ export function transformDeclarations(context: TransformationContext) {
15981626
needsScopeFixMarker = oldNeedsScopeFix;
15991627
resultHasScopeMarker = oldHasScopeFix;
16001628
const mods = ensureModifiers(input);
1601-
return cleanup(factory.updateModuleDeclaration(
1629+
1630+
return cleanup(updateModuleDeclarationAndKeyword(
16021631
input,
16031632
mods,
16041633
isExternalModuleAugmentation(input) ? rewriteModuleSpecifier(input, input.name) : input.name,
@@ -1614,7 +1643,7 @@ export function transformDeclarations(context: TransformationContext) {
16141643
const id = getOriginalNodeId(inner!); // TODO: GH#18217
16151644
const body = lateStatementReplacementMap.get(id);
16161645
lateStatementReplacementMap.delete(id);
1617-
return cleanup(factory.updateModuleDeclaration(
1646+
return cleanup(updateModuleDeclarationAndKeyword(
16181647
input,
16191648
mods,
16201649
input.name,

tests/baselines/reference/aliasInaccessibleModule.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ var M;
1414

1515

1616
//// [aliasInaccessibleModule.d.ts]
17-
declare module M {
18-
module N {
17+
declare namespace M {
18+
namespace N {
1919
}
2020
export import X = N;
2121
export {};

tests/baselines/reference/aliasInaccessibleModule2.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ var M;
2828

2929

3030
//// [aliasInaccessibleModule2.d.ts]
31-
declare module M {
32-
module N {
31+
declare namespace M {
32+
namespace N {
3333
}
3434
import R = N;
3535
export import X = R;

tests/baselines/reference/classConstructorAccessibility.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,5 @@ declare class E {
100100
declare var c: C;
101101
declare var d: any;
102102
declare var e: any;
103-
declare module Generic {
103+
declare namespace Generic {
104104
}

tests/baselines/reference/classDoesNotDependOnPrivateMember.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ var M;
2121

2222

2323
//// [classDoesNotDependOnPrivateMember.d.ts]
24-
declare module M {
24+
declare namespace M {
2525
class C {
2626
private x;
2727
}

tests/baselines/reference/classdecl.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -235,14 +235,14 @@ declare class a {
235235
}
236236
declare class b extends a {
237237
}
238-
declare module m1 {
238+
declare namespace m1 {
239239
class b {
240240
}
241241
interface ib {
242242
}
243243
}
244-
declare module m2 {
245-
module m3 {
244+
declare namespace m2 {
245+
namespace m3 {
246246
class c extends b {
247247
}
248248
class ib2 implements m1.ib {

tests/baselines/reference/commentsDottedModuleName.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ define(["require", "exports"], function (require, exports) {
3232

3333
//// [commentsDottedModuleName.d.ts]
3434
/** this is multi declare module*/
35-
export declare module outerModule.InnerModule {
35+
export declare namespace outerModule.InnerModule {
3636
class b {
3737
}
3838
}

tests/baselines/reference/commentsExternalModules.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,11 @@ define(["require", "exports", "commentsExternalModules_0"], function (require, e
139139

140140
//// [commentsExternalModules_0.d.ts]
141141
/** Module comment*/
142-
export declare module m1 {
142+
export declare namespace m1 {
143143
/** b's comment*/
144144
var b: number;
145145
/** m2 comments*/
146-
module m2 {
146+
namespace m2 {
147147
/** class comment;*/
148148
class c {
149149
}
@@ -154,12 +154,12 @@ export declare module m1 {
154154
function fooExport(): number;
155155
}
156156
/** Module comment */
157-
export declare module m4 {
157+
export declare namespace m4 {
158158
/** b's comment */
159159
var b: number;
160160
/** m2 comments
161161
*/
162-
module m2 {
162+
namespace m2 {
163163
/** class comment; */
164164
class c {
165165
}

tests/baselines/reference/commentsExternalModules2.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,11 @@ define(["require", "exports", "commentsExternalModules2_0"], function (require,
140140

141141
//// [commentsExternalModules2_0.d.ts]
142142
/** Module comment*/
143-
export declare module m1 {
143+
export declare namespace m1 {
144144
/** b's comment*/
145145
var b: number;
146146
/** m2 comments*/
147-
module m2 {
147+
namespace m2 {
148148
/** class comment;*/
149149
class c {
150150
}
@@ -155,12 +155,12 @@ export declare module m1 {
155155
function fooExport(): number;
156156
}
157157
/** Module comment */
158-
export declare module m4 {
158+
export declare namespace m4 {
159159
/** b's comment */
160160
var b: number;
161161
/** m2 comments
162162
*/
163-
module m2 {
163+
namespace m2 {
164164
/** class comment; */
165165
class c {
166166
}

tests/baselines/reference/commentsExternalModules3.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,11 @@ exports.newVar2 = new extMod.m4.m2.c();
138138

139139
//// [commentsExternalModules2_0.d.ts]
140140
/** Module comment*/
141-
export declare module m1 {
141+
export declare namespace m1 {
142142
/** b's comment*/
143143
var b: number;
144144
/** m2 comments*/
145-
module m2 {
145+
namespace m2 {
146146
/** class comment;*/
147147
class c {
148148
}
@@ -153,12 +153,12 @@ export declare module m1 {
153153
function fooExport(): number;
154154
}
155155
/** Module comment */
156-
export declare module m4 {
156+
export declare namespace m4 {
157157
/** b's comment */
158158
var b: number;
159159
/** m2 comments
160160
*/
161-
module m2 {
161+
namespace m2 {
162162
/** class comment; */
163163
class c {
164164
}

tests/baselines/reference/commentsFormatting.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ this is 4 spaces left aligned but above line is empty
190190

191191

192192
//// [commentsFormatting.d.ts]
193-
declare module m {
193+
declare namespace m {
194194
/** this is first line - aligned to class declaration
195195
* this is 4 spaces left aligned
196196
* this is 3 spaces left aligned

tests/baselines/reference/commentsModules.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -272,11 +272,11 @@ new m7.m8.m9.c();
272272

273273
//// [commentsModules.d.ts]
274274
/** Module comment*/
275-
declare module m1 {
275+
declare namespace m1 {
276276
/** b's comment*/
277277
var b: number;
278278
/** m2 comments*/
279-
module m2 {
279+
namespace m2 {
280280
/** class comment;*/
281281
class c {
282282
}
@@ -293,44 +293,44 @@ declare module m1 {
293293
}
294294
declare var myvar: m1.m2.c;
295295
/** module comment of m2.m3*/
296-
declare module m2.m3 {
296+
declare namespace m2.m3 {
297297
/** Exported class comment*/
298298
class c {
299299
}
300300
}
301301
/** module comment of m3.m4.m5*/
302-
declare module m3.m4.m5 {
302+
declare namespace m3.m4.m5 {
303303
/** Exported class comment*/
304304
class c {
305305
}
306306
}
307307
/** module comment of m4.m5.m6*/
308-
declare module m4.m5.m6 {
309-
module m7 {
308+
declare namespace m4.m5.m6 {
309+
namespace m7 {
310310
/** Exported class comment*/
311311
class c {
312312
}
313313
}
314314
}
315315
/** module comment of m5.m6.m7*/
316-
declare module m5.m6.m7 {
316+
declare namespace m5.m6.m7 {
317317
/** module m8 comment*/
318-
module m8 {
318+
namespace m8 {
319319
/** Exported class comment*/
320320
class c {
321321
}
322322
}
323323
}
324-
declare module m6.m7 {
325-
module m8 {
324+
declare namespace m6.m7 {
325+
namespace m8 {
326326
/** Exported class comment*/
327327
class c {
328328
}
329329
}
330330
}
331-
declare module m7.m8 {
331+
declare namespace m7.m8 {
332332
/** module m9 comment*/
333-
module m9 {
333+
namespace m9 {
334334
/** Exported class comment*/
335335
class c {
336336
}

tests/baselines/reference/commentsMultiModuleMultiFile.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,12 @@ define(["require", "exports"], function (require, exports) {
100100

101101
//// [commentsMultiModuleMultiFile_0.d.ts]
102102
/** this is multi declare module*/
103-
export declare module multiM {
103+
export declare namespace multiM {
104104
class b {
105105
}
106106
}
107107
/** thi is multi module 2*/
108-
export declare module multiM {
108+
export declare namespace multiM {
109109
/** class c comment*/
110110
class c {
111111
}
@@ -114,7 +114,7 @@ export declare module multiM {
114114
}
115115
//// [commentsMultiModuleMultiFile_1.d.ts]
116116
/** this is multi module 3 comment*/
117-
export declare module multiM {
117+
export declare namespace multiM {
118118
/** class d comment*/
119119
class d {
120120
}

tests/baselines/reference/commentsMultiModuleSingleFile.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@ new multiM.c();
6767

6868
//// [commentsMultiModuleSingleFile.d.ts]
6969
/** this is multi declare module*/
70-
declare module multiM {
70+
declare namespace multiM {
7171
/** class b*/
7272
class b {
7373
}
7474
class d {
7575
}
7676
}
77-
declare module multiM {
77+
declare namespace multiM {
7878
/** class c comment*/
7979
class c {
8080
}

tests/baselines/reference/commentsdoNotEmitComments.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,12 @@ interface i1 {
160160
prop: string;
161161
}
162162
declare var i1_i: i1;
163-
declare module m1 {
163+
declare namespace m1 {
164164
class b {
165165
x: number;
166166
constructor(x: number);
167167
}
168-
module m2 {
168+
namespace m2 {
169169
}
170170
}
171171
declare var x: any;

tests/baselines/reference/commentsemitComments.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -188,13 +188,13 @@ interface i1 {
188188
/**interface instance comments*/
189189
declare var i1_i: i1;
190190
/** this is module comment*/
191-
declare module m1 {
191+
declare namespace m1 {
192192
/** class b */
193193
class b {
194194
x: number;
195195
constructor(x: number);
196196
}
197-
module m2 {
197+
namespace m2 {
198198
}
199199
}
200200
declare var x: any;

tests/baselines/reference/constDeclarations2.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var M;
2020

2121

2222
//// [constDeclarations2.d.ts]
23-
declare module M {
23+
declare namespace M {
2424
const c1 = false;
2525
const c2: number;
2626
const c3 = 0, c4: string, c5: any;

tests/baselines/reference/declFileAliasUseBeforeDeclaration2.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ declare module "test" {
1616

1717
//// [declFileAliasUseBeforeDeclaration2.d.ts]
1818
declare module "test" {
19-
module A {
19+
namespace A {
2020
class C {
2121
}
2222
}

0 commit comments

Comments
 (0)