From 151b07c25c516d39aebbc87518b4d7e221f9b6cb Mon Sep 17 00:00:00 2001 From: Cuecuexiaoyu Date: Wed, 9 Jul 2025 10:44:58 +0800 Subject: [PATCH] develop v2 Signed-off-by: Cuecuexiaoyu Change-Id: Iead816c2c7c50550915f7991b0d198a5b8129339 --- .../monitor-in-observedv2-class.test.ts | 8 ++--- .../monitor/monitor-in-struct.test.ts | 4 +-- .../decorators/monitor/monitor-params.test.ts | 14 ++++---- .../ui-plugins/property-translators/base.ts | 14 ++++++++ .../property-translators/computed.ts | 1 - .../property-translators/factory.ts | 34 +++++++++++-------- .../ui-plugins/property-translators/index.ts | 20 +++++------ .../property-translators/observedTrack.ts | 11 +++--- .../property-translators/observedV2Trace.ts | 11 +++--- .../ui-plugins/property-translators/once.ts | 12 ++----- .../ui-plugins/property-translators/param.ts | 12 ++----- .../ui-plugins/property-translators/types.ts | 7 +--- .../ui-plugins/struct-translators/utils.ts | 4 +++ arkui-plugins/ui-plugins/ui-factory.ts | 4 +-- 14 files changed, 74 insertions(+), 82 deletions(-) diff --git a/arkui-plugins/test/ut/ui-plugins/decorators/monitor/monitor-in-observedv2-class.test.ts b/arkui-plugins/test/ut/ui-plugins/decorators/monitor/monitor-in-observedv2-class.test.ts index c77090d28..35a2ba44f 100644 --- a/arkui-plugins/test/ut/ui-plugins/decorators/monitor/monitor-in-observedv2-class.test.ts +++ b/arkui-plugins/test/ut/ui-plugins/decorators/monitor/monitor-in-observedv2-class.test.ts @@ -171,7 +171,7 @@ function main() {} public constructor() { this.__monitor_onNameChange = STATE_MGMT_FACTORY.makeMonitor([{ path: "name", - valueCallBack: ((): NullishType => { + valueCallback: ((): NullishType => { return this.name; }), }], ((_m: IMonitor) => { @@ -179,7 +179,7 @@ function main() {} })); this.__monitor_onAgeChange = STATE_MGMT_FACTORY.makeMonitor([{ path: "age", - valueCallBack: ((): NullishType => { + valueCallback: ((): NullishType => { return this.age; }), }], ((_m: IMonitor) => { @@ -187,12 +187,12 @@ function main() {} })); this.__monitor_onChange = STATE_MGMT_FACTORY.makeMonitor([{ path: "region", - valueCallBack: ((): NullishType => { + valueCallback: ((): NullishType => { return this.region; }), }, { path: "job", - valueCallBack: ((): NullishType => { + valueCallback: ((): NullishType => { return this.job; }), }], ((_m: IMonitor) => { diff --git a/arkui-plugins/test/ut/ui-plugins/decorators/monitor/monitor-in-struct.test.ts b/arkui-plugins/test/ut/ui-plugins/decorators/monitor/monitor-in-struct.test.ts index e32d12c5e..df17928a9 100644 --- a/arkui-plugins/test/ut/ui-plugins/decorators/monitor/monitor-in-struct.test.ts +++ b/arkui-plugins/test/ut/ui-plugins/decorators/monitor/monitor-in-struct.test.ts @@ -65,12 +65,12 @@ function main() {} this.__backing_age = STATE_MGMT_FACTORY.makeLocal(this, "age", 24); this.__monitor_onStrChange = STATE_MGMT_FACTORY.makeMonitor([{ path: "message", - valueCallBack: ((): NullishType => { + valueCallback: ((): NullishType => { return this.message; }), }, { path: "name", - valueCallBack: ((): NullishType => { + valueCallback: ((): NullishType => { return this.name; }), }], ((_m: IMonitor) => { diff --git a/arkui-plugins/test/ut/ui-plugins/decorators/monitor/monitor-params.test.ts b/arkui-plugins/test/ut/ui-plugins/decorators/monitor/monitor-params.test.ts index c07566557..5a078ffe8 100644 --- a/arkui-plugins/test/ut/ui-plugins/decorators/monitor/monitor-params.test.ts +++ b/arkui-plugins/test/ut/ui-plugins/decorators/monitor/monitor-params.test.ts @@ -209,7 +209,7 @@ class GGG { public constructor() { this.__monitor_onNameChange = STATE_MGMT_FACTORY.makeMonitor([{ path: "name", - valueCallBack: ((): NullishType => { + valueCallback: ((): NullishType => { return this.name; }), }], ((_m: IMonitor) => { @@ -217,13 +217,13 @@ class GGG { })); this.__monitor_onAgeChange = STATE_MGMT_FACTORY.makeMonitor([{ path: "strArr.0", - valueCallBack: ((): NullishType => { + valueCallback: ((): NullishType => { return ({let gensym___127309370 = this.strArr; (((gensym___127309370) == (null)) ? undefined : gensym___127309370.$_get(0))}); }), }, { path: "age", - valueCallBack: ((): NullishType => { + valueCallback: ((): NullishType => { return this.age; }), }], ((_m: IMonitor) => { @@ -231,7 +231,7 @@ class GGG { })); this.__monitor_onJobChange = STATE_MGMT_FACTORY.makeMonitor([{ path: "job.aaa.bbb.ccc.1.dd.0", - valueCallBack: ((): NullishType => { + valueCallback: ((): NullishType => { return ({let gensym___1661894 = ({let gensym___87426618 = ({let gensym___223024592 = ({let gensym___215921992 = ({let gensym___22088104 = ({let gensym___245550938 = this.job; (((gensym___245550938) == (null)) ? undefined : gensym___245550938.aaa)}); (((gensym___22088104) == (null)) ? undefined : gensym___22088104.bbb)}); @@ -254,19 +254,19 @@ class GGG { this.__backing_numArr = STATE_MGMT_FACTORY.makeLocal>(this, "numArr", ["1", "3", "5"]); this.__monitor_onPerChange = STATE_MGMT_FACTORY.makeMonitor([{ path: "per.ee.ff", - valueCallBack: ((): NullishType => { + valueCallback: ((): NullishType => { return ({let gensym___164069504 = ({let gensym___239523226 = this.per; (((gensym___239523226) == (null)) ? undefined : gensym___239523226.ee)}); (((gensym___164069504) == (null)) ? undefined : gensym___164069504.ff)}); }), }, { path: "v1", - valueCallBack: ((): NullishType => { + valueCallback: ((): NullishType => { return this.v1; }), }, { path: "numArr.1", - valueCallBack: ((): NullishType => { + valueCallback: ((): NullishType => { return ({let gensym___124152275 = this.numArr; (((gensym___124152275) == (null)) ? undefined : gensym___124152275.$_get(1))}); }), diff --git a/arkui-plugins/ui-plugins/property-translators/base.ts b/arkui-plugins/ui-plugins/property-translators/base.ts index 335c2f773..ba0c0bd41 100644 --- a/arkui-plugins/ui-plugins/property-translators/base.ts +++ b/arkui-plugins/ui-plugins/property-translators/base.ts @@ -17,6 +17,7 @@ import * as arkts from '@koalaui/libarkts'; import { collectStateManagementTypeImport, createGetter, createSetter } from './utils'; import { CustomComponentInfo, ClassInfo } from '../utils'; import { StateManagementTypes } from '../../common/predefines'; +import { ClassScopeInfo } from '../struct-translators/utils'; export interface PropertyTranslatorOptions { property: arkts.ClassProperty; @@ -69,6 +70,19 @@ export abstract class MethodTranslator { abstract translateMember(): arkts.AstNode[]; } +export abstract class ObservedPropertyTranslator { + protected property: arkts.ClassProperty; + protected classScopeInfo: ClassScopeInfo; + + constructor(property: arkts.ClassProperty, classScopeInfo: ClassScopeInfo) { + this.property = property; + this.classScopeInfo = classScopeInfo; + } + + abstract translateMember(): arkts.AstNode[]; + abstract createField(originalName: string, newName: string): arkts.ClassProperty[]; +} + export type InterfacePropertyTypes = arkts.MethodDefinition | arkts.ClassProperty; export interface InterfacePropertyTranslatorOptions { diff --git a/arkui-plugins/ui-plugins/property-translators/computed.ts b/arkui-plugins/ui-plugins/property-translators/computed.ts index 0814c6428..82daf847d 100644 --- a/arkui-plugins/ui-plugins/property-translators/computed.ts +++ b/arkui-plugins/ui-plugins/property-translators/computed.ts @@ -28,7 +28,6 @@ export class ComputedTranslator extends MethodTranslator implements InitializerC translateMember(): arkts.AstNode[] { const originalName: string = expectName(this.method.name); const newName: string = computedField(originalName); - this.cacheTranslatedInitializer(newName); return this.translateWithoutInitializer(newName, originalName); } diff --git a/arkui-plugins/ui-plugins/property-translators/factory.ts b/arkui-plugins/ui-plugins/property-translators/factory.ts index da4c5dcc7..af331b60f 100644 --- a/arkui-plugins/ui-plugins/property-translators/factory.ts +++ b/arkui-plugins/ui-plugins/property-translators/factory.ts @@ -94,26 +94,30 @@ export class factory { arkts.Es2pandaTokenType.TOKEN_TYPE_PUNCTUATOR_EQUAL ); const consequent: arkts.Expression = arkts.factory.createUndefinedLiteral(); - const leftIdent: arkts.Identifier = arkts.factory.createIdentifier(testLeft); - const alternate: arkts.MemberExpression = UIFactory.generateMemberExpression( - leftIdent, - info?.isNumeric ? '$_get' : key - ); return arkts.factory.createExpressionStatement( arkts.factory.createConditionalExpression( test, consequent, - info?.isCall - ? arkts.factory.createCallExpression(alternate, undefined, undefined) - : info?.isNumeric - ? arkts.factory.createCallExpression(alternate, undefined, [ - arkts.factory.createNumericLiteral(Number(key)), - ]) - : alternate + this.generateConditionalAlternate(testLeft, key, info) ) ); } + static generateConditionalAlternate(testLeft: string, key: string, info?: OptionalMemberInfo): arkts.Expression { + const leftIdent: arkts.Identifier = arkts.factory.createIdentifier(testLeft); + const alternate: arkts.MemberExpression = UIFactory.generateMemberExpression( + leftIdent, + info?.isNumeric ? '$_get' : key + ); + return info?.isCall + ? arkts.factory.createCallExpression(alternate, undefined, undefined) + : info?.isNumeric + ? arkts.factory.createCallExpression(alternate, undefined, [ + arkts.factory.createNumericLiteral(Number(key)), + ]) + : alternate; + } + /** * generate an substitution for two optional expression ?., e.g. a?.b?.c. * @@ -799,10 +803,10 @@ export class factory { */ static createMonitorPathsInfoParameter(monitorItem: string): arkts.ObjectExpression { const itemNameSplit: string[] = monitorItem.split('.'); - if (itemNameSplit.length === 0) { - throw new Error('Errow @Monitor parameters.'); + let monitorVariable: arkts.Expression = arkts.factory.createUndefinedLiteral(); + if (itemNameSplit.length > 0) { + monitorVariable = this.generateMonitorVariable(itemNameSplit); } - const monitorVariable: arkts.Expression = this.generateMonitorVariable(itemNameSplit); return arkts.factory.createObjectExpression( arkts.Es2pandaAstNodeType.AST_NODE_TYPE_OBJECT_EXPRESSION, [ diff --git a/arkui-plugins/ui-plugins/property-translators/index.ts b/arkui-plugins/ui-plugins/property-translators/index.ts index a5b5e0df0..4aaf83436 100644 --- a/arkui-plugins/ui-plugins/property-translators/index.ts +++ b/arkui-plugins/ui-plugins/property-translators/index.ts @@ -34,7 +34,7 @@ import { ProvideInterfaceTranslator, ProvideTranslator } from './provide'; import { BuilderParamInterfaceTranslator, BuilderParamTranslator } from './builderParam'; import { PropRefInterfaceTranslator, PropRefTranslator } from './propRef'; import { ObservedTrackTranslator } from './observedTrack'; -import { ClassScopeInfo } from './types'; +import { ClassScopeInfo } from '../struct-translators/utils'; import { LocalInterfaceTranslator, LocalTranslator } from './local'; import { StoragePropRefInterfaceTranslator, StoragePropRefTranslator } from './storagePropRef'; import { LocalStoragePropRefInterfaceTranslator, LocalStoragePropRefTranslator } from './localStoragePropRef'; @@ -140,12 +140,12 @@ export function classifyV2Property( if (hasDecorator(property, DecoratorNames.LOCAL)) { return new LocalTranslator({ property, structInfo }); } - if (hasDecorator(property, DecoratorNames.PARAM) && !hasDecorator(property, DecoratorNames.ONCE)) { - return new ParamTranslator({ property, structInfo }); - } - if (hasDecorator(property, DecoratorNames.PARAM) && hasDecorator(property, DecoratorNames.ONCE)) { + if (hasDecorator(property, DecoratorNames.ONCE)) { return new OnceTranslator({ property, structInfo }); } + if (hasDecorator(property, DecoratorNames.PARAM)) { + return new ParamTranslator({ property, structInfo }); + } if (hasDecorator(property, DecoratorNames.PROVIDER)) { return new ProviderTranslator({ property, structInfo }); } @@ -225,12 +225,12 @@ export function classifyV2PropertyInInterface(property: arkts.AstNode): Interfac if (LocalInterfaceTranslator.canBeTranslated(property)) { return new LocalInterfaceTranslator({ property }); } - if (ParamInterfaceTranslator.canBeTranslated(property)) { - return new ParamInterfaceTranslator({ property }); - } if (OnceInterfaceTranslator.canBeTranslated(property)) { return new OnceInterfaceTranslator({ property }); } + if (ParamInterfaceTranslator.canBeTranslated(property)) { + return new ParamInterfaceTranslator({ property }); + } if (ProviderInterfaceTranslator.canBeTranslated(property)) { return new ProviderInterfaceTranslator({ property }); } @@ -242,7 +242,7 @@ export function classifyV2PropertyInInterface(property: arkts.AstNode): Interfac export type ObservedTranslator = ObservedV2TraceTranslator | ObservedTrackTranslator; -export function classifyObservedTrack( +export function classifyObservedClassProperty( member: arkts.ClassProperty, classScopeInfo: ClassScopeInfo ): ObservedTranslator | undefined { @@ -277,7 +277,7 @@ export function classifyInObservedClass( classScopeInfo: ClassScopeInfo ): ObservedTranslator | MethodTranslator | undefined { if (arkts.isClassProperty(member)) { - return classifyObservedTrack(member, classScopeInfo); + return classifyObservedClassProperty(member, classScopeInfo); } else if (arkts.isMethodDefinition(member)) { return classifyMethod(member, false, classScopeInfo.className); } diff --git a/arkui-plugins/ui-plugins/property-translators/observedTrack.ts b/arkui-plugins/ui-plugins/property-translators/observedTrack.ts index 2c84b2fcb..33b78a028 100644 --- a/arkui-plugins/ui-plugins/property-translators/observedTrack.ts +++ b/arkui-plugins/ui-plugins/property-translators/observedTrack.ts @@ -16,6 +16,7 @@ import * as arkts from '@koalaui/libarkts'; import { annotation, backingField, expectName } from '../../common/arkts-utils'; import { DecoratorNames, StateManagementTypes, ObservedNames } from '../../common/predefines'; +import { ObservedPropertyTranslator } from "./base"; import { collectStateManagementTypeImport, generateThisBacking, @@ -24,20 +25,16 @@ import { removeDecorator, removeImplementProperty, } from './utils'; -import { ClassScopeInfo } from './types'; +import { ClassScopeInfo } from '../struct-translators/utils'; import { factory } from './factory'; -import { hasNullOrUndefinedType } from '../../ui-plugins/utils'; import { factory as uiFactory } from '../ui-factory'; -export class ObservedTrackTranslator { - protected property: arkts.ClassProperty; - protected classScopeInfo: ClassScopeInfo; +export class ObservedTrackTranslator extends ObservedPropertyTranslator { private hasImplement: boolean; private isTracked: boolean; constructor(property: arkts.ClassProperty, classScopeInfo: ClassScopeInfo) { - this.property = property; - this.classScopeInfo = classScopeInfo; + super(property, classScopeInfo); this.hasImplement = expectName(this.property.key).startsWith(ObservedNames.PROPERTY_PREFIX); this.isTracked = hasDecorator(this.property, DecoratorNames.TRACK); } diff --git a/arkui-plugins/ui-plugins/property-translators/observedV2Trace.ts b/arkui-plugins/ui-plugins/property-translators/observedV2Trace.ts index 52635d0fb..e55ea797f 100644 --- a/arkui-plugins/ui-plugins/property-translators/observedV2Trace.ts +++ b/arkui-plugins/ui-plugins/property-translators/observedV2Trace.ts @@ -16,6 +16,7 @@ import * as arkts from '@koalaui/libarkts'; import { annotation, backingField, expectName } from '../../common/arkts-utils'; import { DecoratorNames, ObservedNames, StateManagementTypes } from '../../common/predefines'; +import { ObservedPropertyTranslator } from "./base"; import { collectStateManagementTypeImport, generateThisBacking, @@ -24,22 +25,18 @@ import { removeDecorator, removeImplementProperty, } from './utils'; -import { ClassScopeInfo } from './types'; +import { ClassScopeInfo } from '../struct-translators/utils'; import { factory } from './factory'; import { factory as uiFactory } from '../ui-factory'; -import { hasNullOrUndefinedType } from '../../ui-plugins/utils'; import { ImportCollector } from '../../common/import-collector'; -export class ObservedV2TraceTranslator { - protected property: arkts.ClassProperty; - protected classScopeInfo: ClassScopeInfo; +export class ObservedV2TraceTranslator extends ObservedPropertyTranslator { private hasImplement: boolean; private isTraced: boolean; private isStatic: boolean; constructor(property: arkts.ClassProperty, classScopeInfo: ClassScopeInfo) { - this.property = property; - this.classScopeInfo = classScopeInfo; + super(property, classScopeInfo); this.hasImplement = expectName(this.property.key).startsWith(ObservedNames.PROPERTY_PREFIX); this.isTraced = hasDecorator(this.property, DecoratorNames.TRACE); this.isStatic = arkts.hasModifierFlag(this.property, arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_STATIC); diff --git a/arkui-plugins/ui-plugins/property-translators/once.ts b/arkui-plugins/ui-plugins/property-translators/once.ts index 94c20af2b..13f9868ad 100644 --- a/arkui-plugins/ui-plugins/property-translators/once.ts +++ b/arkui-plugins/ui-plugins/property-translators/once.ts @@ -138,17 +138,9 @@ export class OnceInterfaceTranslator extends I } static canBeTranslated(node: arkts.AstNode): node is InterfacePropertyTypes { - if ( - arkts.isMethodDefinition(node) && - hasDecorator(node, DecoratorNames.ONCE) && - hasDecorator(node, DecoratorNames.PARAM) - ) { + if (arkts.isMethodDefinition(node) && hasDecorator(node, DecoratorNames.ONCE)) { return true; - } else if ( - arkts.isClassProperty(node) && - hasDecorator(node, DecoratorNames.ONCE) && - hasDecorator(node, DecoratorNames.PARAM) - ) { + } else if (arkts.isClassProperty(node) && hasDecorator(node, DecoratorNames.ONCE)) { return true; } return false; diff --git a/arkui-plugins/ui-plugins/property-translators/param.ts b/arkui-plugins/ui-plugins/property-translators/param.ts index c419a3a49..5332703c1 100644 --- a/arkui-plugins/ui-plugins/property-translators/param.ts +++ b/arkui-plugins/ui-plugins/property-translators/param.ts @@ -145,17 +145,9 @@ export class ParamInterfaceTranslator extends } static canBeTranslated(node: arkts.AstNode): node is InterfacePropertyTypes { - if ( - arkts.isMethodDefinition(node) && - hasDecorator(node, DecoratorNames.PARAM) && - !hasDecorator(node, DecoratorNames.ONCE) - ) { + if (arkts.isMethodDefinition(node) && hasDecorator(node, DecoratorNames.PARAM)) { return true; - } else if ( - arkts.isClassProperty(node) && - hasDecorator(node, DecoratorNames.PARAM) && - !hasDecorator(node, DecoratorNames.ONCE) - ) { + } else if (arkts.isClassProperty(node) && hasDecorator(node, DecoratorNames.PARAM)) { return true; } return false; diff --git a/arkui-plugins/ui-plugins/property-translators/types.ts b/arkui-plugins/ui-plugins/property-translators/types.ts index 0c9d75bdb..0b903d0e3 100644 --- a/arkui-plugins/ui-plugins/property-translators/types.ts +++ b/arkui-plugins/ui-plugins/property-translators/types.ts @@ -14,7 +14,6 @@ */ import * as arkts from '@koalaui/libarkts'; -import { ObservedAnnoInfo } from '../struct-translators/utils'; export interface GetterSetter { translateGetter( @@ -32,8 +31,4 @@ export interface GetterSetter { export interface InitializerConstructor { cacheTranslatedInitializer(newName: string, originalName: string): void; translateWithoutInitializer(newName: string, originalName: string): arkts.AstNode[]; -} - -export type ClassScopeInfo = ObservedAnnoInfo & { - getters: arkts.MethodDefinition[]; -}; +} \ No newline at end of file diff --git a/arkui-plugins/ui-plugins/struct-translators/utils.ts b/arkui-plugins/ui-plugins/struct-translators/utils.ts index 9dd793228..f133af551 100644 --- a/arkui-plugins/ui-plugins/struct-translators/utils.ts +++ b/arkui-plugins/ui-plugins/struct-translators/utils.ts @@ -79,6 +79,10 @@ export interface ObservedAnnoInfo { classHasTrack: boolean; } +export type ClassScopeInfo = ObservedAnnoInfo & { + getters: arkts.MethodDefinition[]; +}; + export function getResourceParams(id: number, type: number, params: arkts.Expression[]): ResourceParameter { return { id, type, params }; } diff --git a/arkui-plugins/ui-plugins/ui-factory.ts b/arkui-plugins/ui-plugins/ui-factory.ts index df6cad692..c8c7cfc6f 100644 --- a/arkui-plugins/ui-plugins/ui-factory.ts +++ b/arkui-plugins/ui-plugins/ui-factory.ts @@ -204,9 +204,7 @@ export class factory { arkts.factory.createFunctionSignature( config.typeParams ?? original.typeParams, config.params ?? original.params, - Object.hasOwn(config, 'returnTypeAnnotation') - ? config.returnTypeAnnotation - : original.returnTypeAnnotation, + config.returnTypeAnnotation ?? original.returnTypeAnnotation, config.hasReceiver ?? original.hasReceiver ), config.flags ?? original.flags, -- Gitee