From 2a2220c4fbac8847642fef118bb9c23ca3b879b5 Mon Sep 17 00:00:00 2001 From: ZhongNing Date: Tue, 1 Jul 2025 20:11:59 +0800 Subject: [PATCH] fix arkts-no-ts-like-function-call Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICICWF Test scenarios: fix bug Signed-off-by: ZhongNing --- ets2panda/linter/src/lib/TypeScriptLinter.ts | 17 +++++++++++++--- .../src/lib/utils/consts/LikeFunction.ts | 2 ++ .../test/main/explicit_function_type.ets | 6 +++++- .../explicit_function_type.ets.arkts2.json | 20 +++++++++++++++++++ .../explicit_function_type.ets.autofix.json | 20 +++++++++++++++++++ .../explicit_function_type.ets.migrate.ets | 6 +++++- .../explicit_function_type.ets.migrate.json | 20 +++++++++++++++++++ 7 files changed, 86 insertions(+), 5 deletions(-) diff --git a/ets2panda/linter/src/lib/TypeScriptLinter.ts b/ets2panda/linter/src/lib/TypeScriptLinter.ts index 0b7067a939..4607f067f2 100644 --- a/ets2panda/linter/src/lib/TypeScriptLinter.ts +++ b/ets2panda/linter/src/lib/TypeScriptLinter.ts @@ -22,7 +22,7 @@ import { Autofixer } from './autofixes/Autofixer'; import { PROMISE_METHODS, SYMBOL, SYMBOL_CONSTRUCTOR, TsUtils } from './utils/TsUtils'; import { FUNCTION_HAS_NO_RETURN_ERROR_CODE } from './utils/consts/FunctionHasNoReturnErrorCode'; import { LIMITED_STANDARD_UTILITY_TYPES } from './utils/consts/LimitedStandardUtilityTypes'; -import { LIKE_FUNCTION } from './utils/consts/LikeFunction'; +import { LIKE_FUNCTION, LIKE_FUNCTION_CONSTRUCTOR } from './utils/consts/LikeFunction'; import { METHOD_DECLARATION } from './utils/consts/MethodDeclaration'; import { METHOD_SIGNATURE } from './utils/consts/MethodSignature'; import { OPTIONAL_METHOD } from './utils/consts/OptionalMethod'; @@ -4547,10 +4547,21 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { const expression = callExpr.expression; const type = this.tsTypeChecker.getTypeAtLocation(expression); const typeText = this.tsTypeChecker.typeToString(type); - if (typeText === LIKE_FUNCTION) { + + if (LIKE_FUNCTION !== typeText) { + return; + } + if (ts.isNewExpression(expression) || ts.isCallExpression(expression)) { + const exprIndentifier = expression.expression; + const typeExprIndent = this.tsTypeChecker.getTypeAtLocation(exprIndentifier); + const typeTextExprIndent = this.tsTypeChecker.typeToString(typeExprIndent); + if (typeTextExprIndent === LIKE_FUNCTION_CONSTRUCTOR) { + this.incrementCounters(expression, FaultID.ExplicitFunctionType); + } + } else { const autofix = this.autofixer?.fixNoTsLikeFunctionCall(callExpr); this.incrementCounters(expression, FaultID.ExplicitFunctionType, autofix); - } + } } private handleAppStorageCallExpression(tsCallExpr: ts.CallExpression): void { diff --git a/ets2panda/linter/src/lib/utils/consts/LikeFunction.ts b/ets2panda/linter/src/lib/utils/consts/LikeFunction.ts index 669b24fd7b..38a013ece2 100755 --- a/ets2panda/linter/src/lib/utils/consts/LikeFunction.ts +++ b/ets2panda/linter/src/lib/utils/consts/LikeFunction.ts @@ -14,3 +14,5 @@ */ export const LIKE_FUNCTION = 'Function'; + +export const LIKE_FUNCTION_CONSTRUCTOR = 'FunctionConstructor'; diff --git a/ets2panda/linter/test/main/explicit_function_type.ets b/ets2panda/linter/test/main/explicit_function_type.ets index c42f90e3fb..9896f87a36 100755 --- a/ets2panda/linter/test/main/explicit_function_type.ets +++ b/ets2panda/linter/test/main/explicit_function_type.ets @@ -177,4 +177,8 @@ fn29[1](); SysApiWrapper.setTimeout = (handler: Function | string, delay?: number): number => { (handler as Function)?.(); return Math.random(); -} \ No newline at end of file +} + +new Function('return 42')(); + +Function('return 42')(); \ 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 3427352803..1bafbdd1de 100755 --- a/ets2panda/linter/test/main/explicit_function_type.ets.arkts2.json +++ b/ets2panda/linter/test/main/explicit_function_type.ets.arkts2.json @@ -353,6 +353,26 @@ "suggest": "", "rule": "The function type should be explicit (arkts-no-ts-like-function-call)", "severity": "ERROR" + }, + { + "line": 182, + "column": 1, + "endLine": 182, + "endColumn": 26, + "problem": "ExplicitFunctionType", + "suggest": "", + "rule": "The function type should be explicit (arkts-no-ts-like-function-call)", + "severity": "ERROR" + }, + { + "line": 184, + "column": 1, + "endLine": 184, + "endColumn": 22, + "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 9512b29aac..23f1af4921 100644 --- a/ets2panda/linter/test/main/explicit_function_type.ets.autofix.json +++ b/ets2panda/linter/test/main/explicit_function_type.ets.autofix.json @@ -573,6 +573,26 @@ "suggest": "", "rule": "The function type should be explicit (arkts-no-ts-like-function-call)", "severity": "ERROR" + }, + { + "line": 182, + "column": 1, + "endLine": 182, + "endColumn": 26, + "problem": "ExplicitFunctionType", + "suggest": "", + "rule": "The function type should be explicit (arkts-no-ts-like-function-call)", + "severity": "ERROR" + }, + { + "line": 184, + "column": 1, + "endLine": 184, + "endColumn": 22, + "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.migrate.ets b/ets2panda/linter/test/main/explicit_function_type.ets.migrate.ets index c5ecffb429..b9ac8ffe17 100644 --- a/ets2panda/linter/test/main/explicit_function_type.ets.migrate.ets +++ b/ets2panda/linter/test/main/explicit_function_type.ets.migrate.ets @@ -177,4 +177,8 @@ fn29[1].unsafeCall(); SysApiWrapper.setTimeout = (handler: Function | string, delay?: number): number => { (handler as Function)?.unsafeCall(); return Math.random(); -} \ No newline at end of file +} + +new Function('return 42')(); + +Function('return 42')(); \ No newline at end of file diff --git a/ets2panda/linter/test/main/explicit_function_type.ets.migrate.json b/ets2panda/linter/test/main/explicit_function_type.ets.migrate.json index b4e8db486e..e49a4f78fd 100644 --- a/ets2panda/linter/test/main/explicit_function_type.ets.migrate.json +++ b/ets2panda/linter/test/main/explicit_function_type.ets.migrate.json @@ -153,6 +153,26 @@ "suggest": "", "rule": "Array bound not checked. (arkts-runtime-array-check)", "severity": "ERROR" + }, + { + "line": 182, + "column": 1, + "endLine": 182, + "endColumn": 26, + "problem": "ExplicitFunctionType", + "suggest": "", + "rule": "The function type should be explicit (arkts-no-ts-like-function-call)", + "severity": "ERROR" + }, + { + "line": 184, + "column": 1, + "endLine": 184, + "endColumn": 22, + "problem": "ExplicitFunctionType", + "suggest": "", + "rule": "The function type should be explicit (arkts-no-ts-like-function-call)", + "severity": "ERROR" } ] } \ No newline at end of file -- Gitee