From 3daf1467d0844b5d08d04a8512d29e3917e6e2ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=92=9F=E6=9F=A0?= Date: Sat, 2 Aug 2025 09:18:43 +0800 Subject: [PATCH] fix issue for arkts-incompatible-function-types MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Issue:https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICQ73S Test scenarios:fix issue for arkts-incompatible-function-types Signed-off-by: 钟柠 --- ets2panda/linter/src/lib/TypeScriptLinter.ts | 5 ++++- ets2panda/linter/src/lib/utils/TsUtils.ts | 18 +++++++++++++++ .../test/main/incompatible_function.ets | 22 +++++++++++++++++++ .../incompatible_function.ets.arkts2.json | 20 ++++++++--------- 4 files changed, 54 insertions(+), 11 deletions(-) diff --git a/ets2panda/linter/src/lib/TypeScriptLinter.ts b/ets2panda/linter/src/lib/TypeScriptLinter.ts index fb73dd0e1c..2fd1e6068e 100644 --- a/ets2panda/linter/src/lib/TypeScriptLinter.ts +++ b/ets2panda/linter/src/lib/TypeScriptLinter.ts @@ -3882,7 +3882,6 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { if (!ts.isClassDeclaration(classDecl)) { return; } - this.checkIncompatibleFunctionTypes(node); const isStatic = node.modifiers?.some((mod) => { return mod.kind === ts.SyntaxKind.StaticKeyword; @@ -3897,6 +3896,7 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { if (allBaseTypes && allBaseTypes.length > 0) { this.checkMethodType(allBaseTypes, methodName, node, isStatic); } + this.checkIncompatibleFunctionTypes(node); } private checkMethodType( @@ -3961,6 +3961,9 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { if (declaredReturnTypeStr === actualReturnTypeStr) { return; } + if (this.tsUtils.skipCheckForArrayBufferLike(declaredReturnTypeStr, actualReturnTypeStr)) { + return; + } if (actualReturnType.flags & ts.TypeFlags.Any || declaredReturnType.flags & ts.TypeFlags.Any) { return; } diff --git a/ets2panda/linter/src/lib/utils/TsUtils.ts b/ets2panda/linter/src/lib/utils/TsUtils.ts index 54a6bf48a0..c27e278bbf 100644 --- a/ets2panda/linter/src/lib/utils/TsUtils.ts +++ b/ets2panda/linter/src/lib/utils/TsUtils.ts @@ -801,6 +801,9 @@ export class TsUtils { if (this.skipStructuralTypingCheckForFunctionals(lhsType, rhsType)) { return false; } + if (this.skipCheckForArrayBufferLike(lhsType, rhsType)) { + return false; + } if (rhsType.isUnion() || lhsType.isUnion()) { return this.needToDeduceStructuralIdentityHandleUnions(lhsType, rhsType, rhsExpr, isStrict); } @@ -897,6 +900,16 @@ export class TsUtils { return isStrictLhs && this.typeContainsNonSendableClassOrInterface(rhsType); } + skipCheckForArrayBufferLike(lhsType: string | ts.Type, rhsType: string | ts.Type): boolean { + const lhsIsArrayBufferLike = TsUtils.isArrayBufferType( + typeof lhsType === 'string' ? lhsType : this.tsTypeChecker.typeToString(lhsType) + ); + const rhsIsArrayBufferLike = TsUtils.isArrayBufferType( + typeof rhsType === 'string' ? rhsType : this.tsTypeChecker.typeToString(rhsType) + ); + return !!this.options.arkts2 && lhsIsArrayBufferLike && rhsIsArrayBufferLike; + } + private processExtendedParentTypes(typeA: ts.Type, typeB: ts.Type): boolean { /* @@ -2096,6 +2109,11 @@ export class TsUtils { return callSigns && callSigns.length > 0; } + static isArrayBufferType(typeStr: string): boolean { + const arrayBufferLikeArr = ['ArrayBuffer', 'ArrayBufferLike']; + return arrayBufferLikeArr.includes(typeStr); + } + static getFunctionalTypeSignature(type: ts.Type): ts.Signature | undefined { const callSigns = type.getCallSignatures(); if (callSigns.length > 0) { diff --git a/ets2panda/linter/test/main/incompatible_function.ets b/ets2panda/linter/test/main/incompatible_function.ets index db3f4250b7..4330949c71 100644 --- a/ets2panda/linter/test/main/incompatible_function.ets +++ b/ets2panda/linter/test/main/incompatible_function.ets @@ -112,4 +112,26 @@ for (let i = 0; i < totalPromises; i++) { }, delay) ) ); +} + +class D { + fun(a: ArrayBufferLike): ArrayBuffer { + return a; + } +} + +class X { + n: number = 0 + s: string = '' +} + +class Y { + n: number = 0 + s: string = '' +} + +class E { + Dfun(a: X|Y): X { + return a; + } } \ No newline at end of file diff --git a/ets2panda/linter/test/main/incompatible_function.ets.arkts2.json b/ets2panda/linter/test/main/incompatible_function.ets.arkts2.json index 1a8bca46e8..31c19f7df3 100644 --- a/ets2panda/linter/test/main/incompatible_function.ets.arkts2.json +++ b/ets2panda/linter/test/main/incompatible_function.ets.arkts2.json @@ -84,16 +84,6 @@ "rule": "Stricter assignments into variables of function type (arkts-incompatible-function-types)", "severity": "ERROR" }, - { - "line": 92, - "column": 12, - "endLine": 92, - "endColumn": 20, - "problem": "IncompationbleFunctionType", - "suggest": "", - "rule": "Stricter assignments into variables of function type (arkts-incompatible-function-types)", - "severity": "ERROR" - }, { "line": 92, "column": 12, @@ -123,6 +113,16 @@ "suggest": "", "rule": "Stricter assignments into variables of function type (arkts-incompatible-function-types)", "severity": "ERROR" + }, + { + "line": 135, + "column": 12, + "endLine": 135, + "endColumn": 13, + "problem": "StructuralIdentity", + "suggest": "", + "rule": "Structural typing is not supported (arkts-no-structural-typing)", + "severity": "ERROR" } ] } \ No newline at end of file -- Gitee