diff --git a/arkui-plugins/ui-plugins/customdialog.ts b/arkui-plugins/ui-plugins/customdialog.ts index 8a238ab053c2c1e80b9fb0eab0e7599cacb46ea2..3f24d989eca21ca13a06dee2ac725ec3fff7ad38 100644 --- a/arkui-plugins/ui-plugins/customdialog.ts +++ b/arkui-plugins/ui-plugins/customdialog.ts @@ -109,10 +109,15 @@ export function transformCallToArrow(value: arkts.CallExpression): arkts.ArrowFu export function transformController(newInstance: arkts.ETSNewClassInstanceExpression): arkts.ETSNewClassInstanceExpression { const arg = newInstance.getArguments[0]; - if (!arkts.isObjectExpression(arg)) { - throw new Error('Error CustomDialogOptions'); + let argObj = arg; + const needAs = arkts.isTSAsExpression(arg); + if (needAs) { + argObj = (argObj as arkts.TSAsExpression).expr!; + } + if (!arkts.isObjectExpression(argObj)) { + return newInstance; } - const properties = arg.properties as arkts.Property[]; + const properties = argObj.properties as arkts.Property[]; const property = properties[0]; const value = property?.value; if (!(value && arkts.isCallExpression(value) && arkts.isIdentifier(value.expression))) { @@ -125,58 +130,36 @@ export function transformController(newInstance: arkts.ETSNewClassInstanceExpres property.key, memoArrow ); - const newObj = arkts.ObjectExpression.updateObjectExpression( - arg, + let newObj: arkts.Expression = arkts.ObjectExpression.updateObjectExpression( + argObj, arkts.Es2pandaAstNodeType.AST_NODE_TYPE_OBJECT_EXPRESSION, properties, false ); - const asOptions = arkts.factory.createTSAsExpression( - newObj, - arkts.factory.createTypeReference( - arkts.factory.createTypeReferencePart( - arkts.factory.createIdentifier(CustomComponentNames.CUSTOMDIALOG_CONTROLLER_OPTIONS) - ) - ), - false - ); - return arkts.factory.updateETSNewClassInstanceExpression( - newInstance, - newInstance.getTypeRef, - [asOptions] - ); -} - -function createVarExpression(key_name: string, isProperty: boolean): arkts.Expression { - if (!isProperty) { - return arkts.factory.createIdentifier(key_name + '_Temp'); + if (needAs) { + newObj = arkts.factory.updateTSAsExpression( + arg, + newObj, + arg.typeAnnotation, + arg.isConst + ); + } else { + newObj = arkts.factory.createTSAsExpression( + newObj, + arkts.factory.createTypeReference( + arkts.factory.createTypeReferencePart( + arkts.factory.createIdentifier(CustomComponentNames.CUSTOMDIALOG_CONTROLLER_OPTIONS) + ) + ), + false + ); } - return arkts.factory.createMemberExpression( - arkts.factory.createThisExpression(), - arkts.factory.createIdentifier(key_name), - arkts.Es2pandaMemberExpressionKind.MEMBER_EXPRESSION_KIND_PROPERTY_ACCESS, - false, - false - ); + return arkts.factory.updateETSNewClassInstanceExpression(newInstance, newInstance.getTypeRef, [newObj]); } -function createInvoke(key_name: string, isProperty: boolean): arkts.AstNode[] { + +function createInvoke(controller: arkts.Expression): arkts.AstNode[] { const statements: arkts.AstNode[] = []; - const varExpression = createVarExpression(key_name, isProperty); - if (!isProperty) { - const createVar = arkts.factory.createVariableDeclaration( - arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_NONE, - arkts.Es2pandaVariableDeclarationKind.VARIABLE_DECLARATION_KIND_CONST, - [ - arkts.factory.createVariableDeclarator( - arkts.Es2pandaVariableDeclaratorFlag.VARIABLE_DECLARATOR_FLAG_CONST, - arkts.factory.createIdentifier((varExpression as arkts.Identifier).name), - arkts.factory.createIdentifier(key_name) - ) - ] - ); - statements.push(createVar); - } const invoke = arkts.factory.createExpressionStatement( arkts.factory.createCallExpression( arkts.factory.createMemberExpression( @@ -189,7 +172,7 @@ function createInvoke(key_name: string, isProperty: boolean): arkts.AstNode[] { undefined, [ arkts.factory.createTSAsExpression( - varExpression, + arkts.isIdentifier(controller) ? arkts.factory.createIdentifier(controller.name) : controller, arkts.factory.createTypeReference( arkts.factory.createTypeReferencePart( arkts.factory.createIdentifier(CustomComponentNames.CUSTOMDIALOG_CONTROLLER) @@ -204,8 +187,8 @@ function createInvoke(key_name: string, isProperty: boolean): arkts.AstNode[] { return statements; } -function updateStyleBlock(key_name: string, dialogName: string, isProperty: boolean): arkts.BlockStatement { - const invokeSetController = createInvoke(key_name, isProperty); +function updateStyleBlock(controller: arkts.Expression, dialogName: string): arkts.BlockStatement { + const invokeSetController = createInvoke(controller); return arkts.factory.createBlock( [ arkts.factory.createVariableDeclaration( @@ -232,8 +215,8 @@ function updateStyleBlock(key_name: string, dialogName: string, isProperty: bool ); } -function updateStyle(style: arkts.ArrowFunctionExpression, key_name: string, dialogName: string, isProperty: boolean): arkts.ArrowFunctionExpression { - const block = updateStyleBlock(key_name, dialogName, isProperty); +function updateStyle(style: arkts.ArrowFunctionExpression, controller: arkts.Expression, dialogName: string): arkts.ArrowFunctionExpression { + const block = updateStyleBlock(controller, dialogName); return arkts.factory.updateArrowFunction( style, factory.createScriptFunction( @@ -251,7 +234,7 @@ function updateStyle(style: arkts.ArrowFunctionExpression, key_name: string, dia ); } -export function updateArrow(arrow: arkts.ArrowFunctionExpression, controller: string, isProperty: boolean): arkts.ArrowFunctionExpression { +export function updateArrow(arrow: arkts.ArrowFunctionExpression, controller: arkts.Expression): arkts.ArrowFunctionExpression { const scriptFunction = arrow.scriptFunction as arkts.ScriptFunction; const statement = scriptFunction.body!.statements[0] as arkts.ExpressionStatement; const call = statement.expression as arkts.CallExpression; @@ -259,7 +242,7 @@ export function updateArrow(arrow: arkts.ArrowFunctionExpression, controller: st const dialogName = member.object.name; const styleArrow = call.arguments[1] as arkts.ArrowFunctionExpression; - const newStyle = updateStyle(styleArrow, controller, dialogName, isProperty); + const newStyle = updateStyle(styleArrow, controller, dialogName); const newScriptFunction = factory.createScriptFunction( { body: arkts.factory.createBlock([ @@ -340,7 +323,7 @@ export function updateBody(body: arkts.Statement[]): arkts.Statement[] { export function insertImportDeclaration(program: arkts.Program | undefined): void { if (!program) { - throw Error('Failed to insert import: Transformer has no program'); + return; } const imported = arkts.factory.createIdentifier('ExtendableComponent'); createAndInsertImportDeclaration( @@ -374,31 +357,42 @@ export function transformDeclaration(node: arkts.ClassDeclaration): arkts.ClassD return declaration; } -export function updateNewClassInstanceExpression(node: arkts.ETSNewClassInstanceExpression, varName: string, - isProperty: boolean): arkts.ETSNewClassInstanceExpression { - const asExression = node.getArguments[0] as arkts.TSAsExpression; - const arg = asExression.expr as arkts.ObjectExpression; - if (!arkts.isObjectExpression(arg)) { - throw new Error('Error CustomDialogOptions'); - } +function updateBuilder(arg: arkts.ObjectExpression, variable: arkts.Expression): arkts.ObjectExpression { const properties = arg.properties as arkts.Property[]; const builder = properties[0]; const builder_value = builder.value as arkts.ArrowFunctionExpression; - const newBuilderValue = updateArrow(builder_value, varName, isProperty); + const newBuilderValue = updateArrow(builder_value, variable); const newProperty = arkts.factory.updateProperty( builder, builder.key, newBuilderValue ); - const newObj = arkts.factory.updateObjectExpression( + return arkts.factory.updateObjectExpression( arg, arkts.Es2pandaAstNodeType.AST_NODE_TYPE_OBJECT_EXPRESSION, [newProperty, ...properties.slice(1)], false ); +} + +export function updateNewClassInstanceExpression(node: arkts.ETSNewClassInstanceExpression, + variable: arkts.Expression): arkts.ETSNewClassInstanceExpression { + const asExression = node.getArguments[0] as arkts.TSAsExpression; + const arg = asExression.expr; + let newArg = arg; + if (arg && arkts.isTSAsExpression(arg)) { + newArg = arkts.factory.updateTSAsExpression( + arg, + updateBuilder(arg.expr! as arkts.ObjectExpression, variable), + arg.typeAnnotation, + arg.isConst + ); + } else if (arg && arkts.isObjectExpression(arg)) { + newArg = updateBuilder(arg, variable); + } const newAsExpression = arkts.factory.updateTSAsExpression( asExression, - newObj, + newArg, asExression.typeAnnotation, asExression.isConst ); @@ -441,7 +435,7 @@ export function checkCustomDialogController(node: arkts.BlockStatement): arkts.B const varDeclare = statement.declarators[0]; const varName = varDeclare.name.name; const classInstance = varDeclare.initializer; - const newClass = updateNewClassInstanceExpression(classInstance as arkts.ETSNewClassInstanceExpression, varName, false); + const newClass = updateNewClassInstanceExpression(classInstance as arkts.ETSNewClassInstanceExpression, varDeclare.name); const newVar = arkts.factory.updateVariableDeclaration( statement, 0, @@ -456,6 +450,22 @@ export function checkCustomDialogController(node: arkts.BlockStatement): arkts.B ); const initStatement = arkts.factory.createExpressionStatement(initVar); newStatements.push(initStatement); + } else if (arkts.isExpressionStatement(statement) && arkts.isAssignmentExpression(statement.expression) && + isNewCustomDialogController(statement.expression.right)) { + const classInstance = statement.expression.right; + const varible = statement.expression.left; + const newClass = updateNewClassInstanceExpression(classInstance as arkts.ETSNewClassInstanceExpression, varible!); + const result = arkts.factory.updateExpressionStatement( + statement, + arkts.factory.createExpressionStatement( + arkts.factory.createAssignmentExpression( + statement.expression.left!, + statement.expression.operatorType, + newClass + ) + ) + ); + newStatements.push(result); } else { newStatements.push(statement); } diff --git a/arkui-plugins/ui-plugins/property-translators/regularProperty.ts b/arkui-plugins/ui-plugins/property-translators/regularProperty.ts index 7960157cd39293f2316635b8cf304232b5a095c7..4d76c6752f61dde773b47d399ef86681891d5a5d 100644 --- a/arkui-plugins/ui-plugins/property-translators/regularProperty.ts +++ b/arkui-plugins/ui-plugins/property-translators/regularProperty.ts @@ -41,7 +41,14 @@ export class RegularPropertyTranslator extends PropertyTranslator implements Ini cacheTranslatedInitializer(newName: string, originalName: string): void { const value = this.property.value; if (this.isCustomDialogController(value)) { - const newValue = updateNewClassInstanceExpression(value as arkts.ETSNewClassInstanceExpression, this.property.key?.name, true); + const property = arkts.factory.createMemberExpression( + arkts.factory.createThisExpression(), + this.property.key!, + arkts.Es2pandaMemberExpressionKind.MEMBER_EXPRESSION_KIND_PROPERTY_ACCESS, + false, + false + ) + const newValue = updateNewClassInstanceExpression(value as arkts.ETSNewClassInstanceExpression, property); const initializeStruct: arkts.AstNode = this.generateInitializeStruct(newName, originalName, newValue); PropertyCache.getInstance().collectInitializeStruct(this.structInfo.name, [initializeStruct]); } else { @@ -99,7 +106,7 @@ export class RegularPropertyTranslator extends PropertyTranslator implements Ini return createSetter2(originalName, typeAnnotation, statement); } - generateInitializeStruct(newName: string, originalName: string, value: arkts.Expression): arkts.AstNode { + generateInitializeStruct(newName: string, originalName: string, value?: arkts.Expression): arkts.AstNode { const binaryItem = arkts.factory.createBinaryExpression( factory.createBlockStatementForOptionalExpression( arkts.factory.createIdentifier('initializers'),