From d6ea2cab6018ede3127eb2e9d938feb535315a73 Mon Sep 17 00:00:00 2001 From: zhongning5 Date: Wed, 23 Jul 2025 15:08:01 +0800 Subject: [PATCH] fix arkts-no-inferred-generic-params Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICMV4K Test scenarios:Fix bugs for arkts-no-inferred-generic-params Signed-off-by: zhongning5 --- ets2panda/linter/src/lib/TypeScriptLinter.ts | 74 +++- .../linter/src/lib/autofixes/Autofixer.ts | 183 ++++++++- .../appStorageIsMutable_api.ets.arkts2.json | 12 +- .../test/main/func_inferred_type_args_3.ets | 24 +- .../func_inferred_type_args_3.ets.arkts2.json | 210 +++++++++- ...func_inferred_type_args_3.ets.autofix.json | 388 +++++++++++++++++- .../main/func_inferred_type_args_3.ets.json | 43 +- .../func_inferred_type_args_3.ets.migrate.ets | 31 +- ...func_inferred_type_args_3.ets.migrate.json | 58 ++- .../main/oh_modules/common_ts_ets_api.d.ts | 46 +++ 10 files changed, 1010 insertions(+), 59 deletions(-) create mode 100644 ets2panda/linter/test/main/oh_modules/common_ts_ets_api.d.ts diff --git a/ets2panda/linter/src/lib/TypeScriptLinter.ts b/ets2panda/linter/src/lib/TypeScriptLinter.ts index 90024f4880..cf4c52a64a 100644 --- a/ets2panda/linter/src/lib/TypeScriptLinter.ts +++ b/ets2panda/linter/src/lib/TypeScriptLinter.ts @@ -5544,7 +5544,11 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { const providedTypeArgs = callLikeExpr.typeArguments; const startTypeArg = providedTypeArgs?.length ?? 0; let shouldReportError = startTypeArg !== resolvedTypeArgs.length; - if (this.options.arkts2 && callLikeExpr.kind === ts.SyntaxKind.NewExpression) { + const shouldCheck = this.shouldCheckGenericCallExpression(callLikeExpr as ts.CallExpression); + if ( + this.options.arkts2 && + (ts.isNewExpression(callLikeExpr) || ts.isCallExpression(callLikeExpr) && shouldCheck) + ) { shouldReportError = this.shouldReportGenericTypeArgsError( callLikeExpr, resolvedTypeArgs, @@ -5561,6 +5565,69 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { } } + private shouldCheckGenericCallExpression(callExpr: ts.CallExpression): boolean { + const signature = this.tsTypeChecker.getResolvedSignature(callExpr); + if (!signature?.declaration) { + return false; + } + const typeParamsSafeToInfer = this.areTypeParametersReturnTypeOnly(signature.declaration); + if (!typeParamsSafeToInfer) { + return false; + } + return TypeScriptLinter.isInStrictTypeContext(callExpr); + } + + private areTypeParametersReturnTypeOnly(decl: ts.SignatureDeclaration | ts.JSDocSignature): boolean { + if (!decl.typeParameters?.length) { + return false; + } + + const typeParamNames = new Set( + decl.typeParameters.map((tp) => { + return tp.name.getText(); + }) + ); + let affectsParams = false; + + decl.parameters.forEach((param) => { + if (param.type && this.containsTypeParameters(param.type, typeParamNames)) { + affectsParams = true; + } + }); + + return !affectsParams; + } + + private containsTypeParameters(node: ts.Node, typeParamNames: Set): boolean { + let found = false; + ts.forEachChild(node, (child) => { + if (ts.isIdentifier(child) && typeParamNames.has(child.text)) { + found = true; + } + if (!found) { + found = this.containsTypeParameters(child, typeParamNames); + } + }); + return found; + } + + private static isInStrictTypeContext(callExpr: ts.CallExpression): boolean { + const parent = callExpr.parent; + + if ((ts.isVariableDeclaration(parent) || ts.isPropertyDeclaration(parent)) && parent.type) { + return true; + } + + if (ts.isAsExpression(parent) || ts.isTypeAssertionExpression(parent)) { + return true; + } + + if (ts.isCallExpression(parent.parent) && parent.parent.typeArguments) { + return true; + } + return false; + } + private checkForUnknownTypeInNonArkTS2( callLikeExpr: ts.CallExpression | ts.NewExpression | ts.ExpressionWithTypeArguments, resolvedTypeArgs: ts.NodeArray, @@ -5577,7 +5644,10 @@ export class TypeScriptLinter extends BaseTypeScriptLinter { * in ArkTS and already have separate check for it. */ if (typeNode.kind === ts.SyntaxKind.UnknownKeyword) { - this.incrementCounters(callLikeExpr, FaultID.GenericCallNoTypeArgs); + const autofix = ts.isCallExpression(callLikeExpr) ? + this.autofixer?.fixGenericCallNoTypeArgsForUnknown(callLikeExpr) : + undefined; + this.incrementCounters(callLikeExpr, FaultID.GenericCallNoTypeArgs, autofix); break; } } diff --git a/ets2panda/linter/src/lib/autofixes/Autofixer.ts b/ets2panda/linter/src/lib/autofixes/Autofixer.ts index dd88357f64..aecd446f3d 100644 --- a/ets2panda/linter/src/lib/autofixes/Autofixer.ts +++ b/ets2panda/linter/src/lib/autofixes/Autofixer.ts @@ -4960,7 +4960,9 @@ export class Autofixer { ); } - private fixGenericCallNoTypeArgsWithContextualType(node: ts.NewExpression): Autofix[] | undefined { + private fixGenericCallNoTypeArgsWithContextualType( + node: ts.NewExpression | ts.CallExpression + ): Autofix[] | undefined { const contextualType = this.typeChecker.getContextualType(node); if (!contextualType) { return undefined; @@ -4976,18 +4978,26 @@ export class Autofixer { return this.generateGenericTypeArgumentsAutofix(node, reference); } - fixGenericCallNoTypeArgs(node: ts.NewExpression): Autofix[] | undefined { + fixGenericCallNoTypeArgs(node: ts.NewExpression | ts.CallExpression): Autofix[] | undefined { const typeNode = this.getTypeNodeForNewExpression(node); if (!typeNode) { return this.fixGenericCallNoTypeArgsWithContextualType(node); } + let nodeExpressionText = node.expression.getText(); + if (ts.isCallExpression(node)) { + const returnTypeText = this.getReturnTypeFromMethodDeclaration(node); + if (!returnTypeText) { + return undefined; + } + nodeExpressionText = returnTypeText; + } if (ts.isUnionTypeNode(typeNode)) { - return this.fixGenericCallNoTypeArgsForUnionType(node, typeNode); + return this.fixGenericCallNoTypeArgsForUnionType(node, typeNode, nodeExpressionText); } if (ts.isArrayTypeNode(typeNode)) { return this.fixGenericCallNoTypeArgsForArrayType(node, typeNode); } - if (!ts.isTypeReferenceNode(typeNode) || typeNode.typeName.getText() !== node.expression.getText()) { + if (!ts.isTypeReferenceNode(typeNode) || typeNode.typeName.getText() !== nodeExpressionText) { return undefined; } @@ -5000,8 +5010,141 @@ export class Autofixer { return [{ start: insertPos, end: insertPos, replacementText: typeArgsText }]; } + fixGenericCallNoTypeArgsForUnknown(node: ts.CallExpression): Autofix[] | undefined { + if (ts.isPropertyAccessExpression(node.parent)) { + const insertPos = node.expression.getEnd(); + return [{ start: insertPos, end: insertPos, replacementText: '' }]; + } + + const typeNode = this.getTypeNodeForNewExpression(node); + if (!typeNode) { + return undefined; + } + + const nodeExpressionText = ts.isVariableDeclaration(node.parent) ? + this.getReturnTypeFromMethodDeclaration(node) ?? node.expression.getText() : + node.expression.getText(); + + if (ts.isUnionTypeNode(typeNode)) { + const targetTypeRef = typeNode.types.find((t): t is ts.TypeReferenceNode => { + return ts.isTypeReferenceNode(t) && t.typeName.getText() === nodeExpressionText; + }); + if (!targetTypeRef) { + return undefined; + } + return this.createFixWithTypeArgs(node, targetTypeRef.typeArguments); + } + + if (ts.isTypeReferenceNode(typeNode) && typeNode.typeName.getText() === nodeExpressionText) { + return this.createFixWithTypeArgs(node, typeNode.typeArguments); + } + + return undefined; + } + + private createFixWithTypeArgs( + node: ts.CallExpression, + typeArguments: ts.NodeArray | undefined + ): Autofix[] | undefined { + const typeArgsText = this.printGenericCallTypeArgs(node.getSourceFile(), typeArguments); + return typeArgsText ? + [ + { + start: node.expression.getEnd(), + end: node.expression.getEnd(), + replacementText: typeArgsText + } + ] : + undefined; + } + + getReturnTypeFromMethodDeclaration(node: ts.CallExpression): string | undefined { + if (ts.isPropertyAccessExpression(node.expression)) { + const propertyAccess = node.expression; + const methodName = propertyAccess.name.text; + const receiver = propertyAccess.expression; + const receiverType = this.typeChecker.getTypeAtLocation(receiver); + const methodSymbol = this.typeChecker.getPropertyOfType(receiverType, methodName); + + if (!methodSymbol) { + return undefined; + } + const methodDecl = methodSymbol.declarations?.[0] as ts.MethodSignature; + if (!methodDecl.type || !ts.isTypeReferenceNode(methodDecl.type) && !ts.isUnionTypeNode(methodDecl.type)) { + return undefined; + } + if (ts.isTypeReferenceNode(methodDecl.type)) { + return methodDecl.type.typeName.getText(); + } + if (ts.isUnionTypeNode(methodDecl.type)) { + const typeNode = this.getTypeNodeForNewExpression(node); + if (!typeNode) { + return undefined; + } + const targetTypeName = this.getTargetTypeName(typeNode, methodDecl.type); + + if (ts.isUnionTypeNode(typeNode)) { + return targetTypeName; + } + const targetTypeNode = methodDecl.type.types.find((type) => { + return ts.isTypeReferenceNode(type) && type.typeName.getText() === targetTypeName; + }) as ts.TypeReferenceNode; + if (!targetTypeNode) { + return undefined; + } + return targetTypeNode.typeName.getText(); + } + } + return undefined; + } + + private getTargetTypeName(typeNode: ts.TypeNode, methodDeclType: ts.UnionTypeNode): string | undefined { + if (ts.isTypeReferenceNode(typeNode)) { + return typeNode.typeName.getText(); + } + + if (!ts.isUnionTypeNode(typeNode)) { + return undefined; + } + + const hasGenericType = methodDeclType.types.some((type) => { + return this.isGenericTypeParameter(type); + }); + const typeNames = new Set(typeNode.types.map(Autofixer.getTypeName).filter(Boolean)); + + const candidateNames = hasGenericType ? + typeNode.types.map(Autofixer.getTypeName) : + methodDeclType.types.map(Autofixer.getTypeName); + const targetTypeName = candidateNames.find((name) => { + return name && name !== 'undefined' && (hasGenericType || typeNames.has(name)); + }); + return targetTypeName; + } + + private isGenericTypeParameter(type: ts.TypeNode): boolean { + const getType = this.typeChecker.getTypeFromTypeNode(type); + return getType.isTypeParameter(); + } + + private static getTypeName(type: ts.TypeNode): string { + if (ts.isTypeReferenceNode(type)) { + return type.typeName.getText(); + } + if (ts.isLiteralTypeNode(type)) { + return type.literal.getText(); + } + const typeNameMap = { + [ts.SyntaxKind.StringKeyword]: 'string', + [ts.SyntaxKind.NumberKeyword]: 'number', + [ts.SyntaxKind.BooleanKeyword]: 'boolean', + [ts.SyntaxKind.UndefinedKeyword]: 'undefined', + [ts.SyntaxKind.NullKeyword]: 'null' + }; + return typeNameMap[type.kind] || ''; + } + private fixGenericCallNoTypeArgsForArrayType( - node: ts.NewExpression, + node: ts.NewExpression | ts.CallExpression, arrayTypeNode: ts.ArrayTypeNode ): Autofix[] | undefined { const elementTypeNode = arrayTypeNode.elementType; @@ -5015,23 +5158,29 @@ export class Autofixer { } private fixGenericCallNoTypeArgsForUnionType( - node: ts.NewExpression, - unionType: ts.UnionTypeNode + node: ts.NewExpression | ts.CallExpression, + unionType: ts.UnionTypeNode, + nodeExpressionText: string ): Autofix[] | undefined { const matchingTypes = unionType.types.filter((type) => { - return ts.isTypeReferenceNode(type) && type.typeName.getText() === node.expression.getText(); - }) as ts.TypeReferenceNode[]; + return Autofixer.getTypeName(type) === nodeExpressionText; + }); if (matchingTypes.length === 1) { const matchingType = matchingTypes[0]; - if (matchingType.typeArguments) { - const srcFile = node.getSourceFile(); - const typeArgsText = this.printGenericCallTypeArgs(srcFile, matchingType.typeArguments); - if (!typeArgsText) { - return undefined; + if (ts.isTypeReferenceNode(matchingType)) { + if (matchingType.typeArguments) { + const srcFile = node.getSourceFile(); + const typeArgsText = this.printGenericCallTypeArgs(srcFile, matchingType.typeArguments); + if (!typeArgsText) { + return undefined; + } + const insertPos = node.expression.getEnd(); + return [{ start: insertPos, end: insertPos, replacementText: typeArgsText }]; } + } else { const insertPos = node.expression.getEnd(); - return [{ start: insertPos, end: insertPos, replacementText: typeArgsText }]; + return [{ start: insertPos, end: insertPos, replacementText: `<${nodeExpressionText}>` }]; } } return undefined; @@ -5099,7 +5248,7 @@ export class Autofixer { } private generateGenericTypeArgumentsAutofix( - node: ts.NewExpression, + node: ts.NewExpression | ts.CallExpression, typeArgs: ts.TypeReferenceNode[] ): Autofix[] | undefined { const srcFile = node.getSourceFile(); @@ -5131,7 +5280,7 @@ export class Autofixer { return []; } - private getTypeNodeForNewExpression(node: ts.NewExpression): ts.TypeNode | undefined { + private getTypeNodeForNewExpression(node: ts.NewExpression | ts.CallExpression): ts.TypeNode | undefined { if (ts.isVariableDeclaration(node.parent) || ts.isPropertyDeclaration(node.parent)) { return node.parent.type; } else if (ts.isBinaryExpression(node.parent)) { diff --git a/ets2panda/linter/test/deprecatedapi/appStorageIsMutable_api.ets.arkts2.json b/ets2panda/linter/test/deprecatedapi/appStorageIsMutable_api.ets.arkts2.json index 1fc946dc8d..891eb9509c 100755 --- a/ets2panda/linter/test/deprecatedapi/appStorageIsMutable_api.ets.arkts2.json +++ b/ets2panda/linter/test/deprecatedapi/appStorageIsMutable_api.ets.arkts2.json @@ -34,6 +34,16 @@ "rule": "ArkUI deprecated api check (arkui-no-deprecated-api)", "severity": "ERROR" }, + { + "line": 20, + "column": 21, + "endLine": 20, + "endColumn": 44, + "problem": "GenericCallNoTypeArgs", + "suggest": "", + "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", + "severity": "ERROR" + }, { "line": 20, "column": 32, @@ -44,7 +54,7 @@ "rule": "ArkUI deprecated api check (arkui-no-deprecated-api)", "severity": "ERROR" }, - { + { "line": 22, "column": 31, "endLine": 22, diff --git a/ets2panda/linter/test/main/func_inferred_type_args_3.ets b/ets2panda/linter/test/main/func_inferred_type_args_3.ets index 0425eba064..cefc55fba0 100644 --- a/ets2panda/linter/test/main/func_inferred_type_args_3.ets +++ b/ets2panda/linter/test/main/func_inferred_type_args_3.ets @@ -17,6 +17,7 @@ import B from './func_inferred_type_args_ts' import A2 from './func_inferred_type_args_ts2' import {A3} from './func_inferred_type_args_ts2' import { HashMap } from '@kit.ArkTS'; +import { IMonitorValue, IMonitor, LocalStorage, AbstractProperty, SubscribedAbstractProperty, AppStorage} from './oh_modules/common_ts_ets_api'; function test() { const a = new A(); @@ -40,4 +41,25 @@ class Demo{} function test4() { const a = new A4(); a.map = new HashMap(); //error -} \ No newline at end of file +} + +@ComponentV2 +struct Child { + @Param info: Info = new Info(); + @Monitor("info.message") + onMessageChange(monitor: IMonitor) { + let beforeValue: IMonitorValue = monitor.value(); + let beforeValue: IMonitorValue = monitor.value("info.message"); + console.info(`Child message change from ${monitor.value()?.before} to ${monitor.value('info.message')?.now}`); + } +} + +let para: Record = { 'PropA': 47 }; +let storage: LocalStorage = new LocalStorage(para); +let propA: number | undefined = storage.get('PropA'); +let link1: SubscribedAbstractProperty = storage.link('PropA'); +let refToPropA1: AbstractProperty | undefined = storage.ref('PropA'); + +let propA: number | undefined = AppStorage.get('PropA'); +let link1: SubscribedAbstractProperty = AppStorage.link('PropA'); +let refToPropA1: AbstractProperty | undefined = AppStorage.ref('PropA'); \ No newline at end of file diff --git a/ets2panda/linter/test/main/func_inferred_type_args_3.ets.arkts2.json b/ets2panda/linter/test/main/func_inferred_type_args_3.ets.arkts2.json index 868ccdbc43..3d3a2b6001 100644 --- a/ets2panda/linter/test/main/func_inferred_type_args_3.ets.arkts2.json +++ b/ets2panda/linter/test/main/func_inferred_type_args_3.ets.arkts2.json @@ -15,9 +15,9 @@ ], "result": [ { - "line": 23, + "line": 24, "column": 11, - "endLine": 23, + "endLine": 24, "endColumn": 20, "problem": "GenericCallNoTypeArgs", "suggest": "", @@ -25,9 +25,9 @@ "severity": "ERROR" }, { - "line": 27, + "line": 28, "column": 11, - "endLine": 27, + "endLine": 28, "endColumn": 20, "problem": "GenericCallNoTypeArgs", "suggest": "", @@ -35,9 +35,9 @@ "severity": "ERROR" }, { - "line": 31, + "line": 32, "column": 11, - "endLine": 31, + "endLine": 32, "endColumn": 20, "problem": "GenericCallNoTypeArgs", "suggest": "", @@ -45,9 +45,9 @@ "severity": "ERROR" }, { - "line": 42, + "line": 43, "column": 15, - "endLine": 42, + "endLine": 43, "endColumn": 22, "problem": "DynamicCtorCall", "suggest": "", @@ -55,14 +55,204 @@ "severity": "ERROR" }, { - "line": 42, + "line": 43, "column": 11, - "endLine": 42, + "endLine": 43, "endColumn": 24, "problem": "GenericCallNoTypeArgs", "suggest": "", "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", "severity": "ERROR" + }, + { + "line": 48, + "column": 27, + "endLine": 48, + "endColumn": 31, + "problem": "DynamicCtorCall", + "suggest": "", + "rule": "\"new\" expression with dynamic constructor type is not supported (arkts-no-dynamic-ctor-call)", + "severity": "ERROR" + }, + { + "line": 51, + "column": 46, + "endLine": 51, + "endColumn": 61, + "problem": "GenericCallNoTypeArgs", + "suggest": "", + "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", + "severity": "ERROR" + }, + { + "line": 52, + "column": 46, + "endLine": 52, + "endColumn": 75, + "problem": "GenericCallNoTypeArgs", + "suggest": "", + "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", + "severity": "ERROR" + }, + { + "line": 53, + "column": 47, + "endLine": 53, + "endColumn": 62, + "problem": "GenericCallNoTypeArgs", + "suggest": "", + "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", + "severity": "ERROR" + }, + { + "line": 53, + "column": 77, + "endLine": 53, + "endColumn": 106, + "problem": "GenericCallNoTypeArgs", + "suggest": "", + "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", + "severity": "ERROR" + }, + { + "line": 57, + "column": 46, + "endLine": 57, + "endColumn": 48, + "problem": "NumericSemantics", + "suggest": "", + "rule": "Numeric semantics is different for integer values (arkts-numeric-semantic)", + "severity": "ERROR" + }, + { + "line": 59, + "column": 33, + "endLine": 59, + "endColumn": 53, + "problem": "GenericCallNoTypeArgs", + "suggest": "", + "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", + "severity": "ERROR" + }, + { + "line": 60, + "column": 49, + "endLine": 60, + "endColumn": 70, + "problem": "GenericCallNoTypeArgs", + "suggest": "", + "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", + "severity": "ERROR" + }, + { + "line": 61, + "column": 57, + "endLine": 61, + "endColumn": 77, + "problem": "GenericCallNoTypeArgs", + "suggest": "", + "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", + "severity": "ERROR" + }, + { + "line": 63, + "column": 33, + "endLine": 63, + "endColumn": 56, + "problem": "GenericCallNoTypeArgs", + "suggest": "", + "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", + "severity": "ERROR" + }, + { + "line": 64, + "column": 49, + "endLine": 64, + "endColumn": 73, + "problem": "GenericCallNoTypeArgs", + "suggest": "", + "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", + "severity": "ERROR" + }, + { + "line": 65, + "column": 57, + "endLine": 65, + "endColumn": 80, + "problem": "GenericCallNoTypeArgs", + "suggest": "", + "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", + "severity": "ERROR" + }, + { + "line": 46, + "column": 2, + "endLine": 46, + "endColumn": 13, + "problem": "UIInterfaceImport", + "suggest": "", + "rule": "The ArkUI interface \"ComponentV2\" should be imported before it is used (arkui-modular-interface)", + "severity": "ERROR" + }, + { + "line": 48, + "column": 4, + "endLine": 48, + "endColumn": 9, + "problem": "UIInterfaceImport", + "suggest": "", + "rule": "The ArkUI interface \"Param\" should be imported before it is used (arkui-modular-interface)", + "severity": "ERROR" + }, + { + "line": 49, + "column": 4, + "endLine": 49, + "endColumn": 11, + "problem": "UIInterfaceImport", + "suggest": "", + "rule": "The ArkUI interface \"Monitor\" should be imported before it is used (arkui-modular-interface)", + "severity": "ERROR" + }, + { + "line": 51, + "column": 9, + "endLine": 51, + "endColumn": 20, + "problem": "StrictDiagnostic", + "suggest": "Type 'IMonitorValue | undefined' is not assignable to type 'IMonitorValue'.\n Type 'undefined' is not assignable to type 'IMonitorValue'.", + "rule": "Type 'IMonitorValue | undefined' is not assignable to type 'IMonitorValue'.\n Type 'undefined' is not assignable to type 'IMonitorValue'.", + "severity": "ERROR" + }, + { + "line": 52, + "column": 9, + "endLine": 52, + "endColumn": 20, + "problem": "StrictDiagnostic", + "suggest": "Type 'IMonitorValue | undefined' is not assignable to type 'IMonitorValue'.\n Type 'undefined' is not assignable to type 'IMonitorValue'.", + "rule": "Type 'IMonitorValue | undefined' is not assignable to type 'IMonitorValue'.\n Type 'undefined' is not assignable to type 'IMonitorValue'.", + "severity": "ERROR" + }, + { + "line": 60, + "column": 5, + "endLine": 60, + "endColumn": 10, + "problem": "StrictDiagnostic", + "suggest": "Type 'SubscribedAbstractProperty | undefined' is not assignable to type 'SubscribedAbstractProperty'.\n Type 'undefined' is not assignable to type 'SubscribedAbstractProperty'.", + "rule": "Type 'SubscribedAbstractProperty | undefined' is not assignable to type 'SubscribedAbstractProperty'.\n Type 'undefined' is not assignable to type 'SubscribedAbstractProperty'.", + "severity": "ERROR" + }, + { + "line": 64, + "column": 5, + "endLine": 64, + "endColumn": 10, + "problem": "StrictDiagnostic", + "suggest": "Type 'SubscribedAbstractProperty | undefined' is not assignable to type 'SubscribedAbstractProperty'.\n Type 'undefined' is not assignable to type 'SubscribedAbstractProperty'.", + "rule": "Type 'SubscribedAbstractProperty | undefined' is not assignable to type 'SubscribedAbstractProperty'.\n Type 'undefined' is not assignable to type 'SubscribedAbstractProperty'.", + "severity": "ERROR" } ] } \ No newline at end of file diff --git a/ets2panda/linter/test/main/func_inferred_type_args_3.ets.autofix.json b/ets2panda/linter/test/main/func_inferred_type_args_3.ets.autofix.json index 6c9b2975d9..a45c3a1673 100644 --- a/ets2panda/linter/test/main/func_inferred_type_args_3.ets.autofix.json +++ b/ets2panda/linter/test/main/func_inferred_type_args_3.ets.autofix.json @@ -15,19 +15,19 @@ ], "result": [ { - "line": 23, + "line": 24, "column": 11, - "endLine": 23, + "endLine": 24, "endColumn": 20, "problem": "GenericCallNoTypeArgs", "autofix": [ { - "start": 888, - "end": 888, + "start": 1033, + "end": 1033, "replacementText": "", - "line": 23, + "line": 24, "column": 11, - "endLine": 23, + "endLine": 24, "endColumn": 20 } ], @@ -36,9 +36,9 @@ "severity": "ERROR" }, { - "line": 27, + "line": 28, "column": 11, - "endLine": 27, + "endLine": 28, "endColumn": 20, "problem": "GenericCallNoTypeArgs", "suggest": "", @@ -46,19 +46,19 @@ "severity": "ERROR" }, { - "line": 31, + "line": 32, "column": 11, - "endLine": 31, + "endLine": 32, "endColumn": 20, "problem": "GenericCallNoTypeArgs", "autofix": [ { - "start": 1032, - "end": 1032, + "start": 1177, + "end": 1177, "replacementText": "", - "line": 31, + "line": 32, "column": 11, - "endLine": 31, + "endLine": 32, "endColumn": 20 } ], @@ -67,9 +67,9 @@ "severity": "ERROR" }, { - "line": 42, + "line": 43, "column": 15, - "endLine": 42, + "endLine": 43, "endColumn": 22, "problem": "DynamicCtorCall", "suggest": "", @@ -77,25 +77,369 @@ "severity": "ERROR" }, { - "line": 42, + "line": 43, "column": 11, - "endLine": 42, + "endLine": 43, "endColumn": 24, "problem": "GenericCallNoTypeArgs", "autofix": [ { - "start": 1183, - "end": 1183, + "start": 1328, + "end": 1328, "replacementText": "", - "line": 42, + "line": 43, "column": 11, - "endLine": 42, + "endLine": 43, "endColumn": 24 } ], "suggest": "", "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", "severity": "ERROR" + }, + { + "line": 48, + "column": 27, + "endLine": 48, + "endColumn": 31, + "problem": "DynamicCtorCall", + "suggest": "", + "rule": "\"new\" expression with dynamic constructor type is not supported (arkts-no-dynamic-ctor-call)", + "severity": "ERROR" + }, + { + "line": 51, + "column": 46, + "endLine": 51, + "endColumn": 61, + "problem": "GenericCallNoTypeArgs", + "autofix": [ + { + "start": 1529, + "end": 1529, + "replacementText": "", + "line": 51, + "column": 46, + "endLine": 51, + "endColumn": 61 + } + ], + "suggest": "", + "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", + "severity": "ERROR" + }, + { + "line": 52, + "column": 46, + "endLine": 52, + "endColumn": 75, + "problem": "GenericCallNoTypeArgs", + "autofix": [ + { + "start": 1591, + "end": 1591, + "replacementText": "", + "line": 52, + "column": 46, + "endLine": 52, + "endColumn": 75 + } + ], + "suggest": "", + "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", + "severity": "ERROR" + }, + { + "line": 53, + "column": 47, + "endLine": 53, + "endColumn": 62, + "problem": "GenericCallNoTypeArgs", + "autofix": [ + { + "start": 1668, + "end": 1668, + "replacementText": "", + "line": 53, + "column": 47, + "endLine": 53, + "endColumn": 62 + } + ], + "suggest": "", + "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", + "severity": "ERROR" + }, + { + "line": 53, + "column": 77, + "endLine": 53, + "endColumn": 106, + "problem": "GenericCallNoTypeArgs", + "autofix": [ + { + "start": 1698, + "end": 1698, + "replacementText": "", + "line": 53, + "column": 77, + "endLine": 53, + "endColumn": 106 + } + ], + "suggest": "", + "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", + "severity": "ERROR" + }, + { + "line": 57, + "column": 46, + "endLine": 57, + "endColumn": 48, + "problem": "NumericSemantics", + "autofix": [ + { + "start": 1776, + "end": 1778, + "replacementText": "47.0", + "line": 57, + "column": 46, + "endLine": 57, + "endColumn": 48 + } + ], + "suggest": "", + "rule": "Numeric semantics is different for integer values (arkts-numeric-semantic)", + "severity": "ERROR" + }, + { + "line": 59, + "column": 33, + "endLine": 59, + "endColumn": 53, + "problem": "GenericCallNoTypeArgs", + "autofix": [ + { + "start": 1877, + "end": 1877, + "replacementText": "", + "line": 59, + "column": 33, + "endLine": 59, + "endColumn": 53 + } + ], + "suggest": "", + "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", + "severity": "ERROR" + }, + { + "line": 60, + "column": 49, + "endLine": 60, + "endColumn": 70, + "problem": "GenericCallNoTypeArgs", + "autofix": [ + { + "start": 1948, + "end": 1948, + "replacementText": "", + "line": 60, + "column": 49, + "endLine": 60, + "endColumn": 70 + } + ], + "suggest": "", + "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", + "severity": "ERROR" + }, + { + "line": 61, + "column": 57, + "endLine": 61, + "endColumn": 77, + "problem": "GenericCallNoTypeArgs", + "autofix": [ + { + "start": 2026, + "end": 2026, + "replacementText": "", + "line": 61, + "column": 57, + "endLine": 61, + "endColumn": 77 + } + ], + "suggest": "", + "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", + "severity": "ERROR" + }, + { + "line": 63, + "column": 33, + "endLine": 63, + "endColumn": 56, + "problem": "GenericCallNoTypeArgs", + "autofix": [ + { + "start": 2084, + "end": 2084, + "replacementText": "", + "line": 63, + "column": 33, + "endLine": 63, + "endColumn": 56 + } + ], + "suggest": "", + "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", + "severity": "ERROR" + }, + { + "line": 64, + "column": 49, + "endLine": 64, + "endColumn": 73, + "problem": "GenericCallNoTypeArgs", + "autofix": [ + { + "start": 2158, + "end": 2158, + "replacementText": "", + "line": 64, + "column": 49, + "endLine": 64, + "endColumn": 73 + } + ], + "suggest": "", + "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", + "severity": "ERROR" + }, + { + "line": 65, + "column": 57, + "endLine": 65, + "endColumn": 80, + "problem": "GenericCallNoTypeArgs", + "autofix": [ + { + "start": 2239, + "end": 2239, + "replacementText": "", + "line": 65, + "column": 57, + "endLine": 65, + "endColumn": 80 + } + ], + "suggest": "", + "rule": "Type inference in case of generic function calls is limited (arkts-no-inferred-generic-params)", + "severity": "ERROR" + }, + { + "line": 46, + "column": 2, + "endLine": 46, + "endColumn": 13, + "problem": "UIInterfaceImport", + "autofix": [ + { + "start": 603, + "end": 603, + "replacementText": "\n\nimport {\n ComponentV2,\n Param,\n Monitor,\n} from '@kit.ArkUI';\n", + "line": 49, + "column": 4, + "endLine": 49, + "endColumn": 11 + } + ], + "suggest": "", + "rule": "The ArkUI interface \"ComponentV2\" should be imported before it is used (arkui-modular-interface)", + "severity": "ERROR" + }, + { + "line": 48, + "column": 4, + "endLine": 48, + "endColumn": 9, + "problem": "UIInterfaceImport", + "autofix": [ + { + "start": 603, + "end": 603, + "replacementText": "\n\nimport {\n ComponentV2,\n Param,\n Monitor,\n} from '@kit.ArkUI';\n", + "line": 49, + "column": 4, + "endLine": 49, + "endColumn": 11 + } + ], + "suggest": "", + "rule": "The ArkUI interface \"Param\" should be imported before it is used (arkui-modular-interface)", + "severity": "ERROR" + }, + { + "line": 49, + "column": 4, + "endLine": 49, + "endColumn": 11, + "problem": "UIInterfaceImport", + "autofix": [ + { + "start": 603, + "end": 603, + "replacementText": "\n\nimport {\n ComponentV2,\n Param,\n Monitor,\n} from '@kit.ArkUI';\n", + "line": 49, + "column": 4, + "endLine": 49, + "endColumn": 11 + } + ], + "suggest": "", + "rule": "The ArkUI interface \"Monitor\" should be imported before it is used (arkui-modular-interface)", + "severity": "ERROR" + }, + { + "line": 51, + "column": 9, + "endLine": 51, + "endColumn": 20, + "problem": "StrictDiagnostic", + "suggest": "Type 'IMonitorValue | undefined' is not assignable to type 'IMonitorValue'.\n Type 'undefined' is not assignable to type 'IMonitorValue'.", + "rule": "Type 'IMonitorValue | undefined' is not assignable to type 'IMonitorValue'.\n Type 'undefined' is not assignable to type 'IMonitorValue'.", + "severity": "ERROR" + }, + { + "line": 52, + "column": 9, + "endLine": 52, + "endColumn": 20, + "problem": "StrictDiagnostic", + "suggest": "Type 'IMonitorValue | undefined' is not assignable to type 'IMonitorValue'.\n Type 'undefined' is not assignable to type 'IMonitorValue'.", + "rule": "Type 'IMonitorValue | undefined' is not assignable to type 'IMonitorValue'.\n Type 'undefined' is not assignable to type 'IMonitorValue'.", + "severity": "ERROR" + }, + { + "line": 60, + "column": 5, + "endLine": 60, + "endColumn": 10, + "problem": "StrictDiagnostic", + "suggest": "Type 'SubscribedAbstractProperty | undefined' is not assignable to type 'SubscribedAbstractProperty'.\n Type 'undefined' is not assignable to type 'SubscribedAbstractProperty'.", + "rule": "Type 'SubscribedAbstractProperty | undefined' is not assignable to type 'SubscribedAbstractProperty'.\n Type 'undefined' is not assignable to type 'SubscribedAbstractProperty'.", + "severity": "ERROR" + }, + { + "line": 64, + "column": 5, + "endLine": 64, + "endColumn": 10, + "problem": "StrictDiagnostic", + "suggest": "Type 'SubscribedAbstractProperty | undefined' is not assignable to type 'SubscribedAbstractProperty'.\n Type 'undefined' is not assignable to type 'SubscribedAbstractProperty'.", + "rule": "Type 'SubscribedAbstractProperty | undefined' is not assignable to type 'SubscribedAbstractProperty'.\n Type 'undefined' is not assignable to type 'SubscribedAbstractProperty'.", + "severity": "ERROR" } ] } \ No newline at end of file diff --git a/ets2panda/linter/test/main/func_inferred_type_args_3.ets.json b/ets2panda/linter/test/main/func_inferred_type_args_3.ets.json index b7a8809e02..9b8ac8bc7d 100644 --- a/ets2panda/linter/test/main/func_inferred_type_args_3.ets.json +++ b/ets2panda/linter/test/main/func_inferred_type_args_3.ets.json @@ -13,5 +13,46 @@ "See the License for the specific language governing permissions and", "limitations under the License." ], - "result": [] + "result": [ + { + "line": 51, + "column": 9, + "endLine": 51, + "endColumn": 20, + "problem": "StrictDiagnostic", + "suggest": "Type 'IMonitorValue | undefined' is not assignable to type 'IMonitorValue'.\n Type 'undefined' is not assignable to type 'IMonitorValue'.", + "rule": "Type 'IMonitorValue | undefined' is not assignable to type 'IMonitorValue'.\n Type 'undefined' is not assignable to type 'IMonitorValue'.", + "severity": "ERROR" + }, + { + "line": 52, + "column": 9, + "endLine": 52, + "endColumn": 20, + "problem": "StrictDiagnostic", + "suggest": "Type 'IMonitorValue | undefined' is not assignable to type 'IMonitorValue'.\n Type 'undefined' is not assignable to type 'IMonitorValue'.", + "rule": "Type 'IMonitorValue | undefined' is not assignable to type 'IMonitorValue'.\n Type 'undefined' is not assignable to type 'IMonitorValue'.", + "severity": "ERROR" + }, + { + "line": 60, + "column": 5, + "endLine": 60, + "endColumn": 10, + "problem": "StrictDiagnostic", + "suggest": "Type 'SubscribedAbstractProperty | undefined' is not assignable to type 'SubscribedAbstractProperty'.\n Type 'undefined' is not assignable to type 'SubscribedAbstractProperty'.", + "rule": "Type 'SubscribedAbstractProperty | undefined' is not assignable to type 'SubscribedAbstractProperty'.\n Type 'undefined' is not assignable to type 'SubscribedAbstractProperty'.", + "severity": "ERROR" + }, + { + "line": 64, + "column": 5, + "endLine": 64, + "endColumn": 10, + "problem": "StrictDiagnostic", + "suggest": "Type 'SubscribedAbstractProperty | undefined' is not assignable to type 'SubscribedAbstractProperty'.\n Type 'undefined' is not assignable to type 'SubscribedAbstractProperty'.", + "rule": "Type 'SubscribedAbstractProperty | undefined' is not assignable to type 'SubscribedAbstractProperty'.\n Type 'undefined' is not assignable to type 'SubscribedAbstractProperty'.", + "severity": "ERROR" + } + ] } \ No newline at end of file diff --git a/ets2panda/linter/test/main/func_inferred_type_args_3.ets.migrate.ets b/ets2panda/linter/test/main/func_inferred_type_args_3.ets.migrate.ets index c038713ec9..c98487895d 100644 --- a/ets2panda/linter/test/main/func_inferred_type_args_3.ets.migrate.ets +++ b/ets2panda/linter/test/main/func_inferred_type_args_3.ets.migrate.ets @@ -12,11 +12,19 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + +import { + ComponentV2, + Param, + Monitor, +} from '@kit.ArkUI'; + import {A} from './func_inferred_type_args_ts'; import B from './func_inferred_type_args_ts' import A2 from './func_inferred_type_args_ts2' import {A3} from './func_inferred_type_args_ts2' import { HashMap } from '@kit.ArkTS'; +import { IMonitorValue, IMonitor, LocalStorage, AbstractProperty, SubscribedAbstractProperty, AppStorage} from './oh_modules/common_ts_ets_api'; function test() { const a = new A(); @@ -40,4 +48,25 @@ class Demo{} function test4() { const a = new A4(); a.map = new HashMap(); //error -} \ No newline at end of file +} + +@ComponentV2 +struct Child { + @Param info: Info = new Info(); + @Monitor("info.message") + onMessageChange(monitor: IMonitor) { + let beforeValue: IMonitorValue = monitor.value(); + let beforeValue: IMonitorValue = monitor.value("info.message"); + console.info(`Child message change from ${monitor.value()?.before} to ${monitor.value('info.message')?.now}`); + } +} + +let para: Record = { 'PropA': 47.0 }; +let storage: LocalStorage = new LocalStorage(para); +let propA: number | undefined = storage.get('PropA'); +let link1: SubscribedAbstractProperty = storage.link('PropA'); +let refToPropA1: AbstractProperty | undefined = storage.ref('PropA'); + +let propA: number | undefined = AppStorage.get('PropA'); +let link1: SubscribedAbstractProperty = AppStorage.link('PropA'); +let refToPropA1: AbstractProperty | undefined = AppStorage.ref('PropA'); \ No newline at end of file diff --git a/ets2panda/linter/test/main/func_inferred_type_args_3.ets.migrate.json b/ets2panda/linter/test/main/func_inferred_type_args_3.ets.migrate.json index f7a8d2c4bd..62f3ae72ba 100644 --- a/ets2panda/linter/test/main/func_inferred_type_args_3.ets.migrate.json +++ b/ets2panda/linter/test/main/func_inferred_type_args_3.ets.migrate.json @@ -15,9 +15,9 @@ ], "result": [ { - "line": 27, + "line": 35, "column": 11, - "endLine": 27, + "endLine": 35, "endColumn": 20, "problem": "GenericCallNoTypeArgs", "suggest": "", @@ -25,14 +25,64 @@ "severity": "ERROR" }, { - "line": 42, + "line": 50, "column": 15, - "endLine": 42, + "endLine": 50, "endColumn": 22, "problem": "DynamicCtorCall", "suggest": "", "rule": "\"new\" expression with dynamic constructor type is not supported (arkts-no-dynamic-ctor-call)", "severity": "ERROR" + }, + { + "line": 55, + "column": 27, + "endLine": 55, + "endColumn": 31, + "problem": "DynamicCtorCall", + "suggest": "", + "rule": "\"new\" expression with dynamic constructor type is not supported (arkts-no-dynamic-ctor-call)", + "severity": "ERROR" + }, + { + "line": 58, + "column": 9, + "endLine": 58, + "endColumn": 20, + "problem": "StrictDiagnostic", + "suggest": "Type 'IMonitorValue | undefined' is not assignable to type 'IMonitorValue'.\n Type 'undefined' is not assignable to type 'IMonitorValue'.", + "rule": "Type 'IMonitorValue | undefined' is not assignable to type 'IMonitorValue'.\n Type 'undefined' is not assignable to type 'IMonitorValue'.", + "severity": "ERROR" + }, + { + "line": 59, + "column": 9, + "endLine": 59, + "endColumn": 20, + "problem": "StrictDiagnostic", + "suggest": "Type 'IMonitorValue | undefined' is not assignable to type 'IMonitorValue'.\n Type 'undefined' is not assignable to type 'IMonitorValue'.", + "rule": "Type 'IMonitorValue | undefined' is not assignable to type 'IMonitorValue'.\n Type 'undefined' is not assignable to type 'IMonitorValue'.", + "severity": "ERROR" + }, + { + "line": 67, + "column": 5, + "endLine": 67, + "endColumn": 10, + "problem": "StrictDiagnostic", + "suggest": "Type 'SubscribedAbstractProperty | undefined' is not assignable to type 'SubscribedAbstractProperty'.\n Type 'undefined' is not assignable to type 'SubscribedAbstractProperty'.", + "rule": "Type 'SubscribedAbstractProperty | undefined' is not assignable to type 'SubscribedAbstractProperty'.\n Type 'undefined' is not assignable to type 'SubscribedAbstractProperty'.", + "severity": "ERROR" + }, + { + "line": 71, + "column": 5, + "endLine": 71, + "endColumn": 10, + "problem": "StrictDiagnostic", + "suggest": "Type 'SubscribedAbstractProperty | undefined' is not assignable to type 'SubscribedAbstractProperty'.\n Type 'undefined' is not assignable to type 'SubscribedAbstractProperty'.", + "rule": "Type 'SubscribedAbstractProperty | undefined' is not assignable to type 'SubscribedAbstractProperty'.\n Type 'undefined' is not assignable to type 'SubscribedAbstractProperty'.", + "severity": "ERROR" } ] } \ No newline at end of file diff --git a/ets2panda/linter/test/main/oh_modules/common_ts_ets_api.d.ts b/ets2panda/linter/test/main/oh_modules/common_ts_ets_api.d.ts new file mode 100644 index 0000000000..0be36a0a8e --- /dev/null +++ b/ets2panda/linter/test/main/oh_modules/common_ts_ets_api.d.ts @@ -0,0 +1,46 @@ +/* + * 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. + */ + +export declare interface IMonitor { + value(path?: string): IMonitorValue | undefined; +} +export declare interface IMonitorValue { + before: T; + now: T; + path: string; +} +export declare class LocalStorage { + get(propName: string): T | undefined; + ref(propName: string): AbstractProperty | undefined; + link(propName: string): SubscribedAbstractProperty | undefined; +} + +export declare class AppStorage { + static get(propName: string): T | undefined; + static ref(propName: string): AbstractProperty | undefined; + static link(propName: string): SubscribedAbstractProperty | undefined; +} + +export declare interface AbstractProperty { + get(): T; + set(newValue: T): void; + info(): string; +} +export declare abstract class SubscribedAbstractProperty { + info(): string; + abstract get(): T; + abstract set(newValue: T): void; + abstract aboutToBeDeleted(): void; +} \ No newline at end of file -- Gitee