diff --git a/arkui-plugins/ui-plugins/interop/interop.ts b/arkui-plugins/ui-plugins/interop/interop.ts index 8da4bc6d59e6567f8d924e9ead065ab5a57c86dc..d5bea3c1ae916c60f59fbe7430dc26ef290da0ca 100644 --- a/arkui-plugins/ui-plugins/interop/interop.ts +++ b/arkui-plugins/ui-plugins/interop/interop.ts @@ -151,13 +151,17 @@ function newComponent(className: string): arkts.Statement { ); } -function createComponent(className: string): arkts.Statement[] { +function createComponent(className: string, isV2: boolean): arkts.Statement[] { + let viewCreateMethod = 'viewPUCreate'; + if (isV2) { + viewCreateMethod = 'viewV2Create'; + } const component = newComponent(className); - const ViewPU = getPropertyESValue('viewPUCreate', InteroperAbilityNames.GLOBAL, 'viewPUCreate'); + const View = getPropertyESValue(viewCreateMethod, InteroperAbilityNames.GLOBAL, viewCreateMethod); const create = arkts.factory.createExpressionStatement( arkts.factory.createCallExpression( arkts.factory.createMemberExpression( - arkts.factory.createIdentifier('viewPUCreate'), + arkts.factory.createIdentifier(viewCreateMethod), arkts.factory.createIdentifier('invoke'), arkts.Es2pandaMemberExpressionKind.MEMBER_EXPRESSION_KIND_PROPERTY_ACCESS, false, @@ -169,12 +173,12 @@ function createComponent(className: string): arkts.Statement[] { ] ) ); - return [component, ViewPU, create]; + return [component, View, create]; } function createWrapperBlock(context: InteropContext, varMap: Map, - updateProp: arkts.Property[]): arkts.BlockStatement { + updateProp: arkts.Property[], isV2: boolean): arkts.BlockStatement { const className: string = context.className; const path: string = context.path; const args: arkts.ObjectExpression | undefined = context.arguments; @@ -187,21 +191,21 @@ function createWrapperBlock(context: InteropContext, varMap: Map, - updateProp: arkts.Property[]): arkts.ArrowFunctionExpression { - const block = createWrapperBlock(context, varMap, updateProp); + updateProp: arkts.Property[], isV2: boolean): arkts.ArrowFunctionExpression { + const block = createWrapperBlock(context, varMap, updateProp, isV2); return arkts.factory.createArrowFunction( arkts.factory.createScriptFunction( block, @@ -431,9 +435,15 @@ export function generateArkUICompatible(node: arkts.CallExpression): arkts.CallE 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, isComponentV2); const updater = createUpdater(updateProp); const result = arkts.factory.updateCallExpression( node, diff --git a/arkui-plugins/ui-plugins/interop/legacy-transformer.ts b/arkui-plugins/ui-plugins/interop/legacy-transformer.ts index 1b831a9b00f81120621115d64aa85b81b2f64145..aff5ac9008b90d56c31202db342eff0972be924c 100644 --- a/arkui-plugins/ui-plugins/interop/legacy-transformer.ts +++ b/arkui-plugins/ui-plugins/interop/legacy-transformer.ts @@ -246,7 +246,10 @@ export class LegacyTransformer extends AbstractVisitor { enter(node: arkts.AstNode): void { if (arkts.isStructDeclaration(node) && !!node.definition.ident) { - const scopeInfo: ScopeInfo = { name: node.definition.ident.name }; + const isComponent = node.definition.annotations.some( + annotation => annotation.expr instanceof arkts.Identifier && + (annotation.expr.name === 'Component' || annotation.expr.name === 'ComponentV2')); + const scopeInfo: ScopeInfo = { name: node.definition.ident.name, isComponent: isComponent}; this.scopeInfos.push(scopeInfo); } } @@ -321,16 +324,17 @@ export class LegacyTransformer extends AbstractVisitor { if (arkts.isStructDeclaration(newNode)) { const definition = newNode.definition!; const annotations = definition.annotations; - if (annotations.some(annotation => annotation instanceof arkts.Identifier && annotation.name === 'Component')) { + if (annotations.some(annotation => annotation instanceof arkts.Identifier && + (annotation.expr.name === 'Component' || annotation.expr.name === 'ComponentV2'))) { return newNode; } const className = newNode.definition?.ident?.name!; const memberMap = this.collectComponentMembers(newNode as arkts.StructDeclaration, className); this.componentInterfaceCollection.push(this.generateComponentInterface(className, node.modifiers, memberMap)); - const updateNode = this.processComponent(newNode); - this.exit(newNode); - return updateNode; - } + const updateNode = this.processComponent(newNode); + this.exit(newNode); + return updateNode; + } if (this.scopeInfos.length > 0 && arkts.isMethodDefinition(newNode)) { const kind = newNode.kind; if (kind === arkts.Es2pandaMethodDefinitionKind.METHOD_DEFINITION_KIND_CONSTRUCTOR) {