From 4ebda562d17c59d5d6e5ee6ce864aa808ab3bf2f Mon Sep 17 00:00:00 2001 From: wangxiuxiu96 Date: Thu, 11 Sep 2025 19:20:41 +0800 Subject: [PATCH] =?UTF-8?q?[feate]=20=E7=8A=B6=E6=80=81=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E4=BA=92=E6=93=8D=E4=BD=9C-V1/V2=E6=B7=B7=E5=90=88=E5=9C=BA?= =?UTF-8?q?=E6=99=AF=20=201.2=20=E8=B0=83=E7=94=A8=201.1=E5=9C=BA=E6=99=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wangxiuxiu96 --- .../ui-plugins/interop/initstatevar.ts | 65 ++++++++++++++++++- arkui-plugins/ui-plugins/interop/interop.ts | 17 +++-- 2 files changed, 74 insertions(+), 8 deletions(-) diff --git a/arkui-plugins/ui-plugins/interop/initstatevar.ts b/arkui-plugins/ui-plugins/interop/initstatevar.ts index 1f43c179e..22bf008e5 100644 --- a/arkui-plugins/ui-plugins/interop/initstatevar.ts +++ b/arkui-plugins/ui-plugins/interop/initstatevar.ts @@ -23,7 +23,8 @@ import { hasDecorator } from '../property-translators/utils'; import { DecoratorNames } from '../../common/predefines'; -export function initialArgs(args: arkts.ObjectExpression, varMap: Map, updateProp: arkts.Property[]): arkts.Statement[] { +export function initialArgs(args: arkts.ObjectExpression, varMap: Map, + updateProp: arkts.Property[], node: arkts.CallExpression, isV2: boolean): arkts.Statement[] { const result: arkts.Statement[] = []; const proxySet = new Set(); @@ -40,8 +41,9 @@ export function initialArgs(args: arkts.ObjectExpression, varMap: Map valueType = getPropertyType( annotations ) ); + isComponentV2forParent = valueProperty.parent.annotations.some( + annotation => annotation.expr instanceof arkts.Identifier && annotation.expr.name === 'ComponentV2'); + } else { + //依赖于懿后续补充parent信息 + } + if ( keyProperty !== undefined ) { + keyProperty.annotations.some( ( annotations ) => ketType = getPropertyType( annotations ) ); + } + if ( isV2 ) { + if ( !isComponentV2forParent ) { + let errorMessage = `The '${valueType}' property '${valueName}' cannot be assigned to the ${ketType} property ${keyName} when interop`; + const diagnosticKind = arkts.DiagnosticKind.create( errorMessage, arkts.PluginDiagnosticType.ES2PANDA_PLUGIN_ERROR ); + arkts.Diagnostic.logDiagnostic(diagnosticKind, arkts.getStartPosition(node)); + } + } else { + if ( isComponentV2forParent ) { + const annotationsKey = keyProperty.annotations; + if (!isV2 && valueProperty !== undefined && annotationsKey.length !== 0) { + if ( ( hasDecorator( valueProperty, DecoratorNames.LOCAL ) || hasDecorator( valueProperty, DecoratorNames.PROVIDER ) || + hasDecorator( valueProperty, DecoratorNames.CONSUMER ) || hasDecorator( valueProperty, DecoratorNames.PARAM ) || + hasDecorator( valueProperty, DecoratorNames.EVENT ))) { + let errorMessage = `The '${valueType}' property '${valueName}' cannot be assigned to the ${ketType} property ${keyName} when interop`; + const diagnosticKind = arkts.DiagnosticKind.create( errorMessage, arkts.PluginDiagnosticType.ES2PANDA_PLUGIN_ERROR ); + arkts.Diagnostic.logDiagnostic(diagnosticKind, arkts.getStartPosition(node)); + } + const annotations = valueProperty.annotations; + if (annotations.length === 0) { + let errorMessage = `The '${valueType}' property '${valueName}' cannot be assigned to the ${ketType} property ${keyName} when interop`; + const diagnosticKind = arkts.DiagnosticKind.create( errorMessage, arkts.PluginDiagnosticType.ES2PANDA_PLUGIN_ERROR ); + arkts.Diagnostic.logDiagnostic(diagnosticKind, arkts.getStartPosition(node)); + } + } + } + } + } +} + +function getPropertyType(anno: arkts.AnnotationUsage) { + let propertyType = 'regular'; + if (!!anno.expr && arkts.isIdentifier(anno.expr)) { + propertyType = anno.expr.name; + } + if ( propertyType !== 'regular' && propertyType !== '' ) { + return '@' + propertyType; + } else { + return 'regular'; + } +} + export function createVariableLet(varName: string, expression: arkts.AstNode): arkts.VariableDeclaration { return arkts.factory.createVariableDeclaration( arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_NONE, diff --git a/arkui-plugins/ui-plugins/interop/interop.ts b/arkui-plugins/ui-plugins/interop/interop.ts index 554444410..131a55a06 100644 --- a/arkui-plugins/ui-plugins/interop/interop.ts +++ b/arkui-plugins/ui-plugins/interop/interop.ts @@ -174,7 +174,7 @@ function createComponent(className: string): arkts.Statement[] { function createWrapperBlock(context: InteropContext, varMap: Map, - updateProp: arkts.Property[]): arkts.BlockStatement { + updateProp: arkts.Property[], node: arkts.CallExpression, isV2: boolean): arkts.BlockStatement { const className: string = context.className; const path: string = context.path; const args: arkts.ObjectExpression | undefined = context.arguments; @@ -186,7 +186,7 @@ function createWrapperBlock(context: InteropContext, varMap: Map, - updateProp: arkts.Property[]): arkts.ArrowFunctionExpression { - const block = createWrapperBlock(context, varMap, updateProp); + updateProp: arkts.Property[], node: arkts.CallExpression, isV2: boolean): arkts.ArrowFunctionExpression { + const block = createWrapperBlock(context, varMap, updateProp, node, isV2); return arkts.factory.createArrowFunction( arkts.factory.createScriptFunction( block, @@ -432,10 +432,15 @@ export function generateArkUICompatible(node: arkts.CallExpression): arkts.CallE arguments: options, content: content, }; - + const decl = arkts.getDecl(classInterop); + if (!(decl instanceof arkts.ClassDefinition)) { + throw Error("can't find legacy class declaration"); + } const varMap: Map = generateVarMap(context, classInterop); const updateProp: arkts.Property[] = []; - const initializer = createInitializer(context, varMap, updateProp); + const isComponentV2 = decl.annotations.some( + annotation => annotation.expr instanceof arkts.Identifier && annotation.expr.name === 'ComponentV2'); + const initializer = createInitializer(context, varMap, updateProp, node, isComponentV2); const updater = createUpdater(updateProp); const result = arkts.factory.updateCallExpression( node, -- Gitee