diff --git a/ets2panda/linter/src/lib/TypeScriptLinter.ts b/ets2panda/linter/src/lib/TypeScriptLinter.ts index 3328315b274f8e7718d6ed1563ff158f1ef666da..a825c827f88f5a26db0d2fa0775fe9c1470563b7 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 2eaef243aa86d7ebaefd91e471e66efde1909242..1f26e36e862aa393385cf635d5b726a41ec9c02c 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 0000000000000000000000000000000000000000..ce654970620ba7e86c8499b78e79627f440ce790 --- /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 d9f270ec5e6f677618a97014922a8567d43b1ca2..a2c58a7a74942e1fd0176a78637632293ae1c6cd 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 a85d137032e0362398eb7bbc7cce90fccd764b3c..fec7d247c68060bcc7e41499749c6821440628ec 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 46c7e599251bb639a1baaa126c0edf24636fc8b7..b3274fd8740dd0d83d1c45ea3b2eaab78705489b 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 bf760d1cf6aa852407507a04b3e7f8939b97ce40..a6deb3a499e02abb2ae1613ccd00de7a54fe9c5a 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 47c591c577564449d20ae831cd168cb729bf4e87..6e0fbe1a85e191bf8014d763561943d0607f7116 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 8a91a2e1a54a2aae0ac54f5bc5c8f1a0b2eefebc..0ae2dd16e45ef992843652a9100522f7ff3387ba 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 fcfeff2dac66bbfedc5c49ec4c7dc9234df6eff7..34bbf47a13871d124dd6950a45a3dbc5561e2e88 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 74d51ae8db6be6ac8cc4ccb9c338f87680269510..ca74c325590ed20960e5e8f19fa66c59bffd5dbd 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 76d4be8122a044cab26465f3b6e83eb567e024a6..02c6d721fb9db4dc93c627434c7d75870dd29916 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 2956a7461c93662772b93bad5274bb8915f22610..41e86435eea676858d5c259c012831fba6db5278 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 bd65f7efbe5b1a14b6001506f9a8511cd080d1d1..315ec6f0aeb96fceaa1d5d95ab22858aea3255e4 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