From 5f36ba445799c3ebb9286049cd4431feaea55693 Mon Sep 17 00:00:00 2001 From: ZhongNing Date: Fri, 1 Aug 2025 17:56:32 +0800 Subject: [PATCH] Fix for MethodInheritRule Issue:https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICQFBY Test scenarios: Fix Bug Signed-off-by: ZhongNing --- ets2panda/linter/src/lib/TypeScriptLinter.ts | 2 +- .../linter/test/main/method_inheritance2.ets | 243 +++++++++++++- .../main/method_inheritance2.ets.arkts2.json | 304 +++++++++++++++++- 3 files changed, 526 insertions(+), 23 deletions(-) diff --git a/ets2panda/linter/src/lib/TypeScriptLinter.ts b/ets2panda/linter/src/lib/TypeScriptLinter.ts index fb73dd0e1c0..e52b9076446 100644 --- a/ets2panda/linter/src/lib/TypeScriptLinter.ts +++ b/ets2panda/linter/src/lib/TypeScriptLinter.ts @@ -3938,7 +3938,7 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { } private isSameDeclarationType(decl: ts.Node, type: ts.Type, isStatic: boolean): boolean { - if (isStatic && ts.isClassDeclaration(decl)) { + if (isStatic && ts.isClassDeclaration(decl) || ts.isInterfaceDeclaration(decl)) { const staticType = this.tsTypeChecker.getTypeAtLocation(decl); return this.isSameType(staticType, type); } diff --git a/ets2panda/linter/test/main/method_inheritance2.ets b/ets2panda/linter/test/main/method_inheritance2.ets index e4f9112258f..1f8bd037830 100755 --- a/ets2panda/linter/test/main/method_inheritance2.ets +++ b/ets2panda/linter/test/main/method_inheritance2.ets @@ -63,14 +63,14 @@ interface BaseI2 { // T implements class DerivedI2 implements BaseI2 { - foo(obj: A): void { - console.log("Drived"); // (arkts-method-inherit-rule) error + foo(obj: A): void { // (arkts-method-inherit-rule) error + console.log("Derived"); } - foo2(): void { - console.log("Drived"); // (arkts-method-inherit-rule) error + foo2(): void { // (arkts-method-inherit-rule) error + console.log("Derived"); } - foo3(obj: A | B): void { - console.log("Drived"); // (arkts-method-inherit-rule) error + foo3(obj: A | B): void { // (arkts-method-inherit-rule) error + console.log("Derived"); } } @@ -121,6 +121,233 @@ interface I{ } class J implements I{ - pp(key: string,value?:string):void { // 漏报arkts-method-inherit-rule + pp(key: string,value?:string):void { // arkts-method-inherit-rule } -} \ No newline at end of file +} + +interface BaseI { + foo(): A|B ; + foo2(): void; + foo3(): A; +} + +class Derived2 implements BaseI { + foo(): A|B|C{ // (arkts-method-inherit-rule) + console.log("Derived:") + return new A(); + } + + foo2(): A{ // (arkts-method-inherit-rule) + console.log("Derived:") + return new A(); + } + + foo3(): A|B { // (arkts-method-inherit-rule) + console.log("Derived:") + return new A(); + } +} + +let b2:BaseI = new Derived2(); +b2.foo(); +b2.foo2() +b2.foo3() + + +class Base32 { + foo(): A|B { + console.log("base") + return new A(); + } + foo2():void{ + console.log("base") + // return new A(); + } + foo3(): A { + console.log("base") + return new A(); + } + foo4(){ + console.log("base") + // return new A(); + } +} + +class Derived32 extends Base32 { + foo(): A|B|C{ // (arkts-method-inherit-rule) + console.log("Derived:") + return new A(); + } + + foo2(): A{ // (arkts-method-inherit-rule) + console.log("Derived:") + return new A(); + } + + foo3(): A|B { // (arkts-method-inherit-rule) + console.log("Derived:") + return new A(); + } + + foo4(): A{ // (arkts-method-inherit-rule) + console.log("Derived:") + return new A(); + } +} + +interface BaseI4 { + foo(): A|B ; + foo2(): void; + foo3(): A; +} + +class Derived4 implements BaseI4 { + foo(): A|B|C{ // (arkts-method-inherit-rule) + console.log("Derived:") + return new A(); + } + + foo2(): A{ // (arkts-method-inherit-rule) + console.log("Derived:") + return new A(); + } + + foo3(): A|B { // (arkts-method-inherit-rule) + console.log("Derived:") + return new A(); + } +} + +class Animal {} +class Dog extends Animal {} +class Base55 { + public foo(obj: Animal): void { + console.log("base") + } +} + +class Derived55 extends Base55 { + public foo(obj: Dog): void { // (arkts-method-inherit-rule) + console.log("Derived:") + } +} + +class Base6 { + public foo(): Dog { + console.log("base") + return new Dog(); + } +} + +class Derived6 extends Base6 { + public foo(): Animal { // (arkts-method-inherit-rule) + console.log("Derived:") + return new Animal() + } +} + +abstract class Base66{ + abstract foo(); + abstract foo1(); +} + +abstract class Derived66 extends Base66{ + async foo(){ // (arkts-method-inherit-rule) + + } + foo1(): number { // (arkts-method-inherit-rule)? + return 1 + } +} +class Base7{ + foo(){ + + } +} +class Derived7 extends Base7{ + foo(): number { // (arkts-method-inherit-rule) + return 12; + } +} +class Base44 { + public foo(obj: A | B): void { + console.log("base") + } + protected foo2(obj: A | B): void { + console.log("base") + } + async foo3(obj: A | B | C): Promise { + console.log("base") + } +} + +class Derived44 extends Base44 { + public foo(obj: A): void { // (arkts-method-inherit-rule) + console.log("Derived:" + obj.a) + } + protected foo2(): void { // (arkts-method-inherit-rule) + console.log("Derived:") + } + async foo3(obj: A | B): Promise { // (arkts-method-inherit-rule) + console.log("Derived:") + } +} +class Base { + foo(obj: A | B): void { + console.log("base") + } + foo2(obj: A | B): void { + console.log("base") + } + foo3(obj: A | B | C): void { + console.log("base") + } + foo4(obj: A): void { + console.log("base" ); + } +} +class Derived extends Base { + foo(obj: A): void { // (arkts-method-inherit-rule) + console.log("Derived:" + obj.a) + } + foo2(): void { // (arkts-method-inherit-rule) + console.log("Derived:") + } + foo3(obj: A | B): void { // (arkts-method-inherit-rule) + console.log("Derived:") + } + foo4(obj: D): void { // (arkts-method-inherit-rule) + console.log("Derived:" + obj.b) + } +} +class Derived_Derived extends Derived { + foo(): void { // (arkts-method-inherit-rule) + } + + foo3(obj: A): void { // (arkts-method-inherit-rule) + console.log("Derived:") + } +} + +let b:Base = new Derived(); +b.foo(new B()); +b.foo2(new B()) + +interface BaseI22 { + foo(obj: A | B):void; + foo2(obj: A ): void; + foo3(obj: A | B | C): void; +} + +class Derived22 implements BaseI22 { + foo(obj: A): void { // (arkts-method-inherit-rule) + console.log("Derived"); + } + foo2(): void { // (arkts-method-inherit-rule) + console.log("Derived"); + } + foo3(obj: A | B): void { // (arkts-method-inherit-rule) + console.log("Derived"); + } +} +let b22 :BaseI = new Derived2(); \ No newline at end of file diff --git a/ets2panda/linter/test/main/method_inheritance2.ets.arkts2.json b/ets2panda/linter/test/main/method_inheritance2.ets.arkts2.json index 54e9fda0ff0..4e6f89111af 100755 --- a/ets2panda/linter/test/main/method_inheritance2.ets.arkts2.json +++ b/ets2panda/linter/test/main/method_inheritance2.ets.arkts2.json @@ -1,18 +1,4 @@ { - "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": 46, @@ -44,6 +30,36 @@ "rule": "Overridden method parameters and return types must respect type inheritance principles (arkts-method-inherit-rule)", "severity": "ERROR" }, + { + "line": 66, + "column": 10, + "endLine": 66, + "endColumn": 16, + "problem": "MethodInheritRule", + "suggest": "", + "rule": "Overridden method parameters and return types must respect type inheritance principles (arkts-method-inherit-rule)", + "severity": "ERROR" + }, + { + "line": 69, + "column": 3, + "endLine": 69, + "endColumn": 7, + "problem": "MethodInheritRule", + "suggest": "", + "rule": "Overridden method parameters and return types must respect type inheritance principles (arkts-method-inherit-rule)", + "severity": "ERROR" + }, + { + "line": 72, + "column": 11, + "endLine": 72, + "endColumn": 21, + "problem": "MethodInheritRule", + "suggest": "", + "rule": "Overridden method parameters and return types must respect type inheritance principles (arkts-method-inherit-rule)", + "severity": "ERROR" + }, { "line": 98, "column": 17, @@ -93,6 +109,266 @@ "suggest": "", "rule": "Overridden method parameters and return types must respect type inheritance principles (arkts-method-inherit-rule)", "severity": "ERROR" + }, + { + "line": 135, + "column": 10, + "endLine": 135, + "endColumn": 15, + "problem": "MethodInheritRule", + "suggest": "", + "rule": "Overridden method parameters and return types must respect type inheritance principles (arkts-method-inherit-rule)", + "severity": "ERROR" + }, + { + "line": 140, + "column": 11, + "endLine": 140, + "endColumn": 12, + "problem": "MethodInheritRule", + "suggest": "", + "rule": "Overridden method parameters and return types must respect type inheritance principles (arkts-method-inherit-rule)", + "severity": "ERROR" + }, + { + "line": 145, + "column": 11, + "endLine": 145, + "endColumn": 14, + "problem": "MethodInheritRule", + "suggest": "", + "rule": "Overridden method parameters and return types must respect type inheritance principles (arkts-method-inherit-rule)", + "severity": "ERROR" + }, + { + "line": 177, + "column": 13, + "endLine": 177, + "endColumn": 18, + "problem": "MethodInheritRule", + "suggest": "", + "rule": "Overridden method parameters and return types must respect type inheritance principles (arkts-method-inherit-rule)", + "severity": "ERROR" + }, + { + "line": 182, + "column": 14, + "endLine": 182, + "endColumn": 15, + "problem": "MethodInheritRule", + "suggest": "", + "rule": "Overridden method parameters and return types must respect type inheritance principles (arkts-method-inherit-rule)", + "severity": "ERROR" + }, + { + "line": 187, + "column": 14, + "endLine": 187, + "endColumn": 17, + "problem": "MethodInheritRule", + "suggest": "", + "rule": "Overridden method parameters and return types must respect type inheritance principles (arkts-method-inherit-rule)", + "severity": "ERROR" + }, + { + "line": 192, + "column": 14, + "endLine": 192, + "endColumn": 15, + "problem": "MethodInheritRule", + "suggest": "", + "rule": "Overridden method parameters and return types must respect type inheritance principles (arkts-method-inherit-rule)", + "severity": "ERROR" + }, + { + "line": 205, + "column": 13, + "endLine": 205, + "endColumn": 18, + "problem": "MethodInheritRule", + "suggest": "", + "rule": "Overridden method parameters and return types must respect type inheritance principles (arkts-method-inherit-rule)", + "severity": "ERROR" + }, + { + "line": 210, + "column": 14, + "endLine": 210, + "endColumn": 15, + "problem": "MethodInheritRule", + "suggest": "", + "rule": "Overridden method parameters and return types must respect type inheritance principles (arkts-method-inherit-rule)", + "severity": "ERROR" + }, + { + "line": 215, + "column": 14, + "endLine": 215, + "endColumn": 17, + "problem": "MethodInheritRule", + "suggest": "", + "rule": "Overridden method parameters and return types must respect type inheritance principles (arkts-method-inherit-rule)", + "severity": "ERROR" + }, + { + "line": 230, + "column": 14, + "endLine": 230, + "endColumn": 22, + "problem": "MethodInheritRule", + "suggest": "", + "rule": "Overridden method parameters and return types must respect type inheritance principles (arkts-method-inherit-rule)", + "severity": "ERROR" + }, + { + "line": 243, + "column": 17, + "endLine": 243, + "endColumn": 23, + "problem": "MethodInheritRule", + "suggest": "", + "rule": "Overridden method parameters and return types must respect type inheritance principles (arkts-method-inherit-rule)", + "severity": "ERROR" + }, + { + "line": 255, + "column": 9, + "endLine": 255, + "endColumn": 12, + "problem": "MethodInheritRule", + "suggest": "", + "rule": "Overridden method parameters and return types must respect type inheritance principles (arkts-method-inherit-rule)", + "severity": "ERROR" + }, + { + "line": 268, + "column": 10, + "endLine": 268, + "endColumn": 16, + "problem": "MethodInheritRule", + "suggest": "", + "rule": "Overridden method parameters and return types must respect type inheritance principles (arkts-method-inherit-rule)", + "severity": "ERROR" + }, + { + "line": 285, + "column": 14, + "endLine": 285, + "endColumn": 20, + "problem": "MethodInheritRule", + "suggest": "", + "rule": "Overridden method parameters and return types must respect type inheritance principles (arkts-method-inherit-rule)", + "severity": "ERROR" + }, + { + "line": 288, + "column": 13, + "endLine": 288, + "endColumn": 17, + "problem": "MethodInheritRule", + "suggest": "", + "rule": "Overridden method parameters and return types must respect type inheritance principles (arkts-method-inherit-rule)", + "severity": "ERROR" + }, + { + "line": 291, + "column": 14, + "endLine": 291, + "endColumn": 24, + "problem": "MethodInheritRule", + "suggest": "", + "rule": "Overridden method parameters and return types must respect type inheritance principles (arkts-method-inherit-rule)", + "severity": "ERROR" + }, + { + "line": 310, + "column": 7, + "endLine": 310, + "endColumn": 13, + "problem": "MethodInheritRule", + "suggest": "", + "rule": "Overridden method parameters and return types must respect type inheritance principles (arkts-method-inherit-rule)", + "severity": "ERROR" + }, + { + "line": 313, + "column": 3, + "endLine": 313, + "endColumn": 7, + "problem": "MethodInheritRule", + "suggest": "", + "rule": "Overridden method parameters and return types must respect type inheritance principles (arkts-method-inherit-rule)", + "severity": "ERROR" + }, + { + "line": 316, + "column": 8, + "endLine": 316, + "endColumn": 18, + "problem": "MethodInheritRule", + "suggest": "", + "rule": "Overridden method parameters and return types must respect type inheritance principles (arkts-method-inherit-rule)", + "severity": "ERROR" + }, + { + "line": 319, + "column": 8, + "endLine": 319, + "endColumn": 14, + "problem": "MethodInheritRule", + "suggest": "", + "rule": "Overridden method parameters and return types must respect type inheritance principles (arkts-method-inherit-rule)", + "severity": "ERROR" + }, + { + "line": 324, + "column": 3, + "endLine": 324, + "endColumn": 6, + "problem": "MethodInheritRule", + "suggest": "", + "rule": "Overridden method parameters and return types must respect type inheritance principles (arkts-method-inherit-rule)", + "severity": "ERROR" + }, + { + "line": 327, + "column": 8, + "endLine": 327, + "endColumn": 14, + "problem": "MethodInheritRule", + "suggest": "", + "rule": "Overridden method parameters and return types must respect type inheritance principles (arkts-method-inherit-rule)", + "severity": "ERROR" + }, + { + "line": 343, + "column": 7, + "endLine": 343, + "endColumn": 13, + "problem": "MethodInheritRule", + "suggest": "", + "rule": "Overridden method parameters and return types must respect type inheritance principles (arkts-method-inherit-rule)", + "severity": "ERROR" + }, + { + "line": 346, + "column": 3, + "endLine": 346, + "endColumn": 7, + "problem": "MethodInheritRule", + "suggest": "", + "rule": "Overridden method parameters and return types must respect type inheritance principles (arkts-method-inherit-rule)", + "severity": "ERROR" + }, + { + "line": 349, + "column": 8, + "endLine": 349, + "endColumn": 18, + "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 -- Gitee