From 6a204b739e1b904b2ae1171ac41d5ab3265d8cec Mon Sep 17 00:00:00 2001 From: ZhongNing Date: Fri, 25 Jul 2025 12:03:00 +0800 Subject: [PATCH] fix arkts-method-inherit-rule Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICOFBH Test scenarios: fix bug Signed-off-by: ZhongNing --- ets2panda/linter/src/lib/TypeScriptLinter.ts | 105 +++++++++++++----- .../explicit_function_type.ets.arkts2.json | 10 -- .../explicit_function_type.ets.autofix.json | 10 -- .../explicit_function_type.ets.migrate.json | 10 -- .../main/method_inheritance2.ets.arkts2.json | 20 ++++ .../linter/test/main/method_inheritance3.ets | 63 +++++++++++ .../main/method_inheritance3.ets.args.json | 19 ++++ .../main/method_inheritance3.ets.arkts2.json | 78 +++++++++++++ .../test/main/method_inheritance3.ets.json | 17 +++ 9 files changed, 276 insertions(+), 56 deletions(-) create mode 100755 ets2panda/linter/test/main/method_inheritance3.ets create mode 100755 ets2panda/linter/test/main/method_inheritance3.ets.args.json create mode 100755 ets2panda/linter/test/main/method_inheritance3.ets.arkts2.json create mode 100755 ets2panda/linter/test/main/method_inheritance3.ets.json diff --git a/ets2panda/linter/src/lib/TypeScriptLinter.ts b/ets2panda/linter/src/lib/TypeScriptLinter.ts index bc8354d803..be49910c73 100644 --- a/ets2panda/linter/src/lib/TypeScriptLinter.ts +++ b/ets2panda/linter/src/lib/TypeScriptLinter.ts @@ -3865,23 +3865,27 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { } const methodName = node.name.text; if (allBaseTypes && allBaseTypes.length > 0) { - this.checkMethodType(allBaseTypes, methodName, node); + this.checkMethodType(allBaseTypes, methodName, node, isStatic); } } - private checkMethodType(allBaseTypes: ts.Type[], methodName: string, node: ts.MethodDeclaration): void { + private checkMethodType(allBaseTypes: ts.Type[], methodName: string, node: ts.MethodDeclaration, isStatic: boolean = false): void { for (const baseType of allBaseTypes) { - const baseMethod = baseType.getProperty(methodName); + let baseMethod: ts.Symbol | undefined; + if (isStatic) { + const constructorType = this.tsTypeChecker.getTypeOfSymbolAtLocation(baseType.getSymbol()!, node); + baseMethod = constructorType.getProperty(methodName) || + baseType.getSymbol()?.members?.get(ts.escapeLeadingUnderscores(methodName)); + } else { + baseMethod = baseType.getProperty(methodName); + } if (!baseMethod) { continue; } - - const baseMethodDecl = baseMethod.declarations?.find((d) => { - return ( - (ts.isMethodDeclaration(d) || ts.isMethodSignature(d)) && - this.tsTypeChecker.getTypeAtLocation(d.parent) === baseType - ); - }) as ts.MethodDeclaration | ts.MethodSignature; + const baseMethodDecl = baseMethod.declarations?.find(d => + (ts.isMethodDeclaration(d) || ts.isMethodSignature(d)) && + this.isSameDeclarationType(d.parent, baseType, isStatic) + ) as ts.MethodDeclaration | ts.MethodSignature; if (!baseMethodDecl) { continue; @@ -3895,6 +3899,14 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { } } + private isSameDeclarationType(decl: ts.Node, type: ts.Type, isStatic: boolean): boolean { + if (isStatic && ts.isClassDeclaration(decl)) { + const staticType = this.tsTypeChecker.getTypeAtLocation(decl); + return this.isSameType(staticType, type); + } + return this.tsTypeChecker.getTypeAtLocation(decl) === type; + } + private checkIncompatibleFunctionTypes(method: ts.MethodDeclaration): void { const declaredReturnType = this.getActualReturnType(method); if (!declaredReturnType) { @@ -4002,21 +4014,7 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { private getAllBaseTypes(type: ts.Type, classDecl: ts.ClassDeclaration, isStatic?: boolean): ts.Type[] | undefined { if (isStatic) { - const baseTypes: ts.Type[] = []; - if (!classDecl.heritageClauses) { - return baseTypes; - } - for (const clause of classDecl.heritageClauses) { - if (clause.token !== ts.SyntaxKind.ExtendsKeyword) { - continue; - } - for (const typeNode of clause.types) { - const baseType = this.tsTypeChecker.getTypeAtLocation(typeNode); - baseTypes.push(baseType); - } - } - - return baseTypes; + return this.getStaticAllBaseTypes(classDecl); } const baseClasses = type.getBaseTypes() || []; @@ -4028,6 +4026,7 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { if (!classDecl.heritageClauses) { return resolvedBaseClasses; } + const interfaces: ts.Type[] = []; for (const clause of classDecl.heritageClauses) { if (clause.token !== ts.SyntaxKind.ImplementsKeyword) { @@ -4046,7 +4045,33 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { }); } } - return [...resolvedBaseClasses, ...interfaces]; + return [...resolvedBaseClasses, ...interfaces]; + } + + private getStaticAllBaseTypes(classDecl: ts.ClassDeclaration): ts.Type[] | undefined { + const baseTypes: ts.Type[] = []; + if (!classDecl.heritageClauses) { + return baseTypes; + } + + for (const clause of classDecl.heritageClauses) { + if (clause.token !== ts.SyntaxKind.ExtendsKeyword) { + continue; + } + + for (const typeNode of clause.types) { + const baseType = this.tsTypeChecker.getTypeAtLocation(typeNode); + baseTypes.push(baseType); + + const baseDecl = baseType.getSymbol()?.declarations?.[0]; + if (baseDecl && ts.isClassDeclaration(baseDecl)) { + const staticBaseType = this.tsTypeChecker.getTypeAtLocation(baseDecl); + const staticBaseTypes = this.getAllBaseTypes(staticBaseType, baseDecl, true) || []; + baseTypes.push(...staticBaseTypes); + } + } + } + return baseTypes; } /** @@ -4071,6 +4096,12 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { const baseParamType = this.tsTypeChecker.getTypeAtLocation(baseParams[i]); const derivedParamType = this.tsTypeChecker.getTypeAtLocation(derivedParams[i]); + if (baseParamType.flags & ts.TypeFlags.TypeParameter) { + if (!(derivedParamType.flags & ts.TypeFlags.TypeParameter)) { + continue; + } + } + if (!this.isTypeSameOrWider(baseParamType, derivedParamType)) { this.incrementCounters(derivedParams[i], FaultID.MethodInheritRule); } @@ -4085,6 +4116,9 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { derivedMethod: ts.MethodDeclaration, baseMethod: ts.MethodDeclaration | ts.MethodSignature ): void { + if(this.shouldSkipTypeParameterCheck(derivedMethod, baseMethod)) { + return; + } const baseMethodType = this.getActualReturnType(baseMethod); const derivedMethodType = this.getActualReturnType(derivedMethod); const baseMethodTypeIsVoid = TypeScriptLinter.checkMethodTypeIsVoidOrAny(baseMethodType, true); @@ -4127,6 +4161,21 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { } } + private shouldSkipTypeParameterCheck( + derivedMethod: ts.MethodDeclaration, + baseMethod: ts.MethodDeclaration | ts.MethodSignature + ): boolean { + const baseMethodType = this.getActualReturnType(baseMethod); + const derivedMethodType = this.getActualReturnType(derivedMethod); + + if (baseMethodType && (baseMethodType.flags & ts.TypeFlags.TypeParameter)) { + if (derivedMethodType && !(derivedMethodType.flags & ts.TypeFlags.TypeParameter)) { + return true; + } + } + return false; + } + private static checkMethodTypeIsVoidOrAny( methodType: ts.Type | undefined, isVoidOrAny: boolean, @@ -4161,6 +4210,10 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { } private isTypeSameOrWider(baseType: ts.Type, derivedType: ts.Type): boolean { + if (this.tsTypeChecker.typeToString(baseType) === this.tsTypeChecker.typeToString(derivedType)) { + return true; + } + if (derivedType.flags & ts.TypeFlags.Any) { return true; } 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 ab41a3326a..e485c6f536 100644 --- a/ets2panda/linter/test/main/explicit_function_type.ets.arkts2.json +++ b/ets2panda/linter/test/main/explicit_function_type.ets.arkts2.json @@ -84,16 +84,6 @@ "rule": "Numeric semantics is different for integer values (arkts-numeric-semantic)", "severity": "ERROR" }, - { - "line": 96, - "column": 17, - "endLine": 96, - "endColumn": 29, - "problem": "MethodInheritRule", - "suggest": "", - "rule": "Overridden method parameters and return types must respect type inheritance principles (arkts-method-inherit-rule)", - "severity": "ERROR" - }, { "line": 118, "column": 16, 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 cce8e07d1c..f78396b2f0 100644 --- a/ets2panda/linter/test/main/explicit_function_type.ets.autofix.json +++ b/ets2panda/linter/test/main/explicit_function_type.ets.autofix.json @@ -150,16 +150,6 @@ "rule": "Numeric semantics is different for integer values (arkts-numeric-semantic)", "severity": "ERROR" }, - { - "line": 96, - "column": 17, - "endLine": 96, - "endColumn": 29, - "problem": "MethodInheritRule", - "suggest": "", - "rule": "Overridden method parameters and return types must respect type inheritance principles (arkts-method-inherit-rule)", - "severity": "ERROR" - }, { "line": 118, "column": 16, 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 9c5d050cc7..b17d2989de 100644 --- a/ets2panda/linter/test/main/explicit_function_type.ets.migrate.json +++ b/ets2panda/linter/test/main/explicit_function_type.ets.migrate.json @@ -24,16 +24,6 @@ "rule": "Classes cannot be used as objects (arkts-no-classes-as-obj)", "severity": "ERROR" }, - { - "line": 96, - "column": 17, - "endLine": 96, - "endColumn": 29, - "problem": "MethodInheritRule", - "suggest": "", - "rule": "Overridden method parameters and return types must respect type inheritance principles (arkts-method-inherit-rule)", - "severity": "ERROR" - }, { "line": 118, "column": 16, diff --git a/ets2panda/linter/test/main/method_inheritance2.ets.arkts2.json b/ets2panda/linter/test/main/method_inheritance2.ets.arkts2.json index 0a8b389c11..bfb88a65c8 100755 --- a/ets2panda/linter/test/main/method_inheritance2.ets.arkts2.json +++ b/ets2panda/linter/test/main/method_inheritance2.ets.arkts2.json @@ -104,6 +104,16 @@ "rule": "Overridden method parameters and return types must respect type inheritance principles (arkts-method-inherit-rule)", "severity": "ERROR" }, + { + "line": 103, + "column": 18, + "endLine": 103, + "endColumn": 19, + "problem": "MethodInheritRule", + "suggest": "", + "rule": "Overridden method parameters and return types must respect type inheritance principles (arkts-method-inherit-rule)", + "severity": "ERROR" + }, { "line": 108, "column": 17, @@ -113,6 +123,16 @@ "suggest": "", "rule": "Overridden method parameters and return types must respect type inheritance principles (arkts-method-inherit-rule)", "severity": "ERROR" + }, + { + "line": 112, + "column": 18, + "endLine": 112, + "endColumn": 19, + "problem": "MethodInheritRule", + "suggest": "", + "rule": "Overridden method parameters and return types must respect type inheritance principles (arkts-method-inherit-rule)", + "severity": "ERROR" } ] } \ No newline at end of file diff --git a/ets2panda/linter/test/main/method_inheritance3.ets b/ets2panda/linter/test/main/method_inheritance3.ets new file mode 100755 index 0000000000..d31da9911b --- /dev/null +++ b/ets2panda/linter/test/main/method_inheritance3.ets @@ -0,0 +1,63 @@ +/* + * 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. + */ + +class A{ + getData(a:T):T{ + return a; + } +} + +class B extends A{ + getData(a: number): number { // no error + return 123; + } +} + +enum E{ + E1, + E2 +} + +interface I { + fun(e:E):void +} + +class C implements I { + fun(e:E):void{ // no error + + } +} + +Class UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void; + onWindowStageCreate(windowStage: window.WindowStage): void; +} + +class EntryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { // no error + + } + + onWindowStageCreate(windowStage: window.WindowStage): void { // no error + try { + windowStage.loadContent('pages/Index', (err: BusinessError | null): void => { + if (err && err.code) { + return; + } + }); + } catch (e) { + } + } +} \ No newline at end of file diff --git a/ets2panda/linter/test/main/method_inheritance3.ets.args.json b/ets2panda/linter/test/main/method_inheritance3.ets.args.json new file mode 100755 index 0000000000..d8d3390ad9 --- /dev/null +++ b/ets2panda/linter/test/main/method_inheritance3.ets.args.json @@ -0,0 +1,19 @@ +{ + "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." + ], + "mode": { + "arkts2": "" + } +} \ No newline at end of file diff --git a/ets2panda/linter/test/main/method_inheritance3.ets.arkts2.json b/ets2panda/linter/test/main/method_inheritance3.ets.arkts2.json new file mode 100755 index 0000000000..1dfcce5603 --- /dev/null +++ b/ets2panda/linter/test/main/method_inheritance3.ets.arkts2.json @@ -0,0 +1,78 @@ +{ + "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": 24, + "column": 12, + "endLine": 24, + "endColumn": 15, + "problem": "NumericSemantics", + "suggest": "", + "rule": "Numeric semantics is different for integer values (arkts-numeric-semantic)", + "severity": "ERROR" + }, + { + "line": 44, + "column": 67, + "endLine": 44, + "endColumn": 71, + "problem": "VoidOperator", + "suggest": "", + "rule": "\"void\" operator is not supported (arkts-no-void-operator)", + "severity": "ERROR" + }, + { + "line": 45, + "column": 57, + "endLine": 45, + "endColumn": 61, + "problem": "VoidOperator", + "suggest": "", + "rule": "\"void\" operator is not supported (arkts-no-void-operator)", + "severity": "ERROR" + }, + { + "line": 44, + "column": 18, + "endLine": 44, + "endColumn": 22, + "problem": "UIInterfaceImport", + "suggest": "", + "rule": "The ArkUI interface \"Want\" should be imported before it is used (arkui-modular-interface)", + "severity": "ERROR" + }, + { + "line": 49, + "column": 18, + "endLine": 49, + "endColumn": 22, + "problem": "UIInterfaceImport", + "suggest": "", + "rule": "The ArkUI interface \"Want\" should be imported before it is used (arkui-modular-interface)", + "severity": "ERROR" + }, + { + "line": 55, + "column": 52, + "endLine": 55, + "endColumn": 65, + "problem": "UIInterfaceImport", + "suggest": "", + "rule": "The ArkUI interface \"BusinessError\" should be imported before it is used (arkui-modular-interface)", + "severity": "ERROR" + } + ] +} \ No newline at end of file diff --git a/ets2panda/linter/test/main/method_inheritance3.ets.json b/ets2panda/linter/test/main/method_inheritance3.ets.json new file mode 100755 index 0000000000..ca88f857e9 --- /dev/null +++ b/ets2panda/linter/test/main/method_inheritance3.ets.json @@ -0,0 +1,17 @@ +{ + "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": [] +} \ No newline at end of file -- Gitee