From 676b5e7bcba3a25e98dcf4224398dacd54ee0543 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=92=9F=E6=9F=A0?= Date: Wed, 28 May 2025 19:22:28 +0800 Subject: [PATCH] Fix arkts-interop-js2s-js-call-static-func MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Issue:https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICAXBW Test scenarios:Fix bugs for arkts-interop-js2s-js-call-static-func Signed-off-by: 钟柠 --- ets2panda/linter/src/lib/TypeScriptLinter.ts | 29 ++++++--- .../interop/call_function.ets.arkts2.json | 20 ------ .../call_object_methods.ets.arkts2.json | 10 --- .../call_object_methods.ets.autofix.json | 10 --- .../test/interop/interop_import_js_rules.ets | 10 +++ .../interop_import_js_rules.ets.arkts2.json | 62 ++++++++++++++----- .../interop_import_js_rules.ets.autofix.json | 62 ++++++++++++++----- .../interop_import_typeof_js.ets.arkts2.json | 60 ------------------ .../interop_import_typeof_js.ets.autofix.json | 60 ------------------ .../no_await_js_promise.ets.arkts2.json | 40 ------------ .../interop/no_js_instanceof.ets.arkts2.json | 20 ------ 11 files changed, 122 insertions(+), 261 deletions(-) diff --git a/ets2panda/linter/src/lib/TypeScriptLinter.ts b/ets2panda/linter/src/lib/TypeScriptLinter.ts index 309d60b1b1..d5700efe95 100644 --- a/ets2panda/linter/src/lib/TypeScriptLinter.ts +++ b/ets2panda/linter/src/lib/TypeScriptLinter.ts @@ -7795,16 +7795,27 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { return; } - // check if any argument is a `new` expression - const hasNewExpressionArg = callExpr.arguments.some((arg) => { - return ts.isNewExpression(arg); + callExpr.arguments.forEach((arg) => { + const type = this.tsTypeChecker.getTypeAtLocation(arg); + if (ts.isArrowFunction(arg)) { + this.incrementCounters(arg, FaultID.InteropJsObjectCallStaticFunc); + } else if (ts.isIdentifier(arg)) { + const sym = this.tsTypeChecker.getSymbolAtLocation(arg); + const decl = sym?.declarations?.[0]; + if ( + decl && + (ts.isFunctionDeclaration(decl) || + ts.isVariableDeclaration(decl) && decl.initializer && ts.isArrowFunction(decl.initializer)) + ) { + this.incrementCounters(arg, FaultID.InteropJsObjectCallStaticFunc); + } + if (type?.isClassOrInterface()) { + this.incrementCounters(arg, FaultID.InteropJsObjectExpandStaticInstance); + } + } else if (ts.isObjectLiteralExpression(arg) || type?.isClassOrInterface()) { + this.incrementCounters(arg, FaultID.InteropJsObjectExpandStaticInstance); + } }); - - const faultId = hasNewExpressionArg ? - FaultID.InteropJsObjectExpandStaticInstance : - FaultID.InteropJsObjectCallStaticFunc; - - this.incrementCounters(callExpr, faultId); } private fixJsImportExtendsClass( diff --git a/ets2panda/linter/test/interop/call_function.ets.arkts2.json b/ets2panda/linter/test/interop/call_function.ets.arkts2.json index 75b5fe5523..ed94001123 100644 --- a/ets2panda/linter/test/interop/call_function.ets.arkts2.json +++ b/ets2panda/linter/test/interop/call_function.ets.arkts2.json @@ -34,16 +34,6 @@ "rule": "Importing directly from \"JS\" module is not supported (arkts-interop-js2s-import-js)", "severity": "ERROR" }, - { - "line": 18, - "column": 1, - "endLine": 18, - "endColumn": 6, - "problem": "InteropJsObjectCallStaticFunc", - "suggest": "", - "rule": "Direct usage of interop JS functions is not supported (arkts-interop-js2s-js-call-static-function)", - "severity": "ERROR" - }, { "line": 18, "column": 1, @@ -54,16 +44,6 @@ "rule": "ArkTS directly call JS functions or parameters is not supported (arkts-interop-js2s-call-js-func)", "severity": "ERROR" }, - { - "line": 19, - "column": 1, - "endLine": 19, - "endColumn": 9, - "problem": "InteropJsObjectCallStaticFunc", - "suggest": "", - "rule": "Direct usage of interop JS functions is not supported (arkts-interop-js2s-js-call-static-function)", - "severity": "ERROR" - }, { "line": 19, "column": 1, diff --git a/ets2panda/linter/test/interop/call_object_methods.ets.arkts2.json b/ets2panda/linter/test/interop/call_object_methods.ets.arkts2.json index 08caa2a29e..d088101b49 100644 --- a/ets2panda/linter/test/interop/call_object_methods.ets.arkts2.json +++ b/ets2panda/linter/test/interop/call_object_methods.ets.arkts2.json @@ -34,16 +34,6 @@ "rule": "Importing directly from \"JS\" module is not supported (arkts-interop-js2s-import-js)", "severity": "ERROR" }, - { - "line": 18, - "column": 1, - "endLine": 18, - "endColumn": 13, - "problem": "InteropJsObjectCallStaticFunc", - "suggest": "", - "rule": "Direct usage of interop JS functions is not supported (arkts-interop-js2s-js-call-static-function)", - "severity": "ERROR" - }, { "line": 18, "column": 1, diff --git a/ets2panda/linter/test/interop/call_object_methods.ets.autofix.json b/ets2panda/linter/test/interop/call_object_methods.ets.autofix.json index 5b9ab33651..7a42f85097 100644 --- a/ets2panda/linter/test/interop/call_object_methods.ets.autofix.json +++ b/ets2panda/linter/test/interop/call_object_methods.ets.autofix.json @@ -54,16 +54,6 @@ "rule": "Importing directly from \"JS\" module is not supported (arkts-interop-js2s-import-js)", "severity": "ERROR" }, - { - "line": 18, - "column": 1, - "endLine": 18, - "endColumn": 13, - "problem": "InteropJsObjectCallStaticFunc", - "suggest": "", - "rule": "Direct usage of interop JS functions is not supported (arkts-interop-js2s-js-call-static-function)", - "severity": "ERROR" - }, { "line": 18, "column": 1, diff --git a/ets2panda/linter/test/interop/interop_import_js_rules.ets b/ets2panda/linter/test/interop/interop_import_js_rules.ets index 3b88109fc2..38872997a7 100644 --- a/ets2panda/linter/test/interop/interop_import_js_rules.ets +++ b/ets2panda/linter/test/interop/interop_import_js_rules.ets @@ -65,3 +65,13 @@ handle(lambda) class X{a = 1; b= 2; c= 3} expand(new X()) // ERROR expand-static +class Y { + str: string = 'str'; + bool: boolean = false; +} +let testY: Y = { + str: "hello", + bool: false, +} +expand(testY); +expand({x: '1', y: "hello", z: false}); \ No newline at end of file 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 eb568c3a7c..d08c712683 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 @@ -264,16 +264,6 @@ "rule": "Trying to catch JS errors is not permitted (arkts-interop-js2s-js-exception)", "severity": "ERROR" }, - { - "line": 44, - "column": 3, - "endLine": 44, - "endColumn": 8, - "problem": "InteropJsObjectCallStaticFunc", - "suggest": "", - "rule": "Direct usage of interop JS functions is not supported (arkts-interop-js2s-js-call-static-function)", - "severity": "ERROR" - }, { "line": 44, "column": 3, @@ -416,9 +406,9 @@ }, { "line": 63, - "column": 1, + "column": 8, "endLine": 63, - "endColumn": 13, + "endColumn": 12, "problem": "InteropJsObjectCallStaticFunc", "suggest": "", "rule": "Direct usage of interop JS functions is not supported (arkts-interop-js2s-js-call-static-function)", @@ -436,9 +426,9 @@ }, { "line": 64, - "column": 1, + "column": 8, "endLine": 64, - "endColumn": 15, + "endColumn": 14, "problem": "InteropJsObjectCallStaticFunc", "suggest": "", "rule": "Direct usage of interop JS functions is not supported (arkts-interop-js2s-js-call-static-function)", @@ -516,9 +506,9 @@ }, { "line": 67, - "column": 1, + "column": 8, "endLine": 67, - "endColumn": 16, + "endColumn": 15, "problem": "InteropJsObjectExpandStaticInstance", "suggest": "", "rule": "Direct usage of interop JS functions is not supported (arkts-interop-js2s-js-expand-static-instance)", @@ -533,6 +523,46 @@ "suggest": "", "rule": "ArkTS directly call JS functions or parameters is not supported (arkts-interop-js2s-call-js-func)", "severity": "ERROR" + }, + { + "line": 76, + "column": 8, + "endLine": 76, + "endColumn": 13, + "problem": "InteropJsObjectExpandStaticInstance", + "suggest": "", + "rule": "Direct usage of interop JS functions is not supported (arkts-interop-js2s-js-expand-static-instance)", + "severity": "ERROR" + }, + { + "line": 76, + "column": 1, + "endLine": 76, + "endColumn": 14, + "problem": "CallJSFunction", + "suggest": "", + "rule": "ArkTS directly call JS functions or parameters is not supported (arkts-interop-js2s-call-js-func)", + "severity": "ERROR" + }, + { + "line": 77, + "column": 8, + "endLine": 77, + "endColumn": 38, + "problem": "InteropJsObjectExpandStaticInstance", + "suggest": "", + "rule": "Direct usage of interop JS functions is not supported (arkts-interop-js2s-js-expand-static-instance)", + "severity": "ERROR" + }, + { + "line": 77, + "column": 1, + "endLine": 77, + "endColumn": 39, + "problem": "CallJSFunction", + "suggest": "", + "rule": "ArkTS directly call JS functions or parameters is not supported (arkts-interop-js2s-call-js-func)", + "severity": "ERROR" } ] } 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 b9eb9db8fb..a8f273bd7d 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 @@ -479,16 +479,6 @@ "rule": "Trying to catch JS errors is not permitted (arkts-interop-js2s-js-exception)", "severity": "ERROR" }, - { - "line": 44, - "column": 3, - "endLine": 44, - "endColumn": 8, - "problem": "InteropJsObjectCallStaticFunc", - "suggest": "", - "rule": "Direct usage of interop JS functions is not supported (arkts-interop-js2s-js-call-static-function)", - "severity": "ERROR" - }, { "line": 44, "column": 3, @@ -741,9 +731,9 @@ }, { "line": 63, - "column": 1, + "column": 8, "endLine": 63, - "endColumn": 13, + "endColumn": 12, "problem": "InteropJsObjectCallStaticFunc", "suggest": "", "rule": "Direct usage of interop JS functions is not supported (arkts-interop-js2s-js-call-static-function)", @@ -761,9 +751,9 @@ }, { "line": 64, - "column": 1, + "column": 8, "endLine": 64, - "endColumn": 15, + "endColumn": 14, "problem": "InteropJsObjectCallStaticFunc", "suggest": "", "rule": "Direct usage of interop JS functions is not supported (arkts-interop-js2s-js-call-static-function)", @@ -907,9 +897,9 @@ }, { "line": 67, - "column": 1, + "column": 8, "endLine": 67, - "endColumn": 16, + "endColumn": 15, "problem": "InteropJsObjectExpandStaticInstance", "suggest": "", "rule": "Direct usage of interop JS functions is not supported (arkts-interop-js2s-js-expand-static-instance)", @@ -924,6 +914,46 @@ "suggest": "", "rule": "ArkTS directly call JS functions or parameters is not supported (arkts-interop-js2s-call-js-func)", "severity": "ERROR" + }, + { + "line": 76, + "column": 8, + "endLine": 76, + "endColumn": 13, + "problem": "InteropJsObjectExpandStaticInstance", + "suggest": "", + "rule": "Direct usage of interop JS functions is not supported (arkts-interop-js2s-js-expand-static-instance)", + "severity": "ERROR" + }, + { + "line": 76, + "column": 1, + "endLine": 76, + "endColumn": 14, + "problem": "CallJSFunction", + "suggest": "", + "rule": "ArkTS directly call JS functions or parameters is not supported (arkts-interop-js2s-call-js-func)", + "severity": "ERROR" + }, + { + "line": 77, + "column": 8, + "endLine": 77, + "endColumn": 38, + "problem": "InteropJsObjectExpandStaticInstance", + "suggest": "", + "rule": "Direct usage of interop JS functions is not supported (arkts-interop-js2s-js-expand-static-instance)", + "severity": "ERROR" + }, + { + "line": 77, + "column": 1, + "endLine": 77, + "endColumn": 39, + "problem": "CallJSFunction", + "suggest": "", + "rule": "ArkTS directly call JS functions or parameters is not supported (arkts-interop-js2s-call-js-func)", + "severity": "ERROR" } ] } diff --git a/ets2panda/linter/test/interop/interop_import_typeof_js.ets.arkts2.json b/ets2panda/linter/test/interop/interop_import_typeof_js.ets.arkts2.json index 0689065be9..454dd0994b 100755 --- a/ets2panda/linter/test/interop/interop_import_typeof_js.ets.arkts2.json +++ b/ets2panda/linter/test/interop/interop_import_typeof_js.ets.arkts2.json @@ -84,16 +84,6 @@ "rule": "The \"typeof\" expression can't be used with interop JS objects (arkts-interop-js2s-typeof-js-type)", "severity": "ERROR" }, - { - "line": 21, - "column": 8, - "endLine": 21, - "endColumn": 15, - "problem": "InteropJsObjectCallStaticFunc", - "suggest": "", - "rule": "Direct usage of interop JS functions is not supported (arkts-interop-js2s-js-call-static-function)", - "severity": "ERROR" - }, { "line": 21, "column": 8, @@ -104,16 +94,6 @@ "rule": "ArkTS directly call JS functions or parameters is not supported (arkts-interop-js2s-call-js-func)", "severity": "ERROR" }, - { - "line": 22, - "column": 11, - "endLine": 22, - "endColumn": 18, - "problem": "InteropJsObjectCallStaticFunc", - "suggest": "", - "rule": "Direct usage of interop JS functions is not supported (arkts-interop-js2s-js-call-static-function)", - "severity": "ERROR" - }, { "line": 22, "column": 11, @@ -154,16 +134,6 @@ "rule": "Type \"void\" has no instances.(arkts-limited-void-type)", "severity": "ERROR" }, - { - "line": 25, - "column": 8, - "endLine": 25, - "endColumn": 19, - "problem": "InteropJsObjectCallStaticFunc", - "suggest": "", - "rule": "Direct usage of interop JS functions is not supported (arkts-interop-js2s-js-call-static-function)", - "severity": "ERROR" - }, { "line": 25, "column": 8, @@ -194,16 +164,6 @@ "rule": "Type \"void\" has no instances.(arkts-limited-void-type)", "severity": "ERROR" }, - { - "line": 26, - "column": 8, - "endLine": 26, - "endColumn": 20, - "problem": "InteropJsObjectCallStaticFunc", - "suggest": "", - "rule": "Direct usage of interop JS functions is not supported (arkts-interop-js2s-js-call-static-function)", - "severity": "ERROR" - }, { "line": 26, "column": 8, @@ -554,16 +514,6 @@ "rule": "The \"typeof\" expression can't be used with interop JS objects (arkts-interop-js2s-typeof-js-type)", "severity": "ERROR" }, - { - "line": 72, - "column": 8, - "endLine": 72, - "endColumn": 30, - "problem": "InteropJsObjectCallStaticFunc", - "suggest": "", - "rule": "Direct usage of interop JS functions is not supported (arkts-interop-js2s-js-call-static-function)", - "severity": "ERROR" - }, { "line": 72, "column": 8, @@ -604,16 +554,6 @@ "rule": "Type \"void\" has no instances.(arkts-limited-void-type)", "severity": "ERROR" }, - { - "line": 73, - "column": 8, - "endLine": 73, - "endColumn": 31, - "problem": "InteropJsObjectCallStaticFunc", - "suggest": "", - "rule": "Direct usage of interop JS functions is not supported (arkts-interop-js2s-js-call-static-function)", - "severity": "ERROR" - }, { "line": 73, "column": 8, diff --git a/ets2panda/linter/test/interop/interop_import_typeof_js.ets.autofix.json b/ets2panda/linter/test/interop/interop_import_typeof_js.ets.autofix.json index c18bdee176..afe18d8b10 100755 --- a/ets2panda/linter/test/interop/interop_import_typeof_js.ets.autofix.json +++ b/ets2panda/linter/test/interop/interop_import_typeof_js.ets.autofix.json @@ -155,16 +155,6 @@ "rule": "The \"typeof\" expression can't be used with interop JS objects (arkts-interop-js2s-typeof-js-type)", "severity": "ERROR" }, - { - "line": 21, - "column": 8, - "endLine": 21, - "endColumn": 15, - "problem": "InteropJsObjectCallStaticFunc", - "suggest": "", - "rule": "Direct usage of interop JS functions is not supported (arkts-interop-js2s-js-call-static-function)", - "severity": "ERROR" - }, { "line": 21, "column": 8, @@ -175,16 +165,6 @@ "rule": "ArkTS directly call JS functions or parameters is not supported (arkts-interop-js2s-call-js-func)", "severity": "ERROR" }, - { - "line": 22, - "column": 11, - "endLine": 22, - "endColumn": 18, - "problem": "InteropJsObjectCallStaticFunc", - "suggest": "", - "rule": "Direct usage of interop JS functions is not supported (arkts-interop-js2s-js-call-static-function)", - "severity": "ERROR" - }, { "line": 22, "column": 11, @@ -247,16 +227,6 @@ "rule": "Type \"void\" has no instances.(arkts-limited-void-type)", "severity": "ERROR" }, - { - "line": 25, - "column": 8, - "endLine": 25, - "endColumn": 19, - "problem": "InteropJsObjectCallStaticFunc", - "suggest": "", - "rule": "Direct usage of interop JS functions is not supported (arkts-interop-js2s-js-call-static-function)", - "severity": "ERROR" - }, { "line": 25, "column": 8, @@ -298,16 +268,6 @@ "rule": "Type \"void\" has no instances.(arkts-limited-void-type)", "severity": "ERROR" }, - { - "line": 26, - "column": 8, - "endLine": 26, - "endColumn": 20, - "problem": "InteropJsObjectCallStaticFunc", - "suggest": "", - "rule": "Direct usage of interop JS functions is not supported (arkts-interop-js2s-js-call-static-function)", - "severity": "ERROR" - }, { "line": 26, "column": 8, @@ -955,16 +915,6 @@ "rule": "The \"typeof\" expression can't be used with interop JS objects (arkts-interop-js2s-typeof-js-type)", "severity": "ERROR" }, - { - "line": 72, - "column": 8, - "endLine": 72, - "endColumn": 30, - "problem": "InteropJsObjectCallStaticFunc", - "suggest": "", - "rule": "Direct usage of interop JS functions is not supported (arkts-interop-js2s-js-call-static-function)", - "severity": "ERROR" - }, { "line": 72, "column": 8, @@ -1027,16 +977,6 @@ "rule": "Type \"void\" has no instances.(arkts-limited-void-type)", "severity": "ERROR" }, - { - "line": 73, - "column": 8, - "endLine": 73, - "endColumn": 31, - "problem": "InteropJsObjectCallStaticFunc", - "suggest": "", - "rule": "Direct usage of interop JS functions is not supported (arkts-interop-js2s-js-call-static-function)", - "severity": "ERROR" - }, { "line": 73, "column": 8, diff --git a/ets2panda/linter/test/interop/no_await_js_promise.ets.arkts2.json b/ets2panda/linter/test/interop/no_await_js_promise.ets.arkts2.json index 54dff3eb92..5021d02c43 100755 --- a/ets2panda/linter/test/interop/no_await_js_promise.ets.arkts2.json +++ b/ets2panda/linter/test/interop/no_await_js_promise.ets.arkts2.json @@ -54,16 +54,6 @@ "rule": "\"Await\" operator can't be used with interop objects (arkts-interop-js2s-await-js-promise)", "severity": "ERROR" }, - { - "line": 24, - "column": 16, - "endLine": 24, - "endColumn": 21, - "problem": "InteropJsObjectCallStaticFunc", - "suggest": "", - "rule": "Direct usage of interop JS functions is not supported (arkts-interop-js2s-js-call-static-function)", - "severity": "ERROR" - }, { "line": 24, "column": 16, @@ -94,16 +84,6 @@ "rule": "\"Await\" operator can't be used with interop objects (arkts-interop-js2s-await-js-promise)", "severity": "ERROR" }, - { - "line": 32, - "column": 16, - "endLine": 32, - "endColumn": 27, - "problem": "InteropJsObjectCallStaticFunc", - "suggest": "", - "rule": "Direct usage of interop JS functions is not supported (arkts-interop-js2s-js-call-static-function)", - "severity": "ERROR" - }, { "line": 32, "column": 16, @@ -184,16 +164,6 @@ "rule": "\"Await\" operator can't be used with interop objects (arkts-interop-js2s-await-js-promise)", "severity": "ERROR" }, - { - "line": 55, - "column": 18, - "endLine": 55, - "endColumn": 23, - "problem": "InteropJsObjectCallStaticFunc", - "suggest": "", - "rule": "Direct usage of interop JS functions is not supported (arkts-interop-js2s-js-call-static-function)", - "severity": "ERROR" - }, { "line": 55, "column": 18, @@ -234,16 +204,6 @@ "rule": "\"Await\" operator can't be used with interop objects (arkts-interop-js2s-await-js-promise)", "severity": "ERROR" }, - { - "line": 64, - "column": 43, - "endLine": 64, - "endColumn": 54, - "problem": "InteropJsObjectCallStaticFunc", - "suggest": "", - "rule": "Direct usage of interop JS functions is not supported (arkts-interop-js2s-js-call-static-function)", - "severity": "ERROR" - }, { "line": 64, "column": 43, diff --git a/ets2panda/linter/test/interop/no_js_instanceof.ets.arkts2.json b/ets2panda/linter/test/interop/no_js_instanceof.ets.arkts2.json index 7f34d1cc41..6df8274a39 100755 --- a/ets2panda/linter/test/interop/no_js_instanceof.ets.arkts2.json +++ b/ets2panda/linter/test/interop/no_js_instanceof.ets.arkts2.json @@ -64,16 +64,6 @@ "rule": "Usage of \"instanceof\" operator is not allowed with interop objects (arkts-interop-js2s-instanceof-js-type)", "severity": "ERROR" }, - { - "line": 39, - "column": 28, - "endLine": 39, - "endColumn": 50, - "problem": "InteropJsObjectCallStaticFunc", - "suggest": "", - "rule": "Direct usage of interop JS functions is not supported (arkts-interop-js2s-js-call-static-function)", - "severity": "ERROR" - }, { "line": 39, "column": 28, @@ -154,16 +144,6 @@ "rule": "Usage of \"instanceof\" operator is not allowed with interop objects (arkts-interop-js2s-instanceof-js-type)", "severity": "ERROR" }, - { - "line": 69, - "column": 4, - "endLine": 69, - "endColumn": 7, - "problem": "InteropJsObjectCallStaticFunc", - "suggest": "", - "rule": "Direct usage of interop JS functions is not supported (arkts-interop-js2s-js-call-static-function)", - "severity": "ERROR" - }, { "line": 69, "column": 4, -- Gitee