diff --git a/ets2panda/linter/src/lib/TypeScriptLinter.ts b/ets2panda/linter/src/lib/TypeScriptLinter.ts index fb73dd0e1c0c8d685bd4be1a9eb0ae9290b9e2f7..2fd1e6068e2b7108f65963b43abb1440dd9bf2b2 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 54a6bf48a0740d8cabd9bdb957864db482ac97f5..c27e278bbf0ae496764a24cc5c3a8fd890cdcff3 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 db3f4250b777e4141bd55293a133e44c7096f477..4330949c719a3717839e8f7a3f98bb4b417793b5 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 1a8bca46e8f7a8fb9a05e0a3c15bad19f819c366..31c19f7df35cda1cf1b42ca41cbba563768f8813 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