diff --git a/ets2panda/linter/src/lib/TypeScriptLinter.ts b/ets2panda/linter/src/lib/TypeScriptLinter.ts index 17e696930bad3bcaa73cee91459fd3fade113e20..b95c1ac8269b5508a038ca0aff55674e078cd179 100644 --- a/ets2panda/linter/src/lib/TypeScriptLinter.ts +++ b/ets2panda/linter/src/lib/TypeScriptLinter.ts @@ -9424,11 +9424,6 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { return; } - // Guard: left side must be applicationContext - if (!this.isApplicationContext(callExpr)) { - return; - } - // Guard: exactly two arguments const args = callExpr.arguments; if (args.length !== 2) { @@ -9456,6 +9451,11 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { return; } + // Guard: receiver must originate from appMgr.getApplicationContext() + if (!this.isApplicationContext(callExpr)) { + return; + } + // Report the legacy callback usage this.incrementCounters(callExpr, FaultID.SdkAbilityLifecycleMonitor); } @@ -9468,8 +9468,28 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { private isApplicationContext(node: ts.CallExpression): boolean { void this; - const left = (node.expression as ts.PropertyAccessExpression).expression; - return ts.isIdentifier(left) && left.text === 'applicationContext'; + // Identify the receiver identifier of `.on` + const expr = node.expression as ts.PropertyAccessExpression; + const receiver = expr.expression; + if (!ts.isIdentifier(receiver)) { + return false; + } + // Resolve its variable declaration + const varDecl = this.findVariableDeclaration(receiver); + if ( + varDecl?.initializer && + ts.isCallExpression(varDecl.initializer) && + ts.isPropertyAccessExpression(varDecl.initializer.expression) + ) { + const callProp = varDecl.initializer.expression; + // Must be appMgr.getApplicationContext() + return ( + ts.isIdentifier(callProp.expression) && + callProp.expression.text === 'appMgr' && + callProp.name.text === 'getApplicationContext' + ); + } + return false; } private handleForOfJsArray(node: ts.ForOfStatement): void { diff --git a/ets2panda/linter/test/main/sdk_ability_lifecycle_monitor.ets b/ets2panda/linter/test/main/sdk_ability_lifecycle_monitor.ets index a204f773197277e1b47b3e819c5e992938846f32..5f92ac578e341b1111ace04259d65fba95efa319 100644 --- a/ets2panda/linter/test/main/sdk_ability_lifecycle_monitor.ets +++ b/ets2panda/linter/test/main/sdk_ability_lifecycle_monitor.ets @@ -25,9 +25,27 @@ class MyAbilityStage extends AbilityStage { } } let applicationContext = appMgr.getApplicationContext(); + let context = appMgr.getApplicationContext(); try { // 2.通过applicationContext注册监听应用内生命周期 lifecycleId = applicationContext.on('abilityLifecycle', abilityLifecycleCallback); // Error + lifecycleId2 = context.on('abilityLifecycle', abilityLifecycleCallback); // Error + } catch (paramError) { + console.error(`error: ${(paramError as BusinessError).code}, ${(paramError as BusinessError).message}`); + } + } +} + +class MyAbilityStage2 extends AbilityStage { + onCreate() { + let abilityLifecycleCallback: AbilityLifecycleCallback = {} + } + let applicationContext = ''; + let context = this.context.getApparentProperties(); + try { + // 2.通过applicationContext注册监听应用内生命周期 + lifecycleId = applicationContext.on('abilityLifecycle', abilityLifecycleCallback); // NO Error + lifecycleId2 = context.on('abilityLifecycle', abilityLifecycleCallback); // NO Error } catch (paramError) { console.error(`error: ${(paramError as BusinessError).code}, ${(paramError as BusinessError).message}`); } diff --git a/ets2panda/linter/test/main/sdk_ability_lifecycle_monitor.ets.arkts2.json b/ets2panda/linter/test/main/sdk_ability_lifecycle_monitor.ets.arkts2.json index 1bb9b8c66ff1e070ddc91480fad1d52e4bc853c3..7b569589ceb9f1ad19f8c48d8de3eece1288973b 100644 --- a/ets2panda/linter/test/main/sdk_ability_lifecycle_monitor.ets.arkts2.json +++ b/ets2panda/linter/test/main/sdk_ability_lifecycle_monitor.ets.arkts2.json @@ -1,74 +1,138 @@ { - "result": [ - { - "line": 22, - "column": 7, - "endLine": 25, - "endColumn": 6, - "problem": "ObjectLiteralProperty", - "suggest": "", - "rule": "Object literal properties can only contain name-value pairs (arkts-obj-literal-props)", - "severity": "ERROR" - }, - { - "line": 22, - "column": 23, - "endLine": 22, - "endColumn": 30, - "problem": "AnyType", - "suggest": "", - "rule": "Use explicit types instead of \"any\", \"unknown\" (arkts-no-any-unknown)", - "severity": "ERROR" - }, - { - "line": 22, - "column": 31, - "endLine": 22, - "endColumn": 40, - "problem": "AnyType", - "suggest": "", - "rule": "Use explicit types instead of \"any\", \"unknown\" (arkts-no-any-unknown)", - "severity": "ERROR" - }, - { - "line": 27, - "column": 9, - "endLine": 27, - "endColumn": 60, - "problem": "AnyType", - "suggest": "", - "rule": "Use explicit types instead of \"any\", \"unknown\" (arkts-no-any-unknown)", - "severity": "ERROR" - }, - { - "line": 30, - "column": 21, - "endLine": 30, - "endColumn": 88, - "problem": "SdkAbilityLifecycleMonitor", - "suggest": "", - "rule": "The UIAbility of 1.2 needs to be listened by the new StaticAbilityLifecycleCallback. The original AbilityLifecycleCallback can only listen to the UIAbility of 1.1 (sdk-ability-lifecycle-monitor)", - "severity": "ERROR" - }, - { - "line": 32, - "column": 46, - "endLine": 32, - "endColumn": 59, - "problem": "UIInterfaceImport", - "suggest": "", - "rule": "The ArkUI interface \"BusinessError\" should be imported before it is used (arkui-modular-interface)", - "severity": "ERROR" - }, - { - "line": 32, - "column": 85, - "endLine": 32, - "endColumn": 98, - "problem": "UIInterfaceImport", - "suggest": "", - "rule": "The ArkUI interface \"BusinessError\" should be imported before it is used (arkui-modular-interface)", - "severity": "ERROR" - } - ] + "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": 22, + "column": 7, + "endLine": 25, + "endColumn": 6, + "problem": "ObjectLiteralProperty", + "suggest": "", + "rule": "Object literal properties can only contain name-value pairs (arkts-obj-literal-props)", + "severity": "ERROR" + }, + { + "line": 22, + "column": 23, + "endLine": 22, + "endColumn": 30, + "problem": "AnyType", + "suggest": "", + "rule": "Use explicit types instead of \"any\", \"unknown\" (arkts-no-any-unknown)", + "severity": "ERROR" + }, + { + "line": 22, + "column": 31, + "endLine": 22, + "endColumn": 40, + "problem": "AnyType", + "suggest": "", + "rule": "Use explicit types instead of \"any\", \"unknown\" (arkts-no-any-unknown)", + "severity": "ERROR" + }, + { + "line": 27, + "column": 9, + "endLine": 27, + "endColumn": 60, + "problem": "AnyType", + "suggest": "", + "rule": "Use explicit types instead of \"any\", \"unknown\" (arkts-no-any-unknown)", + "severity": "ERROR" + }, + { + "line": 28, + "column": 9, + "endLine": 28, + "endColumn": 49, + "problem": "AnyType", + "suggest": "", + "rule": "Use explicit types instead of \"any\", \"unknown\" (arkts-no-any-unknown)", + "severity": "ERROR" + }, + { + "line": 31, + "column": 21, + "endLine": 31, + "endColumn": 88, + "problem": "SdkAbilityLifecycleMonitor", + "suggest": "", + "rule": "The UIAbility of 1.2 needs to be listened by the new StaticAbilityLifecycleCallback. The original AbilityLifecycleCallback can only listen to the UIAbility of 1.1 (sdk-ability-lifecycle-monitor)", + "severity": "ERROR" + }, + { + "line": 32, + "column": 22, + "endLine": 32, + "endColumn": 78, + "problem": "SdkAbilityLifecycleMonitor", + "suggest": "", + "rule": "The UIAbility of 1.2 needs to be listened by the new StaticAbilityLifecycleCallback. The original AbilityLifecycleCallback can only listen to the UIAbility of 1.1 (sdk-ability-lifecycle-monitor)", + "severity": "ERROR" + }, + { + "line": 44, + "column": 9, + "endLine": 44, + "endColumn": 55, + "problem": "AnyType", + "suggest": "", + "rule": "Use explicit types instead of \"any\", \"unknown\" (arkts-no-any-unknown)", + "severity": "ERROR" + }, + { + "line": 34, + "column": 46, + "endLine": 34, + "endColumn": 59, + "problem": "UIInterfaceImport", + "suggest": "", + "rule": "The ArkUI interface \"BusinessError\" should be imported before it is used (arkui-modular-interface)", + "severity": "ERROR" + }, + { + "line": 34, + "column": 85, + "endLine": 34, + "endColumn": 98, + "problem": "UIInterfaceImport", + "suggest": "", + "rule": "The ArkUI interface \"BusinessError\" should be imported before it is used (arkui-modular-interface)", + "severity": "ERROR" + }, + { + "line": 50, + "column": 46, + "endLine": 50, + "endColumn": 59, + "problem": "UIInterfaceImport", + "suggest": "", + "rule": "The ArkUI interface \"BusinessError\" should be imported before it is used (arkui-modular-interface)", + "severity": "ERROR" + }, + { + "line": 50, + "column": 85, + "endLine": 50, + "endColumn": 98, + "problem": "UIInterfaceImport", + "suggest": "", + "rule": "The ArkUI interface \"BusinessError\" should be imported before it is used (arkui-modular-interface)", + "severity": "ERROR" + } + ] } diff --git a/ets2panda/linter/test/main/sdk_ability_lifecycle_monitor.ets.json b/ets2panda/linter/test/main/sdk_ability_lifecycle_monitor.ets.json index d651a483feeff55940b384a204da36fb4dd6ddf6..e3cc1b936a868ceedd4c309df0fae52881d614d0 100644 --- a/ets2panda/linter/test/main/sdk_ability_lifecycle_monitor.ets.json +++ b/ets2panda/linter/test/main/sdk_ability_lifecycle_monitor.ets.json @@ -1,34 +1,68 @@ { - "result": [ - { - "line": 22, - "column": 23, - "endLine": 22, - "endColumn": 30, - "problem": "AnyType", - "suggest": "", - "rule": "Use explicit types instead of \"any\", \"unknown\" (arkts-no-any-unknown)", - "severity": "ERROR" - }, - { - "line": 22, - "column": 31, - "endLine": 22, - "endColumn": 40, - "problem": "AnyType", - "suggest": "", - "rule": "Use explicit types instead of \"any\", \"unknown\" (arkts-no-any-unknown)", - "severity": "ERROR" - }, - { - "line": 27, - "column": 9, - "endLine": 27, - "endColumn": 60, - "problem": "AnyType", - "suggest": "", - "rule": "Use explicit types instead of \"any\", \"unknown\" (arkts-no-any-unknown)", - "severity": "ERROR" - } - ] -} \ No newline at end of file + "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": 22, + "column": 23, + "endLine": 22, + "endColumn": 30, + "problem": "AnyType", + "suggest": "", + "rule": "Use explicit types instead of \"any\", \"unknown\" (arkts-no-any-unknown)", + "severity": "ERROR" + }, + { + "line": 22, + "column": 31, + "endLine": 22, + "endColumn": 40, + "problem": "AnyType", + "suggest": "", + "rule": "Use explicit types instead of \"any\", \"unknown\" (arkts-no-any-unknown)", + "severity": "ERROR" + }, + { + "line": 27, + "column": 9, + "endLine": 27, + "endColumn": 60, + "problem": "AnyType", + "suggest": "", + "rule": "Use explicit types instead of \"any\", \"unknown\" (arkts-no-any-unknown)", + "severity": "ERROR" + }, + { + "line": 28, + "column": 9, + "endLine": 28, + "endColumn": 49, + "problem": "AnyType", + "suggest": "", + "rule": "Use explicit types instead of \"any\", \"unknown\" (arkts-no-any-unknown)", + "severity": "ERROR" + }, + { + "line": 44, + "column": 9, + "endLine": 44, + "endColumn": 55, + "problem": "AnyType", + "suggest": "", + "rule": "Use explicit types instead of \"any\", \"unknown\" (arkts-no-any-unknown)", + "severity": "ERROR" + } + ] +}