From 96ab6a9c5613ecd179300aebbec35f1513d7b89f Mon Sep 17 00:00:00 2001 From: Zzzen Date: Sun, 31 Mar 2024 02:02:58 +0800 Subject: [PATCH 1/4] Deprecate module keyword for namespace declarations --- src/compiler/checker.ts | 9 ++++ src/compiler/diagnosticMessages.json | 5 +++ src/compiler/utilities.ts | 10 +++++ ...moduleDeclarationDeprecated_suggestion1.ts | 41 +++++++++++++++++++ 4 files changed, 65 insertions(+) create mode 100644 tests/cases/fourslash/moduleDeclarationDeprecated_suggestion1.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index b23462ed4f3b9..783a011244f59 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -330,6 +330,7 @@ import { getNamespaceDeclarationNode, getNewTargetContainer, getNonAugmentationDeclaration, + getNonModifierTokenPosOfNode, getNormalizedAbsolutePath, getObjectFlags, getOriginalNode, @@ -46011,6 +46012,14 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { if (isIdentifier(node.name)) { checkCollisionsForDeclarationName(node, node.name); + if (!(node.flags & (NodeFlags.Namespace | NodeFlags.GlobalAugmentation))) { + 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 dfbb41b3d409d..30b1351701792 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -1645,6 +1645,11 @@ "category": "Error", "code": 1498 }, + "A namespace declaration should not be declared using the module keyword. Please use the namespace keyword instead.": { + "category": "Suggestion", + "code": 1499, + "reportsDeprecated": true + }, "The types of '{0}' are incompatible between these types.": { "category": "Error", diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 184f68b0c6221..20fbd0fb1180e 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -1181,6 +1181,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] + }, +]) From 8c334e52c9eb7cff6a618745096b3e3db7467dbb Mon Sep 17 00:00:00 2001 From: Zzzen Date: Fri, 31 May 2024 22:30:53 +0800 Subject: [PATCH 2/4] add test --- .../fourslash/moduleDeclarationDeprecated_suggestion2.ts | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 tests/cases/fourslash/moduleDeclarationDeprecated_suggestion2.ts diff --git a/tests/cases/fourslash/moduleDeclarationDeprecated_suggestion2.ts b/tests/cases/fourslash/moduleDeclarationDeprecated_suggestion2.ts new file mode 100644 index 0000000000000..f592302417d0b --- /dev/null +++ b/tests/cases/fourslash/moduleDeclarationDeprecated_suggestion2.ts @@ -0,0 +1,6 @@ +/// +// @Filename: a.ts +////declare module + +const ranges = test.ranges(); +verify.getSuggestionDiagnostics([]) From 921bfdd48453d2bad57ea4d84e685eca0d5b37bb Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Tue, 23 Jul 2024 10:59:57 -0700 Subject: [PATCH 3/4] Surround namespace/module with single quotes, flatten tests, add test for ambient module declaration. --- src/compiler/diagnosticMessages.json | 2 +- ...moduleDeclarationDeprecated_suggestion1.ts | 43 +++++++------------ 2 files changed, 17 insertions(+), 28 deletions(-) diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 76ae58ceef9c0..b0a05caf0eb13 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -1805,7 +1805,7 @@ "category": "Error", "code": 1537 }, - "A namespace declaration should not be declared using the module keyword. Please use the namespace keyword instead.": { + "A 'namespace' declaration should not be declared using the 'module' keyword. Please use the 'namespace' keyword instead.": { "category": "Suggestion", "code": 1538, "reportsDeprecated": true diff --git a/tests/cases/fourslash/moduleDeclarationDeprecated_suggestion1.ts b/tests/cases/fourslash/moduleDeclarationDeprecated_suggestion1.ts index d5ef9802a1259..a574b44c534d7 100644 --- a/tests/cases/fourslash/moduleDeclarationDeprecated_suggestion1.ts +++ b/tests/cases/fourslash/moduleDeclarationDeprecated_suggestion1.ts @@ -6,36 +6,25 @@ ////export declare [|module|] mod4 { export let x: number } ////namespace mod5 { export let x: number } ////declare namespace mod6 { export let x: number } +////declare module "module-augmentation" {} ////declare global {} ////mod1.x = 1; ////mod2.x = 1; ////mod5.x = 1; ////mod6.x = 1; -const ranges = test.ranges(); -verify.getSuggestionDiagnostics([ - { - "code": 1538, - "message": "A namespace declaration should not be declared using the module keyword. Please use the namespace keyword instead.", - "reportsDeprecated": true, - "range": ranges[0] - }, - { - "code": 1538, - "message": "A namespace declaration should not be declared using the module keyword. Please use the namespace keyword instead.", - "reportsDeprecated": true, - "range": ranges[1] - }, - { - "code": 1538, - "message": "A namespace declaration should not be declared using the module keyword. Please use the namespace keyword instead.", - "reportsDeprecated": true, - "range": ranges[2] - }, - { - "code": 1538, - "message": "A namespace declaration should not be declared using the module keyword. Please use the namespace keyword instead.", - "reportsDeprecated": true, - "range": ranges[3] - }, -]) +// @Filename: b.ts +////module "global-ambient-module" {} + +goTo.file("a.ts") +const diagnostics = test.ranges().map(range => ({ + code: 1538, + message: "A 'namespace' declaration should not be declared using the 'module' keyword. Please use the 'namespace' keyword instead.", + reportsDeprecated: true, + range, +})); +verify.getSuggestionDiagnostics(diagnostics) + +goTo.file("b.ts") +verify.getSuggestionDiagnostics([]) + From 89d66b2e846be41a8528f91f3791cbf1f2753fe2 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Tue, 23 Jul 2024 11:32:37 -0700 Subject: [PATCH 4/4] Switch code to 1540 for conflict. --- src/compiler/diagnosticMessages.json | 2 +- .../cases/fourslash/moduleDeclarationDeprecated_suggestion1.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index b0a05caf0eb13..45ba0f4273c87 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -1807,7 +1807,7 @@ }, "A 'namespace' declaration should not be declared using the 'module' keyword. Please use the 'namespace' keyword instead.": { "category": "Suggestion", - "code": 1538, + "code": 1540, "reportsDeprecated": true }, diff --git a/tests/cases/fourslash/moduleDeclarationDeprecated_suggestion1.ts b/tests/cases/fourslash/moduleDeclarationDeprecated_suggestion1.ts index a574b44c534d7..5554300a5a0b9 100644 --- a/tests/cases/fourslash/moduleDeclarationDeprecated_suggestion1.ts +++ b/tests/cases/fourslash/moduleDeclarationDeprecated_suggestion1.ts @@ -18,7 +18,7 @@ goTo.file("a.ts") const diagnostics = test.ranges().map(range => ({ - code: 1538, + code: 1540, message: "A 'namespace' declaration should not be declared using the 'module' keyword. Please use the 'namespace' keyword instead.", reportsDeprecated: true, range, pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy