From 5703c8b37896987772c9d2e7ec63b1183e803bdb Mon Sep 17 00:00:00 2001 From: Konstantin Baladurin Date: Thu, 24 Aug 2023 00:41:10 +0300 Subject: [PATCH] Linter: fix arkts-no-ambient-decls diagnostic Signed-off-by: Konstantin Baladurin --- linter-4.2/src/TypeScriptLinter.ts | 6 ++- linter-4.2/test/ambient_module.ts | 8 +++ linter-4.2/test/ambient_module.ts.relax.json | 53 ++++++++++++++++--- linter-4.2/test/ambient_module.ts.strict.json | 53 ++++++++++++++++--- linter/src/TypeScriptLinter.ts | 8 ++- linter/test/ambient_module.ts | 8 +++ linter/test/ambient_module.ts.relax.json | 53 ++++++++++++++++--- linter/test/ambient_module.ts.strict.json | 53 ++++++++++++++++--- 8 files changed, 208 insertions(+), 34 deletions(-) diff --git a/linter-4.2/src/TypeScriptLinter.ts b/linter-4.2/src/TypeScriptLinter.ts index dafd72b8f..a2247c08a 100644 --- a/linter-4.2/src/TypeScriptLinter.ts +++ b/linter-4.2/src/TypeScriptLinter.ts @@ -1064,6 +1064,7 @@ export class TypeScriptLinter { private handleModuleDeclaration(node: ts.Node) { let tsModuleDecl = node as ts.ModuleDeclaration; + this.countDeclarationsWithDuplicateName( this.tsTypeChecker.getSymbolAtLocation(tsModuleDecl.name), tsModuleDecl @@ -1092,7 +1093,10 @@ export class TypeScriptLinter { } } } - } else if (this.tsUtils.hasModifier(tsModifiers, ts.SyntaxKind.DeclareKeyword)) { + } + + if (!(tsModuleDecl.flags & ts.NodeFlags.Namespace) && + this.tsUtils.hasModifier(tsModifiers, ts.SyntaxKind.DeclareKeyword)) { this.incrementCounters(tsModuleDecl, FaultID.ShorthandAmbientModuleDecl); } diff --git a/linter-4.2/test/ambient_module.ts b/linter-4.2/test/ambient_module.ts index 80f9520da..cd1b67be3 100644 --- a/linter-4.2/test/ambient_module.ts +++ b/linter-4.2/test/ambient_module.ts @@ -28,3 +28,11 @@ declare module 'json!*' { } declare module 'library/*'; + +declare module 'abc' { + export function f(): void; +} + +declare namespace abc2 { + export function f(): void; +} diff --git a/linter-4.2/test/ambient_module.ts.relax.json b/linter-4.2/test/ambient_module.ts.relax.json index 384f229b6..a02ca7fc8 100644 --- a/linter-4.2/test/ambient_module.ts.relax.json +++ b/linter-4.2/test/ambient_module.ts.relax.json @@ -17,42 +17,79 @@ { "line": 16, "column": 1, - "problem": "ShorthandAmbientModuleDecl" + "problem": "ShorthandAmbientModuleDecl", + "suggest": "", + "rule": "Ambient module declaration is not supported (arkts-no-ambient-decls)" }, { "line": 22, "column": 3, - "problem": "NonDeclarationInNamespace" + "problem": "NonDeclarationInNamespace", + "suggest": "", + "rule": "Non-declaration statements in namespaces are not supported (arkts-no-ns-statements)" }, { "line": 20, "column": 1, - "problem": "WildcardsInModuleName" + "problem": "ShorthandAmbientModuleDecl", + "suggest": "", + "rule": "Ambient module declaration is not supported (arkts-no-ambient-decls)" + }, + { + "line": 20, + "column": 1, + "problem": "WildcardsInModuleName", + "suggest": "", + "rule": "Wildcards in module names are not supported (arkts-no-module-wildcards)" }, { "line": 27, "column": 3, - "problem": "NonDeclarationInNamespace" + "problem": "NonDeclarationInNamespace", + "suggest": "", + "rule": "Non-declaration statements in namespaces are not supported (arkts-no-ns-statements)" }, { "line": 25, "column": 1, - "problem": "WildcardsInModuleName" + "problem": "ShorthandAmbientModuleDecl", + "suggest": "", + "rule": "Ambient module declaration is not supported (arkts-no-ambient-decls)" + }, + { + "line": 25, + "column": 1, + "problem": "WildcardsInModuleName", + "suggest": "", + "rule": "Wildcards in module names are not supported (arkts-no-module-wildcards)" }, { "line": 26, "column": 16, - "problem": "AnyType" + "problem": "AnyType", + "suggest": "", + "rule": "Use explicit types instead of \"any\", \"unknown\" (arkts-no-any-unknown)" }, { "line": 30, "column": 1, - "problem": "ShorthandAmbientModuleDecl" + "problem": "ShorthandAmbientModuleDecl", + "suggest": "", + "rule": "Ambient module declaration is not supported (arkts-no-ambient-decls)" }, { "line": 30, "column": 1, - "problem": "WildcardsInModuleName" + "problem": "WildcardsInModuleName", + "suggest": "", + "rule": "Wildcards in module names are not supported (arkts-no-module-wildcards)" + }, + { + "line": 32, + "column": 1, + "problem": "ShorthandAmbientModuleDecl", + "suggest": "", + "rule": "Ambient module declaration is not supported (arkts-no-ambient-decls)" } ] } \ No newline at end of file diff --git a/linter-4.2/test/ambient_module.ts.strict.json b/linter-4.2/test/ambient_module.ts.strict.json index 384f229b6..a02ca7fc8 100644 --- a/linter-4.2/test/ambient_module.ts.strict.json +++ b/linter-4.2/test/ambient_module.ts.strict.json @@ -17,42 +17,79 @@ { "line": 16, "column": 1, - "problem": "ShorthandAmbientModuleDecl" + "problem": "ShorthandAmbientModuleDecl", + "suggest": "", + "rule": "Ambient module declaration is not supported (arkts-no-ambient-decls)" }, { "line": 22, "column": 3, - "problem": "NonDeclarationInNamespace" + "problem": "NonDeclarationInNamespace", + "suggest": "", + "rule": "Non-declaration statements in namespaces are not supported (arkts-no-ns-statements)" }, { "line": 20, "column": 1, - "problem": "WildcardsInModuleName" + "problem": "ShorthandAmbientModuleDecl", + "suggest": "", + "rule": "Ambient module declaration is not supported (arkts-no-ambient-decls)" + }, + { + "line": 20, + "column": 1, + "problem": "WildcardsInModuleName", + "suggest": "", + "rule": "Wildcards in module names are not supported (arkts-no-module-wildcards)" }, { "line": 27, "column": 3, - "problem": "NonDeclarationInNamespace" + "problem": "NonDeclarationInNamespace", + "suggest": "", + "rule": "Non-declaration statements in namespaces are not supported (arkts-no-ns-statements)" }, { "line": 25, "column": 1, - "problem": "WildcardsInModuleName" + "problem": "ShorthandAmbientModuleDecl", + "suggest": "", + "rule": "Ambient module declaration is not supported (arkts-no-ambient-decls)" + }, + { + "line": 25, + "column": 1, + "problem": "WildcardsInModuleName", + "suggest": "", + "rule": "Wildcards in module names are not supported (arkts-no-module-wildcards)" }, { "line": 26, "column": 16, - "problem": "AnyType" + "problem": "AnyType", + "suggest": "", + "rule": "Use explicit types instead of \"any\", \"unknown\" (arkts-no-any-unknown)" }, { "line": 30, "column": 1, - "problem": "ShorthandAmbientModuleDecl" + "problem": "ShorthandAmbientModuleDecl", + "suggest": "", + "rule": "Ambient module declaration is not supported (arkts-no-ambient-decls)" }, { "line": 30, "column": 1, - "problem": "WildcardsInModuleName" + "problem": "WildcardsInModuleName", + "suggest": "", + "rule": "Wildcards in module names are not supported (arkts-no-module-wildcards)" + }, + { + "line": 32, + "column": 1, + "problem": "ShorthandAmbientModuleDecl", + "suggest": "", + "rule": "Ambient module declaration is not supported (arkts-no-ambient-decls)" } ] } \ No newline at end of file diff --git a/linter/src/TypeScriptLinter.ts b/linter/src/TypeScriptLinter.ts index ad41b9016..f5ee6bd7f 100644 --- a/linter/src/TypeScriptLinter.ts +++ b/linter/src/TypeScriptLinter.ts @@ -1033,10 +1033,12 @@ export class TypeScriptLinter { private handleModuleDeclaration(node: ts.Node) { let tsModuleDecl = node as ts.ModuleDeclaration; + this.countDeclarationsWithDuplicateName( this.tsTypeChecker.getSymbolAtLocation(tsModuleDecl.name), tsModuleDecl ); + let tsModuleBody = tsModuleDecl.body; let tsModifiers = ts.getModifiers(tsModuleDecl); if (tsModuleBody) { @@ -1060,9 +1062,13 @@ export class TypeScriptLinter { } } } - } else if (this.tsUtils.hasModifier(tsModifiers, ts.SyntaxKind.DeclareKeyword)) { + } + + if (!(tsModuleDecl.flags & ts.NodeFlags.Namespace) && + this.tsUtils.hasModifier(tsModifiers, ts.SyntaxKind.DeclareKeyword)) { this.incrementCounters(tsModuleDecl, FaultID.ShorthandAmbientModuleDecl); } + if (ts.isStringLiteral(tsModuleDecl.name) && tsModuleDecl.name.text.includes('*')) this.incrementCounters(tsModuleDecl, FaultID.WildcardsInModuleName); } diff --git a/linter/test/ambient_module.ts b/linter/test/ambient_module.ts index 80f9520da..cd1b67be3 100644 --- a/linter/test/ambient_module.ts +++ b/linter/test/ambient_module.ts @@ -28,3 +28,11 @@ declare module 'json!*' { } declare module 'library/*'; + +declare module 'abc' { + export function f(): void; +} + +declare namespace abc2 { + export function f(): void; +} diff --git a/linter/test/ambient_module.ts.relax.json b/linter/test/ambient_module.ts.relax.json index 384f229b6..a02ca7fc8 100755 --- a/linter/test/ambient_module.ts.relax.json +++ b/linter/test/ambient_module.ts.relax.json @@ -17,42 +17,79 @@ { "line": 16, "column": 1, - "problem": "ShorthandAmbientModuleDecl" + "problem": "ShorthandAmbientModuleDecl", + "suggest": "", + "rule": "Ambient module declaration is not supported (arkts-no-ambient-decls)" }, { "line": 22, "column": 3, - "problem": "NonDeclarationInNamespace" + "problem": "NonDeclarationInNamespace", + "suggest": "", + "rule": "Non-declaration statements in namespaces are not supported (arkts-no-ns-statements)" }, { "line": 20, "column": 1, - "problem": "WildcardsInModuleName" + "problem": "ShorthandAmbientModuleDecl", + "suggest": "", + "rule": "Ambient module declaration is not supported (arkts-no-ambient-decls)" + }, + { + "line": 20, + "column": 1, + "problem": "WildcardsInModuleName", + "suggest": "", + "rule": "Wildcards in module names are not supported (arkts-no-module-wildcards)" }, { "line": 27, "column": 3, - "problem": "NonDeclarationInNamespace" + "problem": "NonDeclarationInNamespace", + "suggest": "", + "rule": "Non-declaration statements in namespaces are not supported (arkts-no-ns-statements)" }, { "line": 25, "column": 1, - "problem": "WildcardsInModuleName" + "problem": "ShorthandAmbientModuleDecl", + "suggest": "", + "rule": "Ambient module declaration is not supported (arkts-no-ambient-decls)" + }, + { + "line": 25, + "column": 1, + "problem": "WildcardsInModuleName", + "suggest": "", + "rule": "Wildcards in module names are not supported (arkts-no-module-wildcards)" }, { "line": 26, "column": 16, - "problem": "AnyType" + "problem": "AnyType", + "suggest": "", + "rule": "Use explicit types instead of \"any\", \"unknown\" (arkts-no-any-unknown)" }, { "line": 30, "column": 1, - "problem": "ShorthandAmbientModuleDecl" + "problem": "ShorthandAmbientModuleDecl", + "suggest": "", + "rule": "Ambient module declaration is not supported (arkts-no-ambient-decls)" }, { "line": 30, "column": 1, - "problem": "WildcardsInModuleName" + "problem": "WildcardsInModuleName", + "suggest": "", + "rule": "Wildcards in module names are not supported (arkts-no-module-wildcards)" + }, + { + "line": 32, + "column": 1, + "problem": "ShorthandAmbientModuleDecl", + "suggest": "", + "rule": "Ambient module declaration is not supported (arkts-no-ambient-decls)" } ] } \ No newline at end of file diff --git a/linter/test/ambient_module.ts.strict.json b/linter/test/ambient_module.ts.strict.json index 384f229b6..a02ca7fc8 100755 --- a/linter/test/ambient_module.ts.strict.json +++ b/linter/test/ambient_module.ts.strict.json @@ -17,42 +17,79 @@ { "line": 16, "column": 1, - "problem": "ShorthandAmbientModuleDecl" + "problem": "ShorthandAmbientModuleDecl", + "suggest": "", + "rule": "Ambient module declaration is not supported (arkts-no-ambient-decls)" }, { "line": 22, "column": 3, - "problem": "NonDeclarationInNamespace" + "problem": "NonDeclarationInNamespace", + "suggest": "", + "rule": "Non-declaration statements in namespaces are not supported (arkts-no-ns-statements)" }, { "line": 20, "column": 1, - "problem": "WildcardsInModuleName" + "problem": "ShorthandAmbientModuleDecl", + "suggest": "", + "rule": "Ambient module declaration is not supported (arkts-no-ambient-decls)" + }, + { + "line": 20, + "column": 1, + "problem": "WildcardsInModuleName", + "suggest": "", + "rule": "Wildcards in module names are not supported (arkts-no-module-wildcards)" }, { "line": 27, "column": 3, - "problem": "NonDeclarationInNamespace" + "problem": "NonDeclarationInNamespace", + "suggest": "", + "rule": "Non-declaration statements in namespaces are not supported (arkts-no-ns-statements)" }, { "line": 25, "column": 1, - "problem": "WildcardsInModuleName" + "problem": "ShorthandAmbientModuleDecl", + "suggest": "", + "rule": "Ambient module declaration is not supported (arkts-no-ambient-decls)" + }, + { + "line": 25, + "column": 1, + "problem": "WildcardsInModuleName", + "suggest": "", + "rule": "Wildcards in module names are not supported (arkts-no-module-wildcards)" }, { "line": 26, "column": 16, - "problem": "AnyType" + "problem": "AnyType", + "suggest": "", + "rule": "Use explicit types instead of \"any\", \"unknown\" (arkts-no-any-unknown)" }, { "line": 30, "column": 1, - "problem": "ShorthandAmbientModuleDecl" + "problem": "ShorthandAmbientModuleDecl", + "suggest": "", + "rule": "Ambient module declaration is not supported (arkts-no-ambient-decls)" }, { "line": 30, "column": 1, - "problem": "WildcardsInModuleName" + "problem": "WildcardsInModuleName", + "suggest": "", + "rule": "Wildcards in module names are not supported (arkts-no-module-wildcards)" + }, + { + "line": 32, + "column": 1, + "problem": "ShorthandAmbientModuleDecl", + "suggest": "", + "rule": "Ambient module declaration is not supported (arkts-no-ambient-decls)" } ] } \ No newline at end of file -- Gitee