Skip to content

Commit ab7b624

Browse files
Deprecate module keyword for namespace declarations (#58007)
Co-authored-by: Daniel Rosenwasser <[email protected]>
1 parent 5bd4e00 commit ab7b624

File tree

5 files changed

+60
-0
lines changed

5 files changed

+60
-0
lines changed

src/compiler/checker.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ import {
341341
getNamespaceDeclarationNode,
342342
getNewTargetContainer,
343343
getNonAugmentationDeclaration,
344+
getNonModifierTokenPosOfNode,
344345
getNormalizedAbsolutePath,
345346
getObjectFlags,
346347
getOriginalNode,
@@ -46886,6 +46887,14 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4688646887

4688746888
if (isIdentifier(node.name)) {
4688846889
checkCollisionsForDeclarationName(node, node.name);
46890+
if (!(node.flags & (NodeFlags.Namespace | NodeFlags.GlobalAugmentation))) {
46891+
const sourceFile = getSourceFileOfNode(node);
46892+
const pos = getNonModifierTokenPosOfNode(node);
46893+
const span = getSpanOfTokenAtPosition(sourceFile, pos);
46894+
suggestionDiagnostics.add(
46895+
createFileDiagnostic(sourceFile, span.start, span.length, Diagnostics.A_namespace_declaration_should_not_be_declared_using_the_module_keyword_Please_use_the_namespace_keyword_instead),
46896+
);
46897+
}
4688946898
}
4689046899

4689146900
checkExportsOnMergedDeclarations(node);

src/compiler/diagnosticMessages.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1813,6 +1813,11 @@
18131813
"category": "Error",
18141814
"code": 1539
18151815
},
1816+
"A 'namespace' declaration should not be declared using the 'module' keyword. Please use the 'namespace' keyword instead.": {
1817+
"category": "Suggestion",
1818+
"code": 1540,
1819+
"reportsDeprecated": true
1820+
},
18161821

18171822
"The types of '{0}' are incompatible between these types.": {
18181823
"category": "Error",

src/compiler/utilities.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,6 +1259,16 @@ export function getNonDecoratorTokenPosOfNode(node: Node, sourceFile?: SourceFil
12591259
return skipTrivia((sourceFile || getSourceFileOfNode(node)).text, lastDecorator.end);
12601260
}
12611261

1262+
/** @internal */
1263+
export function getNonModifierTokenPosOfNode(node: Node, sourceFile?: SourceFileLike): number {
1264+
const lastModifier = !nodeIsMissing(node) && canHaveModifiers(node) && node.modifiers ? last(node.modifiers) : undefined;
1265+
if (!lastModifier) {
1266+
return getTokenPosOfNode(node, sourceFile);
1267+
}
1268+
1269+
return skipTrivia((sourceFile || getSourceFileOfNode(node)).text, lastModifier.end);
1270+
}
1271+
12621272
/** @internal */
12631273
export function getSourceTextOfNodeFromSourceFile(sourceFile: SourceFile, node: Node, includeTrivia = false): string {
12641274
return getTextOfNodeFromSourceText(sourceFile.text, node, includeTrivia);
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
///<reference path="fourslash.ts" />
2+
// @Filename: a.ts
3+
////[|module|] mod1 { export let x: number }
4+
////declare [|module|] mod2 { export let x: number }
5+
////export [|module|] mod3 { export let x: number }
6+
////export declare [|module|] mod4 { export let x: number }
7+
////namespace mod5 { export let x: number }
8+
////declare namespace mod6 { export let x: number }
9+
////declare module "module-augmentation" {}
10+
////declare global {}
11+
////mod1.x = 1;
12+
////mod2.x = 1;
13+
////mod5.x = 1;
14+
////mod6.x = 1;
15+
16+
// @Filename: b.ts
17+
////module "global-ambient-module" {}
18+
19+
goTo.file("a.ts")
20+
const diagnostics = test.ranges().map(range => ({
21+
code: 1540,
22+
message: "A 'namespace' declaration should not be declared using the 'module' keyword. Please use the 'namespace' keyword instead.",
23+
reportsDeprecated: true,
24+
range,
25+
}));
26+
verify.getSuggestionDiagnostics(diagnostics)
27+
28+
goTo.file("b.ts")
29+
verify.getSuggestionDiagnostics([])
30+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
///<reference path="fourslash.ts" />
2+
// @Filename: a.ts
3+
////declare module
4+
5+
const ranges = test.ranges();
6+
verify.getSuggestionDiagnostics([])

0 commit comments

Comments
 (0)