From 3040e9bc122b06096e606a0a199bc53064ad29af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=92=9F=E6=9F=A0?= Date: Fri, 29 Aug 2025 16:11:31 +0800 Subject: [PATCH] fix issue for arkts-no-ts-like-smart-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/ICV3OT Test scenarios:fix issue for arkts-no-ts-like-smart-type Signed-off-by: 钟柠 --- ets2panda/linter/src/lib/TypeScriptLinter.ts | 8 +++ .../test/main/no_ts_like_smart_type.ets | 20 +++++++ .../no_ts_like_smart_type.ets.arkts2.json | 54 +++++++++++++++++++ .../test/main/no_ts_like_smart_type.ets.json | 54 +++++++++++++++++++ 4 files changed, 136 insertions(+) diff --git a/ets2panda/linter/src/lib/TypeScriptLinter.ts b/ets2panda/linter/src/lib/TypeScriptLinter.ts index 317c4a8bf6..9ce97631b7 100644 --- a/ets2panda/linter/src/lib/TypeScriptLinter.ts +++ b/ets2panda/linter/src/lib/TypeScriptLinter.ts @@ -12692,6 +12692,14 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { methodReturnType: ts.Type ): void { const propName = propAccess.name.getText(); + const decl = this.tsTypeChecker.getSymbolAtLocation(propAccess.name)?.declarations?.[0]; + if (decl && ts.isPropertyDeclaration(decl) && decl.type) { + if (decl.type.kind === ts.SyntaxKind.BooleanKeyword && decl.questionToken === undefined) { + return; + } else if (!this.tsTypeChecker.getTypeAtLocation(decl.name).isUnion()) { + return; + } + } const propType = propsMap.get(propName); if (propType && this.isExactlySameType(propType, methodReturnType)) { diff --git a/ets2panda/linter/test/main/no_ts_like_smart_type.ets b/ets2panda/linter/test/main/no_ts_like_smart_type.ets index 367815236a..9700a97a80 100755 --- a/ets2panda/linter/test/main/no_ts_like_smart_type.ets +++ b/ets2panda/linter/test/main/no_ts_like_smart_type.ets @@ -373,4 +373,24 @@ function test(value: string) { if (isOperator(value)) { //no error console.log('5') } +} + +class Temp { + public ret: boolean = true; + + async checkAppItem(elementNameVal: Want) { + logger.info(TAG, 'checkAppItem in bundleName:' + elementNameVal.bundleName + ' | abilityName:' + + elementNameVal.abilityName); + let want: Want = { + 'bundleName': elementNameVal.bundleName, + 'abilityName': elementNameVal.abilityName + }; + let userId: UserId = { localId: 0 }; + let retVal = await accountManager.getAccountUserId(userId); + if (!retVal) { + logger.warn(TAG, 'checkAppItem getAccountUserId fail!'); + this.ret = false; + return this.ret; + } + } } \ No newline at end of file diff --git a/ets2panda/linter/test/main/no_ts_like_smart_type.ets.arkts2.json b/ets2panda/linter/test/main/no_ts_like_smart_type.ets.arkts2.json index e62b41b882..d3e8d22442 100644 --- a/ets2panda/linter/test/main/no_ts_like_smart_type.ets.arkts2.json +++ b/ets2panda/linter/test/main/no_ts_like_smart_type.ets.arkts2.json @@ -1,4 +1,18 @@ { + "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": [ { "line": 30, @@ -330,6 +344,36 @@ "rule": "Use explicit types instead of \"any\", \"unknown\" (arkts-no-any-unknown)", "severity": "ERROR" }, + { + "line": 389, + "column": 9, + "endLine": 389, + "endColumn": 63, + "problem": "AnyType", + "suggest": "", + "rule": "Use explicit types instead of \"any\", \"unknown\" (arkts-no-any-unknown)", + "severity": "ERROR" + }, + { + "line": 381, + "column": 38, + "endLine": 381, + "endColumn": 42, + "problem": "UIInterfaceImport", + "suggest": "", + "rule": "The ArkUI interface \"Want\" should be imported before it is used (arkui-modular-interface)", + "severity": "ERROR" + }, + { + "line": 384, + "column": 15, + "endLine": 384, + "endColumn": 19, + "problem": "UIInterfaceImport", + "suggest": "", + "rule": "The ArkUI interface \"Want\" should be imported before it is used (arkui-modular-interface)", + "severity": "ERROR" + }, { "line": 137, "column": 5, @@ -399,6 +443,16 @@ "suggest": "'obj' is possibly 'undefined'.", "rule": "'obj' is possibly 'undefined'.", "severity": "ERROR" + }, + { + "line": 381, + "column": 9, + "endLine": 381, + "endColumn": 21, + "problem": "StrictDiagnostic", + "suggest": "Not all code paths return a value.", + "rule": "Not all code paths return a value.", + "severity": "ERROR" } ] } \ No newline at end of file diff --git a/ets2panda/linter/test/main/no_ts_like_smart_type.ets.json b/ets2panda/linter/test/main/no_ts_like_smart_type.ets.json index 996a2de958..434d7ca4b0 100644 --- a/ets2panda/linter/test/main/no_ts_like_smart_type.ets.json +++ b/ets2panda/linter/test/main/no_ts_like_smart_type.ets.json @@ -1,4 +1,18 @@ { + "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": [ { "line": 339, @@ -100,6 +114,36 @@ "rule": "Use explicit types instead of \"any\", \"unknown\" (arkts-no-any-unknown)", "severity": "ERROR" }, + { + "line": 385, + "column": 7, + "endLine": 385, + "endColumn": 19, + "problem": "LiteralAsPropertyName", + "suggest": "", + "rule": "Objects with property names that are not identifiers are not supported (arkts-identifiers-as-prop-names)", + "severity": "ERROR" + }, + { + "line": 386, + "column": 7, + "endLine": 386, + "endColumn": 20, + "problem": "LiteralAsPropertyName", + "suggest": "", + "rule": "Objects with property names that are not identifiers are not supported (arkts-identifiers-as-prop-names)", + "severity": "ERROR" + }, + { + "line": 389, + "column": 9, + "endLine": 389, + "endColumn": 63, + "problem": "AnyType", + "suggest": "", + "rule": "Use explicit types instead of \"any\", \"unknown\" (arkts-no-any-unknown)", + "severity": "ERROR" + }, { "line": 137, "column": 5, @@ -169,6 +213,16 @@ "suggest": "'obj' is possibly 'undefined'.", "rule": "'obj' is possibly 'undefined'.", "severity": "ERROR" + }, + { + "line": 381, + "column": 9, + "endLine": 381, + "endColumn": 21, + "problem": "StrictDiagnostic", + "suggest": "Not all code paths return a value.", + "rule": "Not all code paths return a value.", + "severity": "ERROR" } ] } \ No newline at end of file -- Gitee