From 20406b4c83ee51f2fc88ec744947ae1519988b47 Mon Sep 17 00:00:00 2001 From: zhongning Date: Thu, 19 Jun 2025 15:09:19 +0800 Subject: [PATCH] fix bug for arkts-no-ts-like-function-call Issue:https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICG62V Test scenarios:fix bug Signed-off-by: zhongning --- ets2panda/linter/src/lib/TypeScriptLinter.ts | 2 +- .../linter/src/lib/autofixes/Autofixer.ts | 25 +++++++------ .../test/main/explicit_function_type.ets | 7 +++- .../explicit_function_type.ets.arkts2.json | 10 ++++++ .../explicit_function_type.ets.autofix.json | 35 +++++++++++++++---- .../explicit_function_type.ets.migrate.ets | 7 +++- 6 files changed, 66 insertions(+), 20 deletions(-) diff --git a/ets2panda/linter/src/lib/TypeScriptLinter.ts b/ets2panda/linter/src/lib/TypeScriptLinter.ts index 7a6dd23d96..f45e6c47ed 100644 --- a/ets2panda/linter/src/lib/TypeScriptLinter.ts +++ b/ets2panda/linter/src/lib/TypeScriptLinter.ts @@ -4392,7 +4392,7 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { const type = this.tsTypeChecker.getTypeAtLocation(expression); const typeText = this.tsTypeChecker.typeToString(type); if (typeText === LIKE_FUNCTION) { - const autofix = this.autofixer?.fixNoTsLikeFunctionCall(expression); + const autofix = this.autofixer?.fixNoTsLikeFunctionCall(callExpr); this.incrementCounters(expression, FaultID.ExplicitFunctionType, autofix); } } diff --git a/ets2panda/linter/src/lib/autofixes/Autofixer.ts b/ets2panda/linter/src/lib/autofixes/Autofixer.ts index dc78eabc87..5cb783b140 100644 --- a/ets2panda/linter/src/lib/autofixes/Autofixer.ts +++ b/ets2panda/linter/src/lib/autofixes/Autofixer.ts @@ -4627,17 +4627,22 @@ export class Autofixer { return [{ start: argExpr.getStart(), end: argExpr.getEnd(), replacementText: `${argExpr.getText()} as int` }]; } - fixNoTsLikeFunctionCall(identifier: ts.Node): Autofix[] { + fixNoTsLikeFunctionCall(callExpr: ts.CallExpression): Autofix[] { void this; - const funcName = identifier.getText(); - const replacementText = `${funcName}.unsafeCall`; - return [ - { - replacementText, - start: identifier.getStart(), - end: identifier.getEnd() - } - ]; + const expr = callExpr.expression; + const hasOptionalChain = !!callExpr.questionDotToken; + + const replacementText = hasOptionalChain + ? `${expr.getText()}${callExpr.questionDotToken.getText()}unsafeCall` + : `${expr.getText()}.unsafeCall`; + + return [{ + start: expr.getStart(), + end: hasOptionalChain + ? callExpr.questionDotToken.getEnd() + : expr.getEnd(), + replacementText + }]; } private static createBuiltInTypeInitializer(type: ts.TypeReferenceNode): ts.Expression | undefined { diff --git a/ets2panda/linter/test/main/explicit_function_type.ets b/ets2panda/linter/test/main/explicit_function_type.ets index 9b44052197..c42f90e3fb 100755 --- a/ets2panda/linter/test/main/explicit_function_type.ets +++ b/ets2panda/linter/test/main/explicit_function_type.ets @@ -172,4 +172,9 @@ let ab3 = new AB3(); ab3.fn3(); const fn29: Function[] = []; -fn29[1](); \ No newline at end of file +fn29[1](); + +SysApiWrapper.setTimeout = (handler: Function | string, delay?: number): number => { + (handler as Function)?.(); + return Math.random(); +} \ No newline at end of file diff --git a/ets2panda/linter/test/main/explicit_function_type.ets.arkts2.json b/ets2panda/linter/test/main/explicit_function_type.ets.arkts2.json index 54fab7a23c..3427352803 100755 --- a/ets2panda/linter/test/main/explicit_function_type.ets.arkts2.json +++ b/ets2panda/linter/test/main/explicit_function_type.ets.arkts2.json @@ -343,6 +343,16 @@ "suggest": "", "rule": "Array bound not checked. (arkts-runtime-array-check)", "severity": "ERROR" + }, + { + "line": 178, + "column": 3, + "endLine": 178, + "endColumn": 24, + "problem": "ExplicitFunctionType", + "suggest": "", + "rule": "The function type should be explicit (arkts-no-ts-like-function-call)", + "severity": "ERROR" } ] } \ No newline at end of file diff --git a/ets2panda/linter/test/main/explicit_function_type.ets.autofix.json b/ets2panda/linter/test/main/explicit_function_type.ets.autofix.json index a9ddde793c..9512b29aac 100644 --- a/ets2panda/linter/test/main/explicit_function_type.ets.autofix.json +++ b/ets2panda/linter/test/main/explicit_function_type.ets.autofix.json @@ -106,9 +106,9 @@ "problem": "ExplicitFunctionType", "autofix": [ { - "replacementText": "fn.unsafeCall", "start": 1572, "end": 1574, + "replacementText": "fn.unsafeCall", "line": 57, "column": 3, "endLine": 57, @@ -374,9 +374,9 @@ "problem": "ExplicitFunctionType", "autofix": [ { - "replacementText": "callback.unsafeCall", "start": 2921, "end": 2929, + "replacementText": "callback.unsafeCall", "line": 139, "column": 3, "endLine": 139, @@ -446,9 +446,9 @@ "problem": "ExplicitFunctionType", "autofix": [ { - "replacementText": "curr.unsafeCall", "start": 3458, "end": 3462, + "replacementText": "curr.unsafeCall", "line": 160, "column": 62, "endLine": 160, @@ -467,9 +467,9 @@ "problem": "ExplicitFunctionType", "autofix": [ { - "replacementText": "prev.unsafeCall", "start": 3463, "end": 3467, + "replacementText": "prev.unsafeCall", "line": 160, "column": 67, "endLine": 160, @@ -488,9 +488,9 @@ "problem": "ExplicitFunctionType", "autofix": [ { - "replacementText": "f1.unsafeCall", "start": 3586, "end": 3588, + "replacementText": "f1.unsafeCall", "line": 166, "column": 1, "endLine": 166, @@ -509,9 +509,9 @@ "problem": "ExplicitFunctionType", "autofix": [ { - "replacementText": "ab3.fn3.unsafeCall", "start": 3656, "end": 3663, + "replacementText": "ab3.fn3.unsafeCall", "line": 172, "column": 1, "endLine": 172, @@ -530,9 +530,9 @@ "problem": "ExplicitFunctionType", "autofix": [ { - "replacementText": "fn29[1].unsafeCall", "start": 3697, "end": 3704, + "replacementText": "fn29[1].unsafeCall", "line": 175, "column": 1, "endLine": 175, @@ -552,6 +552,27 @@ "suggest": "", "rule": "Array bound not checked. (arkts-runtime-array-check)", "severity": "ERROR" + }, + { + "line": 178, + "column": 3, + "endLine": 178, + "endColumn": 24, + "problem": "ExplicitFunctionType", + "autofix": [ + { + "start": 3796, + "end": 3819, + "replacementText": "(handler as Function)?.unsafeCall", + "line": 178, + "column": 3, + "endLine": 178, + "endColumn": 24 + } + ], + "suggest": "", + "rule": "The function type should be explicit (arkts-no-ts-like-function-call)", + "severity": "ERROR" } ] } \ No newline at end of file diff --git a/ets2panda/linter/test/main/explicit_function_type.ets.migrate.ets b/ets2panda/linter/test/main/explicit_function_type.ets.migrate.ets index 7d20e3f229..c5ecffb429 100644 --- a/ets2panda/linter/test/main/explicit_function_type.ets.migrate.ets +++ b/ets2panda/linter/test/main/explicit_function_type.ets.migrate.ets @@ -172,4 +172,9 @@ let ab3 = new AB3(); ab3.fn3.unsafeCall(); const fn29: Function[] = []; -fn29[1].unsafeCall(); \ No newline at end of file +fn29[1].unsafeCall(); + +SysApiWrapper.setTimeout = (handler: Function | string, delay?: number): number => { + (handler as Function)?.unsafeCall(); + return Math.random(); +} \ No newline at end of file -- Gitee