From 18e9a0056ec3c39238ff5ce3a7cee0bcbb63c36f Mon Sep 17 00:00:00 2001 From: Raif Mirza Erten Date: Fri, 25 Jul 2025 15:27:42 +0300 Subject: [PATCH] arkts-no-template-string-type rule implementation Issue: #ICOX4A Description: arkts-no-template-string-type rule implementation Signed-off-by: Raif Mirza Erten --- ets2panda/linter/rule-config.json | 1 + ets2panda/linter/src/lib/CookBookMsg.ts | 2 +- ets2panda/linter/src/lib/FaultAttrs.ts | 1 + ets2panda/linter/src/lib/FaultDesc.ts | 1 + ets2panda/linter/src/lib/Problems.ts | 1 + ets2panda/linter/src/lib/TypeScriptLinter.ts | 8 +++ .../main/arkts-no-template-string-type.ets | 26 ++++++++ ...rkts-no-template-string-type.ets.args.json | 19 ++++++ ...ts-no-template-string-type.ets.arkts2.json | 64 +++++++++++++++++++ .../arkts-no-template-string-type.ets.json | 17 +++++ 10 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 ets2panda/linter/test/main/arkts-no-template-string-type.ets create mode 100644 ets2panda/linter/test/main/arkts-no-template-string-type.ets.args.json create mode 100644 ets2panda/linter/test/main/arkts-no-template-string-type.ets.arkts2.json create mode 100644 ets2panda/linter/test/main/arkts-no-template-string-type.ets.json diff --git a/ets2panda/linter/rule-config.json b/ets2panda/linter/rule-config.json index cb6e602316..d2c3c0420d 100644 --- a/ets2panda/linter/rule-config.json +++ b/ets2panda/linter/rule-config.json @@ -16,6 +16,7 @@ "arkts-no-class-add-super-prop-with-readonly", "arkts-no-classes-as-obj", "arkts-obj-literal-props", + "arkts-no-template-string-type", "arkts-obj-literal-key-type", "arkts-optional-methods", "arkts-numeric-semantic", diff --git a/ets2panda/linter/src/lib/CookBookMsg.ts b/ets2panda/linter/src/lib/CookBookMsg.ts index 3d57443dac..6ad0564ae5 100644 --- a/ets2panda/linter/src/lib/CookBookMsg.ts +++ b/ets2panda/linter/src/lib/CookBookMsg.ts @@ -104,7 +104,7 @@ cookBookTag[73] = ''; cookBookTag[74] = 'Destructuring variable declarations are not supported (arkts-no-destruct-decls)'; cookBookTag[75] = 'Use string-literal keys with Record (arkts-obj-literal-key-type)'; cookBookTag[76] = 'Type of parameter must be defined explicitly (arkts-require-func-arg-type)'; -cookBookTag[77] = ''; +cookBookTag[77] = 'Template string type is not supported (arkts-no-template-string-type)'; cookBookTag[78] = ''; cookBookTag[79] = 'Type annotation in catch clause is not supported (arkts-no-types-in-catch)'; cookBookTag[80] = '"for .. in" is not supported (arkts-no-for-in)'; diff --git a/ets2panda/linter/src/lib/FaultAttrs.ts b/ets2panda/linter/src/lib/FaultAttrs.ts index a862704a0c..3e2879c02d 100644 --- a/ets2panda/linter/src/lib/FaultAttrs.ts +++ b/ets2panda/linter/src/lib/FaultAttrs.ts @@ -66,6 +66,7 @@ faultsAttrs[FaultID.CommaOperator] = new FaultAttributes(71); faultsAttrs[FaultID.DestructuringDeclaration] = new FaultAttributes(74); faultsAttrs[FaultID.ObjectLiteralKeyType] = new FaultAttributes(75); faultsAttrs[FaultID.ParameterType] = new FaultAttributes(76); +faultsAttrs[FaultID.TemplateStringType] = new FaultAttributes(77); faultsAttrs[FaultID.CatchWithUnsupportedType] = new FaultAttributes(79); faultsAttrs[FaultID.ForInStatement] = new FaultAttributes(80); faultsAttrs[FaultID.MappedType] = new FaultAttributes(83); diff --git a/ets2panda/linter/src/lib/FaultDesc.ts b/ets2panda/linter/src/lib/FaultDesc.ts index 93a31cd080..a0fa2572b5 100644 --- a/ets2panda/linter/src/lib/FaultDesc.ts +++ b/ets2panda/linter/src/lib/FaultDesc.ts @@ -84,6 +84,7 @@ faultDesc[FaultID.ImportAssignment] = 'Import assignments (import = ..)'; faultDesc[FaultID.GenericCallNoTypeArgs] = 'Generic calls without type arguments'; faultDesc[FaultID.ParameterProperties] = 'Parameter properties in constructor'; faultDesc[FaultID.InstanceofUnsupported] = 'Left-hand side of "instanceof" is wrong'; +faultDesc[FaultID.TemplateStringType] = 'Template string type'; faultDesc[FaultID.ShorthandAmbientModuleDecl] = 'Shorthand ambient module declaration'; faultDesc[FaultID.WildcardsInModuleName] = 'Wildcards in module name'; faultDesc[FaultID.UMDModuleDefinition] = 'UMD module definition'; diff --git a/ets2panda/linter/src/lib/Problems.ts b/ets2panda/linter/src/lib/Problems.ts index 649892bfc7..ea86a9a955 100644 --- a/ets2panda/linter/src/lib/Problems.ts +++ b/ets2panda/linter/src/lib/Problems.ts @@ -173,6 +173,7 @@ export enum FaultID { TaggedTemplates, IncompationbleFunctionType, InvalidIdentifier, + TemplateStringType, NoImportJsonFile, ExtendsExpression, NumericSemantics, diff --git a/ets2panda/linter/src/lib/TypeScriptLinter.ts b/ets2panda/linter/src/lib/TypeScriptLinter.ts index e58892c530..8b0ea62724 100644 --- a/ets2panda/linter/src/lib/TypeScriptLinter.ts +++ b/ets2panda/linter/src/lib/TypeScriptLinter.ts @@ -402,6 +402,7 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { [ts.SyntaxKind.TypeAssertionExpression, this.handleTypeAssertionExpression], [ts.SyntaxKind.MethodDeclaration, this.handleMethodDeclaration], [ts.SyntaxKind.TupleType, this.handleTupleType], + [ts.SyntaxKind.TemplateLiteralType, this.handleTemplateType], [ts.SyntaxKind.MethodSignature, this.handleMethodSignature], [ts.SyntaxKind.ClassStaticBlockDeclaration, this.handleClassStaticBlockDeclaration], [ts.SyntaxKind.Identifier, this.handleIdentifier], @@ -3689,6 +3690,13 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { } } + private handleTemplateType(node: ts.TemplateLiteralTypeNode): void { + if (!this.options.arkts2) { + return; + } + this.incrementCounters(node, FaultID.TemplateStringType); + } + private handleTupleType(node: ts.TupleTypeNode): void { if (!this.options.arkts2) { return; diff --git a/ets2panda/linter/test/main/arkts-no-template-string-type.ets b/ets2panda/linter/test/main/arkts-no-template-string-type.ets new file mode 100644 index 0000000000..151de4d06d --- /dev/null +++ b/ets2panda/linter/test/main/arkts-no-template-string-type.ets @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +type Action = "verify"; +type ContentMatch = "match"; +type Outcome = `${Action}_${ContentMatch}`; +type Outcome2 = `${Action}_match`; +type Outcome3 = `verify_${ContentMatch}`; + +type Foo = "Foo"; +type Bar = "Bar"; + +let template: `${Foo}_${Bar}`; +function foo(template: `${Foo}_${Bar}`): `${number}` {} diff --git a/ets2panda/linter/test/main/arkts-no-template-string-type.ets.args.json b/ets2panda/linter/test/main/arkts-no-template-string-type.ets.args.json new file mode 100644 index 0000000000..66fb88f859 --- /dev/null +++ b/ets2panda/linter/test/main/arkts-no-template-string-type.ets.args.json @@ -0,0 +1,19 @@ +{ + "copyright": [ + "Copyright (c) 2025 Huawei Device Co., Ltd.", + "Licensed under the Apache License, Version 2.0 (the 'License');", + "you may not use this file except in compliance with the License.", + "You may obtain a copy of the License at", + "", + "http://www.apache.org/licenses/LICENSE-2.0", + "", + "Unless required by applicable law or agreed to in writing, software", + "distributed under the License is distributed on an 'AS IS' BASIS,", + "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.", + "See the License for the specific language governing permissions and", + "limitations under the License." + ], + "mode": { + "arkts2": "" + } +} diff --git a/ets2panda/linter/test/main/arkts-no-template-string-type.ets.arkts2.json b/ets2panda/linter/test/main/arkts-no-template-string-type.ets.arkts2.json new file mode 100644 index 0000000000..859370ed7b --- /dev/null +++ b/ets2panda/linter/test/main/arkts-no-template-string-type.ets.arkts2.json @@ -0,0 +1,64 @@ +{ + "result": [ + { + "line": 18, + "column": 16, + "endLine": 18, + "endColumn": 43, + "problem": "TemplateStringType", + "suggest": "", + "rule": "Template string type is not supported (arkts-no-template-string-type)", + "severity": "ERROR" + }, + { + "line": 19, + "column": 17, + "endLine": 19, + "endColumn": 34, + "problem": "TemplateStringType", + "suggest": "", + "rule": "Template string type is not supported (arkts-no-template-string-type)", + "severity": "ERROR" + }, + { + "line": 20, + "column": 17, + "endLine": 20, + "endColumn": 41, + "problem": "TemplateStringType", + "suggest": "", + "rule": "Template string type is not supported (arkts-no-template-string-type)", + "severity": "ERROR" + }, + { + "line": 25, + "column": 15, + "endLine": 25, + "endColumn": 30, + "problem": "TemplateStringType", + "suggest": "", + "rule": "Template string type is not supported (arkts-no-template-string-type)", + "severity": "ERROR" + }, + { + "line": 26, + "column": 24, + "endLine": 26, + "endColumn": 39, + "problem": "TemplateStringType", + "suggest": "", + "rule": "Template string type is not supported (arkts-no-template-string-type)", + "severity": "ERROR" + }, + { + "line": 26, + "column": 42, + "endLine": 26, + "endColumn": 53, + "problem": "TemplateStringType", + "suggest": "", + "rule": "Template string type is not supported (arkts-no-template-string-type)", + "severity": "ERROR" + } + ] +} \ No newline at end of file diff --git a/ets2panda/linter/test/main/arkts-no-template-string-type.ets.json b/ets2panda/linter/test/main/arkts-no-template-string-type.ets.json new file mode 100644 index 0000000000..dd03fcf544 --- /dev/null +++ b/ets2panda/linter/test/main/arkts-no-template-string-type.ets.json @@ -0,0 +1,17 @@ +{ + "copyright": [ + "Copyright (c) 2025 Huawei Device Co., Ltd.", + "Licensed under the Apache License, Version 2.0 (the 'License');", + "you may not use this file except in compliance with the License.", + "You may obtain a copy of the License at", + "", + "http://www.apache.org/licenses/LICENSE-2.0", + "", + "Unless required by applicable law or agreed to in writing, software", + "distributed under the License is distributed on an 'AS IS' BASIS,", + "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.", + "See the License for the specific language governing permissions and", + "limitations under the License." + ], + "result": [] +} -- Gitee