Skip to content
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

Deprecate module keyword for namespace declarations #58007

Merged
merged 8 commits into from
Jul 24, 2024
9 changes: 9 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ import {
getNamespaceDeclarationNode,
getNewTargetContainer,
getNonAugmentationDeclaration,
getNonModifierTokenPosOfNode,
getNormalizedAbsolutePath,
getObjectFlags,
getOriginalNode,
Expand Down Expand Up @@ -46365,6 +46366,14 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {

if (isIdentifier(node.name)) {
checkCollisionsForDeclarationName(node, node.name);
if (!(node.flags & (NodeFlags.Namespace | NodeFlags.GlobalAugmentation))) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will only report errors in editor and there is no way to report these errors on command line. I think we need to handle "deprecation" version here instead to issue error

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The intent is to not report the in tsc yet, per #57913

const sourceFile = getSourceFileOfNode(node);
const pos = getNonModifierTokenPosOfNode(node);
const span = getSpanOfTokenAtPosition(sourceFile, pos);
suggestionDiagnostics.add(
createFileDiagnostic(sourceFile, span.start, span.length, Diagnostics.A_namespace_declaration_should_not_be_declared_using_the_module_keyword_Please_use_the_namespace_keyword_instead),
);
}
}

checkExportsOnMergedDeclarations(node);
Expand Down
5 changes: 5 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1797,6 +1797,11 @@
"category": "Error",
"code": 1535
},
"A namespace declaration should not be declared using the module keyword. Please use the namespace keyword instead.": {
"category": "Suggestion",
"code": 1536,
"reportsDeprecated": true
},

"The types of '{0}' are incompatible between these types.": {
"category": "Error",
Expand Down
10 changes: 10 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1207,6 +1207,16 @@ export function getNonDecoratorTokenPosOfNode(node: Node, sourceFile?: SourceFil
return skipTrivia((sourceFile || getSourceFileOfNode(node)).text, lastDecorator.end);
}

/** @internal */
export function getNonModifierTokenPosOfNode(node: Node, sourceFile?: SourceFileLike): number {
const lastModifier = !nodeIsMissing(node) && canHaveModifiers(node) && node.modifiers ? last(node.modifiers) : undefined;
if (!lastModifier) {
return getTokenPosOfNode(node, sourceFile);
}

return skipTrivia((sourceFile || getSourceFileOfNode(node)).text, lastModifier.end);
}

/** @internal */
export function getSourceTextOfNodeFromSourceFile(sourceFile: SourceFile, node: Node, includeTrivia = false): string {
return getTextOfNodeFromSourceText(sourceFile.text, node, includeTrivia);
Expand Down
41 changes: 41 additions & 0 deletions tests/cases/fourslash/moduleDeclarationDeprecated_suggestion1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
///<reference path="fourslash.ts" />
// @Filename: a.ts
////[|module|] mod1 { export let x: number }
////declare [|module|] mod2 { export let x: number }
////export [|module|] mod3 { export let x: number }
////export declare [|module|] mod4 { export let x: number }
////namespace mod5 { export let x: number }
////declare namespace mod6 { export let x: number }
////declare global {}
////mod1.x = 1;
////mod2.x = 1;
////mod5.x = 1;
////mod6.x = 1;

const ranges = test.ranges();
verify.getSuggestionDiagnostics([
{
"code": 1536,
"message": "A namespace declaration should not be declared using the module keyword. Please use the namespace keyword instead.",
"reportsDeprecated": true,
"range": ranges[0]
},
{
"code": 1536,
"message": "A namespace declaration should not be declared using the module keyword. Please use the namespace keyword instead.",
"reportsDeprecated": true,
"range": ranges[1]
},
{
"code": 1536,
"message": "A namespace declaration should not be declared using the module keyword. Please use the namespace keyword instead.",
"reportsDeprecated": true,
"range": ranges[2]
},
{
"code": 1536,
"message": "A namespace declaration should not be declared using the module keyword. Please use the namespace keyword instead.",
"reportsDeprecated": true,
"range": ranges[3]
},
])