From 5601f22d652763235aea56b57e738b81b456c231 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=92=9F=E6=9F=A0?= Date: Thu, 10 Jul 2025 16:33:50 +0800 Subject: [PATCH] fix issue for arkts-incompatible-function-types MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICKYL4 Test scenarios: arkts-incompatible-function-types Signed-off-by: 钟柠 --- ets2panda/linter/src/lib/TypeScriptLinter.ts | 31 ++++- ..._promise_need_void_resolve.ets.arkts2.json | 10 ++ .../arkts_promise_need_void_resolve.ets.json | 13 +- .../func_inferred_type_args.ets.arkts2.json | 10 ++ .../main/func_inferred_type_args.ets.json | 10 ++ .../func_inferred_type_args_2.ets.arkts2.json | 20 +++ ...func_inferred_type_args_2.ets.autofix.json | 20 +++ .../main/func_inferred_type_args_2.ets.json | 20 +++ ...func_inferred_type_args_2.ets.migrate.json | 20 +++ .../test/main/incompatible_function.ets | 23 +++- .../incompatible_function.ets.arkts2.json | 130 ++++++++++++++++++ .../test/main/incompatible_function.ets.json | 43 +++++- ...tructural_identity_promise.ets.arkts2.json | 13 +- .../main/structural_identity_promise.ets.json | 13 +- 14 files changed, 370 insertions(+), 6 deletions(-) diff --git a/ets2panda/linter/src/lib/TypeScriptLinter.ts b/ets2panda/linter/src/lib/TypeScriptLinter.ts index ca04dbcfcd..7f80b8cebd 100644 --- a/ets2panda/linter/src/lib/TypeScriptLinter.ts +++ b/ets2panda/linter/src/lib/TypeScriptLinter.ts @@ -5080,7 +5080,7 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { this.handleSharedArrayBuffer(tsNewExpr); this.handleSdkGlobalApi(tsNewExpr); this.checkCreatingPrimitiveTypes(tsNewExpr); - + this.checkPromiseReturnType(tsNewExpr); if (this.options.advancedClassChecks || this.options.arkts2) { const calleeExpr = tsNewExpr.expression; const calleeType = this.tsTypeChecker.getTypeAtLocation(calleeExpr); @@ -5110,6 +5110,35 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { this.handlePromiseNeedVoidResolve(tsNewExpr); } + private checkPromiseReturnType(newExpr: ts.NewExpression): void { + if (!ts.isIdentifier(newExpr.expression)) { return }; + if (newExpr.expression.text !== 'Promise') { return }; + + const callback = newExpr.arguments?.[0]; + if (!callback || !ts.isFunctionLike(callback)) { return }; + + const callbackReturnType = this.tsTypeChecker.getTypeAtLocation(callback); + const callSignatures = callbackReturnType.getCallSignatures(); + if (callSignatures.length === 0) { return }; + + const actualReturnType = callSignatures[0].getReturnType(); + + const voidType = this.tsTypeChecker.getTypeAtLocation( + ts.factory.createVoidZero()); + if (!this.areTypesEqual(actualReturnType, voidType)) { + this.incrementCounters(callback, FaultID.IncompationbleFunctionType); + } + } + + private areTypesEqual(type1: ts.Type, type2: ts.Type): boolean { + if (type1.flags !== type2.flags) { return false }; + + const type1Str = this.tsTypeChecker.typeToString(type1); + const type2Str = this.tsTypeChecker.typeToString(type2); + + return type1Str === type2Str; + } + handlePromiseNeedVoidResolve(newExpr: ts.NewExpression): void { if (!this.options.arkts2) { return; diff --git a/ets2panda/linter/test/main/arkts_promise_need_void_resolve.ets.arkts2.json b/ets2panda/linter/test/main/arkts_promise_need_void_resolve.ets.arkts2.json index 83f403ba1f..88c5e5728c 100755 --- a/ets2panda/linter/test/main/arkts_promise_need_void_resolve.ets.arkts2.json +++ b/ets2panda/linter/test/main/arkts_promise_need_void_resolve.ets.arkts2.json @@ -14,6 +14,16 @@ "limitations under the License." ], "result": [ + { + "line": 17, + "column": 31, + "endLine": 17, + "endColumn": 58, + "problem": "IncompationbleFunctionType", + "suggest": "", + "rule": "Stricter assignments into variables of function type (arkts-incompatible-function-types)", + "severity": "ERROR" + }, { "line": 17, "column": 40, diff --git a/ets2panda/linter/test/main/arkts_promise_need_void_resolve.ets.json b/ets2panda/linter/test/main/arkts_promise_need_void_resolve.ets.json index 127a98a334..d4d6e1cb05 100755 --- a/ets2panda/linter/test/main/arkts_promise_need_void_resolve.ets.json +++ b/ets2panda/linter/test/main/arkts_promise_need_void_resolve.ets.json @@ -13,5 +13,16 @@ "See the License for the specific language governing permissions and", "limitations under the License." ], - "result": [] + "result": [ + { + "line": 17, + "column": 31, + "endLine": 17, + "endColumn": 58, + "problem": "IncompationbleFunctionType", + "suggest": "", + "rule": "Stricter assignments into variables of function type (arkts-incompatible-function-types)", + "severity": "ERROR" + } + ] } \ No newline at end of file diff --git a/ets2panda/linter/test/main/func_inferred_type_args.ets.arkts2.json b/ets2panda/linter/test/main/func_inferred_type_args.ets.arkts2.json index eee42f58f0..bea02a4020 100755 --- a/ets2panda/linter/test/main/func_inferred_type_args.ets.arkts2.json +++ b/ets2panda/linter/test/main/func_inferred_type_args.ets.arkts2.json @@ -434,6 +434,16 @@ "rule": "Use explicit types instead of \"any\", \"unknown\" (arkts-no-any-unknown)", "severity": "ERROR" }, + { + "line": 92, + "column": 29, + "endLine": 92, + "endColumn": 44, + "problem": "IncompationbleFunctionType", + "suggest": "", + "rule": "Stricter assignments into variables of function type (arkts-incompatible-function-types)", + "severity": "ERROR" + }, { "line": 92, "column": 16, diff --git a/ets2panda/linter/test/main/func_inferred_type_args.ets.json b/ets2panda/linter/test/main/func_inferred_type_args.ets.json index 2d86c954b2..6e35f7f6eb 100644 --- a/ets2panda/linter/test/main/func_inferred_type_args.ets.json +++ b/ets2panda/linter/test/main/func_inferred_type_args.ets.json @@ -224,6 +224,16 @@ "rule": "Use explicit types instead of \"any\", \"unknown\" (arkts-no-any-unknown)", "severity": "ERROR" }, + { + "line": 92, + "column": 29, + "endLine": 92, + "endColumn": 44, + "problem": "IncompationbleFunctionType", + "suggest": "", + "rule": "Stricter assignments into variables of function type (arkts-incompatible-function-types)", + "severity": "ERROR" + }, { "line": 92, "column": 16, diff --git a/ets2panda/linter/test/main/func_inferred_type_args_2.ets.arkts2.json b/ets2panda/linter/test/main/func_inferred_type_args_2.ets.arkts2.json index d1ca60d4ff..d80ed23dda 100644 --- a/ets2panda/linter/test/main/func_inferred_type_args_2.ets.arkts2.json +++ b/ets2panda/linter/test/main/func_inferred_type_args_2.ets.arkts2.json @@ -244,6 +244,16 @@ "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", "severity": "ERROR" }, + { + "line": 41, + "column": 46, + "endLine": 41, + "endColumn": 66, + "problem": "IncompationbleFunctionType", + "suggest": "", + "rule": "Stricter assignments into variables of function type (arkts-incompatible-function-types)", + "severity": "ERROR" + }, { "line": 41, "column": 34, @@ -474,6 +484,16 @@ "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", "severity": "ERROR" }, + { + "line": 105, + "column": 27, + "endLine": 105, + "endColumn": 42, + "problem": "IncompationbleFunctionType", + "suggest": "", + "rule": "Stricter assignments into variables of function type (arkts-incompatible-function-types)", + "severity": "ERROR" + }, { "line": 105, "column": 14, diff --git a/ets2panda/linter/test/main/func_inferred_type_args_2.ets.autofix.json b/ets2panda/linter/test/main/func_inferred_type_args_2.ets.autofix.json index c53b7da247..920bace1fc 100644 --- a/ets2panda/linter/test/main/func_inferred_type_args_2.ets.autofix.json +++ b/ets2panda/linter/test/main/func_inferred_type_args_2.ets.autofix.json @@ -354,6 +354,16 @@ "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", "severity": "ERROR" }, + { + "line": 41, + "column": 46, + "endLine": 41, + "endColumn": 66, + "problem": "IncompationbleFunctionType", + "suggest": "", + "rule": "Stricter assignments into variables of function type (arkts-incompatible-function-types)", + "severity": "ERROR" + }, { "line": 41, "column": 34, @@ -727,6 +737,16 @@ "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", "severity": "ERROR" }, + { + "line": 105, + "column": 27, + "endLine": 105, + "endColumn": 42, + "problem": "IncompationbleFunctionType", + "suggest": "", + "rule": "Stricter assignments into variables of function type (arkts-incompatible-function-types)", + "severity": "ERROR" + }, { "line": 105, "column": 14, diff --git a/ets2panda/linter/test/main/func_inferred_type_args_2.ets.json b/ets2panda/linter/test/main/func_inferred_type_args_2.ets.json index c3cf571d47..0bfd2339e0 100644 --- a/ets2panda/linter/test/main/func_inferred_type_args_2.ets.json +++ b/ets2panda/linter/test/main/func_inferred_type_args_2.ets.json @@ -84,6 +84,16 @@ "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", "severity": "ERROR" }, + { + "line": 41, + "column": 46, + "endLine": 41, + "endColumn": 66, + "problem": "IncompationbleFunctionType", + "suggest": "", + "rule": "Stricter assignments into variables of function type (arkts-incompatible-function-types)", + "severity": "ERROR" + }, { "line": 52, "column": 10, @@ -124,6 +134,16 @@ "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", "severity": "ERROR" }, + { + "line": 105, + "column": 27, + "endLine": 105, + "endColumn": 42, + "problem": "IncompationbleFunctionType", + "suggest": "", + "rule": "Stricter assignments into variables of function type (arkts-incompatible-function-types)", + "severity": "ERROR" + }, { "line": 103, "column": 22, diff --git a/ets2panda/linter/test/main/func_inferred_type_args_2.ets.migrate.json b/ets2panda/linter/test/main/func_inferred_type_args_2.ets.migrate.json index 0b10990af1..6280062327 100644 --- a/ets2panda/linter/test/main/func_inferred_type_args_2.ets.migrate.json +++ b/ets2panda/linter/test/main/func_inferred_type_args_2.ets.migrate.json @@ -144,6 +144,16 @@ "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", "severity": "ERROR" }, + { + "line": 48, + "column": 54, + "endLine": 48, + "endColumn": 74, + "problem": "IncompationbleFunctionType", + "suggest": "", + "rule": "Stricter assignments into variables of function type (arkts-incompatible-function-types)", + "severity": "ERROR" + }, { "line": 59, "column": 10, @@ -244,6 +254,16 @@ "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", "severity": "ERROR" }, + { + "line": 112, + "column": 35, + "endLine": 112, + "endColumn": 50, + "problem": "IncompationbleFunctionType", + "suggest": "", + "rule": "Stricter assignments into variables of function type (arkts-incompatible-function-types)", + "severity": "ERROR" + }, { "line": 118, "column": 22, diff --git a/ets2panda/linter/test/main/incompatible_function.ets b/ets2panda/linter/test/main/incompatible_function.ets index e137fe5ecc..412909e603 100644 --- a/ets2panda/linter/test/main/incompatible_function.ets +++ b/ets2panda/linter/test/main/incompatible_function.ets @@ -79,4 +79,25 @@ type FuncTypeNoParams = () => void; let fNoParams: FuncTypeNoParams = () => { return 0; -}; \ No newline at end of file +}; + +function sleep(time: number): Promise { + return new Promise((resolve) => setTimeout(resolve, time)); +} +sleep(100) + +const caseName = 'PromiseAnyTest0200'; +console.info(${caseName} test start); +const startTime = Date.now(); +const promises: Promise[] = []; +const totalPromises = 1000; +const delay = 100; +for (let i = 0; i < totalPromises; i++) { + promises.push( + new Promise((_, reject) => + setTimeout(() => { + reject(new Error(Failure ${i})); + }, delay) + ) + ); +} \ No newline at end of file diff --git a/ets2panda/linter/test/main/incompatible_function.ets.arkts2.json b/ets2panda/linter/test/main/incompatible_function.ets.arkts2.json index da908095d3..080087fe96 100644 --- a/ets2panda/linter/test/main/incompatible_function.ets.arkts2.json +++ b/ets2panda/linter/test/main/incompatible_function.ets.arkts2.json @@ -203,6 +203,136 @@ "suggest": "", "rule": "Numeric semantics is different for integer values (arkts-numeric-semantic)", "severity": "ERROR" + }, + { + "line": 85, + "column": 23, + "endLine": 85, + "endColumn": 30, + "problem": "IncompationbleFunctionType", + "suggest": "", + "rule": "Stricter assignments into variables of function type (arkts-incompatible-function-types)", + "severity": "ERROR" + }, + { + "line": 85, + "column": 10, + "endLine": 85, + "endColumn": 61, + "problem": "GenericCallNoTypeArgs", + "suggest": "", + "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", + "severity": "ERROR" + }, + { + "line": 85, + "column": 55, + "endLine": 85, + "endColumn": 59, + "problem": "NoTsLikeSmartType", + "suggest": "", + "rule": "Smart type differences (arkts-no-ts-like-smart-type)", + "severity": "ERROR" + }, + { + "line": 87, + "column": 7, + "endLine": 87, + "endColumn": 10, + "problem": "NumericSemantics", + "suggest": "", + "rule": "Numeric semantics is different for integer values (arkts-numeric-semantic)", + "severity": "ERROR" + }, + { + "line": 91, + "column": 7, + "endLine": 91, + "endColumn": 29, + "problem": "NumericSemantics", + "suggest": "", + "rule": "Numeric semantics is different for integer values (arkts-numeric-semantic)", + "severity": "ERROR" + }, + { + "line": 93, + "column": 7, + "endLine": 93, + "endColumn": 27, + "problem": "NumericSemantics", + "suggest": "", + "rule": "Numeric semantics is different for integer values (arkts-numeric-semantic)", + "severity": "ERROR" + }, + { + "line": 93, + "column": 23, + "endLine": 93, + "endColumn": 27, + "problem": "NumericSemantics", + "suggest": "", + "rule": "Numeric semantics is different for integer values (arkts-numeric-semantic)", + "severity": "ERROR" + }, + { + "line": 94, + "column": 7, + "endLine": 94, + "endColumn": 18, + "problem": "NumericSemantics", + "suggest": "", + "rule": "Numeric semantics is different for integer values (arkts-numeric-semantic)", + "severity": "ERROR" + }, + { + "line": 94, + "column": 15, + "endLine": 94, + "endColumn": 18, + "problem": "NumericSemantics", + "suggest": "", + "rule": "Numeric semantics is different for integer values (arkts-numeric-semantic)", + "severity": "ERROR" + }, + { + "line": 95, + "column": 10, + "endLine": 95, + "endColumn": 15, + "problem": "NumericSemantics", + "suggest": "", + "rule": "Numeric semantics is different for integer values (arkts-numeric-semantic)", + "severity": "ERROR" + }, + { + "line": 95, + "column": 14, + "endLine": 95, + "endColumn": 15, + "problem": "NumericSemantics", + "suggest": "", + "rule": "Numeric semantics is different for integer values (arkts-numeric-semantic)", + "severity": "ERROR" + }, + { + "line": 97, + "column": 18, + "endLine": 97, + "endColumn": 27, + "problem": "IncompationbleFunctionType", + "suggest": "", + "rule": "Stricter assignments into variables of function type (arkts-incompatible-function-types)", + "severity": "ERROR" + }, + { + "line": 97, + "column": 5, + "endLine": 101, + "endColumn": 6, + "problem": "GenericCallNoTypeArgs", + "suggest": "", + "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", + "severity": "ERROR" } ] } \ No newline at end of file diff --git a/ets2panda/linter/test/main/incompatible_function.ets.json b/ets2panda/linter/test/main/incompatible_function.ets.json index 2844fc25ba..f031ea6468 100644 --- a/ets2panda/linter/test/main/incompatible_function.ets.json +++ b/ets2panda/linter/test/main/incompatible_function.ets.json @@ -13,5 +13,46 @@ "See the License for the specific language governing permissions and", "limitations under the License." ], - "result": [] + "result": [ + { + "line": 85, + "column": 23, + "endLine": 85, + "endColumn": 30, + "problem": "IncompationbleFunctionType", + "suggest": "", + "rule": "Stricter assignments into variables of function type (arkts-incompatible-function-types)", + "severity": "ERROR" + }, + { + "line": 85, + "column": 10, + "endLine": 85, + "endColumn": 61, + "problem": "GenericCallNoTypeArgs", + "suggest": "", + "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", + "severity": "ERROR" + }, + { + "line": 97, + "column": 18, + "endLine": 97, + "endColumn": 27, + "problem": "IncompationbleFunctionType", + "suggest": "", + "rule": "Stricter assignments into variables of function type (arkts-incompatible-function-types)", + "severity": "ERROR" + }, + { + "line": 97, + "column": 5, + "endLine": 101, + "endColumn": 6, + "problem": "GenericCallNoTypeArgs", + "suggest": "", + "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", + "severity": "ERROR" + } + ] } \ No newline at end of file diff --git a/ets2panda/linter/test/main/structural_identity_promise.ets.arkts2.json b/ets2panda/linter/test/main/structural_identity_promise.ets.arkts2.json index ca88f857e9..20fe94b977 100644 --- a/ets2panda/linter/test/main/structural_identity_promise.ets.arkts2.json +++ b/ets2panda/linter/test/main/structural_identity_promise.ets.arkts2.json @@ -13,5 +13,16 @@ "See the License for the specific language governing permissions and", "limitations under the License." ], - "result": [] + "result": [ + { + "line": 19, + "column": 31, + "endLine": 19, + "endColumn": 40, + "problem": "IncompationbleFunctionType", + "suggest": "", + "rule": "Stricter assignments into variables of function type (arkts-incompatible-function-types)", + "severity": "ERROR" + } + ] } \ No newline at end of file diff --git a/ets2panda/linter/test/main/structural_identity_promise.ets.json b/ets2panda/linter/test/main/structural_identity_promise.ets.json index ca88f857e9..20fe94b977 100644 --- a/ets2panda/linter/test/main/structural_identity_promise.ets.json +++ b/ets2panda/linter/test/main/structural_identity_promise.ets.json @@ -13,5 +13,16 @@ "See the License for the specific language governing permissions and", "limitations under the License." ], - "result": [] + "result": [ + { + "line": 19, + "column": 31, + "endLine": 19, + "endColumn": 40, + "problem": "IncompationbleFunctionType", + "suggest": "", + "rule": "Stricter assignments into variables of function type (arkts-incompatible-function-types)", + "severity": "ERROR" + } + ] } \ No newline at end of file -- Gitee