diff --git a/ets2panda/linter/src/lib/TypeScriptLinter.ts b/ets2panda/linter/src/lib/TypeScriptLinter.ts index ca04dbcfcd70cd084c01b8f28c23f983dd279534..0f4821f4574bf6a7d1d476eda4a416c9aad50712 100644 --- a/ets2panda/linter/src/lib/TypeScriptLinter.ts +++ b/ets2panda/linter/src/lib/TypeScriptLinter.ts @@ -2055,7 +2055,11 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { this.handleTsInterop(tsLhsExpr, () => { this.checkUsageOfTsTypes(leftOperandType, tsBinaryExpr); }); - this.checkAssignmentMatching(tsBinaryExpr, leftOperandType, tsRhsExpr); + if (!this.options.arkts2) { + this.checkAssignmentMatching(tsBinaryExpr, leftOperandType, tsRhsExpr); + } else if (TsUtils.hasGenericParameters(leftOperandType)) { + this.checkAssignmentMatching(tsBinaryExpr, leftOperandType, tsRhsExpr); + } this.checkFunctionTypeCompatible(typeNode, tsRhsExpr); this.handleEsObjectAssignment(tsBinaryExpr, typeNode, tsRhsExpr); this.handleSdkGlobalApi(tsBinaryExpr); @@ -2519,11 +2523,19 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { this.checkVarDeclForDuplicateNames(tsVarDecl.name); if (tsVarDecl.type && tsVarDecl.initializer) { - this.checkAssignmentMatching( - tsVarDecl, - this.tsTypeChecker.getTypeAtLocation(tsVarDecl.type), - tsVarDecl.initializer - ); + if (!this.options.arkts2) { + this.checkAssignmentMatching( + tsVarDecl, + this.tsTypeChecker.getTypeAtLocation(tsVarDecl.type), + tsVarDecl.initializer + ); + } else if (TsUtils.hasGenericParameters(this.tsTypeChecker.getTypeAtLocation(tsVarDecl.type))) { + this.checkAssignmentMatching( + tsVarDecl, + this.tsTypeChecker.getTypeAtLocation(tsVarDecl.type), + tsVarDecl.initializer + ); + } this.checkFunctionTypeCompatible(tsVarDecl.type, tsVarDecl.initializer); } this.handleEsValueDeclaration(tsVarDecl); @@ -4579,9 +4591,11 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { return false; } - private checkRestrictedAPICall(node: ts.CallExpression): void { - if (TypeScriptLinter.isReflectAPICall(node)) { - this.incrementCounters(node.parent, FaultID.InteropCallReflect); + private checkRestrictedAPICall(node: ts.Node): void { + if (ts.isCallExpression(node)) { + if (TypeScriptLinter.isReflectAPICall(node)) { + this.incrementCounters(node.parent, FaultID.InteropCallReflect); + } } } @@ -4891,7 +4905,11 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { if (!tsParamType) { continue; } - this.checkAssignmentMatching(tsArg, tsParamType, tsArg); + if (!this.options.arkts2) { + this.checkAssignmentMatching(tsArg, tsParamType, tsArg); + } else if (TsUtils.hasGenericParameters(tsArgType)) { + this.checkAssignmentMatching(tsArg, tsParamType, tsArg); + } } } } diff --git a/ets2panda/linter/src/lib/utils/TsUtils.ts b/ets2panda/linter/src/lib/utils/TsUtils.ts index db9e766033add2d72cf30c8f2f477b363aa9e1d9..ad2b6f598c4a71e5841f00f96784120c2516f8f6 100644 --- a/ets2panda/linter/src/lib/utils/TsUtils.ts +++ b/ets2panda/linter/src/lib/utils/TsUtils.ts @@ -811,6 +811,9 @@ export class TsUtils { // eslint-disable-next-line no-param-reassign rhsType = TsUtils.reduceReference(rhsType); } + if (this.options.arkts2) { + return this.checkArkTs2StructuralIdentity(lhsType, rhsType); + } return ( lhsType.isClassOrInterface() && rhsType.isClassOrInterface() && @@ -818,6 +821,277 @@ export class TsUtils { ); } + private checkArkTs2StructuralIdentity(lhsType: ts.Type, rhsType: ts.Type): boolean { + return ( + lhsType.isClassOrInterface() && + rhsType.isClassOrInterface() && + !this.relatedByInheritanceOrIdentical(rhsType, lhsType) && + this.haveNoCommonAncestorOrInterface(rhsType, lhsType) && + this.checkPublicAPIMatch(lhsType, rhsType) + ); + } + + private haveNoCommonAncestorOrInterface( + lhsType: ts.Type, + rhsType: ts.Type, + visited: Set = new Set() + ): boolean { + if (visited.has(lhsType) || visited.has(rhsType)) { + return true; + } + visited.add(lhsType).add(rhsType); + + const parentsA = this.getParentTypes(lhsType); + const parentsB = this.getParentTypes(rhsType); + + if (TsUtils.hasDirectCommonParent(parentsA, parentsB)) { + return false; + } + + return this.hasNoIndirectCommonParent(parentsA, parentsB, visited); + } + + private getParentTypes(type: ts.Type): ts.Type[] { + const declarations = TsUtils.getValidClassOrInterfaceDeclarations(type); + return this.extractParentTypesFromDeclarations(declarations); + } + + private extractParentTypesFromDeclarations(declarations: ts.Declaration[]): ts.Type[] { + const parents: ts.Type[] = []; + + for (const decl of declarations) { + const heritageClauses = TsUtils.getHeritageClausesArray(decl); + this.processHeritageClauses(heritageClauses, parents); + } + + return parents; + } + + private static getHeritageClausesArray(decl: ts.Declaration): ts.HeritageClause[] { + const clauses = (decl as ts.ClassDeclaration | ts.InterfaceDeclaration).heritageClauses; + return clauses ? Array.from(clauses) : []; + } + + private processHeritageClauses(heritageClauses: ts.HeritageClause[], parents: ts.Type[]): void { + for (const clause of heritageClauses) { + for (const type of clause.types) { + const parentType = this.tsTypeChecker.getTypeAtLocation(type); + if (parentType) { + parents.push(parentType); + } + } + } + } + + private static getValidClassOrInterfaceDeclarations(type: ts.Type): ts.Declaration[] { + return ( + type.symbol?.declarations?.filter((decl) => { + return ts.isClassDeclaration(decl) || ts.isInterfaceDeclaration(decl); + }) ?? [] + ); + } + + private static hasDirectCommonParent(parentsA: ts.Type[], parentsB: ts.Type[]): boolean { + return parentsA.some((parentA) => { + return parentsB.some((parentB) => { + return parentA.symbol?.name === parentB.symbol?.name; + }); + }); + } + + private hasNoIndirectCommonParent( + parentsA: ts.Type[], + parentsB: ts.Type[], + visited: Set + ): boolean { + return parentsA.every((parentA) => { + return parentsB.every((parentB) => { + return this.haveNoCommonAncestorOrInterface(parentA, parentB, visited); + }); + }); + } + + checkPublicAPIMatch(typeA: ts.Type, typeB: ts.Type): boolean { + const lhsProps = this.tsTypeChecker.getPropertiesOfType(typeA); + const rhsProps = this.tsTypeChecker.getPropertiesOfType(typeB); + + if (lhsProps.length > rhsProps.length || rhsProps.length === 0 && lhsProps.length === 0) { + return false; + } + + return lhsProps.every((lhsProp) => { + return this.checkSinglePropertyMatch(lhsProp, rhsProps); + }); + } + + private checkSinglePropertyMatch(lhsProp: ts.Symbol, rhsProps: ts.Symbol[]): boolean { + const rhsProp = rhsProps.find((p) => { + return p.name === lhsProp.name; + }); + if (!rhsProp) { + return false; + } + + const typeAProp = + (this.tsTypeChecker as any).getTypeOfSymbol?.(lhsProp) ?? + (lhsProp.valueDeclaration && this.tsTypeChecker.getTypeAtLocation(lhsProp.valueDeclaration)); + const typeBProp = + (this.tsTypeChecker as any).getTypeOfSymbol?.(rhsProp) ?? + (rhsProp.valueDeclaration && this.tsTypeChecker.getTypeAtLocation(rhsProp.valueDeclaration)); + + const isSameType = + typeAProp && + typeBProp && + ((this.tsTypeChecker as any).isAssignableTo?.(typeBProp, typeAProp) ?? + TsUtils.fallbackTypeCheck(typeBProp, typeAProp)); + if (!isSameType) { + return false; + } + + return TsUtils.checkPropertyModifiersMatch(lhsProp, rhsProp); + } + + private static checkPropertyModifiersMatch(lhsProp: ts.Symbol, rhsProp: ts.Symbol): boolean { + const lhsDecl = lhsProp.declarations?.[0]; + const rhsDecl = rhsProp.declarations?.[0]; + + if (!lhsDecl || !rhsDecl) { + return false; + } + + let matcher; + if (ts.isPropertyDeclaration(lhsDecl) || ts.isMethodDeclaration(lhsDecl)) { + matcher = TsUtils.matchClassMemberModifiers; + if (!(ts.isPropertyDeclaration(rhsDecl) || ts.isMethodDeclaration(rhsDecl))) { + return false; + } + } else if (ts.isMethodSignature(lhsDecl) || ts.isPropertySignature(lhsDecl)) { + matcher = TsUtils.matchInterfaceMemberModifiers; + if (!(ts.isMethodSignature(rhsDecl) || ts.isPropertySignature(rhsDecl))) { + return false; + } + } else { + return false; + } + + return matcher((lhsDecl as any).modifiers, (rhsDecl as any).modifiers); + } + + private static fallbackTypeCheck(source: ts.Type, target: ts.Type): boolean { + return source.flags === target.flags && source.symbol?.name === target.symbol?.name; + } + + private static matchInterfaceMemberModifiers( + lhsModifiers: ts.NodeArray | undefined, + rhsModifiers: ts.NodeArray | undefined + ): boolean { + const basicCheckResult = TsUtils.checkBasicModifierCases(lhsModifiers, rhsModifiers); + if (basicCheckResult !== undefined) { + return basicCheckResult; + } + return TsUtils.checkComplexModifierCases(lhsModifiers!, rhsModifiers!); + } + + private static checkBasicModifierCases( + lhsModifiers: ts.NodeArray | undefined, + rhsModifiers: ts.NodeArray | undefined + ): boolean | undefined { + if (!lhsModifiers && !rhsModifiers) { + return true; + } + if (!lhsModifiers && rhsModifiers) { + return rhsModifiers.length === 1 && rhsModifiers[0].kind === ts.SyntaxKind.PublicKeyword; + } + if (lhsModifiers && !rhsModifiers) { + return lhsModifiers.length === 1 && lhsModifiers[0].kind === ts.SyntaxKind.PublicKeyword; + } + return undefined; + } + + private static checkComplexModifierCases( + lhsModifiers: ts.NodeArray, + rhsModifiers: ts.NodeArray + ): boolean { + const lhsMemberSet = new Set(lhsModifiers); + const rhsMemberSet = new Set(rhsModifiers); + + const lhsUniqueMember = lhsModifiers.filter((modifier) => { + return !rhsMemberSet.has(modifier); + }); + const rhsUniqueMember = rhsModifiers.filter((modifier) => { + return !lhsMemberSet.has(modifier); + }); + + if (lhsUniqueMember.length > 0) { + return ( + lhsUniqueMember.length === 1 && + lhsUniqueMember[0].kind === ts.SyntaxKind.PublicKeyword && + rhsUniqueMember.length === 0 + ); + } + + return ( + rhsUniqueMember.length === 0 || + rhsUniqueMember.length === 1 && rhsUniqueMember[0].kind === ts.SyntaxKind.PublicKeyword + ); + } + + private static matchClassMemberModifiers( + lhsModifiers: ts.NodeArray | undefined, + rhsModifiers: ts.NodeArray | undefined + ): boolean { + const basicCheckResult = TsUtils.checkBasicModifierCasesly(lhsModifiers, rhsModifiers); + if (basicCheckResult !== undefined) { + return basicCheckResult; + } + + return TsUtils.checkComplexModifierCasesly(lhsModifiers!, rhsModifiers!); + } + + private static checkBasicModifierCasesly( + lhsModifiers: ts.NodeArray | undefined, + rhsModifiers: ts.NodeArray | undefined + ): boolean | undefined { + if (!lhsModifiers && !rhsModifiers) { + return true; + } + if (!lhsModifiers && rhsModifiers) { + return rhsModifiers.length === 1 && rhsModifiers[0].kind === ts.SyntaxKind.PublicKeyword; + } + if (lhsModifiers && !rhsModifiers) { + return lhsModifiers.length === 1 && lhsModifiers[0].kind === ts.SyntaxKind.PublicKeyword; + } + return undefined; + } + + private static checkComplexModifierCasesly( + lhsModifiers: ts.NodeArray, + rhsModifiers: ts.NodeArray + ): boolean { + const lhsMemberSet = new Set(lhsModifiers); + const rhsMemberSet = new Set(rhsModifiers); + + const lhsUniqueMember = lhsModifiers.filter((modifier) => { + return !rhsMemberSet.has(modifier); + }); + const rhsUniqueMember = rhsModifiers.filter((modifier) => { + return !lhsMemberSet.has(modifier); + }); + + if (lhsUniqueMember.length > 0) { + return ( + lhsUniqueMember.length === 1 && + lhsUniqueMember[0].kind === ts.SyntaxKind.PublicKeyword && + rhsUniqueMember.length === 0 + ); + } + + return ( + rhsUniqueMember.length === 0 || + rhsUniqueMember.length === 1 && rhsUniqueMember[0].kind === ts.SyntaxKind.PublicKeyword + ); + } + needToDeduceStructuralIdentityIsStrict( lhsType: ts.Type, rhsType: ts.Type, @@ -3804,4 +4078,8 @@ export class TsUtils { const sym = type.getSymbol(); return !!sym && sym.getName() === 'PromiseLike' && isStdLibrarySymbol(sym); } + + static hasGenericParameters(type: ts.Type): boolean { + return TsUtils.isTypeReference(type) && type.typeArguments !== undefined && type.typeArguments.length > 0; + } } diff --git a/ets2panda/linter/test/main/func_inferred_type_args_2.ets.arkts2.json b/ets2panda/linter/test/main/func_inferred_type_args_2.ets.arkts2.json index d1ca60d4ff31e83b6b4d74b04b596d8d7099ea38..131dc190cfdcf90f7b36a104064aec00d6f85118 100644 --- a/ets2panda/linter/test/main/func_inferred_type_args_2.ets.arkts2.json +++ b/ets2panda/linter/test/main/func_inferred_type_args_2.ets.arkts2.json @@ -134,16 +134,6 @@ "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", "severity": "ERROR" }, - { - "line": 27, - "column": 10, - "endLine": 27, - "endColumn": 23, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, { "line": 27, "column": 39, @@ -276,12 +266,12 @@ }, { "line": 52, - "column": 10, + "column": 14, "endLine": 52, - "endColumn": 19, - "problem": "StructuralIdentity", + "endColumn": 17, + "problem": "ConstructorIfaceFromSdk", "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", + "rule": "Construct signatures are not supported in interfaces.(sdk-ctor-signatures-iface)", "severity": "ERROR" }, { @@ -296,12 +286,12 @@ }, { "line": 56, - "column": 10, + "column": 14, "endLine": 56, - "endColumn": 19, - "problem": "StructuralIdentity", + "endColumn": 17, + "problem": "ConstructorIfaceFromSdk", "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", + "rule": "Construct signatures are not supported in interfaces.(sdk-ctor-signatures-iface)", "severity": "ERROR" }, { @@ -336,12 +326,12 @@ }, { "line": 69, - "column": 12, + "column": 16, "endLine": 69, - "endColumn": 21, - "problem": "StructuralIdentity", + "endColumn": 19, + "problem": "ConstructorIfaceFromSdk", "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", + "rule": "Construct signatures are not supported in interfaces.(sdk-ctor-signatures-iface)", "severity": "ERROR" }, { @@ -356,12 +346,12 @@ }, { "line": 73, - "column": 12, + "column": 16, "endLine": 73, - "endColumn": 21, - "problem": "StructuralIdentity", + "endColumn": 19, + "problem": "ConstructorIfaceFromSdk", "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", + "rule": "Construct signatures are not supported in interfaces.(sdk-ctor-signatures-iface)", "severity": "ERROR" }, { diff --git a/ets2panda/linter/test/main/func_inferred_type_args_2.ets.autofix.json b/ets2panda/linter/test/main/func_inferred_type_args_2.ets.autofix.json index c53b7da247fd5afc1070851dfb394632b7dedcc7..45ba2c3c5750e3a8f478271563b33dad9a0c6fcc 100644 --- a/ets2panda/linter/test/main/func_inferred_type_args_2.ets.autofix.json +++ b/ets2panda/linter/test/main/func_inferred_type_args_2.ets.autofix.json @@ -178,16 +178,6 @@ "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", "severity": "ERROR" }, - { - "line": 27, - "column": 10, - "endLine": 27, - "endColumn": 23, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, { "line": 27, "column": 39, @@ -419,12 +409,12 @@ }, { "line": 52, - "column": 10, + "column": 14, "endLine": 52, - "endColumn": 19, - "problem": "StructuralIdentity", + "endColumn": 17, + "problem": "ConstructorIfaceFromSdk", "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", + "rule": "Construct signatures are not supported in interfaces.(sdk-ctor-signatures-iface)", "severity": "ERROR" }, { @@ -439,12 +429,12 @@ }, { "line": 56, - "column": 10, + "column": 14, "endLine": 56, - "endColumn": 19, - "problem": "StructuralIdentity", + "endColumn": 17, + "problem": "ConstructorIfaceFromSdk", "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", + "rule": "Construct signatures are not supported in interfaces.(sdk-ctor-signatures-iface)", "severity": "ERROR" }, { @@ -501,12 +491,12 @@ }, { "line": 69, - "column": 12, + "column": 16, "endLine": 69, - "endColumn": 21, - "problem": "StructuralIdentity", + "endColumn": 19, + "problem": "ConstructorIfaceFromSdk", "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", + "rule": "Construct signatures are not supported in interfaces.(sdk-ctor-signatures-iface)", "severity": "ERROR" }, { @@ -521,12 +511,12 @@ }, { "line": 73, - "column": 12, + "column": 16, "endLine": 73, - "endColumn": 21, - "problem": "StructuralIdentity", + "endColumn": 19, + "problem": "ConstructorIfaceFromSdk", "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", + "rule": "Construct signatures are not supported in interfaces.(sdk-ctor-signatures-iface)", "severity": "ERROR" }, { diff --git a/ets2panda/linter/test/main/func_inferred_type_args_2.ets.migrate.json b/ets2panda/linter/test/main/func_inferred_type_args_2.ets.migrate.json index 0b10990af1e4f586fff32414b1e2111eb5fe7f15..33f6cfa4a97221bc1e94c554a63d7bfbeb57af28 100644 --- a/ets2panda/linter/test/main/func_inferred_type_args_2.ets.migrate.json +++ b/ets2panda/linter/test/main/func_inferred_type_args_2.ets.migrate.json @@ -94,16 +94,6 @@ "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", "severity": "ERROR" }, - { - "line": 34, - "column": 10, - "endLine": 34, - "endColumn": 23, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, { "line": 34, "column": 39, @@ -146,12 +136,12 @@ }, { "line": 59, - "column": 10, + "column": 14, "endLine": 59, - "endColumn": 19, - "problem": "StructuralIdentity", + "endColumn": 17, + "problem": "ConstructorIfaceFromSdk", "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", + "rule": "Construct signatures are not supported in interfaces.(sdk-ctor-signatures-iface)", "severity": "ERROR" }, { @@ -166,12 +156,12 @@ }, { "line": 63, - "column": 10, + "column": 14, "endLine": 63, - "endColumn": 19, - "problem": "StructuralIdentity", + "endColumn": 17, + "problem": "ConstructorIfaceFromSdk", "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", + "rule": "Construct signatures are not supported in interfaces.(sdk-ctor-signatures-iface)", "severity": "ERROR" }, { @@ -186,12 +176,12 @@ }, { "line": 76, - "column": 12, + "column": 16, "endLine": 76, - "endColumn": 21, - "problem": "StructuralIdentity", + "endColumn": 19, + "problem": "ConstructorIfaceFromSdk", "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", + "rule": "Construct signatures are not supported in interfaces.(sdk-ctor-signatures-iface)", "severity": "ERROR" }, { @@ -206,12 +196,12 @@ }, { "line": 80, - "column": 12, + "column": 16, "endLine": 80, - "endColumn": 21, - "problem": "StructuralIdentity", + "endColumn": 19, + "problem": "ConstructorIfaceFromSdk", "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", + "rule": "Construct signatures are not supported in interfaces.(sdk-ctor-signatures-iface)", "severity": "ERROR" }, { diff --git a/ets2panda/linter/test/main/structural_identity.ets.arkts2.json b/ets2panda/linter/test/main/structural_identity.ets.arkts2.json index cadab9b537c3cf72ee3692e146eaf5b553907b1d..1e94e23adaed9cd38000ca8589112a8a9a3f8948 100644 --- a/ets2panda/linter/test/main/structural_identity.ets.arkts2.json +++ b/ets2panda/linter/test/main/structural_identity.ets.arkts2.json @@ -15,623 +15,33 @@ ], "result": [ { - "line": 46, - "column": 5, - "endLine": 46, - "endColumn": 10, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 46, - "column": 19, - "endLine": 46, - "endColumn": 24, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 49, - "column": 1, - "endLine": 49, - "endColumn": 10, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 51, - "column": 5, - "endLine": 51, - "endColumn": 17, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 54, - "column": 3, - "endLine": 54, - "endColumn": 18, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 54, - "column": 3, - "endLine": 54, - "endColumn": 10, - "problem": "RuntimeArrayCheck", - "suggest": "", - "rule": "Array bound not checked. (arkts-runtime-array-check)", - "severity": "ERROR" - }, - { - "line": 57, - "column": 13, - "endLine": 57, - "endColumn": 23, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 108, - "column": 4, - "endLine": 108, - "endColumn": 5, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 109, - "column": 4, - "endLine": 109, - "endColumn": 6, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 111, - "column": 4, - "endLine": 111, - "endColumn": 6, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 112, - "column": 4, - "endLine": 112, - "endColumn": 5, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 115, - "column": 4, - "endLine": 115, - "endColumn": 6, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 138, - "column": 4, - "endLine": 138, - "endColumn": 6, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 139, - "column": 4, - "endLine": 139, - "endColumn": 6, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 140, - "column": 4, - "endLine": 140, - "endColumn": 7, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 141, - "column": 4, - "endLine": 141, - "endColumn": 7, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 142, - "column": 4, - "endLine": 142, - "endColumn": 7, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 143, - "column": 4, - "endLine": 143, - "endColumn": 7, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 144, - "column": 4, - "endLine": 144, - "endColumn": 6, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 145, - "column": 4, - "endLine": 145, - "endColumn": 6, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 146, - "column": 4, - "endLine": 146, - "endColumn": 7, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 147, - "column": 4, - "endLine": 147, - "endColumn": 7, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 148, - "column": 4, - "endLine": 148, - "endColumn": 7, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 149, - "column": 4, - "endLine": 149, - "endColumn": 7, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 175, - "column": 4, - "endLine": 175, - "endColumn": 6, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 176, - "column": 4, - "endLine": 176, - "endColumn": 6, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 177, - "column": 4, - "endLine": 177, - "endColumn": 7, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 178, - "column": 4, - "endLine": 178, - "endColumn": 7, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 179, - "column": 4, - "endLine": 179, - "endColumn": 7, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 180, - "column": 4, - "endLine": 180, - "endColumn": 7, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 181, - "column": 4, - "endLine": 181, - "endColumn": 6, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 182, - "column": 4, - "endLine": 182, - "endColumn": 6, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 183, - "column": 4, - "endLine": 183, - "endColumn": 7, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 184, - "column": 4, - "endLine": 184, - "endColumn": 7, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 185, - "column": 4, - "endLine": 185, - "endColumn": 7, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 186, - "column": 4, - "endLine": 186, - "endColumn": 7, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 214, - "column": 1, - "endLine": 214, - "endColumn": 15, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 215, - "column": 1, - "endLine": 215, - "endColumn": 16, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 217, - "column": 1, - "endLine": 217, - "endColumn": 16, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 218, - "column": 1, - "endLine": 218, - "endColumn": 15, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 221, - "column": 1, - "endLine": 221, - "endColumn": 16, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 319, - "column": 15, - "endLine": 319, - "endColumn": 16, - "problem": "NumericSemantics", - "suggest": "", - "rule": "Numeric semantics is different for integer values (arkts-numeric-semantic)", - "severity": "ERROR" - }, - { - "line": 329, - "column": 10, - "endLine": 329, - "endColumn": 21, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 333, - "column": 12, - "endLine": 333, - "endColumn": 18, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 341, - "column": 3, - "endLine": 341, - "endColumn": 9, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 344, - "column": 3, - "endLine": 344, - "endColumn": 10, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 345, - "column": 3, - "endLine": 345, - "endColumn": 10, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 348, - "column": 3, - "endLine": 348, - "endColumn": 11, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 351, - "column": 3, - "endLine": 351, - "endColumn": 8, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 356, - "column": 3, - "endLine": 356, - "endColumn": 10, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 357, + "line": 54, "column": 3, - "endLine": 357, + "endLine": 54, "endColumn": 10, - "problem": "StructuralIdentity", + "problem": "RuntimeArrayCheck", "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", + "rule": "Array bound not checked. (arkts-runtime-array-check)", "severity": "ERROR" }, { - "line": 360, - "column": 3, - "endLine": 360, - "endColumn": 10, + "line": 57, + "column": 13, + "endLine": 57, + "endColumn": 23, "problem": "StructuralIdentity", "suggest": "", "rule": "Structural typing is not supported (arkts-no-structural-typing)", "severity": "ERROR" }, { - "line": 451, - "column": 3, - "endLine": 451, + "line": 319, + "column": 15, + "endLine": 319, "endColumn": 16, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 460, - "column": 3, - "endLine": 460, - "endColumn": 37, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 463, - "column": 3, - "endLine": 463, - "endColumn": 20, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 464, - "column": 3, - "endLine": 464, - "endColumn": 22, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 465, - "column": 3, - "endLine": 465, - "endColumn": 17, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 467, - "column": 3, - "endLine": 467, - "endColumn": 50, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 468, - "column": 3, - "endLine": 468, - "endColumn": 42, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 469, - "column": 3, - "endLine": 469, - "endColumn": 52, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 471, - "column": 3, - "endLine": 471, - "endColumn": 43, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 472, - "column": 3, - "endLine": 472, - "endColumn": 43, - "problem": "StructuralIdentity", + "problem": "NumericSemantics", "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", + "rule": "Numeric semantics is different for integer values (arkts-numeric-semantic)", "severity": "ERROR" }, { @@ -654,16 +64,6 @@ "rule": "Usage of standard library is restricted(arkts-limited-stdlib-no-sendable-decorator)", "severity": "ERROR" }, - { - "line": 502, - "column": 7, - "endLine": 502, - "endColumn": 30, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, { "line": 503, "column": 28, @@ -684,16 +84,6 @@ "rule": "Structural typing is not supported (arkts-no-structural-typing)", "severity": "ERROR" }, - { - "line": 511, - "column": 1, - "endLine": 511, - "endColumn": 8, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, { "line": 512, "column": 6, @@ -714,16 +104,6 @@ "rule": "Structural typing is not supported (arkts-no-structural-typing)", "severity": "ERROR" }, - { - "line": 520, - "column": 4, - "endLine": 520, - "endColumn": 6, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, { "line": 521, "column": 4, @@ -744,16 +124,6 @@ "rule": "Structural typing is not supported (arkts-no-structural-typing)", "severity": "ERROR" }, - { - "line": 527, - "column": 21, - "endLine": 527, - "endColumn": 23, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, { "line": 528, "column": 21, @@ -774,16 +144,6 @@ "rule": "Structural typing is not supported (arkts-no-structural-typing)", "severity": "ERROR" }, - { - "line": 542, - "column": 9, - "endLine": 542, - "endColumn": 11, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, { "line": 543, "column": 9, @@ -874,166 +234,6 @@ "rule": "Structural typing is not supported (arkts-no-structural-typing)", "severity": "ERROR" }, - { - "line": 574, - "column": 44, - "endLine": 574, - "endColumn": 49, - "problem": "NoTsLikeSmartType", - "suggest": "", - "rule": "Smart type differences (arkts-no-ts-like-smart-type)", - "severity": "ERROR" - }, - { - "line": 575, - "column": 44, - "endLine": 575, - "endColumn": 49, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 575, - "column": 44, - "endLine": 575, - "endColumn": 49, - "problem": "NoTsLikeSmartType", - "suggest": "", - "rule": "Smart type differences (arkts-no-ts-like-smart-type)", - "severity": "ERROR" - }, - { - "line": 576, - "column": 38, - "endLine": 576, - "endColumn": 43, - "problem": "NoTsLikeSmartType", - "suggest": "", - "rule": "Smart type differences (arkts-no-ts-like-smart-type)", - "severity": "ERROR" - }, - { - "line": 577, - "column": 38, - "endLine": 577, - "endColumn": 43, - "problem": "NoTsLikeSmartType", - "suggest": "", - "rule": "Smart type differences (arkts-no-ts-like-smart-type)", - "severity": "ERROR" - }, - { - "line": 578, - "column": 38, - "endLine": 578, - "endColumn": 43, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 578, - "column": 38, - "endLine": 578, - "endColumn": 43, - "problem": "NoTsLikeSmartType", - "suggest": "", - "rule": "Smart type differences (arkts-no-ts-like-smart-type)", - "severity": "ERROR" - }, - { - "line": 579, - "column": 44, - "endLine": 579, - "endColumn": 49, - "problem": "NoTsLikeSmartType", - "suggest": "", - "rule": "Smart type differences (arkts-no-ts-like-smart-type)", - "severity": "ERROR" - }, - { - "line": 580, - "column": 44, - "endLine": 580, - "endColumn": 49, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 580, - "column": 44, - "endLine": 580, - "endColumn": 49, - "problem": "NoTsLikeSmartType", - "suggest": "", - "rule": "Smart type differences (arkts-no-ts-like-smart-type)", - "severity": "ERROR" - }, - { - "line": 590, - "column": 10, - "endLine": 590, - "endColumn": 20, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 593, - "column": 10, - "endLine": 593, - "endColumn": 20, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 599, - "column": 32, - "endLine": 599, - "endColumn": 36, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 600, - "column": 26, - "endLine": 600, - "endColumn": 30, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 604, - "column": 3, - "endLine": 604, - "endColumn": 4, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 605, - "column": 3, - "endLine": 605, - "endColumn": 4, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, { "line": 609, "column": 15, @@ -1094,16 +294,6 @@ "rule": "Use explicit types instead of \"any\", \"unknown\" (arkts-no-any-unknown)", "severity": "ERROR" }, - { - "line": 626, - "column": 5, - "endLine": 626, - "endColumn": 22, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, { "line": 627, "column": 18, diff --git a/ets2panda/linter/test/main/structural_identity1.ets b/ets2panda/linter/test/main/structural_identity1.ets new file mode 100755 index 0000000000000000000000000000000000000000..76ae7461e50979fa39c06d71c74ef4cb62fdb550 --- /dev/null +++ b/ets2panda/linter/test/main/structural_identity1.ets @@ -0,0 +1,281 @@ +/* + * 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 Base {} + +class Base1 {} + +interface I1 { + v1: string; +} + +interface I2 { + v1:string; +} +interface I3 { + v1:string; + v2:string +} + +interface I4 { + v1:string; + test():string +} + +interface I5 { + v1:string; + test():string +} + +interface I6 { + v1:string; + test():void +} + +interface I7 { + v1:number; + test():string +} + +class C { + u: T; + constructor(u: T) { + this.u = u; + } +} + +class A extends Base { + v: string = '0'; + test(): void {} +} + +class B extends Base { + v: string = '0'; + test(): void {} +} + +let b: B = new A(); + +let bb: B; +bb = new A(); + +class C1 { + b: B = new A(); +} + +function greeter(b: B): void { + b.test(); +} +let b1: B = new B(); +let a1: A = new A(); +greeter(b1); +greeter(a1); + +let b: C = new C(new A()); // not error + +class A1 implements I1 { + v: string = '0'; + test(): void {} +} + +class B1 implements I1 { + v: string = '0'; + test(): void {} +} + +let b: C = new C(new A1()); // not error + +class A2 extends Base { + v: string = '0'; + test(): void {} +} + +class B2 extends Base1 { + v: string = '0'; + test(): void {} +} + +let b: C = new C(new A2()); // error + +class A3 implements I1 { + v: string = '0'; + test(): void {} +} + +class B3 implements I2 { + v: string = '0'; + test(): void {} +} + +let b: C = new C(new A3()); // error + +class A4 extends Base { + v: string = '0'; + test(): void {} +} + +class B4 extends Base1 { + private v: string = '0'; + test(): void {} +} + +let b: C = new C(new A4()); // not error + +class A5 implements I1 { + private v: string = '0'; + test(): void {} +} + +class B5 implements I2 { + v: string = '0'; + test(): void {} +} + +let b: C = new C(new A5()); // not error + + +class D { + v: string = '0' + test(): void {} +} +class E { + v: string = '0' + test(): void {} +} + +let b1: C = new C(new E()) //ArkTS1.1 OK,ArkTS1.2 ERROR +let b2: C = new C({v1:"This is I2"}) //ArkTS1.1 OK,ArkTS1.2 ERROR +let b3: C = new C({v1:"This is I3.v1", v2:"This is I3.v2"}) //ArkTS1.1 OK,ArkTS1.2 ERROR +let b4: C = new C({v1:"This is I2"}) + +let foo1 = ():D => { return new E() } //ArkTS1.1 OK,ArkTS1.2 ERROR +let foo2 = ():I1 => { return {v1:"This is I3.v1", v2:"This is I3.v2"} } +let foo3 = ():I1 => { return {v1:"This is I3.v1", v2:"This is I3.v2"} as I3 } //ArkTS1.1 OK,ArkTS1.2 ERROR +let foo4 = ():I3 => { return {v1:"This is I1"} } + +function foo1():D { + return new E(); //ArkTS1.1 OK,ArkTS1.2 ERROR +} +function foo2 ():I1 { + return {v1:"This is I3.v1", v2:"This is I3.v2"}; +} + +function foo3 ():I1 { + return {v1:"This is I3.v1", v2:"This is I3.v2"} as I3; //ArkTS1.1 OK,ArkTS1.2 ERROR +} +function foo4 ():I3 { + return {v1:"This is I1"}; +} + +class F { + getD():D { + return new E(); //ArkTS1.2ERROR + } +} +class G { + getI1():I1 { + return {v1:"This is I3.v1", v2:"This is I3.v2"}; + } +} +class H { + getI1():I1 { + return {v1:"This is I3.v1", v2:"This is I3.v2"} as I3; //ArkTS1.1 OK,ArkTS1.2 ERROR + } +} +class J { + getI3():I3 { + return {v1:"This is I1"}; + } +} + +function func1(): I4 { + const obj: I5 = { + v1: "Hello", + test() { + return this.v1; + } + }; + return obj; // ArkTS1.2 ERROR +} + +function func2(): I4 { + const obj: I6 = { + v1: "Hello", + test() { + } + }; + return obj; // ArkTS1.2ERROR +} + +function func3(): I4 { + const obj: I7 = { + v1: 1, + test() { + return this.v1; + } + }; + return obj; +} + +//case1 +import image from '@ohos.multimedia.image'; + +class Z { + protected async loadImages(fileNameList?: string[]): Promise { + let imageArr: image.PixelMap[] = []; + return imageArr; + } + + public async loadImagesTest(fileNameList?: string[]): Promise { + return this.loadImages(fileNameList); + } +} + +return this.loadImages(fileNameList);//no need report arkts-no-structural-typing + +//case2 +class K {} +class L {} +function test(ab?: K|L) { + const a = ab as K ?? new K(); // no need report arkts-no-structural-typing + const b = ab as L ?? new L(); // no need report arkts-no-structural-typing +} + +//case3 +class U { + +} + +class I { + +} + +class P extends U { + +} + +function tt1(): U | null { + return new I(); // no need report arkts-no-structural-typing +} + +function tt2(a?: U): P | undefined { + return a ? a as p : undefined;// no need report arkts-no-structural-typing +} + +function sleep(ms: number): PromiseLike { + return new Promise( + (resolve: (value: T | PromiseLike) => void): number => setTimeout(resolve, ms) // no need report arkts-no-structural-typing + ); +} \ No newline at end of file diff --git a/ets2panda/linter/test/main/structural_identity1.ets.args.json b/ets2panda/linter/test/main/structural_identity1.ets.args.json new file mode 100755 index 0000000000000000000000000000000000000000..5513609b5b57781e787b7a34eadb988b508bd87b --- /dev/null +++ b/ets2panda/linter/test/main/structural_identity1.ets.args.json @@ -0,0 +1,19 @@ +{ + "copyright": [ + "Copyright (c) 2024 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/structural_identity1.ets.arkts2.json b/ets2panda/linter/test/main/structural_identity1.ets.arkts2.json new file mode 100755 index 0000000000000000000000000000000000000000..3678561addd2c17c034e116edad209ce1b3f23dc --- /dev/null +++ b/ets2panda/linter/test/main/structural_identity1.ets.arkts2.json @@ -0,0 +1,328 @@ +{ + "copyright": [ + "Copyright (c) 2024-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": 110, + "column": 5, + "endLine": 110, + "endColumn": 35, + "problem": "StructuralIdentity", + "suggest": "", + "rule": "Structural typing is not supported (arkts-no-structural-typing)", + "severity": "ERROR" + }, + { + "line": 122, + "column": 5, + "endLine": 122, + "endColumn": 35, + "problem": "StructuralIdentity", + "suggest": "", + "rule": "Structural typing is not supported (arkts-no-structural-typing)", + "severity": "ERROR" + }, + { + "line": 158, + "column": 5, + "endLine": 158, + "endColumn": 33, + "problem": "StructuralIdentity", + "suggest": "", + "rule": "Structural typing is not supported (arkts-no-structural-typing)", + "severity": "ERROR" + }, + { + "line": 159, + "column": 5, + "endLine": 159, + "endColumn": 45, + "problem": "StructuralIdentity", + "suggest": "", + "rule": "Structural typing is not supported (arkts-no-structural-typing)", + "severity": "ERROR" + }, + { + "line": 160, + "column": 5, + "endLine": 160, + "endColumn": 68, + "problem": "StructuralIdentity", + "suggest": "", + "rule": "Structural typing is not supported (arkts-no-structural-typing)", + "severity": "ERROR" + }, + { + "line": 163, + "column": 29, + "endLine": 163, + "endColumn": 36, + "problem": "StructuralIdentity", + "suggest": "", + "rule": "Structural typing is not supported (arkts-no-structural-typing)", + "severity": "ERROR" + }, + { + "line": 164, + "column": 30, + "endLine": 164, + "endColumn": 31, + "problem": "ObjectLiteralNoContextType", + "suggest": "", + "rule": "Object literal must correspond to some explicitly declared class or interface (arkts-no-untyped-obj-literals)", + "severity": "ERROR" + }, + { + "line": 165, + "column": 30, + "endLine": 165, + "endColumn": 76, + "problem": "StructuralIdentity", + "suggest": "", + "rule": "Structural typing is not supported (arkts-no-structural-typing)", + "severity": "ERROR" + }, + { + "line": 169, + "column": 10, + "endLine": 169, + "endColumn": 17, + "problem": "StructuralIdentity", + "suggest": "", + "rule": "Structural typing is not supported (arkts-no-structural-typing)", + "severity": "ERROR" + }, + { + "line": 172, + "column": 10, + "endLine": 172, + "endColumn": 11, + "problem": "ObjectLiteralNoContextType", + "suggest": "", + "rule": "Object literal must correspond to some explicitly declared class or interface (arkts-no-untyped-obj-literals)", + "severity": "ERROR" + }, + { + "line": 176, + "column": 10, + "endLine": 176, + "endColumn": 56, + "problem": "StructuralIdentity", + "suggest": "", + "rule": "Structural typing is not supported (arkts-no-structural-typing)", + "severity": "ERROR" + }, + { + "line": 184, + "column": 16, + "endLine": 184, + "endColumn": 23, + "problem": "StructuralIdentity", + "suggest": "", + "rule": "Structural typing is not supported (arkts-no-structural-typing)", + "severity": "ERROR" + }, + { + "line": 189, + "column": 16, + "endLine": 189, + "endColumn": 17, + "problem": "ObjectLiteralNoContextType", + "suggest": "", + "rule": "Object literal must correspond to some explicitly declared class or interface (arkts-no-untyped-obj-literals)", + "severity": "ERROR" + }, + { + "line": 194, + "column": 16, + "endLine": 194, + "endColumn": 62, + "problem": "StructuralIdentity", + "suggest": "", + "rule": "Structural typing is not supported (arkts-no-structural-typing)", + "severity": "ERROR" + }, + { + "line": 207, + "column": 14, + "endLine": 207, + "endColumn": 18, + "problem": "FunctionContainsThis", + "suggest": "", + "rule": "Using \"this\" inside stand-alone functions is not supported (arkts-no-standalone-this)", + "severity": "ERROR" + }, + { + "line": 204, + "column": 19, + "endLine": 204, + "endColumn": 20, + "problem": "ObjectLiteralNoContextType", + "suggest": "", + "rule": "Object literal must correspond to some explicitly declared class or interface (arkts-no-untyped-obj-literals)", + "severity": "ERROR" + }, + { + "line": 206, + "column": 5, + "endLine": 208, + "endColumn": 6, + "problem": "ObjectLiteralProperty", + "suggest": "", + "rule": "Object literal properties can only contain name-value pairs (arkts-obj-literal-props)", + "severity": "ERROR" + }, + { + "line": 206, + "column": 5, + "endLine": 206, + "endColumn": 9, + "problem": "LimitedReturnTypeInference", + "suggest": "", + "rule": "Function return type inference is limited (arkts-no-implicit-return-types)", + "severity": "ERROR" + }, + { + "line": 210, + "column": 10, + "endLine": 210, + "endColumn": 13, + "problem": "StructuralIdentity", + "suggest": "", + "rule": "Structural typing is not supported (arkts-no-structural-typing)", + "severity": "ERROR" + }, + { + "line": 214, + "column": 19, + "endLine": 214, + "endColumn": 20, + "problem": "ObjectLiteralNoContextType", + "suggest": "", + "rule": "Object literal must correspond to some explicitly declared class or interface (arkts-no-untyped-obj-literals)", + "severity": "ERROR" + }, + { + "line": 216, + "column": 5, + "endLine": 217, + "endColumn": 6, + "problem": "ObjectLiteralProperty", + "suggest": "", + "rule": "Object literal properties can only contain name-value pairs (arkts-obj-literal-props)", + "severity": "ERROR" + }, + { + "line": 219, + "column": 10, + "endLine": 219, + "endColumn": 13, + "problem": "StructuralIdentity", + "suggest": "", + "rule": "Structural typing is not supported (arkts-no-structural-typing)", + "severity": "ERROR" + }, + { + "line": 226, + "column": 14, + "endLine": 226, + "endColumn": 18, + "problem": "FunctionContainsThis", + "suggest": "", + "rule": "Using \"this\" inside stand-alone functions is not supported (arkts-no-standalone-this)", + "severity": "ERROR" + }, + { + "line": 223, + "column": 19, + "endLine": 223, + "endColumn": 20, + "problem": "ObjectLiteralNoContextType", + "suggest": "", + "rule": "Object literal must correspond to some explicitly declared class or interface (arkts-no-untyped-obj-literals)", + "severity": "ERROR" + }, + { + "line": 225, + "column": 5, + "endLine": 227, + "endColumn": 6, + "problem": "ObjectLiteralProperty", + "suggest": "", + "rule": "Object literal properties can only contain name-value pairs (arkts-obj-literal-props)", + "severity": "ERROR" + }, + { + "line": 224, + "column": 9, + "endLine": 224, + "endColumn": 10, + "problem": "NumericSemantics", + "suggest": "", + "rule": "Numeric semantics is different for integer values (arkts-numeric-semantic)", + "severity": "ERROR" + }, + { + "line": 225, + "column": 5, + "endLine": 225, + "endColumn": 9, + "problem": "LimitedReturnTypeInference", + "suggest": "", + "rule": "Function return type inference is limited (arkts-no-implicit-return-types)", + "severity": "ERROR" + }, + { + "line": 233, + "column": 1, + "endLine": 233, + "endColumn": 44, + "problem": "ImportAfterStatement", + "suggest": "", + "rule": "\"import\" statements after other statements are not allowed (arkts-no-misplaced-imports)", + "severity": "ERROR" + }, + { + "line": 278, + "column": 10, + "endLine": 280, + "endColumn": 4, + "problem": "GenericCallNoTypeArgs", + "suggest": "", + "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", + "severity": "ERROR" + }, + { + "line": 279, + "column": 71, + "endLine": 279, + "endColumn": 78, + "problem": "NoTsLikeSmartType", + "suggest": "", + "rule": "Smart type differences (arkts-no-ts-like-smart-type)", + "severity": "ERROR" + }, + { + "line": 279, + "column": 80, + "endLine": 279, + "endColumn": 82, + "problem": "NoTsLikeSmartType", + "suggest": "", + "rule": "Smart type differences (arkts-no-ts-like-smart-type)", + "severity": "ERROR" + } + ] +} \ No newline at end of file diff --git a/ets2panda/linter/test/main/structural_identity1.ets.json b/ets2panda/linter/test/main/structural_identity1.ets.json new file mode 100755 index 0000000000000000000000000000000000000000..aa05c66edd8500fec0b6f8bfbd789722fca0d0e3 --- /dev/null +++ b/ets2panda/linter/test/main/structural_identity1.ets.json @@ -0,0 +1,158 @@ +{ + "copyright": [ + "Copyright (c) 2023-2024 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": 69, + "column": 5, + "endLine": 69, + "endColumn": 19, + "problem": "StructuralIdentity", + "suggest": "", + "rule": "Structural typing is not supported (arkts-no-structural-typing)", + "severity": "ERROR" + }, + { + "line": 72, + "column": 1, + "endLine": 72, + "endColumn": 13, + "problem": "StructuralIdentity", + "suggest": "", + "rule": "Structural typing is not supported (arkts-no-structural-typing)", + "severity": "ERROR" + }, + { + "line": 84, + "column": 9, + "endLine": 84, + "endColumn": 11, + "problem": "StructuralIdentity", + "suggest": "", + "rule": "Structural typing is not supported (arkts-no-structural-typing)", + "severity": "ERROR" + }, + { + "line": 164, + "column": 30, + "endLine": 164, + "endColumn": 31, + "problem": "ObjectLiteralNoContextType", + "suggest": "", + "rule": "Object literal must correspond to some explicitly declared class or interface (arkts-no-untyped-obj-literals)", + "severity": "ERROR" + }, + { + "line": 172, + "column": 10, + "endLine": 172, + "endColumn": 11, + "problem": "ObjectLiteralNoContextType", + "suggest": "", + "rule": "Object literal must correspond to some explicitly declared class or interface (arkts-no-untyped-obj-literals)", + "severity": "ERROR" + }, + { + "line": 189, + "column": 16, + "endLine": 189, + "endColumn": 17, + "problem": "ObjectLiteralNoContextType", + "suggest": "", + "rule": "Object literal must correspond to some explicitly declared class or interface (arkts-no-untyped-obj-literals)", + "severity": "ERROR" + }, + { + "line": 207, + "column": 14, + "endLine": 207, + "endColumn": 18, + "problem": "FunctionContainsThis", + "suggest": "", + "rule": "Using \"this\" inside stand-alone functions is not supported (arkts-no-standalone-this)", + "severity": "ERROR" + }, + { + "line": 204, + "column": 19, + "endLine": 204, + "endColumn": 20, + "problem": "ObjectLiteralNoContextType", + "suggest": "", + "rule": "Object literal must correspond to some explicitly declared class or interface (arkts-no-untyped-obj-literals)", + "severity": "ERROR" + }, + { + "line": 206, + "column": 5, + "endLine": 206, + "endColumn": 9, + "problem": "LimitedReturnTypeInference", + "suggest": "", + "rule": "Function return type inference is limited (arkts-no-implicit-return-types)", + "severity": "ERROR" + }, + { + "line": 214, + "column": 19, + "endLine": 214, + "endColumn": 20, + "problem": "ObjectLiteralNoContextType", + "suggest": "", + "rule": "Object literal must correspond to some explicitly declared class or interface (arkts-no-untyped-obj-literals)", + "severity": "ERROR" + }, + { + "line": 226, + "column": 14, + "endLine": 226, + "endColumn": 18, + "problem": "FunctionContainsThis", + "suggest": "", + "rule": "Using \"this\" inside stand-alone functions is not supported (arkts-no-standalone-this)", + "severity": "ERROR" + }, + { + "line": 223, + "column": 19, + "endLine": 223, + "endColumn": 20, + "problem": "ObjectLiteralNoContextType", + "suggest": "", + "rule": "Object literal must correspond to some explicitly declared class or interface (arkts-no-untyped-obj-literals)", + "severity": "ERROR" + }, + { + "line": 225, + "column": 5, + "endLine": 225, + "endColumn": 9, + "problem": "LimitedReturnTypeInference", + "suggest": "", + "rule": "Function return type inference is limited (arkts-no-implicit-return-types)", + "severity": "ERROR" + }, + { + "line": 233, + "column": 1, + "endLine": 233, + "endColumn": 44, + "problem": "ImportAfterStatement", + "suggest": "", + "rule": "\"import\" statements after other statements are not allowed (arkts-no-misplaced-imports)", + "severity": "ERROR" + } + ] +} \ No newline at end of file diff --git a/ets2panda/linter/test/main/structural_identity_extended_inheritance.ets.arkts2.json b/ets2panda/linter/test/main/structural_identity_extended_inheritance.ets.arkts2.json index 72f2d82336d0211362673a622b13fd07d4a314fa..7369a7adf678a7a532fe6f91a32ddc0f26fd22fc 100644 --- a/ets2panda/linter/test/main/structural_identity_extended_inheritance.ets.arkts2.json +++ b/ets2panda/linter/test/main/structural_identity_extended_inheritance.ets.arkts2.json @@ -174,26 +174,6 @@ "rule": "Array type is immutable in ArkTS1.2 (arkts-array-type-immutable)", "severity": "ERROR" }, - { - "line": 73, - "column": 1, - "endLine": 73, - "endColumn": 8, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, - { - "line": 74, - "column": 1, - "endLine": 74, - "endColumn": 8, - "problem": "StructuralIdentity", - "suggest": "", - "rule": "Structural typing is not supported (arkts-no-structural-typing)", - "severity": "ERROR" - }, { "line": 66, "column": 3,