From 7c121d1ebf49adeeac64e3ce72cf212b992c4764 Mon Sep 17 00:00:00 2001 From: Yuxin Feng Date: Thu, 5 Jun 2025 11:10:25 +0800 Subject: [PATCH] refactor statemgmt uiplugin transform Signed-off-by: Yuxin Feng Change-Id: Ifdb04be4bf7a2ad4a2d1797bbf12a08e17cd2aa0 --- arkui-plugins/common/predefines.ts | 91 +++--- .../property-translators/consume.ts | 21 +- .../property-translators/factory.ts | 258 ++++++++++++++---- .../ui-plugins/property-translators/link.ts | 24 +- .../property-translators/objectlink.ts | 53 ++-- .../property-translators/observedTrack.ts | 209 ++++---------- .../ui-plugins/property-translators/prop.ts | 68 ++--- .../property-translators/provide.ts | 28 +- .../ui-plugins/property-translators/state.ts | 24 +- .../property-translators/storageProp.ts | 32 +-- .../property-translators/storagelink.ts | 33 +-- .../ui-plugins/property-translators/utils.ts | 3 +- .../ui-plugins/struct-translators/factory.ts | 50 +--- 13 files changed, 445 insertions(+), 449 deletions(-) diff --git a/arkui-plugins/common/predefines.ts b/arkui-plugins/common/predefines.ts index 044a48b95..4abbc3b52 100644 --- a/arkui-plugins/common/predefines.ts +++ b/arkui-plugins/common/predefines.ts @@ -78,32 +78,44 @@ export enum DecoratorIntrinsicNames { } export enum StateManagementTypes { - STATE_DECORATED = 'StateDecoratedVariable', - LINK_DECORATED = 'LinkDecoratedVariable', - STORAGE_LINK_DECORATED = 'StorageLinkDecoratedVariable', - STORAGE_PROP_DECORATED = 'StoragePropDecoratedVariable', - DECORATED_V1 = 'DecoratedV1VariableBase', - PROP_DECORATED = 'PropDecoratedVariable', + STATE_MANAGEMENT_FACTORY = 'STATE_MGMT_FACTORY', + STATE_DECORATED = 'IStateDecoratedVariable', + LINK_DECORATED = 'ILinkDecoratedVariable', + LINK_SOURCE_TYPE = 'LinkSourceType', + STORAGE_LINK_DECORATED = 'IStorageLinkDecoratedVariable', + STORAGE_PROP_DECORATED = 'IStoragePropDecoratedVariable', + PROP_DECORATED = 'IPropDecoratedVariable', MUTABLE_STATE = 'MutableState', - MUTABLE_STATE_META = 'MutableStateMeta', SYNCED_PROPERTY = 'SyncedProperty', - PROVIDE_DECORATED = 'ProvideDecoratedVariable', - CONSUME_DECORATED = 'ConsumeDecoratedVariable', - OBJECT_LINK_DECORATED = 'ObjectLinkDecoratedVariable', - BACKING_VALUE = 'BackingValue', - SET_OBSERVATION_DEPTH = 'setObservationDepth', + PROVIDE_DECORATED = 'IProvideDecoratedVariable', + CONSUME_DECORATED = 'IConsumeDecoratedVariable', + OBJECT_LINK_DECORATED = 'IObjectLinkDecoratedVariable', + MUTABLE_STATE_META = 'IMutableStateMeta', OBSERVED_OBJECT = 'IObservedObject', WATCH_ID_TYPE = 'WatchIdType', - SUBSCRIBED_WATCHES = 'SubscribedWatches', + RENDER_ID_TYPE = 'RenderIdType', + OBSERVE = 'OBSERVE', + META = '__meta', + SUBSCRIBED_WATCHES = 'ISubscribedWatches', STORAGE_LINK_STATE = 'StorageLinkState', OBSERVABLE_PROXY = 'observableProxy', PROP_STATE = 'propState', - INT_32 = 'int32', + UPDATE = 'update', + MAKE_STATE = 'makeState', + MAKE_LINK = 'makeLink', + MAKE_PROP = 'makeProp', + MAKE_STORAGE_PROP = 'makeStorageProp', + MAKE_STORAGE_LINK = 'makeStorageLink', + MAKE_PROVIDE = 'makeProvide', + MAKE_CONSUME = 'makeConsume', + MAKE_OBJECT_LINK = 'makeObjectLink', + MAKE_SUBSCRIBED_WATCHES = 'makeSubscribedWatches', + MAKE_MUTABLESTATE_META = 'makeMutableStateMeta', } export const DECORATOR_TYPE_MAP = new Map([ [DecoratorNames.STATE, StateManagementTypes.STATE_DECORATED], - [DecoratorNames.LINK, StateManagementTypes.DECORATED_V1], + [DecoratorNames.LINK, StateManagementTypes.LINK_SOURCE_TYPE], [DecoratorNames.PROP, StateManagementTypes.PROP_DECORATED], [DecoratorNames.STORAGE_LINK, StateManagementTypes.STORAGE_LINK_DECORATED], [DecoratorNames.STORAGE_PROP, StateManagementTypes.STORAGE_PROP_DECORATED], @@ -118,14 +130,14 @@ export const INTERMEDIATE_IMPORT_SOURCE: Map = new Map = new Map = new Map([ [Dollars.TRANSFORM_DOLLAR_RESOURCE, 'arkui.component.resources'], [Dollars.TRANSFORM_DOLLAR_RAWFILE, 'arkui.component.resources'], - [StateManagementTypes.STATE_DECORATED, 'arkui.stateManagement.decorators.decoratorState'], - [StateManagementTypes.LINK_DECORATED, 'arkui.stateManagement.decorators.decoratorLink'], - [StateManagementTypes.STORAGE_LINK_DECORATED, 'arkui.stateManagement.decorators.decoratorStorageLink'], - [StateManagementTypes.STORAGE_PROP_DECORATED, 'arkui.stateManagement.decorators.decoratorStorageProp'], - [StateManagementTypes.DECORATED_V1, 'arkui.stateManagement.base.decoratorBase'], - [StateManagementTypes.PROP_DECORATED, 'arkui.stateManagement.decorators.decoratorProp'], [StateManagementTypes.MUTABLE_STATE, 'arkui.stateManagement.runtime'], - [StateManagementTypes.MUTABLE_STATE_META, 'arkui.stateManagement.base.mutableStateMeta'], [StateManagementTypes.SYNCED_PROPERTY, 'arkui.stateManagement.runtime'], - [StateManagementTypes.PROVIDE_DECORATED, 'arkui.stateManagement.decorators.decoratorProvide'], - [StateManagementTypes.CONSUME_DECORATED, 'arkui.stateManagement.decorators.decoratorConsume'], - [StateManagementTypes.OBJECT_LINK_DECORATED, 'arkui.stateManagement.decorators.decoratorObjectLink'], - [StateManagementTypes.BACKING_VALUE, 'arkui.stateManagement.base.backingValue'], - [StateManagementTypes.SET_OBSERVATION_DEPTH, 'arkui.stateManagement.base.iObservedObject'], - [StateManagementTypes.OBSERVED_OBJECT, 'arkui.stateManagement.base.iObservedObject'], - [StateManagementTypes.WATCH_ID_TYPE, 'arkui.stateManagement.decorators.decoratorWatch'], - [StateManagementTypes.SUBSCRIBED_WATCHES, 'arkui.stateManagement.decorators.decoratorWatch'], [StateManagementTypes.STORAGE_LINK_STATE, 'arkui.stateManagement.runtime'], [StateManagementTypes.OBSERVABLE_PROXY, 'arkui.stateManagement.runtime'], [StateManagementTypes.PROP_STATE, 'arkui.stateManagement.runtime'], - [StateManagementTypes.INT_32, '@koalaui.runtime.common'], ]); + +export enum GetSetTypes { + GET = 'get', + SET = 'set', +} \ No newline at end of file diff --git a/arkui-plugins/ui-plugins/property-translators/consume.ts b/arkui-plugins/ui-plugins/property-translators/consume.ts index d4f0176b2..8bd40ec2e 100644 --- a/arkui-plugins/ui-plugins/property-translators/consume.ts +++ b/arkui-plugins/ui-plugins/property-translators/consume.ts @@ -16,7 +16,7 @@ import * as arkts from '@koalaui/libarkts'; import { backingField, expectName } from '../../common/arkts-utils'; -import { DecoratorNames, StateManagementTypes } from '../../common/predefines'; +import { DecoratorNames, GetSetTypes, StateManagementTypes } from '../../common/predefines'; import { generateToRecord, createGetter, @@ -56,9 +56,9 @@ export class ConsumeTranslator extends PropertyTranslator implements Initializer arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_PRIVATE ); const thisValue: arkts.Expression = generateThisBacking(newName, false, true); - const thisGet: arkts.CallExpression = generateGetOrSetCall(thisValue, 'get'); + const thisGet: arkts.CallExpression = generateGetOrSetCall(thisValue, GetSetTypes.GET); const thisSet: arkts.ExpressionStatement = arkts.factory.createExpressionStatement( - generateGetOrSetCall(thisValue, 'set') + generateGetOrSetCall(thisValue, GetSetTypes.SET) ); const getter: arkts.MethodDefinition = this.translateGetter( originalName, @@ -91,11 +91,22 @@ export class ConsumeTranslator extends PropertyTranslator implements Initializer } generateInitializeStruct(originalName: string, newName: string): arkts.AstNode { - const alias = getValueInAnnotation(this.property, DecoratorNames.CONSUME); + const args: arkts.Expression[] = [ + arkts.factory.create1StringLiteral(originalName), + arkts.factory.create1StringLiteral( + getValueInAnnotation(this.property, DecoratorNames.CONSUME) ?? originalName + ), + ]; + factory.judgeIfAddWatchFunc(args, this.property); const assign: arkts.AssignmentExpression = arkts.factory.createAssignmentExpression( generateThisBacking(newName), arkts.Es2pandaTokenType.TOKEN_TYPE_PUNCTUATOR_SUBSTITUTION, - factory.generateInitConsumeCall(originalName, this.property, alias ?? originalName) + factory.generateStateMgmtFactoryCall( + StateManagementTypes.MAKE_CONSUME, + this.property.typeAnnotation, + args, + true + ) ); return arkts.factory.createExpressionStatement(assign); } diff --git a/arkui-plugins/ui-plugins/property-translators/factory.ts b/arkui-plugins/ui-plugins/property-translators/factory.ts index 904811f12..f77047363 100644 --- a/arkui-plugins/ui-plugins/property-translators/factory.ts +++ b/arkui-plugins/ui-plugins/property-translators/factory.ts @@ -18,7 +18,7 @@ import { GenSymGenerator } from '../../common/gensym-generator'; import { DecoratorNames, DECORATOR_TYPE_MAP, StateManagementTypes } from '../../common/predefines'; import { factory as UIFactory } from '../ui-factory'; import { collectStateManagementTypeImport, getValueInAnnotation, hasDecorator, removeDecorator } from './utils'; -import { addMemoAnnotation, findCanAddMemoFromTypeAnnotation } from '../utils'; +import { addMemoAnnotation, findCanAddMemoFromTypeAnnotation, CustomComponentNames } from '../utils'; export class factory { /** @@ -235,64 +235,49 @@ export class factory { } /* - * create `this.addProvidedVar(, , initializers?. ?? , , watchFunc)`. + * create `StateMgmtFactory.(this, ...);`. */ - static generateAddProvideVarCall( - originalName: string, - property: arkts.ClassProperty, - alias: string, - allowOverride: boolean = false + static generateStateMgmtFactoryCall( + makeType: StateManagementTypes, + typeArguments: arkts.TypeNode | undefined, + args: arkts.AstNode[], + argsContainsThis: boolean ): arkts.CallExpression { - const args: arkts.Expression[] = [ - arkts.factory.create1StringLiteral(originalName), - arkts.factory.create1StringLiteral(alias), - arkts.factory.createBinaryExpression( - factory.createBlockStatementForOptionalExpression( - arkts.factory.createIdentifier('initializers'), - originalName - ), - property.value ?? arkts.factory.createUndefinedLiteral(), - arkts.Es2pandaTokenType.TOKEN_TYPE_PUNCTUATOR_NULLISH_COALESCING - ), - arkts.factory.createBooleanLiteral(allowOverride), - ]; - factory.judgeIfAddWatchFunc(args, property); + collectStateManagementTypeImport(StateManagementTypes.STATE_MANAGEMENT_FACTORY); return arkts.factory.createCallExpression( arkts.factory.createMemberExpression( - arkts.factory.createThisExpression(), - arkts.factory.createIdentifier('addProvidedVar'), + arkts.factory.createIdentifier(StateManagementTypes.STATE_MANAGEMENT_FACTORY), + arkts.factory.createIdentifier(makeType), arkts.Es2pandaMemberExpressionKind.MEMBER_EXPRESSION_KIND_PROPERTY_ACCESS, false, false ), - property.typeAnnotation ? [property.typeAnnotation.clone()] : undefined, - args + typeArguments ? [typeArguments] : undefined, + [...(argsContainsThis ? [arkts.factory.createThisExpression()] : []), ...args] ); } /* - * create `this.initConsume(, , watchFunc)`. + * create if statement in __updateStruct method. */ - static generateInitConsumeCall( + static createIfInUpdateStruct( originalName: string, - property: arkts.ClassProperty, - alias: string - ): arkts.CallExpression { - const args: arkts.Expression[] = [ - arkts.factory.create1StringLiteral(originalName), - arkts.factory.create1StringLiteral(alias), - ]; - factory.judgeIfAddWatchFunc(args, property); - return arkts.factory.createCallExpression( - arkts.factory.createMemberExpression( - arkts.factory.createThisExpression(), - arkts.factory.createIdentifier('initConsume'), - arkts.Es2pandaMemberExpressionKind.MEMBER_EXPRESSION_KIND_PROPERTY_ACCESS, - false, - false + member: arkts.Expression, + args: arkts.AstNode[] + ): arkts.IfStatement { + const binaryItem = arkts.factory.createBinaryExpression( + factory.createBlockStatementForOptionalExpression( + arkts.factory.createIdentifier(CustomComponentNames.COMPONENT_INITIALIZERS_NAME), + originalName ), - property.typeAnnotation ? [property.typeAnnotation.clone()] : undefined, - args + arkts.factory.createUndefinedLiteral(), + arkts.Es2pandaTokenType.TOKEN_TYPE_PUNCTUATOR_NOT_STRICT_EQUAL + ); + return arkts.factory.createIfStatement( + binaryItem, + arkts.factory.createBlock([ + arkts.factory.createExpressionStatement(arkts.factory.createCallExpression(member, undefined, args)), + ]) ); } @@ -341,17 +326,13 @@ export class factory { ); } + /* + * create watch related members in Observed/Track classes + */ static createWatchMembers(): arkts.AstNode[] { const subscribedWatches: arkts.ClassProperty = arkts.factory.createClassProperty( arkts.factory.createIdentifier('subscribedWatches'), - arkts.factory.createETSNewClassInstanceExpression( - arkts.factory.createTypeReference( - arkts.factory.createTypeReferencePart( - arkts.factory.createIdentifier(StateManagementTypes.SUBSCRIBED_WATCHES) - ) - ), - [] - ), + factory.generateStateMgmtFactoryCall(StateManagementTypes.MAKE_SUBSCRIBED_WATCHES, undefined, [], false), arkts.factory.createTypeReference( arkts.factory.createTypeReferencePart( arkts.factory.createIdentifier(StateManagementTypes.SUBSCRIBED_WATCHES) @@ -389,6 +370,9 @@ export class factory { return [subscribedWatches, addWatchSubscriber, removeWatchSubscriber, executeOnSubscribingWatches]; } + /* + * helper for createWatchMembers to create watch methods + */ static createWatchMethod( methodName: string, returnType: arkts.Es2pandaPrimitiveType, @@ -441,6 +425,9 @@ export class factory { ); } + /* + * helper for createWatchMethod, generates this.subscribedWatches.xxx + */ static thisSubscribedWatchesMember(member: string): arkts.MemberExpression { return arkts.factory.createMemberExpression( arkts.factory.createMemberExpression( @@ -457,6 +444,173 @@ export class factory { ); } + /* + * create ____V1RenderId related members in Observed/Track classes + */ + static createV1RenderIdMembers(): arkts.AstNode[] { + const v1RenderId: arkts.ClassProperty = arkts.factory.createClassProperty( + arkts.factory.createIdentifier('____V1RenderId'), + arkts.factory.createNumericLiteral(0), + arkts.factory.createTypeReference( + arkts.factory.createTypeReferencePart( + arkts.factory.createIdentifier(StateManagementTypes.RENDER_ID_TYPE) + ) + ), + arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_PRIVATE, + false + ); + collectStateManagementTypeImport(StateManagementTypes.RENDER_ID_TYPE); + const setV1RenderId: arkts.MethodDefinition = factory.setV1RenderId(); + return [v1RenderId, setV1RenderId]; + } + + /* + * helper for createV1RenderIdMembers to generate setV1RenderId method + */ + static setV1RenderId(): arkts.MethodDefinition { + const assignRenderId: arkts.ExpressionStatement = arkts.factory.createExpressionStatement( + arkts.factory.createAssignmentExpression( + arkts.factory.createMemberExpression( + arkts.factory.createThisExpression(), + arkts.factory.createIdentifier('____V1RenderId'), + arkts.Es2pandaMemberExpressionKind.MEMBER_EXPRESSION_KIND_PROPERTY_ACCESS, + false, + false + ), + arkts.Es2pandaTokenType.TOKEN_TYPE_PUNCTUATOR_SUBSTITUTION, + arkts.factory.createIdentifier('renderId') + ) + ); + const funcSig: arkts.FunctionSignature = arkts.factory.createFunctionSignature( + undefined, + [ + arkts.factory.createParameterDeclaration( + arkts.factory.createIdentifier( + 'renderId', + arkts.factory.createTypeReference( + arkts.factory.createTypeReferencePart( + arkts.factory.createIdentifier(StateManagementTypes.RENDER_ID_TYPE) + ) + ) + ), + undefined + ), + ], + arkts.factory.createPrimitiveType(arkts.Es2pandaPrimitiveType.PRIMITIVE_TYPE_VOID), + false + ); + return arkts.factory.createMethodDefinition( + arkts.Es2pandaMethodDefinitionKind.METHOD_DEFINITION_KIND_METHOD, + arkts.factory.createIdentifier('setV1RenderId'), + arkts.factory.createScriptFunction( + arkts.factory.createBlock([assignRenderId]), + funcSig, + arkts.Es2pandaScriptFunctionFlags.SCRIPT_FUNCTION_FLAGS_METHOD, + arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_PUBLIC + ), + arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_PUBLIC, + false + ); + } + + /* + * create conditionalAddRef method in Observed/Track classes + */ + static conditionalAddRef(): arkts.MethodDefinition { + const funcSig: arkts.FunctionSignature = arkts.factory.createFunctionSignature( + undefined, + [ + arkts.factory.createParameterDeclaration( + arkts.factory.createIdentifier( + 'meta', + arkts.factory.createTypeReference( + arkts.factory.createTypeReferencePart( + arkts.factory.createIdentifier(StateManagementTypes.MUTABLE_STATE_META) + ) + ) + ), + undefined + ), + ], + arkts.factory.createPrimitiveType(arkts.Es2pandaPrimitiveType.PRIMITIVE_TYPE_VOID), + false + ); + collectStateManagementTypeImport(StateManagementTypes.MUTABLE_STATE_META); + const shouldAddRef: arkts.IfStatement = factory.shouldAddRef(); + return arkts.factory.createMethodDefinition( + arkts.Es2pandaMethodDefinitionKind.METHOD_DEFINITION_KIND_METHOD, + arkts.factory.createIdentifier('conditionalAddRef'), + arkts.factory.createScriptFunction( + arkts.factory.createBlock([shouldAddRef]), + funcSig, + arkts.Es2pandaScriptFunctionFlags.SCRIPT_FUNCTION_FLAGS_METHOD, + arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_PROTECTED + ), + arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_PROTECTED, + false + ); + } + + /* + * helper for conditionalAddRef to generate shouldAddRef method + */ + static shouldAddRef(): arkts.IfStatement { + const test: arkts.CallExpression = arkts.factory.createCallExpression( + arkts.factory.createMemberExpression( + arkts.factory.createIdentifier(StateManagementTypes.OBSERVE), + arkts.factory.createIdentifier('shouldAddRef'), + arkts.Es2pandaMemberExpressionKind.MEMBER_EXPRESSION_KIND_PROPERTY_ACCESS, + false, + false + ), + undefined, + [ + arkts.factory.createMemberExpression( + arkts.factory.createThisExpression(), + arkts.factory.createIdentifier('____V1RenderId'), + arkts.Es2pandaMemberExpressionKind.MEMBER_EXPRESSION_KIND_PROPERTY_ACCESS, + false, + false + ), + ] + ); + collectStateManagementTypeImport(StateManagementTypes.OBSERVE); + const consequent: arkts.BlockStatement = arkts.factory.createBlock([ + arkts.factory.createExpressionStatement( + arkts.factory.createCallExpression( + arkts.factory.createMemberExpression( + arkts.factory.createIdentifier('meta'), + arkts.factory.createIdentifier('addRef'), + arkts.Es2pandaMemberExpressionKind.MEMBER_EXPRESSION_KIND_PROPERTY_ACCESS, + false, + false + ), + undefined, + undefined + ) + ), + ]); + return arkts.factory.createIfStatement(test, consequent); + } + + /* + * helper to create meta field in classes with only @Observe and no @Track + */ + static createMetaInObservedClass(): arkts.ClassProperty { + collectStateManagementTypeImport(StateManagementTypes.MUTABLE_STATE_META); + return arkts.factory.createClassProperty( + arkts.factory.createIdentifier(StateManagementTypes.META), + factory.generateStateMgmtFactoryCall(StateManagementTypes.MAKE_MUTABLESTATE_META, undefined, [], false), + arkts.factory.createTypeReference( + arkts.factory.createTypeReferencePart( + arkts.factory.createIdentifier(StateManagementTypes.MUTABLE_STATE_META) + ) + ), + arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_PRIVATE, + false + ); + } + /** * add `@memo` to the `@Builder` methods in class. */ diff --git a/arkui-plugins/ui-plugins/property-translators/link.ts b/arkui-plugins/ui-plugins/property-translators/link.ts index 09a72cda4..65217d539 100644 --- a/arkui-plugins/ui-plugins/property-translators/link.ts +++ b/arkui-plugins/ui-plugins/property-translators/link.ts @@ -16,7 +16,8 @@ import * as arkts from '@koalaui/libarkts'; import { backingField, expectName } from '../../common/arkts-utils'; -import { DecoratorNames, StateManagementTypes } from '../../common/predefines'; +import { DecoratorNames, GetSetTypes, StateManagementTypes } from '../../common/predefines'; +import { CustomComponentNames } from '../utils'; import { generateToRecord, createGetter, @@ -51,27 +52,34 @@ export class LinkTranslator extends PropertyTranslator implements InitializerCon generateInitializeStruct(newName: string, originalName: string) { const test = factory.createBlockStatementForOptionalExpression( - arkts.factory.createIdentifier('initializers'), + arkts.factory.createIdentifier(CustomComponentNames.COMPONENT_INITIALIZERS_NAME), newName ); const args: arkts.Expression[] = [ arkts.factory.create1StringLiteral(originalName), arkts.factory.createTSNonNullExpression( - factory.createNonNullOrOptionalMemberExpression('initializers', newName, false, true) + factory.createNonNullOrOptionalMemberExpression( + CustomComponentNames.COMPONENT_INITIALIZERS_NAME, + newName, + false, + true + ) ), ]; factory.judgeIfAddWatchFunc(args, this.property); collectStateManagementTypeImport(StateManagementTypes.LINK_DECORATED); + collectStateManagementTypeImport(StateManagementTypes.LINK_SOURCE_TYPE); const consequent = arkts.BlockStatement.createBlockStatement([ arkts.factory.createExpressionStatement( arkts.factory.createAssignmentExpression( generateThisBacking(newName, false, false), arkts.Es2pandaTokenType.TOKEN_TYPE_PUNCTUATOR_SUBSTITUTION, - factory.createNewDecoratedInstantiate( - StateManagementTypes.LINK_DECORATED, + factory.generateStateMgmtFactoryCall( + StateManagementTypes.MAKE_LINK, this.property.typeAnnotation, - args + args, + true ) ) ), @@ -88,9 +96,9 @@ export class LinkTranslator extends PropertyTranslator implements InitializerCon arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_PRIVATE ); const thisValue: arkts.Expression = generateThisBacking(newName, false, true); - const thisGet: arkts.CallExpression = generateGetOrSetCall(thisValue, 'get'); + const thisGet: arkts.CallExpression = generateGetOrSetCall(thisValue, GetSetTypes.GET); const thisSet: arkts.ExpressionStatement = arkts.factory.createExpressionStatement( - generateGetOrSetCall(thisValue, 'set') + generateGetOrSetCall(thisValue, GetSetTypes.SET) ); const getter: arkts.MethodDefinition = this.translateGetter( originalName, diff --git a/arkui-plugins/ui-plugins/property-translators/objectlink.ts b/arkui-plugins/ui-plugins/property-translators/objectlink.ts index 27e7af8fc..2c2089794 100644 --- a/arkui-plugins/ui-plugins/property-translators/objectlink.ts +++ b/arkui-plugins/ui-plugins/property-translators/objectlink.ts @@ -16,7 +16,8 @@ import * as arkts from '@koalaui/libarkts'; import { backingField, expectName } from '../../common/arkts-utils'; -import { DecoratorNames, StateManagementTypes } from '../../common/predefines'; +import { DecoratorNames, GetSetTypes, StateManagementTypes } from '../../common/predefines'; +import { CustomComponentNames } from '../utils'; import { createGetter, generateGetOrSetCall, @@ -55,60 +56,42 @@ export class ObjectLinkTranslator extends PropertyTranslator implements Initiali generateInitializeStruct(newName: string, originalName: string): arkts.AstNode { const initializers = arkts.factory.createTSNonNullExpression( factory.createBlockStatementForOptionalExpression( - arkts.factory.createIdentifier('initializers'), + arkts.factory.createIdentifier(CustomComponentNames.COMPONENT_INITIALIZERS_NAME), originalName ) ); - const args: arkts.Expression[] = [arkts.factory.create1StringLiteral(originalName), initializers]; factory.judgeIfAddWatchFunc(args, this.property); - const newClass = arkts.factory.createETSNewClassInstanceExpression( - arkts.factory.createTypeReference( - arkts.factory.createTypeReferencePart( - arkts.factory.createIdentifier('ObjectLinkDecoratedVariable'), - arkts.factory.createTSTypeParameterInstantiation( - this.property.typeAnnotation ? [this.property.typeAnnotation] : [] - ) - ) - ), - args - ); - return arkts.factory.createAssignmentExpression( generateThisBacking(newName), arkts.Es2pandaTokenType.TOKEN_TYPE_PUNCTUATOR_SUBSTITUTION, - newClass + factory.generateStateMgmtFactoryCall( + StateManagementTypes.MAKE_OBJECT_LINK, + this.property.typeAnnotation, + args, + true + ) ); } generateUpdateStruct(newName: string, originalName: string): arkts.AstNode { - const binaryItem = arkts.factory.createBinaryExpression( - factory.createBlockStatementForOptionalExpression( - arkts.factory.createIdentifier('initializers'), - originalName - ), - arkts.factory.createUndefinedLiteral(), - arkts.Es2pandaTokenType.TOKEN_TYPE_PUNCTUATOR_NOT_STRICT_EQUAL - ); const member: arkts.MemberExpression = arkts.factory.createMemberExpression( generateThisBacking(newName, false, true), - arkts.factory.createIdentifier('update'), + arkts.factory.createIdentifier(StateManagementTypes.UPDATE), arkts.Es2pandaMemberExpressionKind.MEMBER_EXPRESSION_KIND_PROPERTY_ACCESS, false, false ); const nonNullItem = arkts.factory.createTSNonNullExpression( - factory.createNonNullOrOptionalMemberExpression('initializers', originalName, false, true) - ); - return arkts.factory.createIfStatement( - binaryItem, - arkts.factory.createBlock([ - arkts.factory.createExpressionStatement( - arkts.factory.createCallExpression(member, undefined, [nonNullItem]) - ), - ]) + factory.createNonNullOrOptionalMemberExpression( + CustomComponentNames.COMPONENT_INITIALIZERS_NAME, + originalName, + false, + true + ) ); + return factory.createIfInUpdateStruct(originalName, member, [nonNullItem]); } translateWithoutInitializer(newName: string, originalName: string): arkts.AstNode[] { @@ -119,7 +102,7 @@ export class ObjectLinkTranslator extends PropertyTranslator implements Initiali arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_PRIVATE ); const thisValue: arkts.Expression = generateThisBacking(newName, false, true); - const thisGet: arkts.CallExpression = generateGetOrSetCall(thisValue, 'get'); + const thisGet: arkts.CallExpression = generateGetOrSetCall(thisValue, GetSetTypes.GET); const getter: arkts.MethodDefinition = this.translateGetter( originalName, this.property.typeAnnotation, diff --git a/arkui-plugins/ui-plugins/property-translators/observedTrack.ts b/arkui-plugins/ui-plugins/property-translators/observedTrack.ts index 91e3fdf99..7bf4e179a 100644 --- a/arkui-plugins/ui-plugins/property-translators/observedTrack.ts +++ b/arkui-plugins/ui-plugins/property-translators/observedTrack.ts @@ -16,11 +16,9 @@ import * as arkts from '@koalaui/libarkts'; import { backingField, expectName } from '../../common/arkts-utils'; import { DecoratorNames, StateManagementTypes } from '../../common/predefines'; -import { - collectStateManagementTypeImport, - hasDecorator, -} from './utils'; +import { collectStateManagementTypeImport, hasDecorator } from './utils'; import { ClassScopeInfo } from './types'; +import { factory } from './factory'; export class ObservedTrackTranslator { protected property: arkts.ClassProperty; @@ -43,31 +41,19 @@ export class ObservedTrackTranslator { ? this.removeImplementProperty(expectName(this.property.key)) : expectName(this.property.key); const newName: string = backingField(originalName); - let properyIsClass = false; - - if (this.property.typeAnnotation && arkts.isETSTypeReference(this.property.typeAnnotation)) { - const decl = arkts.getDecl(this.property.typeAnnotation.part?.name!); - if (arkts.isClassDefinition(decl!)) { - properyIsClass = true; - } - } - const field = this.createField(originalName, newName, properyIsClass); - - this.transformGetterSetter(originalName, newName, properyIsClass); - + const field = this.createField(originalName, newName); + this.transformGetterSetter(originalName, newName); return [...field]; } - createField(originalName: string, newName: string, properyIsClass: boolean): arkts.ClassProperty[] { - const backingField = properyIsClass - ? this.propertyIsClassField(newName) - : arkts.factory.createClassProperty( - arkts.factory.createIdentifier(newName), - this.property.value, - this.property.typeAnnotation, - arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_PRIVATE, - false - ); + createField(originalName: string, newName: string): arkts.ClassProperty[] { + const backingField = arkts.factory.createClassProperty( + arkts.factory.createIdentifier(newName), + this.property.value, + this.property.typeAnnotation, + arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_PRIVATE, + false + ); if (!this.isTracked) { return [backingField]; } @@ -75,14 +61,31 @@ export class ObservedTrackTranslator { return [backingField, metaField]; } - createGetter(originalName: string, newName: string, properyIsClass: boolean): arkts.MethodDefinition { - const ifRefDepth: arkts.IfStatement = this.getterIfRefDepth(originalName); - const returnMember: arkts.ReturnStatement = this.getterReturnMember(properyIsClass, newName); - const body = arkts.factory.createBlock([ - ifRefDepth, - ...(properyIsClass ? [this.getterSetObservationDepth(newName)] : []), - returnMember, - ]); + createGetter(originalName: string, newName: string): arkts.MethodDefinition { + const conditionalAddRef = arkts.factory.createExpressionStatement( + arkts.factory.createCallExpression( + arkts.factory.createMemberExpression( + arkts.factory.createThisExpression(), + arkts.factory.createIdentifier('conditionalAddRef'), + arkts.Es2pandaMemberExpressionKind.MEMBER_EXPRESSION_KIND_PROPERTY_ACCESS, + false, + false + ), + undefined, + [ + arkts.factory.createMemberExpression( + arkts.factory.createThisExpression(), + this.metaIdentifier(originalName), + arkts.Es2pandaMemberExpressionKind.MEMBER_EXPRESSION_KIND_PROPERTY_ACCESS, + false, + false + ), + ] + ) + ); + const returnMember: arkts.ReturnStatement = arkts.factory.createReturnStatement(this.genThisBacking(newName)); + + const body = arkts.factory.createBlock([conditionalAddRef, returnMember]); const scriptFunction = arkts.factory.createScriptFunction( body, @@ -100,8 +103,8 @@ export class ObservedTrackTranslator { ); } - createSetter(originalName: string, newName: string, properyIsClass: boolean): arkts.MethodDefinition { - const ifEqualsNewValue: arkts.IfStatement = this.setterIfEqualsNewValue(properyIsClass, originalName, newName); + createSetter(originalName: string, newName: string): arkts.MethodDefinition { + const ifEqualsNewValue: arkts.IfStatement = this.setterIfEqualsNewValue(originalName, newName); const body = arkts.factory.createBlock([ifEqualsNewValue]); const param = arkts.factory.createParameterDeclaration( arkts.factory.createIdentifier('newValue', this.property.typeAnnotation), @@ -134,20 +137,10 @@ export class ObservedTrackTranslator { ); } - genThisBackingValue(newName: string): arkts.MemberExpression { - return arkts.factory.createMemberExpression( - this.genThisBacking(newName), - arkts.factory.createIdentifier('value'), - arkts.Es2pandaMemberExpressionKind.MEMBER_EXPRESSION_KIND_PROPERTY_ACCESS, - false, - false - ); - } - metaIdentifier(originalName: string): arkts.Identifier { return this.isTracked - ? arkts.factory.createIdentifier(`__meta_${originalName}`) - : arkts.factory.createIdentifier('__meta'); + ? arkts.factory.createIdentifier(`${StateManagementTypes.META}_${originalName}`) + : arkts.factory.createIdentifier(StateManagementTypes.META); } removeImplementProperty(originalName: string): string { @@ -155,9 +148,9 @@ export class ObservedTrackTranslator { return originalName.substring(prefix.length); } - transformGetterSetter(originalName: string, newName: string, properyIsClass: boolean): void { - const newGetter = this.createGetter(originalName, newName, properyIsClass); - const newSetter = this.createSetter(originalName, newName, properyIsClass); + transformGetterSetter(originalName: string, newName: string): void { + const newGetter = this.createGetter(originalName, newName); + const newSetter = this.createSetter(originalName, newName); if (this.hasImplement) { { const idx: number = this.classScopeInfo.getters.findIndex( @@ -190,48 +183,11 @@ export class ObservedTrackTranslator { } } - propertyIsClassField(newName: string): arkts.ClassProperty { - collectStateManagementTypeImport(StateManagementTypes.BACKING_VALUE); - return arkts.factory.createClassProperty( - arkts.factory.createIdentifier(newName), - this.property.value - ? arkts.factory.createETSNewClassInstanceExpression( - arkts.factory.createTypeReference( - arkts.factory.createTypeReferencePart( - arkts.factory.createIdentifier(StateManagementTypes.BACKING_VALUE), - arkts.factory.createTSTypeParameterInstantiation( - this.property.typeAnnotation ? [this.property.typeAnnotation] : [] - ) - ) - ), - [this.property.value] - ) - : undefined, - arkts.factory.createTypeReference( - arkts.factory.createTypeReferencePart( - arkts.factory.createIdentifier(StateManagementTypes.BACKING_VALUE), - arkts.factory.createTSTypeParameterInstantiation( - this.property.typeAnnotation ? [this.property.typeAnnotation] : [] - ) - ) - ), - arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_PRIVATE, - false - ); - } - metaField(originalName: string): arkts.ClassProperty { collectStateManagementTypeImport(StateManagementTypes.MUTABLE_STATE_META); return arkts.factory.createClassProperty( - arkts.factory.createIdentifier(`__meta_${originalName}`), - arkts.factory.createETSNewClassInstanceExpression( - arkts.factory.createTypeReference( - arkts.factory.createTypeReferencePart( - arkts.factory.createIdentifier(StateManagementTypes.MUTABLE_STATE_META) - ) - ), - [arkts.factory.createStringLiteral('@Track')] - ), + arkts.factory.createIdentifier(`${StateManagementTypes.META}_${originalName}`), + factory.generateStateMgmtFactoryCall(StateManagementTypes.MAKE_MUTABLESTATE_META, undefined, [], false), arkts.factory.createTypeReference( arkts.factory.createTypeReferencePart( arkts.factory.createIdentifier(StateManagementTypes.MUTABLE_STATE_META) @@ -242,77 +198,8 @@ export class ObservedTrackTranslator { ); } - getterIfRefDepth(originalName: string): arkts.IfStatement { - return arkts.factory.createIfStatement( - arkts.factory.createBinaryExpression( - arkts.factory.createMemberExpression( - arkts.factory.createThisExpression(), - arkts.factory.createIdentifier('_permissibleAddRefDepth'), - arkts.Es2pandaMemberExpressionKind.MEMBER_EXPRESSION_KIND_PROPERTY_ACCESS, - false, - false - ), - arkts.factory.createNumericLiteral(0), - arkts.Es2pandaTokenType.TOKEN_TYPE_PUNCTUATOR_GREATER_THAN - ), - arkts.factory.createBlock([ - arkts.factory.createExpressionStatement( - arkts.factory.createCallExpression( - arkts.factory.createMemberExpression( - arkts.factory.createMemberExpression( - arkts.factory.createThisExpression(), - this.metaIdentifier(originalName), - arkts.Es2pandaMemberExpressionKind.MEMBER_EXPRESSION_KIND_PROPERTY_ACCESS, - false, - false - ), - arkts.factory.createIdentifier('addRef'), - arkts.Es2pandaMemberExpressionKind.MEMBER_EXPRESSION_KIND_PROPERTY_ACCESS, - false, - false - ), - undefined, - undefined, - false, - false - ) - ), - ]) - ); - } - - getterSetObservationDepth(newName: string): arkts.ExpressionStatement { - collectStateManagementTypeImport(StateManagementTypes.SET_OBSERVATION_DEPTH); - return arkts.factory.createExpressionStatement( - arkts.factory.createCallExpression( - arkts.factory.createIdentifier(StateManagementTypes.SET_OBSERVATION_DEPTH), - undefined, - [ - this.genThisBackingValue(newName), - arkts.factory.createBinaryExpression( - arkts.factory.createMemberExpression( - arkts.factory.createThisExpression(), - arkts.factory.createIdentifier('_permissibleAddRefDepth'), - arkts.Es2pandaMemberExpressionKind.MEMBER_EXPRESSION_KIND_PROPERTY_ACCESS, - false, - false - ), - arkts.factory.createNumericLiteral(1), - arkts.Es2pandaTokenType.TOKEN_TYPE_PUNCTUATOR_MINUS - ), - ] - ) - ); - } - - getterReturnMember(properyIsClass: boolean, newName: string): arkts.ReturnStatement { - return arkts.factory.createReturnStatement( - properyIsClass ? this.genThisBackingValue(newName) : this.genThisBacking(newName) - ); - } - - setterIfEqualsNewValue(properyIsClass: boolean, originalName: string, newName: string): arkts.IfStatement { - const backingValue = properyIsClass ? this.genThisBackingValue(newName) : this.genThisBacking(newName); + setterIfEqualsNewValue(originalName: string, newName: string): arkts.IfStatement { + const backingValue = this.genThisBacking(newName); const setNewValue = arkts.factory.createExpressionStatement( arkts.factory.createAssignmentExpression( diff --git a/arkui-plugins/ui-plugins/property-translators/prop.ts b/arkui-plugins/ui-plugins/property-translators/prop.ts index 8cfacd224..df2bce793 100644 --- a/arkui-plugins/ui-plugins/property-translators/prop.ts +++ b/arkui-plugins/ui-plugins/property-translators/prop.ts @@ -16,7 +16,8 @@ import * as arkts from '@koalaui/libarkts'; import { backingField, expectName } from '../../common/arkts-utils'; -import { DecoratorNames, StateManagementTypes } from '../../common/predefines'; +import { DecoratorNames, GetSetTypes, StateManagementTypes } from '../../common/predefines'; +import { CustomComponentNames } from '../utils'; import { generateToRecord, createGetter, @@ -60,9 +61,9 @@ export class PropTranslator extends PropertyTranslator implements InitializerCon arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_PRIVATE ); const thisValue: arkts.Expression = generateThisBacking(newName, false, true); - const thisGet: arkts.CallExpression = generateGetOrSetCall(thisValue, 'get'); + const thisGet: arkts.CallExpression = generateGetOrSetCall(thisValue, GetSetTypes.GET); const thisSet: arkts.ExpressionStatement = arkts.factory.createExpressionStatement( - generateGetOrSetCall(thisValue, 'set') + generateGetOrSetCall(thisValue, GetSetTypes.SET) ); const getter: arkts.MethodDefinition = this.translateGetter( originalName, @@ -97,7 +98,7 @@ export class PropTranslator extends PropertyTranslator implements InitializerCon generateInitializeStruct(newName: string, originalName: string): arkts.AstNode { const binaryItem = arkts.factory.createBinaryExpression( factory.createBlockStatementForOptionalExpression( - arkts.factory.createIdentifier('initializers'), + arkts.factory.createIdentifier(CustomComponentNames.COMPONENT_INITIALIZERS_NAME), originalName ), this.property.value ?? arkts.factory.createUndefinedLiteral(), @@ -108,62 +109,51 @@ export class PropTranslator extends PropertyTranslator implements InitializerCon this.property.value ? binaryItem : arkts.factory.createTSAsExpression( - factory.createNonNullOrOptionalMemberExpression('initializers', originalName, false, true), + factory.createNonNullOrOptionalMemberExpression( + CustomComponentNames.COMPONENT_INITIALIZERS_NAME, + originalName, + false, + true + ), this.property.typeAnnotation ? this.property.typeAnnotation.clone() : undefined, false ), ]; factory.judgeIfAddWatchFunc(args, this.property); collectStateManagementTypeImport(StateManagementTypes.PROP_DECORATED); - const right = arkts.factory.createETSNewClassInstanceExpression( - arkts.factory.createTypeReference( - arkts.factory.createTypeReferencePart( - arkts.factory.createIdentifier(StateManagementTypes.PROP_DECORATED), - arkts.factory.createTSTypeParameterInstantiation( - this.property.typeAnnotation ? [this.property.typeAnnotation] : [] - ) - ) - ), - args - ); const assign: arkts.AssignmentExpression = arkts.factory.createAssignmentExpression( generateThisBacking(newName), arkts.Es2pandaTokenType.TOKEN_TYPE_PUNCTUATOR_SUBSTITUTION, - right + factory.generateStateMgmtFactoryCall( + StateManagementTypes.MAKE_PROP, + this.property.typeAnnotation, + args, + true + ) ); return arkts.factory.createExpressionStatement(assign); } generateUpdateStruct(mutableThis: arkts.Expression, originalName: string): arkts.AstNode { - const binaryItem = arkts.factory.createBinaryExpression( - factory.createBlockStatementForOptionalExpression( - arkts.factory.createIdentifier('initializers'), - originalName - ), - arkts.factory.createUndefinedLiteral(), - arkts.Es2pandaTokenType.TOKEN_TYPE_PUNCTUATOR_NOT_STRICT_EQUAL - ); const member: arkts.MemberExpression = arkts.factory.createMemberExpression( arkts.factory.createTSNonNullExpression(mutableThis), - arkts.factory.createIdentifier('update'), + arkts.factory.createIdentifier(StateManagementTypes.UPDATE), arkts.Es2pandaMemberExpressionKind.MEMBER_EXPRESSION_KIND_PROPERTY_ACCESS, false, false ); - return arkts.factory.createIfStatement( - binaryItem, - arkts.factory.createBlock([ - arkts.factory.createExpressionStatement( - arkts.factory.createCallExpression(member, undefined, [ - arkts.factory.createTSAsExpression( - factory.createNonNullOrOptionalMemberExpression('initializers', originalName, false, true), - this.property.typeAnnotation ? this.property.typeAnnotation.clone() : undefined, - false - ), - ]) + return factory.createIfInUpdateStruct(originalName, member, [ + arkts.factory.createTSAsExpression( + factory.createNonNullOrOptionalMemberExpression( + CustomComponentNames.COMPONENT_INITIALIZERS_NAME, + originalName, + false, + true ), - ]) - ); + this.property.typeAnnotation ? this.property.typeAnnotation.clone() : undefined, + false + ), + ]); } } diff --git a/arkui-plugins/ui-plugins/property-translators/provide.ts b/arkui-plugins/ui-plugins/property-translators/provide.ts index c765413cc..c1141a5ac 100644 --- a/arkui-plugins/ui-plugins/property-translators/provide.ts +++ b/arkui-plugins/ui-plugins/property-translators/provide.ts @@ -16,7 +16,8 @@ import * as arkts from '@koalaui/libarkts'; import { backingField, expectName } from '../../common/arkts-utils'; -import { DecoratorNames, StateManagementTypes } from '../../common/predefines'; +import { DecoratorNames, GetSetTypes, StateManagementTypes } from '../../common/predefines'; +import { CustomComponentNames } from '../utils'; import { createGetter, generateToRecord, @@ -57,9 +58,9 @@ export class ProvideTranslator extends PropertyTranslator implements Initializer arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_PRIVATE ); const thisValue: arkts.Expression = generateThisBacking(newName, false, true); - const thisGet: arkts.CallExpression = generateGetOrSetCall(thisValue, 'get'); + const thisGet: arkts.CallExpression = generateGetOrSetCall(thisValue, GetSetTypes.GET); const thisSet: arkts.ExpressionStatement = arkts.factory.createExpressionStatement( - generateGetOrSetCall(thisValue, 'set') + generateGetOrSetCall(thisValue, GetSetTypes.SET) ); const getter: arkts.MethodDefinition = this.translateGetter( originalName, @@ -95,10 +96,29 @@ export class ProvideTranslator extends PropertyTranslator implements Initializer const options: undefined | ProvideOptions = getValueInProvideAnnotation(this.property); const alias: string = options?.alias ?? originalName; const allowOverride: boolean = options?.allowOverride ?? false; + const args: arkts.Expression[] = [ + arkts.factory.create1StringLiteral(originalName), + arkts.factory.create1StringLiteral(alias), + arkts.factory.createBinaryExpression( + factory.createBlockStatementForOptionalExpression( + arkts.factory.createIdentifier(CustomComponentNames.COMPONENT_INITIALIZERS_NAME), + originalName + ), + this.property.value ?? arkts.factory.createUndefinedLiteral(), + arkts.Es2pandaTokenType.TOKEN_TYPE_PUNCTUATOR_NULLISH_COALESCING + ), + arkts.factory.createBooleanLiteral(allowOverride), + ]; + factory.judgeIfAddWatchFunc(args, this.property); const assign: arkts.AssignmentExpression = arkts.factory.createAssignmentExpression( generateThisBacking(newName), arkts.Es2pandaTokenType.TOKEN_TYPE_PUNCTUATOR_SUBSTITUTION, - factory.generateAddProvideVarCall(originalName, this.property, alias, allowOverride) + factory.generateStateMgmtFactoryCall( + StateManagementTypes.MAKE_PROVIDE, + this.property.typeAnnotation, + args, + true + ) ); return arkts.factory.createExpressionStatement(assign); } diff --git a/arkui-plugins/ui-plugins/property-translators/state.ts b/arkui-plugins/ui-plugins/property-translators/state.ts index 731b0cd8c..704a56f71 100644 --- a/arkui-plugins/ui-plugins/property-translators/state.ts +++ b/arkui-plugins/ui-plugins/property-translators/state.ts @@ -16,7 +16,7 @@ import * as arkts from '@koalaui/libarkts'; import { backingField, expectName } from '../../common/arkts-utils'; -import { DecoratorNames, StateManagementTypes } from '../../common/predefines'; +import { DecoratorNames, GetSetTypes, StateManagementTypes } from '../../common/predefines'; import { CustomComponentNames } from '../utils'; import { generateToRecord, @@ -57,9 +57,9 @@ export class StateTranslator extends PropertyTranslator implements InitializerCo arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_PRIVATE ); const thisValue: arkts.Expression = generateThisBacking(newName, false, true); - const thisGet: arkts.CallExpression = generateGetOrSetCall(thisValue, 'get'); + const thisGet: arkts.CallExpression = generateGetOrSetCall(thisValue, GetSetTypes.GET); const thisSet: arkts.ExpressionStatement = arkts.factory.createExpressionStatement( - generateGetOrSetCall(thisValue, 'set') + generateGetOrSetCall(thisValue, GetSetTypes.SET) ); const getter: arkts.MethodDefinition = this.translateGetter( originalName, @@ -103,21 +103,15 @@ export class StateTranslator extends PropertyTranslator implements InitializerCo const args: arkts.Expression[] = [arkts.factory.create1StringLiteral(originalName), binaryItem]; factory.judgeIfAddWatchFunc(args, this.property); collectStateManagementTypeImport(StateManagementTypes.STATE_DECORATED); - const right = arkts.factory.createETSNewClassInstanceExpression( - arkts.factory.createTypeReference( - arkts.factory.createTypeReferencePart( - arkts.factory.createIdentifier(StateManagementTypes.STATE_DECORATED), - arkts.factory.createTSTypeParameterInstantiation( - this.property.typeAnnotation ? [this.property.typeAnnotation] : [] - ) - ) - ), - args - ); const assign: arkts.AssignmentExpression = arkts.factory.createAssignmentExpression( generateThisBacking(newName), arkts.Es2pandaTokenType.TOKEN_TYPE_PUNCTUATOR_SUBSTITUTION, - right + factory.generateStateMgmtFactoryCall( + StateManagementTypes.MAKE_STATE, + this.property.typeAnnotation, + args, + true + ) ); return arkts.factory.createExpressionStatement(assign); } diff --git a/arkui-plugins/ui-plugins/property-translators/storageProp.ts b/arkui-plugins/ui-plugins/property-translators/storageProp.ts index 2f33c03ed..ecec9362b 100644 --- a/arkui-plugins/ui-plugins/property-translators/storageProp.ts +++ b/arkui-plugins/ui-plugins/property-translators/storageProp.ts @@ -16,7 +16,7 @@ import * as arkts from '@koalaui/libarkts'; import { backingField, expectName } from '../../common/arkts-utils'; -import { DecoratorNames, StateManagementTypes } from '../../common/predefines'; +import { DecoratorNames, GetSetTypes, StateManagementTypes } from '../../common/predefines'; import { InterfacePropertyTranslator, InterfacePropertyTypes, PropertyTranslator } from './base'; import { GetterSetter, InitializerConstructor } from './types'; import { @@ -91,28 +91,16 @@ export class StoragePropTranslator extends PropertyTranslator implements Initial ]; factory.judgeIfAddWatchFunc(args, this.property); collectStateManagementTypeImport(StateManagementTypes.STORAGE_PROP_DECORATED); - const newClass = arkts.factory.createETSNewClassInstanceExpression( - arkts.factory.createTypeReference( - arkts.factory.createTypeReferencePart( - arkts.factory.createIdentifier(StateManagementTypes.STORAGE_PROP_DECORATED), - arkts.factory.createTSTypeParameterInstantiation( - this.property.typeAnnotation ? [this.property.typeAnnotation] : [] - ) - ) - ), - args - ); return arkts.factory.createAssignmentExpression( - arkts.factory.createMemberExpression( - arkts.factory.createThisExpression(), - arkts.factory.createIdentifier(newName), - arkts.Es2pandaMemberExpressionKind.MEMBER_EXPRESSION_KIND_PROPERTY_ACCESS, - false, - false - ), + generateThisBacking(newName), arkts.Es2pandaTokenType.TOKEN_TYPE_PUNCTUATOR_SUBSTITUTION, - newClass + factory.generateStateMgmtFactoryCall( + StateManagementTypes.MAKE_STORAGE_PROP, + this.property.typeAnnotation, + args, + true + ) ); } @@ -124,9 +112,9 @@ export class StoragePropTranslator extends PropertyTranslator implements Initial arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_PRIVATE ); const thisValue: arkts.Expression = generateThisBacking(newName, false, true); - const thisGet: arkts.CallExpression = generateGetOrSetCall(thisValue, 'get'); + const thisGet: arkts.CallExpression = generateGetOrSetCall(thisValue, GetSetTypes.GET); const thisSet: arkts.ExpressionStatement = arkts.factory.createExpressionStatement( - generateGetOrSetCall(thisValue, 'set') + generateGetOrSetCall(thisValue, GetSetTypes.SET) ); const getter: arkts.MethodDefinition = this.translateGetter( originalName, diff --git a/arkui-plugins/ui-plugins/property-translators/storagelink.ts b/arkui-plugins/ui-plugins/property-translators/storagelink.ts index 68f3f7865..8e3256a90 100644 --- a/arkui-plugins/ui-plugins/property-translators/storagelink.ts +++ b/arkui-plugins/ui-plugins/property-translators/storagelink.ts @@ -16,7 +16,7 @@ import * as arkts from '@koalaui/libarkts'; import { backingField, expectName } from '../../common/arkts-utils'; -import { DecoratorNames, StateManagementTypes } from '../../common/predefines'; +import { DecoratorNames, GetSetTypes, StateManagementTypes } from '../../common/predefines'; import { InterfacePropertyTranslator, InterfacePropertyTypes, PropertyTranslator } from './base'; import { GetterSetter, InitializerConstructor } from './types'; import { @@ -91,28 +91,15 @@ export class StorageLinkTranslator extends PropertyTranslator implements Initial ]; factory.judgeIfAddWatchFunc(args, this.property); collectStateManagementTypeImport(StateManagementTypes.STORAGE_LINK_DECORATED); - const newClass = arkts.factory.createETSNewClassInstanceExpression( - arkts.factory.createTypeReference( - arkts.factory.createTypeReferencePart( - arkts.factory.createIdentifier(StateManagementTypes.STORAGE_LINK_DECORATED), - arkts.factory.createTSTypeParameterInstantiation( - this.property.typeAnnotation ? [this.property.typeAnnotation] : [] - ) - ) - ), - args - ); - return arkts.factory.createAssignmentExpression( - arkts.factory.createMemberExpression( - arkts.factory.createThisExpression(), - arkts.factory.createIdentifier(newName), - arkts.Es2pandaMemberExpressionKind.MEMBER_EXPRESSION_KIND_PROPERTY_ACCESS, - false, - false - ), + generateThisBacking(newName), arkts.Es2pandaTokenType.TOKEN_TYPE_PUNCTUATOR_SUBSTITUTION, - newClass + factory.generateStateMgmtFactoryCall( + StateManagementTypes.MAKE_STORAGE_LINK, + this.property.typeAnnotation, + args, + true + ) ); } @@ -124,9 +111,9 @@ export class StorageLinkTranslator extends PropertyTranslator implements Initial arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_PRIVATE ); const thisValue: arkts.Expression = generateThisBacking(newName, false, true); - const thisGet: arkts.CallExpression = generateGetOrSetCall(thisValue, 'get'); + const thisGet: arkts.CallExpression = generateGetOrSetCall(thisValue, GetSetTypes.GET); const thisSet: arkts.ExpressionStatement = arkts.factory.createExpressionStatement( - generateGetOrSetCall(thisValue, 'set') + generateGetOrSetCall(thisValue, GetSetTypes.SET) ); const getter: arkts.MethodDefinition = this.translateGetter( originalName, diff --git a/arkui-plugins/ui-plugins/property-translators/utils.ts b/arkui-plugins/ui-plugins/property-translators/utils.ts index c7421e7c0..2a750395a 100644 --- a/arkui-plugins/ui-plugins/property-translators/utils.ts +++ b/arkui-plugins/ui-plugins/property-translators/utils.ts @@ -23,6 +23,7 @@ import { DecoratorNames, DECORATOR_TYPE_MAP, StateManagementTypes, + GetSetTypes } from '../../common/predefines'; import { addMemoAnnotation, findCanAddMemoFromParamExpression, findCanAddMemoFromTypeAnnotation } from '../utils'; @@ -359,7 +360,7 @@ function getDifferentAnnoTypeValue(value: arkts.Expression): string | boolean { return value.dumpSrc(); } -export function generateGetOrSetCall(beforCall: arkts.AstNode, type: string) { +export function generateGetOrSetCall(beforCall: arkts.AstNode, type: GetSetTypes) { return arkts.factory.createCallExpression( arkts.factory.createMemberExpression( beforCall, diff --git a/arkui-plugins/ui-plugins/struct-translators/factory.ts b/arkui-plugins/ui-plugins/struct-translators/factory.ts index f3f39c246..031e865b9 100644 --- a/arkui-plugins/ui-plugins/struct-translators/factory.ts +++ b/arkui-plugins/ui-plugins/struct-translators/factory.ts @@ -550,6 +550,13 @@ export class factory { ) ) ), + arkts.TSClassImplements.createTSClassImplements( + arkts.factory.createTypeReference( + arkts.factory.createTypeReferencePart( + arkts.factory.createIdentifier(StateManagementTypes.SUBSCRIBED_WATCHES) + ) + ) + ), ], undefined, node.super, @@ -561,49 +568,14 @@ export class factory { return updateClassDef; } - static createPermissibleAddRefDepthInObservedClass(): arkts.ClassProperty { - return arkts.factory.createClassProperty( - arkts.factory.createIdentifier('_permissibleAddRefDepth'), - arkts.factory.createNumericLiteral(0), - arkts.factory.createTypeReference( - arkts.factory.createTypeReferencePart(arkts.factory.createIdentifier(StateManagementTypes.INT_32)) - ), - arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_PUBLIC, - false - ); - } - - static createMutableStateMetaInObservedClass(): arkts.ClassProperty { - return arkts.factory.createClassProperty( - arkts.factory.createIdentifier('__meta'), - arkts.factory.createETSNewClassInstanceExpression( - arkts.factory.createTypeReference( - arkts.factory.createTypeReferencePart( - arkts.factory.createIdentifier(StateManagementTypes.MUTABLE_STATE_META) - ) - ), - [arkts.factory.createStringLiteral('@Observe properties (no @Track)')] - ), - arkts.factory.createTypeReference( - arkts.factory.createTypeReferencePart( - arkts.factory.createIdentifier(StateManagementTypes.MUTABLE_STATE_META) - ) - ), - arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_PRIVATE, - false - ); - } - static observedTrackPropertyMembers( classHasTrack: boolean, definition: arkts.ClassDefinition, isObserved: boolean ): arkts.AstNode[] { const watchMembers: arkts.AstNode[] = propertyFactory.createWatchMembers(); - const permissibleAddRefDepth: arkts.ClassProperty = factory.createPermissibleAddRefDepthInObservedClass(); - collectStateManagementTypeImport(StateManagementTypes.INT_32); - const meta: arkts.ClassProperty = factory.createMutableStateMetaInObservedClass(); - collectStateManagementTypeImport(StateManagementTypes.MUTABLE_STATE_META); + const v1RenderIdMembers: arkts.AstNode[] = propertyFactory.createV1RenderIdMembers(); + const conditionalAddRef: arkts.MethodDefinition = propertyFactory.conditionalAddRef(); const getters: arkts.MethodDefinition[] = getGettersFromClassDecl(definition); const classScopeInfo: ClassScopeInfo = { isObserved: isObserved, @@ -623,8 +595,8 @@ export class factory { ) ); return [ - ...watchMembers, - ...(classHasTrack ? [permissibleAddRefDepth] : [permissibleAddRefDepth, meta]), + ...[...watchMembers, ...v1RenderIdMembers, conditionalAddRef], + ...(classHasTrack ? [] : [propertyFactory.createMetaInObservedClass()]), ...collect(...propertyMembers), ...nonClassPropertyOrGetter, ...classScopeInfo.getters, -- Gitee