From 8a1e2c18fa04a7b344f1a331e651bd482d4923ec Mon Sep 17 00:00:00 2001 From: Igor Rossinski Date: Tue, 26 Dec 2023 02:11:46 +0300 Subject: [PATCH] [ArkTS Linter] fix err#14863 Number literals as interface property signatures names Signed-off-by: Igor Rossinski --- linter-4.2/src/TypeScriptLinter.ts | 16 +++++++--------- .../test/literals_as_prop_names.ts.strict.json | 7 +++++++ linter-4.2/test/types.ts.strict.json | 7 +++++++ linter-4.2/test_rules/rule1.ts | 5 +++++ linter-4.2/test_rules/rule1.ts.autofix.json | 8 ++++++++ linter-4.2/test_rules/rule1.ts.strict.json | 7 +++++++ linter/lib/TypeScriptLinter.ts | 10 ++++------ .../test/literals_as_prop_names.ts.strict.json | 7 +++++++ linter/test/types.ts.strict.json | 7 +++++++ linter/test_rules/rule1.ts | 4 ++++ linter/test_rules/rule1.ts.autofix.json | 8 ++++++++ linter/test_rules/rule1.ts.strict.json | 7 +++++++ 12 files changed, 78 insertions(+), 15 deletions(-) diff --git a/linter-4.2/src/TypeScriptLinter.ts b/linter-4.2/src/TypeScriptLinter.ts index f92cb256..a9954c67 100644 --- a/linter-4.2/src/TypeScriptLinter.ts +++ b/linter-4.2/src/TypeScriptLinter.ts @@ -757,12 +757,10 @@ export class TypeScriptLinter { } private handlePropertySignature(node: ts.PropertySignature): void { - - /** - * Handler in case any further restrictions are put on properties of interfaces - */ - void this; - void node; + const propName = node.name; + if (!!propName && ts.isNumericLiteral(propName)) { + this.incrementCounters(node, FaultID.LiteralAsPropertyName, ); + } } private filterOutDecoratorsDiagnostics( @@ -2062,7 +2060,7 @@ export class TypeScriptLinter { } private handleGetAccessor(node: ts.Node) { - + /** * Reserved if needed */ @@ -2071,7 +2069,7 @@ export class TypeScriptLinter { } private handleSetAccessor(node: ts.Node) { - + /** * Reserved if needed */ @@ -2106,7 +2104,7 @@ export class TypeScriptLinter { // option is enabled, compiler attempts to infer type from variable references: // see https://github.com/microsoft/TypeScript/pull/11263. // In this case, we still want to report the error, since ArkTS doesn't allow - // to omit both type annotation and initializer. + // to omit both type annotation and initializer. if (((ts.isVariableDeclaration(decl) && ts.isVariableStatement(decl.parent.parent)) || ts.isPropertyDeclaration(decl)) && !decl.initializer) { this.incrementCounters(decl, FaultID.AnyType); diff --git a/linter-4.2/test/literals_as_prop_names.ts.strict.json b/linter-4.2/test/literals_as_prop_names.ts.strict.json index 40de3a10..96feeb09 100644 --- a/linter-4.2/test/literals_as_prop_names.ts.strict.json +++ b/linter-4.2/test/literals_as_prop_names.ts.strict.json @@ -84,6 +84,13 @@ "suggest": "", "rule": "Object literal must correspond to some explicitly declared class or interface (arkts-no-untyped-obj-literals)" }, + { + "line": 67, + "column": 3, + "problem": "LiteralAsPropertyName", + "suggest": "", + "rule": "Objects with property names that are not identifiers are not supported (arkts-identifiers-as-prop-names)" + }, { "line": 34, "column": 11, diff --git a/linter-4.2/test/types.ts.strict.json b/linter-4.2/test/types.ts.strict.json index 31431c27..0be6ecd0 100644 --- a/linter-4.2/test/types.ts.strict.json +++ b/linter-4.2/test/types.ts.strict.json @@ -98,6 +98,13 @@ "suggest": "", "rule": "Object literals cannot be used as type declarations (arkts-no-obj-literals-as-types)" }, + { + "line": 56, + "column": 3, + "problem": "LiteralAsPropertyName", + "suggest": "", + "rule": "Objects with property names that are not identifiers are not supported (arkts-identifiers-as-prop-names)" + }, { "line": 57, "column": 3, diff --git a/linter-4.2/test_rules/rule1.ts b/linter-4.2/test_rules/rule1.ts index 0a37d2e6..fea7f2cb 100644 --- a/linter-4.2/test_rules/rule1.ts +++ b/linter-4.2/test_rules/rule1.ts @@ -39,3 +39,8 @@ const a2: A2 = { S1["s1"]; S2["s2"]; + + +interface I { + 2: number; +} \ No newline at end of file diff --git a/linter-4.2/test_rules/rule1.ts.autofix.json b/linter-4.2/test_rules/rule1.ts.autofix.json index d6f3182b..170b37dc 100644 --- a/linter-4.2/test_rules/rule1.ts.autofix.json +++ b/linter-4.2/test_rules/rule1.ts.autofix.json @@ -85,6 +85,14 @@ "autofixable": false, "suggest": "", "rule": "Objects with property names that are not identifiers are not supported (arkts-identifiers-as-prop-names)" + }, + { + "line": 45, + "column": 3, + "problem": "LiteralAsPropertyName", + "autofixable": false, + "suggest": "", + "rule": "Objects with property names that are not identifiers are not supported (arkts-identifiers-as-prop-names)" } ] } \ No newline at end of file diff --git a/linter-4.2/test_rules/rule1.ts.strict.json b/linter-4.2/test_rules/rule1.ts.strict.json index 2f185ba3..1fd9a452 100644 --- a/linter-4.2/test_rules/rule1.ts.strict.json +++ b/linter-4.2/test_rules/rule1.ts.strict.json @@ -62,6 +62,13 @@ "problem": "ComputedPropertyName", "suggest": "", "rule": "Objects with property names that are not identifiers are not supported (arkts-identifiers-as-prop-names)" + }, + { + "line": 45, + "column": 3, + "problem": "LiteralAsPropertyName", + "suggest": "", + "rule": "Objects with property names that are not identifiers are not supported (arkts-identifiers-as-prop-names)" } ] } \ No newline at end of file diff --git a/linter/lib/TypeScriptLinter.ts b/linter/lib/TypeScriptLinter.ts index 6b889e48..ee3722f5 100644 --- a/linter/lib/TypeScriptLinter.ts +++ b/linter/lib/TypeScriptLinter.ts @@ -754,12 +754,10 @@ export class TypeScriptLinter { } private handlePropertySignature(node: ts.PropertySignature): void { - - /** - * Handler in case any further restrictions are put on properties of interfaces - */ - void this; - void node; + const propName = node.name; + if (!!propName && ts.isNumericLiteral(propName)) { + this.incrementCounters(node, FaultID.LiteralAsPropertyName, ); + } } private filterOutDecoratorsDiagnostics( diff --git a/linter/test/literals_as_prop_names.ts.strict.json b/linter/test/literals_as_prop_names.ts.strict.json index 40de3a10..96feeb09 100644 --- a/linter/test/literals_as_prop_names.ts.strict.json +++ b/linter/test/literals_as_prop_names.ts.strict.json @@ -84,6 +84,13 @@ "suggest": "", "rule": "Object literal must correspond to some explicitly declared class or interface (arkts-no-untyped-obj-literals)" }, + { + "line": 67, + "column": 3, + "problem": "LiteralAsPropertyName", + "suggest": "", + "rule": "Objects with property names that are not identifiers are not supported (arkts-identifiers-as-prop-names)" + }, { "line": 34, "column": 11, diff --git a/linter/test/types.ts.strict.json b/linter/test/types.ts.strict.json index 31431c27..0be6ecd0 100644 --- a/linter/test/types.ts.strict.json +++ b/linter/test/types.ts.strict.json @@ -98,6 +98,13 @@ "suggest": "", "rule": "Object literals cannot be used as type declarations (arkts-no-obj-literals-as-types)" }, + { + "line": 56, + "column": 3, + "problem": "LiteralAsPropertyName", + "suggest": "", + "rule": "Objects with property names that are not identifiers are not supported (arkts-identifiers-as-prop-names)" + }, { "line": 57, "column": 3, diff --git a/linter/test_rules/rule1.ts b/linter/test_rules/rule1.ts index 0a37d2e6..3e6e04ac 100644 --- a/linter/test_rules/rule1.ts +++ b/linter/test_rules/rule1.ts @@ -39,3 +39,7 @@ const a2: A2 = { S1["s1"]; S2["s2"]; + +interface I { + 2: number; +} \ No newline at end of file diff --git a/linter/test_rules/rule1.ts.autofix.json b/linter/test_rules/rule1.ts.autofix.json index d6f3182b..ab1eb2fb 100644 --- a/linter/test_rules/rule1.ts.autofix.json +++ b/linter/test_rules/rule1.ts.autofix.json @@ -85,6 +85,14 @@ "autofixable": false, "suggest": "", "rule": "Objects with property names that are not identifiers are not supported (arkts-identifiers-as-prop-names)" + }, + { + "line": 44, + "column": 3, + "problem": "LiteralAsPropertyName", + "autofixable": false, + "suggest": "", + "rule": "Objects with property names that are not identifiers are not supported (arkts-identifiers-as-prop-names)" } ] } \ No newline at end of file diff --git a/linter/test_rules/rule1.ts.strict.json b/linter/test_rules/rule1.ts.strict.json index 2f185ba3..272db883 100644 --- a/linter/test_rules/rule1.ts.strict.json +++ b/linter/test_rules/rule1.ts.strict.json @@ -62,6 +62,13 @@ "problem": "ComputedPropertyName", "suggest": "", "rule": "Objects with property names that are not identifiers are not supported (arkts-identifiers-as-prop-names)" + }, + { + "line": 44, + "column": 3, + "problem": "LiteralAsPropertyName", + "suggest": "", + "rule": "Objects with property names that are not identifiers are not supported (arkts-identifiers-as-prop-names)" } ] } \ No newline at end of file -- Gitee