From d20b6ab68f4f08f98aa076b932fe2317accb3172 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=92=9F=E6=9F=A0?= Date: Sat, 14 Jun 2025 11:33:19 +0800 Subject: [PATCH] Fix bugs for arkts-no-ts-like-catch-type MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Issue:https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICEEYQ Test scenarios:Fix bugs for arkts-no-ts-like-catch-type Signed-off-by: 钟柠 --- ets2panda/linter/src/lib/TypeScriptLinter.ts | 26 +++++++-- .../src/lib/utils/consts/ArkTS2Rules.ts | 1 + .../linter/src/lib/utils/consts/ErrorProp.ts | 16 +++++ .../interop_import_js_rules.ets.arkts2.json | 10 ---- .../interop_import_js_rules.ets.autofix.json | 10 ---- .../test/interop/unique_types.ets.arkts2.json | 10 ---- .../interop/unique_types.ets.autofix.json | 10 ---- .../interop/unique_types.ets.migrate.json | 10 ---- .../test/main/limit_void_type.ets.arkts2.json | 10 ---- .../main/limit_void_type.ets.autofix.json | 10 ---- .../main/limit_void_type.ets.migrate.json | 10 ---- .../linter/test/main/ts-like-catch-type.ets | 20 +++---- .../main/ts-like-catch-type.ets.arkts2.json | 58 ++----------------- .../test/main/ts-like-catch-type.ets.json | 23 +------- 14 files changed, 50 insertions(+), 174 deletions(-) create mode 100644 ets2panda/linter/src/lib/utils/consts/ErrorProp.ts diff --git a/ets2panda/linter/src/lib/TypeScriptLinter.ts b/ets2panda/linter/src/lib/TypeScriptLinter.ts index 3328315b27..a825c827f8 100644 --- a/ets2panda/linter/src/lib/TypeScriptLinter.ts +++ b/ets2panda/linter/src/lib/TypeScriptLinter.ts @@ -140,6 +140,7 @@ import type { ArrayAccess, UncheckedIdentifier, CheckedIdentifier } from './util import { CheckResult } from './utils/consts/RuntimeCheckAPI'; import { NUMBER_LITERAL } from './utils/consts/RuntimeCheckAPI'; import { globalApiAssociatedInfo } from './utils/consts/AssociatedInfo'; +import { ERROR_PROP_LIST } from './utils/consts/ErrorProp'; interface InterfaceSymbolTypeResult { propNames: string[]; @@ -2929,12 +2930,25 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { this.incrementCounters(node, FaultID.CatchWithUnsupportedType, autofix); } - if ( - this.options.arkts2 && - tsCatch.variableDeclaration && - TsUtils.isAnyType(this.tsTypeChecker.getTypeAtLocation(tsCatch.variableDeclaration)) - ) { - this.incrementCounters(node, FaultID.TsLikeCatchType); + if (this.options.arkts2 && tsCatch.variableDeclaration?.name) { + const varDeclName = tsCatch.variableDeclaration?.name.getText(); + tsCatch.block.statements.forEach((statement) => { + this.checkTsLikeCatchType(statement, varDeclName); + }); + } + } + + private checkTsLikeCatchType(node: ts.Node, variableDeclarationName: string): void { + if (!node) { + return; + } + for (const child of node.getChildren()) { + if (ts.isPropertyAccessExpression(child)) { + if (child.expression.getText() === variableDeclarationName && !ERROR_PROP_LIST.has(child.name.getText())) { + this.incrementCounters(child, FaultID.TsLikeCatchType); + } + } + this.checkTsLikeCatchType(child, variableDeclarationName); } } diff --git a/ets2panda/linter/src/lib/utils/consts/ArkTS2Rules.ts b/ets2panda/linter/src/lib/utils/consts/ArkTS2Rules.ts index 2eaef243aa..1f26e36e86 100644 --- a/ets2panda/linter/src/lib/utils/consts/ArkTS2Rules.ts +++ b/ets2panda/linter/src/lib/utils/consts/ArkTS2Rules.ts @@ -21,6 +21,7 @@ export const arkts2Rules: number[] = [ 37, 29, 111, + 134, 137, 139, 140, diff --git a/ets2panda/linter/src/lib/utils/consts/ErrorProp.ts b/ets2panda/linter/src/lib/utils/consts/ErrorProp.ts new file mode 100644 index 0000000000..ce65497062 --- /dev/null +++ b/ets2panda/linter/src/lib/utils/consts/ErrorProp.ts @@ -0,0 +1,16 @@ +/* + * 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. + */ + +export const ERROR_PROP_LIST: Set = new Set(['name', 'message', 'stack', 'code']); diff --git a/ets2panda/linter/test/interop/interop_import_js_rules.ets.arkts2.json b/ets2panda/linter/test/interop/interop_import_js_rules.ets.arkts2.json index d9f270ec5e..a2c58a7a74 100644 --- a/ets2panda/linter/test/interop/interop_import_js_rules.ets.arkts2.json +++ b/ets2panda/linter/test/interop/interop_import_js_rules.ets.arkts2.json @@ -294,16 +294,6 @@ "rule": "ArkTS directly call JS functions or parameters is not supported (arkts-interop-js2s-call-js-func)", "severity": "ERROR" }, - { - "line": 46, - "column": 3, - "endLine": 48, - "endColumn": 2, - "problem": "TsLikeCatchType", - "suggest": "", - "rule": "TS catch type are not supported (arkts-no-ts-like-catch-type)", - "severity": "ERROR" - }, { "line": 50, "column": 11, diff --git a/ets2panda/linter/test/interop/interop_import_js_rules.ets.autofix.json b/ets2panda/linter/test/interop/interop_import_js_rules.ets.autofix.json index a85d137032..fec7d247c6 100644 --- a/ets2panda/linter/test/interop/interop_import_js_rules.ets.autofix.json +++ b/ets2panda/linter/test/interop/interop_import_js_rules.ets.autofix.json @@ -551,16 +551,6 @@ "rule": "ArkTS directly call JS functions or parameters is not supported (arkts-interop-js2s-call-js-func)", "severity": "ERROR" }, - { - "line": 46, - "column": 3, - "endLine": 48, - "endColumn": 2, - "problem": "TsLikeCatchType", - "suggest": "", - "rule": "TS catch type are not supported (arkts-no-ts-like-catch-type)", - "severity": "ERROR" - }, { "line": 50, "column": 11, diff --git a/ets2panda/linter/test/interop/unique_types.ets.arkts2.json b/ets2panda/linter/test/interop/unique_types.ets.arkts2.json index 46c7e59925..b3274fd874 100644 --- a/ets2panda/linter/test/interop/unique_types.ets.arkts2.json +++ b/ets2panda/linter/test/interop/unique_types.ets.arkts2.json @@ -54,16 +54,6 @@ "rule": "Cannot access typescript types directly (arkts-interop-ts2s-static-access-ts-type)", "severity": "ERROR" }, - { - "line": 30, - "column": 3, - "endLine": 31, - "endColumn": 2, - "problem": "TsLikeCatchType", - "suggest": "", - "rule": "TS catch type are not supported (arkts-no-ts-like-catch-type)", - "severity": "ERROR" - }, { "line": 33, "column": 1, diff --git a/ets2panda/linter/test/interop/unique_types.ets.autofix.json b/ets2panda/linter/test/interop/unique_types.ets.autofix.json index bf760d1cf6..a6deb3a499 100644 --- a/ets2panda/linter/test/interop/unique_types.ets.autofix.json +++ b/ets2panda/linter/test/interop/unique_types.ets.autofix.json @@ -76,16 +76,6 @@ "rule": "Cannot access typescript types directly (arkts-interop-ts2s-static-access-ts-type)", "severity": "ERROR" }, - { - "line": 30, - "column": 3, - "endLine": 31, - "endColumn": 2, - "problem": "TsLikeCatchType", - "suggest": "", - "rule": "TS catch type are not supported (arkts-no-ts-like-catch-type)", - "severity": "ERROR" - }, { "line": 33, "column": 1, diff --git a/ets2panda/linter/test/interop/unique_types.ets.migrate.json b/ets2panda/linter/test/interop/unique_types.ets.migrate.json index 47c591c577..6e0fbe1a85 100644 --- a/ets2panda/linter/test/interop/unique_types.ets.migrate.json +++ b/ets2panda/linter/test/interop/unique_types.ets.migrate.json @@ -34,16 +34,6 @@ "rule": "Cannot access typescript types directly (arkts-interop-ts2s-static-access-ts-type)", "severity": "ERROR" }, - { - "line": 30, - "column": 3, - "endLine": 31, - "endColumn": 2, - "problem": "TsLikeCatchType", - "suggest": "", - "rule": "TS catch type are not supported (arkts-no-ts-like-catch-type)", - "severity": "ERROR" - }, { "line": 33, "column": 1, diff --git a/ets2panda/linter/test/main/limit_void_type.ets.arkts2.json b/ets2panda/linter/test/main/limit_void_type.ets.arkts2.json index 8a91a2e1a5..0ae2dd16e4 100644 --- a/ets2panda/linter/test/main/limit_void_type.ets.arkts2.json +++ b/ets2panda/linter/test/main/limit_void_type.ets.arkts2.json @@ -1423,16 +1423,6 @@ "suggest": "", "rule": "Type \"void\" has no instances.(arkts-limited-void-type)", "severity": "ERROR" - }, - { - "line": 260, - "column": 5, - "endLine": 262, - "endColumn": 4, - "problem": "TsLikeCatchType", - "suggest": "", - "rule": "TS catch type are not supported (arkts-no-ts-like-catch-type)", - "severity": "ERROR" } ] } \ No newline at end of file diff --git a/ets2panda/linter/test/main/limit_void_type.ets.autofix.json b/ets2panda/linter/test/main/limit_void_type.ets.autofix.json index fcfeff2dac..34bbf47a13 100644 --- a/ets2panda/linter/test/main/limit_void_type.ets.autofix.json +++ b/ets2panda/linter/test/main/limit_void_type.ets.autofix.json @@ -2177,16 +2177,6 @@ "suggest": "", "rule": "Type \"void\" has no instances.(arkts-limited-void-type)", "severity": "ERROR" - }, - { - "line": 260, - "column": 5, - "endLine": 262, - "endColumn": 4, - "problem": "TsLikeCatchType", - "suggest": "", - "rule": "TS catch type are not supported (arkts-no-ts-like-catch-type)", - "severity": "ERROR" } ] } \ No newline at end of file diff --git a/ets2panda/linter/test/main/limit_void_type.ets.migrate.json b/ets2panda/linter/test/main/limit_void_type.ets.migrate.json index 74d51ae8db..ca74c32559 100644 --- a/ets2panda/linter/test/main/limit_void_type.ets.migrate.json +++ b/ets2panda/linter/test/main/limit_void_type.ets.migrate.json @@ -434,16 +434,6 @@ "rule": "The switch expression type must be of type char, byte, short, int, long, string or enum (arkts-switch-expr)", "severity": "ERROR" }, - { - "line": 270, - "column": 5, - "endLine": 272, - "endColumn": 4, - "problem": "TsLikeCatchType", - "suggest": "", - "rule": "TS catch type are not supported (arkts-no-ts-like-catch-type)", - "severity": "ERROR" - }, { "line": 219, "column": 29, diff --git a/ets2panda/linter/test/main/ts-like-catch-type.ets b/ets2panda/linter/test/main/ts-like-catch-type.ets index 76d4be8122..02c6d721fb 100644 --- a/ets2panda/linter/test/main/ts-like-catch-type.ets +++ b/ets2panda/linter/test/main/ts-like-catch-type.ets @@ -13,18 +13,14 @@ * limitations under the License. */ -function test() { - try { - let a: number = 1; - } catch (e) { - console.log('catch') - } +try { + throw new Error(); +} catch(e) { + e.message; + e.prop; } -function test2() { - try { - let a: number = 1; - } catch (e: any) { - console.log('catch') - } +try { + throw new Error(); +} catch(e) { } \ No newline at end of file diff --git a/ets2panda/linter/test/main/ts-like-catch-type.ets.arkts2.json b/ets2panda/linter/test/main/ts-like-catch-type.ets.arkts2.json index 2956a7461c..41e86435ee 100644 --- a/ets2panda/linter/test/main/ts-like-catch-type.ets.arkts2.json +++ b/ets2panda/linter/test/main/ts-like-catch-type.ets.arkts2.json @@ -15,64 +15,14 @@ ], "result": [ { - "line": 18, - "column": 21, - "endLine": 18, - "endColumn": 22, - "problem": "NumericSemantics", - "suggest": "", - "rule": "Numeric semantics is different for integer values (arkts-numeric-semantic)", - "severity": "ERROR" - }, - { - "line": 19, - "column": 5, - "endLine": 21, - "endColumn": 4, + "line": 20, + "column": 3, + "endLine": 20, + "endColumn": 9, "problem": "TsLikeCatchType", "suggest": "", "rule": "TS catch type are not supported (arkts-no-ts-like-catch-type)", "severity": "ERROR" - }, - { - "line": 26, - "column": 21, - "endLine": 26, - "endColumn": 22, - "problem": "NumericSemantics", - "suggest": "", - "rule": "Numeric semantics is different for integer values (arkts-numeric-semantic)", - "severity": "ERROR" - }, - { - "line": 27, - "column": 12, - "endLine": 27, - "endColumn": 18, - "problem": "CatchWithUnsupportedType", - "suggest": "", - "rule": "Type annotation in catch clause is not supported (arkts-no-types-in-catch)", - "severity": "ERROR" - }, - { - "line": 27, - "column": 5, - "endLine": 29, - "endColumn": 4, - "problem": "TsLikeCatchType", - "suggest": "", - "rule": "TS catch type are not supported (arkts-no-ts-like-catch-type)", - "severity": "ERROR" - }, - { - "line": 27, - "column": 15, - "endLine": 27, - "endColumn": 18, - "problem": "AnyType", - "suggest": "", - "rule": "Use explicit types instead of \"any\", \"unknown\" (arkts-no-any-unknown)", - "severity": "ERROR" } ] } \ No newline at end of file diff --git a/ets2panda/linter/test/main/ts-like-catch-type.ets.json b/ets2panda/linter/test/main/ts-like-catch-type.ets.json index bd65f7efbe..315ec6f0ae 100644 --- a/ets2panda/linter/test/main/ts-like-catch-type.ets.json +++ b/ets2panda/linter/test/main/ts-like-catch-type.ets.json @@ -13,26 +13,5 @@ "See the License for the specific language governing permissions and", "limitations under the License." ], - "result": [ - { - "line": 27, - "column": 12, - "endLine": 27, - "endColumn": 18, - "problem": "CatchWithUnsupportedType", - "suggest": "", - "rule": "Type annotation in catch clause is not supported (arkts-no-types-in-catch)", - "severity": "ERROR" - }, - { - "line": 27, - "column": 15, - "endLine": 27, - "endColumn": 18, - "problem": "AnyType", - "suggest": "", - "rule": "Use explicit types instead of \"any\", \"unknown\" (arkts-no-any-unknown)", - "severity": "ERROR" - } - ] + "result": [] } \ No newline at end of file -- Gitee