diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index d94e86969c21a..6f31f00daa2f6 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -331,6 +331,7 @@ import { getNamespaceDeclarationNode, getNewTargetContainer, getNonAugmentationDeclaration, + getNonModifierTokenPosOfNode, getNormalizedAbsolutePath, getObjectFlags, getOriginalNode, @@ -46056,6 +46057,15 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { if (isIdentifier(node.name)) { checkCollisionsForDeclarationName(node, node.name); + if (!(node.flags & (NodeFlags.Namespace | NodeFlags.GlobalAugmentation))) { + error(node.name, Diagnostics.A_namespace_declaration_should_not_be_declared_using_the_module_keyword_Please_use_the_namespace_keyword_instead); + // 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); diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 0173f67bbb910..b5e94893a6727 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -1645,6 +1645,10 @@ "category": "Error", "code": 1498 }, + "A namespace declaration should not be declared using the module keyword. Please use the namespace keyword instead.": { + "category": "Error", + "code": 1499 + }, "The types of '{0}' are incompatible between these types.": { "category": "Error", diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index ddcb5c258dd37..6883f612e0259 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -1184,6 +1184,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); diff --git a/tests/cases/fourslash/moduleDeclarationDeprecated_suggestion1.ts b/tests/cases/fourslash/moduleDeclarationDeprecated_suggestion1.ts new file mode 100644 index 0000000000000..01b6c18c0af83 --- /dev/null +++ b/tests/cases/fourslash/moduleDeclarationDeprecated_suggestion1.ts @@ -0,0 +1,41 @@ +/// +// @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": 1499, + "message": "A namespace declaration should not be declared using the module keyword. Please use the namespace keyword instead.", + "reportsDeprecated": true, + "range": ranges[0] + }, + { + "code": 1499, + "message": "A namespace declaration should not be declared using the module keyword. Please use the namespace keyword instead.", + "reportsDeprecated": true, + "range": ranges[1] + }, + { + "code": 1499, + "message": "A namespace declaration should not be declared using the module keyword. Please use the namespace keyword instead.", + "reportsDeprecated": true, + "range": ranges[2] + }, + { + "code": 1499, + "message": "A namespace declaration should not be declared using the module keyword. Please use the namespace keyword instead.", + "reportsDeprecated": true, + "range": ranges[3] + }, +])