diff --git a/arkui-plugins/common/predefines.ts b/arkui-plugins/common/predefines.ts index 68abfc5bb60b8d99b9b43ccf2be35f7fa7c9890c..642d0219a8693c3b6e6ab4fad069871cdb3595ab 100644 --- a/arkui-plugins/common/predefines.ts +++ b/arkui-plugins/common/predefines.ts @@ -128,7 +128,6 @@ export enum InnerComponentAttributes { export enum DecoratorNames { STATE = 'State', STORAGE_LINK = 'StorageLink', - STORAGE_PROP = 'StorageProp', LINK = 'Link', PROP = 'Prop', PROVIDE = 'Provide', @@ -140,7 +139,6 @@ export enum DecoratorNames { BUILDER_PARAM = 'BuilderParam', BUILDER = 'Builder', CUSTOM_DIALOG = 'CustomDialog', - LOCAL_STORAGE_PROP = 'LocalStorageProp', LOCAL_STORAGE_LINK = 'LocalStorageLink', REUSABLE = 'Reusable', TRACK = 'Track', @@ -183,7 +181,6 @@ export enum StateManagementTypes { STORAGE_PROP_REF_DECORATED = 'IStoragePropRefDecoratedVariable', LOCAL_STORAGE_LINK_DECORATED = 'ILocalStorageLinkDecoratedVariable', PROP_DECORATED = 'IPropDecoratedVariable', - SYNCED_PROPERTY = 'SyncedProperty', PROVIDE_DECORATED = 'IProvideDecoratedVariable', CONSUME_DECORATED = 'IConsumeDecoratedVariable', OBJECT_LINK_DECORATED = 'IObjectLinkDecoratedVariable', @@ -209,7 +206,6 @@ export enum StateManagementTypes { UPDATE = 'update', MAKE_STATE = 'makeState', MAKE_LINK = 'makeLink', - MAKE_PROP = 'makeProp', MAKE_PROP_REF = 'makePropRef', MAKE_LOCAL = 'makeLocal', MAKE_STATIC_LOCAL = 'makeStaticLocal', @@ -277,13 +273,10 @@ export const RESOURCE_TYPE: Record = { export const DECORATOR_TYPE_MAP = new Map([ [DecoratorNames.STATE, StateManagementTypes.STATE_DECORATED], [DecoratorNames.LINK, StateManagementTypes.LINK_SOURCE_TYPE], - [DecoratorNames.PROP, StateManagementTypes.PROP_DECORATED], [DecoratorNames.PROP_REF, StateManagementTypes.PROP_REF_DECORATED], [DecoratorNames.STORAGE_LINK, StateManagementTypes.STORAGE_LINK_DECORATED], - [DecoratorNames.STORAGE_PROP, StateManagementTypes.STORAGE_PROP_REF_DECORATED], [DecoratorNames.STORAGE_PROP_REF, StateManagementTypes.STORAGE_PROP_REF_DECORATED], [DecoratorNames.LOCAL_STORAGE_PROP_REF, StateManagementTypes.LOCAL_STORAGE_PROP_REF_DECORATED], - [DecoratorNames.LOCAL_STORAGE_PROP, StateManagementTypes.SYNCED_PROPERTY], [DecoratorNames.LOCAL_STORAGE_LINK, StateManagementTypes.LOCAL_STORAGE_LINK_DECORATED], [DecoratorNames.OBJECT_LINK, StateManagementTypes.OBJECT_LINK_DECORATED], [DecoratorNames.PROVIDE, StateManagementTypes.PROVIDE_DECORATED], @@ -301,11 +294,9 @@ export const INTERMEDIATE_IMPORT_SOURCE: Map = new Map = new Map = new Map = new Map([ [Dollars.TRANSFORM_DOLLAR_RESOURCE, 'arkui.component.resources'], [Dollars.TRANSFORM_DOLLAR_RAWFILE, 'arkui.component.resources'], - [StateManagementTypes.SYNCED_PROPERTY, 'arkui.stateManagement.runtime'], [StateManagementTypes.STORAGE_LINK_STATE, 'arkui.stateManagement.runtime'], [StateManagementTypes.OBSERVABLE_PROXY, 'arkui.stateManagement.runtime'], [StateManagementTypes.PROP_STATE, 'arkui.stateManagement.runtime'], diff --git a/arkui-plugins/test/demo/mock/decorators/reusable/reusable-basic.ets b/arkui-plugins/test/demo/mock/decorators/reusable/reusable-basic.ets index 510a2de33dfcc4c120a3ec95da7e49e1381ed221..95aba23334957b3ff81fcc4630dbb2422ab29e8e 100644 --- a/arkui-plugins/test/demo/mock/decorators/reusable/reusable-basic.ets +++ b/arkui-plugins/test/demo/mock/decorators/reusable/reusable-basic.ets @@ -19,7 +19,7 @@ import { State, PropRef } from "@ohos.arkui.stateManagement" @Component struct MyStateSample { build() { - Child({ num: 5 } ) + Child({ num1: 5 } ) } } diff --git a/arkui-plugins/test/demo/mock/decorators/watch/watch-basic.ets b/arkui-plugins/test/demo/mock/decorators/watch/watch-basic.ets index 631524dcff9cbbd3605c4ea302445b506054d934..8fb667a2f32689110cb08d7f8a85cf665ae16c22 100644 --- a/arkui-plugins/test/demo/mock/decorators/watch/watch-basic.ets +++ b/arkui-plugins/test/demo/mock/decorators/watch/watch-basic.ets @@ -14,7 +14,7 @@ */ import { Component, Entry, Column } from "@ohos.arkui.component" -import { State, PropRef, StorageLink, StorageProp, Link, Watch, ObjectLink, Observed, Track, Provide, Consume } from "@ohos.arkui.stateManagement" +import { State, PropRef, StorageLink, StoragePropRef, Link, Watch, ObjectLink, Observed, Track, Provide, Consume } from "@ohos.arkui.stateManagement" @Observed class A { @@ -29,7 +29,7 @@ struct MyStateSample { @PropRef @Watch('propOnChange') propvar: string = 'Hello World'; @Link @Watch('linkOnChange') linkvar: string; @StorageLink('prop1') @Watch('storageLinkOnChange') storagelinkvar: string = 'Hello World'; - @StorageProp('prop2') @Watch('storagePropOnChange') storagepropvar: string = 'Hello World'; + @StoragePropRef('prop2') @Watch('storagePropOnChange') storagepropvar: string = 'Hello World'; @ObjectLink @Watch('objectLinkOnChange') objectlinkvar: A; @Provide @Watch('ProvideOnChange') providevar: string = 'Hello World'; diff --git a/arkui-plugins/test/ut/ui-plugins/decorators/decorator-no-type.test.ts b/arkui-plugins/test/ut/ui-plugins/decorators/decorator-no-type.test.ts index c74183c8ac0e102f6078a795b64ca2aa3658eaf0..5d0255fc674b3e1ff68b8fdb1f97d59d9dc82cd2 100644 --- a/arkui-plugins/test/ut/ui-plugins/decorators/decorator-no-type.test.ts +++ b/arkui-plugins/test/ut/ui-plugins/decorators/decorator-no-type.test.ts @@ -86,6 +86,7 @@ import { Provider as Provider, Consumer as Consumer, Once as Once, Observed as O function main() {} + class Per { public num: number; diff --git a/arkui-plugins/test/ut/ui-plugins/decorators/reusable/reusable-basic.test.ts b/arkui-plugins/test/ut/ui-plugins/decorators/reusable/reusable-basic.test.ts index abcfd4ebe4d24460847e51a19a76e8facbbc8ac8..733f63063fcfc9167f5bfe0013fcf3d04f463871 100644 --- a/arkui-plugins/test/ut/ui-plugins/decorators/reusable/reusable-basic.test.ts +++ b/arkui-plugins/test/ut/ui-plugins/decorators/reusable/reusable-basic.test.ts @@ -63,8 +63,8 @@ function main() {} Child._instantiateImpl(undefined, (() => { return new Child(); }), { - num: 5, - __options_has_num: true, + num1: 5, + __options_has_num1: true, }, "Child", undefined); } diff --git a/arkui-plugins/test/ut/ui-plugins/decorators/watch/watch-basic.test.ts b/arkui-plugins/test/ut/ui-plugins/decorators/watch/watch-basic.test.ts index 0fa0184ca5c12129831ad13e96aa57a3e9edcbaf..84ea8f8769c3bef331612826c5fd2e3d3ddc979a 100644 --- a/arkui-plugins/test/ut/ui-plugins/decorators/watch/watch-basic.test.ts +++ b/arkui-plugins/test/ut/ui-plugins/decorators/watch/watch-basic.test.ts @@ -86,7 +86,7 @@ import { CustomComponent as CustomComponent } from "arkui.component.customCompon import { Component as Component, Entry as Entry, Column as Column } from "@ohos.arkui.component"; -import { State as State, PropRef as PropRef, StorageLink as StorageLink, StorageProp as StorageProp, Link as Link, Watch as Watch, ObjectLink as ObjectLink, Observed as Observed, Track as Track, Provide as Provide, Consume as Consume } from "@ohos.arkui.stateManagement"; +import { State as State, PropRef as PropRef, StorageLink as StorageLink, StoragePropRef as StoragePropRef, Link as Link, Watch as Watch, ObjectLink as ObjectLink, Observed as Observed, Track as Track, Provide as Provide, Consume as Consume } from "@ohos.arkui.stateManagement"; function main() {} diff --git a/arkui-plugins/ui-plugins/property-translators/index.ts b/arkui-plugins/ui-plugins/property-translators/index.ts index 914006824c8ec1c4ffbcb63e1103f23147d53c3b..ff8a89025895f1b719f6321aa71f315984bc1c8e 100644 --- a/arkui-plugins/ui-plugins/property-translators/index.ts +++ b/arkui-plugins/ui-plugins/property-translators/index.ts @@ -19,16 +19,13 @@ import { DecoratorNames } from '../../common/predefines'; import { InterfacePropertyTranslator, MethodTranslator, PropertyTranslator } from './base'; import { hasDecorator } from './utils'; import { StateInterfaceTranslator, StateTranslator } from './state'; -import { PropInterfaceTranslator, PropTranslator } from './prop'; import { StorageLinkInterfaceTranslator, StorageLinkTranslator } from './storagelink'; import { LocalStorageLinkInterfaceTranslator, LocalStorageLinkTranslator } from './localstoragelink'; import { LinkInterfaceTranslator, LinkTranslator } from './link'; import { ObjectLinkInterfaceTranslator, ObjectLinkTranslator } from './objectlink'; -import { LocalStoragePropInterfaceTranslator, LocalStoragePropTranslator } from './localstorageprop'; import { RegularInterfaceTranslator, RegularPropertyTranslator } from './regularProperty'; import { StaticPropertyTranslator } from './staticProperty'; import { CustomComponentInfo } from '../utils'; -import { StoragePropInterfaceTranslator, StoragePropTranslator } from './storageProp'; import { ConsumeInterfaceTranslator, ConsumeTranslator } from './consume'; import { ProvideInterfaceTranslator, ProvideTranslator } from './provide'; import { BuilderParamInterfaceTranslator, BuilderParamTranslator } from './builderParam'; @@ -103,21 +100,12 @@ export function classifyV1Property( if (hasDecorator(property, DecoratorNames.OBJECT_LINK)) { return new ObjectLinkTranslator({ property, structInfo }); } - if (hasDecorator(property, DecoratorNames.LOCAL_STORAGE_PROP)) { - return new LocalStoragePropTranslator({ property, structInfo }); - } if (hasDecorator(property, DecoratorNames.LOCAL_STORAGE_PROP_REF)) { return new LocalStoragePropRefTranslator({ property, structInfo }); } - if (hasDecorator(property, DecoratorNames.STORAGE_PROP)) { - return new StoragePropTranslator({ property, structInfo }); - } if (hasDecorator(property, DecoratorNames.STORAGE_PROP_REF)) { return new StoragePropRefTranslator({ property, structInfo }); } - if (hasDecorator(property, DecoratorNames.PROP)) { - return new PropTranslator({ property, structInfo }); - } if (hasDecorator(property, DecoratorNames.PROP_REF)) { return new PropRefTranslator({ property, structInfo }); } @@ -183,9 +171,6 @@ export function classifyV1PropertyInInterface(property: arkts.AstNode): Interfac if (LinkInterfaceTranslator.canBeTranslated(property)) { return new LinkInterfaceTranslator({ property }); } - if (PropInterfaceTranslator.canBeTranslated(property)) { - return new PropInterfaceTranslator({ property }); - } if (PropRefInterfaceTranslator.canBeTranslated(property)) { return new PropRefInterfaceTranslator({ property }); } @@ -195,9 +180,6 @@ export function classifyV1PropertyInInterface(property: arkts.AstNode): Interfac if (ConsumeInterfaceTranslator.canBeTranslated(property)) { return new ConsumeInterfaceTranslator({ property }); } - if (StoragePropInterfaceTranslator.canBeTranslated(property)) { - return new StoragePropInterfaceTranslator({ property }); - } if (StorageLinkInterfaceTranslator.canBeTranslated(property)) { return new StorageLinkInterfaceTranslator({ property }); } @@ -210,9 +192,6 @@ export function classifyV1PropertyInInterface(property: arkts.AstNode): Interfac if (BuilderParamInterfaceTranslator.canBeTranslated(property)) { return new BuilderParamInterfaceTranslator({ property }); } - if (LocalStoragePropInterfaceTranslator.canBeTranslated(property)) { - return new LocalStoragePropInterfaceTranslator({ property }); - } if (LocalStorageLinkInterfaceTranslator.canBeTranslated(property)) { return new LocalStorageLinkInterfaceTranslator({ property }); } diff --git a/arkui-plugins/ui-plugins/property-translators/localstorageprop.ts b/arkui-plugins/ui-plugins/property-translators/localstorageprop.ts deleted file mode 100644 index 7b5840e62f24d9e7e365605ac92fd2656b23ebfb..0000000000000000000000000000000000000000 --- a/arkui-plugins/ui-plugins/property-translators/localstorageprop.ts +++ /dev/null @@ -1,229 +0,0 @@ -/* - * Copyright (c) 2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import * as arkts from '@koalaui/libarkts'; - -import { backingField, expectName } from '../../common/arkts-utils'; -import { DecoratorNames, StateManagementTypes } from '../../common/predefines'; -import { collectStateManagementTypeImport, generateToRecord, hasDecorator } from './utils'; -import { InterfacePropertyTranslator, InterfacePropertyTypes, PropertyTranslator } from './base'; -import { GetterSetter, InitializerConstructor } from './types'; -import { factory } from './factory'; -import { PropertyCache } from './cache/propertyCache'; - -function getLocalStorageporpValueStr(node: arkts.AstNode): string | undefined { - if (!arkts.isClassProperty(node) || !node.value) return undefined; - - return arkts.isStringLiteral(node.value) ? node.value.str : undefined; -} -function getLocalStorageporpAnnotationValue(anno: arkts.AnnotationUsage): string | undefined { - const isLocalStorageporpAnnotation: boolean = - !!anno.expr && arkts.isIdentifier(anno.expr) && anno.expr.name === DecoratorNames.LOCAL_STORAGE_PROP; - - if (isLocalStorageporpAnnotation && anno.properties.length === 1) { - return getLocalStorageporpValueStr(anno.properties.at(0)!); - } - return undefined; -} - -function getLocalStorageporpValueInAnnotation(node: arkts.ClassProperty): string | undefined { - const annotations: readonly arkts.AnnotationUsage[] = node.annotations; - - for (let i = 0; i < annotations.length; i++) { - const anno: arkts.AnnotationUsage = annotations[i]; - const str: string | undefined = getLocalStorageporpAnnotationValue(anno); - if (!!str) { - return str; - } - } - - return undefined; -} - -export class LocalStoragePropTranslator extends PropertyTranslator implements InitializerConstructor, GetterSetter { - translateMember(): arkts.AstNode[] { - const originalName: string = expectName(this.property.key); - const newName: string = backingField(originalName); - - this.cacheTranslatedInitializer(newName, originalName); - return this.translateWithoutInitializer(newName, originalName); - } - - cacheTranslatedInitializer(newName: string, originalName: string): void { - const initializeStruct: arkts.AstNode = this.generateInitializeStruct(newName, originalName); - PropertyCache.getInstance().collectInitializeStruct(this.structInfo.name, [initializeStruct]); - const updateStruct: arkts.AstNode = this.generateUpdateStruct(newName, originalName); - PropertyCache.getInstance().collectUpdateStruct(this.structInfo.name, [updateStruct]); - if (!!this.structInfo.annotations?.reusable) { - const toRecord = generateToRecord(newName, originalName); - PropertyCache.getInstance().collectToRecord(this.structInfo.name, [toRecord]); - } - } - - translateWithoutInitializer(newName: string, originalName: string): arkts.AstNode[] { - const field = factory.createOptionalClassProperty( - newName, - this.property, - StateManagementTypes.SYNCED_PROPERTY, - arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_PRIVATE - ); - - const member = arkts.factory.createTSNonNullExpression( - arkts.factory.createMemberExpression( - arkts.factory.createThisExpression(), - arkts.factory.createIdentifier(newName), - arkts.Es2pandaMemberExpressionKind.MEMBER_EXPRESSION_KIND_PROPERTY_ACCESS, - false, - false - ) - ); - const thisValue: arkts.MemberExpression = arkts.factory.createMemberExpression( - member, - arkts.factory.createIdentifier('value'), - arkts.Es2pandaMemberExpressionKind.MEMBER_EXPRESSION_KIND_PROPERTY_ACCESS, - false, - false - ); - - const getter: arkts.MethodDefinition = this.translateGetter(originalName, this.propertyType, thisValue); - const setter: arkts.MethodDefinition = this.translateSetter(originalName, this.propertyType, thisValue); - return [field, getter, setter]; - } - - generateInitializeStruct(newName: string, originalName: string): arkts.AstNode { - const localStorageporpValueStr: string | undefined = getLocalStorageporpValueInAnnotation(this.property); - if (!localStorageporpValueStr) { - throw new Error('LocalStorageProp required only one value!!'); - } - const insideMember = arkts.factory.createMemberExpression( - arkts.factory.createThisExpression(), - arkts.factory.createIdentifier('_entry_local_storage_'), - arkts.Es2pandaMemberExpressionKind.MEMBER_EXPRESSION_KIND_PROPERTY_ACCESS, - false, - false - ); - const binaryItem = arkts.factory.createCallExpression( - arkts.factory.createIdentifier(StateManagementTypes.STORAGE_LINK_STATE), - this.propertyType ? [this.propertyType] : [], - [ - insideMember, - arkts.factory.createStringLiteral(localStorageporpValueStr), - this.property.value ?? arkts.factory.createUndefinedLiteral(), - ] - ); - collectStateManagementTypeImport(StateManagementTypes.STORAGE_LINK_STATE); - const call = arkts.factory.createCallExpression( - arkts.factory.createIdentifier(StateManagementTypes.PROP_STATE), - this.propertyType ? [this.propertyType] : [], - [ - arkts.factory.createMemberExpression( - binaryItem, - arkts.factory.createIdentifier('value'), - arkts.Es2pandaMemberExpressionKind.MEMBER_EXPRESSION_KIND_PROPERTY_ACCESS, - false, - false - ), - ] - ); - collectStateManagementTypeImport(StateManagementTypes.PROP_STATE); - return arkts.factory.createAssignmentExpression( - arkts.factory.createMemberExpression( - arkts.factory.createThisExpression(), - arkts.factory.createIdentifier(newName), - arkts.Es2pandaMemberExpressionKind.MEMBER_EXPRESSION_KIND_PROPERTY_ACCESS, - false, - false - ), - arkts.Es2pandaTokenType.TOKEN_TYPE_PUNCTUATOR_SUBSTITUTION, - call - ); - } - - generateUpdateStruct(newName: string, originalName: string): arkts.AstNode { - const localStorageporpValueStr: string | undefined = getLocalStorageporpValueInAnnotation(this.property); - if (!localStorageporpValueStr) { - throw new Error('StorageLink required only one value!!'); - } - const StorageLinkStateValue = factory.createStorageLinkStateValue(this.property, localStorageporpValueStr); - collectStateManagementTypeImport(StateManagementTypes.STORAGE_LINK_STATE); - const test = arkts.factory.createMemberExpression( - arkts.factory.createThisExpression(), - arkts.factory.createIdentifier(newName), - arkts.Es2pandaMemberExpressionKind.MEMBER_EXPRESSION_KIND_PROPERTY_ACCESS, - false, - false - ); - const consequent = arkts.BlockStatement.createBlockStatement([ - arkts.factory.createExpressionStatement( - arkts.factory.createCallExpression( - arkts.factory.createMemberExpression( - arkts.factory.createTSNonNullExpression(test), - arkts.factory.createIdentifier('update'), - arkts.Es2pandaMemberExpressionKind.MEMBER_EXPRESSION_KIND_PROPERTY_ACCESS, - false, - false - ), - undefined, - [StorageLinkStateValue] - ) - ), - ]); - return arkts.factory.createExpressionStatement(arkts.factory.createIfStatement(test, consequent)); - } -} - -export class LocalStoragePropInterfaceTranslator< - T extends InterfacePropertyTypes -> extends InterfacePropertyTranslator { - translateProperty(): T { - if (arkts.isMethodDefinition(this.property)) { - this.modified = true; - return this.updateStateMethodInInterface(this.property) as T; - } else if (arkts.isClassProperty(this.property)) { - this.modified = true; - return this.updateStatePropertyInInterface(this.property) as T; - } - return this.property; - } - - static canBeTranslated(node: arkts.AstNode): node is InterfacePropertyTypes { - if (arkts.isMethodDefinition(node) && hasDecorator(node, DecoratorNames.LOCAL_STORAGE_PROP)) { - return true; - } else if (arkts.isClassProperty(node) && hasDecorator(node, DecoratorNames.LOCAL_STORAGE_PROP)) { - return true; - } - return false; - } - - /** - * Wrap getter's return type and setter's param type (expecting an union type with `T` and `undefined`) - * to `SyncedProperty | undefined`. - * - * @param method expecting getter with `@LocalStorageProp` and a setter with `@LocalStorageProp` in the overloads. - */ - private updateStateMethodInInterface(method: arkts.MethodDefinition): arkts.MethodDefinition { - return factory.wrapStateManagementTypeToMethodInInterface(method, DecoratorNames.LOCAL_STORAGE_PROP); - } - - /** - * Wrap to the type of the property (expecting an union type with `T` and `undefined`) - * to `SyncedProperty | undefined`. - * - * @param property expecting property with `@LocalStorageProp`. - */ - private updateStatePropertyInInterface(property: arkts.ClassProperty): arkts.ClassProperty { - return factory.wrapStateManagementTypeToPropertyInInterface(property, DecoratorNames.LOCAL_STORAGE_PROP); - } -} diff --git a/arkui-plugins/ui-plugins/property-translators/prop.ts b/arkui-plugins/ui-plugins/property-translators/prop.ts deleted file mode 100644 index f4e6fbcb1038f592abc98f3a3f301b488a9451d4..0000000000000000000000000000000000000000 --- a/arkui-plugins/ui-plugins/property-translators/prop.ts +++ /dev/null @@ -1,168 +0,0 @@ -/* - * Copyright (c) 2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import * as arkts from '@koalaui/libarkts'; - -import { backingField, expectName } from '../../common/arkts-utils'; -import { DecoratorNames, GetSetTypes, StateManagementTypes } from '../../common/predefines'; -import { CustomComponentNames } from '../utils'; -import { - collectStateManagementTypeImport, - createGetter, - createSetter2, - generateGetOrSetCall, - generateThisBacking, - generateToRecord, - hasDecorator, -} from './utils'; -import { InterfacePropertyTranslator, InterfacePropertyTypes, PropertyTranslator } from './base'; -import { GetterSetter, InitializerConstructor } from './types'; -import { factory } from './factory'; -import { PropertyCache } from './cache/propertyCache'; - -export class PropTranslator extends PropertyTranslator implements InitializerConstructor, GetterSetter { - translateMember(): arkts.AstNode[] { - const originalName: string = expectName(this.property.key); - const newName: string = backingField(originalName); - - this.cacheTranslatedInitializer(newName, originalName); - return this.translateWithoutInitializer(newName, originalName); - } - - cacheTranslatedInitializer(newName: string, originalName: string): void { - const mutableThis: arkts.Expression = generateThisBacking(newName); - const initializeStruct: arkts.AstNode = this.generateInitializeStruct(newName, originalName); - PropertyCache.getInstance().collectInitializeStruct(this.structInfo.name, [initializeStruct]); - const updateStruct: arkts.AstNode = this.generateUpdateStruct(mutableThis, originalName); - PropertyCache.getInstance().collectUpdateStruct(this.structInfo.name, [updateStruct]); - if (!!this.structInfo.annotations?.reusable) { - const toRecord = generateToRecord(newName, originalName); - PropertyCache.getInstance().collectToRecord(this.structInfo.name, [toRecord]); - } - } - - translateWithoutInitializer(newName: string, originalName: string): arkts.AstNode[] { - const field: arkts.ClassProperty = factory.createOptionalClassProperty( - newName, - this.property, - StateManagementTypes.PROP_DECORATED, - arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_PRIVATE - ); - const thisValue: arkts.Expression = generateThisBacking(newName, false, true); - const thisGet: arkts.CallExpression = generateGetOrSetCall(thisValue, GetSetTypes.GET); - const thisSet: arkts.ExpressionStatement = arkts.factory.createExpressionStatement( - generateGetOrSetCall(thisValue, GetSetTypes.SET) - ); - const getter: arkts.MethodDefinition = this.translateGetter(originalName, this.propertyType, thisGet); - const setter: arkts.MethodDefinition = this.translateSetter(originalName, this.propertyType, thisSet); - - return [field, getter, setter]; - } - - translateGetter( - originalName: string, - typeAnnotation: arkts.TypeNode | undefined, - returnValue: arkts.Expression - ): arkts.MethodDefinition { - return createGetter(originalName, typeAnnotation, returnValue); - } - - translateSetter( - originalName: string, - typeAnnotation: arkts.TypeNode | undefined, - statement: arkts.AstNode - ): arkts.MethodDefinition { - return createSetter2(originalName, typeAnnotation, statement); - } - - generateInitializeStruct(newName: string, originalName: string): arkts.AstNode { - const args: arkts.Expression[] = [ - arkts.factory.create1StringLiteral(originalName), - factory.generateInitializeValue(this.property, this.propertyType, originalName), - ]; - factory.judgeIfAddWatchFunc(args, this.property); - collectStateManagementTypeImport(StateManagementTypes.PROP_DECORATED); - const assign: arkts.AssignmentExpression = arkts.factory.createAssignmentExpression( - generateThisBacking(newName), - arkts.Es2pandaTokenType.TOKEN_TYPE_PUNCTUATOR_SUBSTITUTION, - factory.generateStateMgmtFactoryCall(StateManagementTypes.MAKE_PROP, this.propertyType, args, true) - ); - return arkts.factory.createExpressionStatement(assign); - } - - generateUpdateStruct(mutableThis: arkts.Expression, originalName: string): arkts.AstNode { - const member: arkts.MemberExpression = arkts.factory.createMemberExpression( - arkts.factory.createTSNonNullExpression(mutableThis), - arkts.factory.createIdentifier(StateManagementTypes.UPDATE), - arkts.Es2pandaMemberExpressionKind.MEMBER_EXPRESSION_KIND_PROPERTY_ACCESS, - false, - false - ); - return factory.createIfInUpdateStruct(originalName, member, [ - arkts.factory.createTSAsExpression( - factory.createNonNullOrOptionalMemberExpression( - CustomComponentNames.COMPONENT_INITIALIZERS_NAME, - originalName, - false, - true - ), - this.propertyType, - false - ), - ]); - } -} - -export class PropInterfaceTranslator extends InterfacePropertyTranslator { - translateProperty(): T { - if (arkts.isMethodDefinition(this.property)) { - this.modified = true; - return this.updateStateMethodInInterface(this.property) as T; - } else if (arkts.isClassProperty(this.property)) { - this.modified = true; - return this.updateStatePropertyInInterface(this.property) as T; - } - return this.property; - } - - static canBeTranslated(node: arkts.AstNode): node is InterfacePropertyTypes { - if (arkts.isMethodDefinition(node) && hasDecorator(node, DecoratorNames.PROP)) { - return true; - } else if (arkts.isClassProperty(node) && hasDecorator(node, DecoratorNames.PROP)) { - return true; - } - return false; - } - - /** - * Wrap getter's return type and setter's param type (expecting an union type with `T` and `undefined`) - * to `PropDecoratedVariable | undefined`. - * - * @param method expecting getter with `@Prop` and a setter with `@Prop` in the overloads. - */ - private updateStateMethodInInterface(method: arkts.MethodDefinition): arkts.MethodDefinition { - return factory.wrapStateManagementTypeToMethodInInterface(method, DecoratorNames.PROP); - } - - /** - * Wrap to the type of the property (expecting an union type with `T` and `undefined`) - * to `PropDecoratedVariable | undefined`. - * - * @param property expecting property with `@Prop`. - */ - private updateStatePropertyInInterface(property: arkts.ClassProperty): arkts.ClassProperty { - return factory.wrapStateManagementTypeToPropertyInInterface(property, DecoratorNames.PROP); - } -} diff --git a/arkui-plugins/ui-plugins/property-translators/storageProp.ts b/arkui-plugins/ui-plugins/property-translators/storageProp.ts deleted file mode 100644 index e4b0978d2f40629895d1f76d9d88aae656a74086..0000000000000000000000000000000000000000 --- a/arkui-plugins/ui-plugins/property-translators/storageProp.ts +++ /dev/null @@ -1,156 +0,0 @@ -/* - * Copyright (c) 2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import * as arkts from '@koalaui/libarkts'; - -import { backingField, expectName } from '../../common/arkts-utils'; -import { DecoratorNames, GetSetTypes, StateManagementTypes } from '../../common/predefines'; -import { InterfacePropertyTranslator, InterfacePropertyTypes, PropertyTranslator } from './base'; -import { GetterSetter, InitializerConstructor } from './types'; -import { - generateToRecord, - createGetter, - createSetter2, - generateThisBacking, - generateGetOrSetCall, - collectStateManagementTypeImport, - hasDecorator, - getValueInAnnotation, -} from './utils'; -import { factory } from './factory'; -import { PropertyCache } from './cache/propertyCache'; - -export class StoragePropTranslator extends PropertyTranslator implements InitializerConstructor, GetterSetter { - translateMember(): arkts.AstNode[] { - const originalName: string = expectName(this.property.key); - const newName: string = backingField(originalName); - - this.cacheTranslatedInitializer(newName, originalName); - return this.translateWithoutInitializer(newName, originalName); - } - - cacheTranslatedInitializer(newName: string, originalName: string): void { - const initializeStruct: arkts.AstNode = this.generateInitializeStruct(newName, originalName); - PropertyCache.getInstance().collectInitializeStruct(this.structInfo.name, [initializeStruct]); - if (!!this.structInfo.annotations?.reusable) { - const toRecord = generateToRecord(newName, originalName); - PropertyCache.getInstance().collectToRecord(this.structInfo.name, [toRecord]); - } - } - - generateInitializeStruct(newName: string, originalName: string): arkts.AstNode { - const storagePropValueStr: string | undefined = getValueInAnnotation( - this.property, - DecoratorNames.STORAGE_PROP - ); - if (!storagePropValueStr) { - throw new Error('StorageProp required only one value!!'); - } - - const args: arkts.Expression[] = [ - arkts.factory.createStringLiteral(storagePropValueStr), - arkts.factory.create1StringLiteral(originalName), - this.property.value ?? arkts.factory.createUndefinedLiteral(), - ]; - factory.judgeIfAddWatchFunc(args, this.property); - collectStateManagementTypeImport(StateManagementTypes.STORAGE_PROP_REF_DECORATED); - - return arkts.factory.createAssignmentExpression( - generateThisBacking(newName), - arkts.Es2pandaTokenType.TOKEN_TYPE_PUNCTUATOR_SUBSTITUTION, - factory.generateStateMgmtFactoryCall( - StateManagementTypes.MAKE_STORAGE_PROP_REF, - this.propertyType, - args, - true - ) - ); - } - - translateWithoutInitializer(newName: string, originalName: string): arkts.AstNode[] { - const field = factory.createOptionalClassProperty( - newName, - this.property, - StateManagementTypes.STORAGE_PROP_REF_DECORATED, - arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_PRIVATE - ); - const thisValue: arkts.Expression = generateThisBacking(newName, false, true); - const thisGet: arkts.CallExpression = generateGetOrSetCall(thisValue, GetSetTypes.GET); - const thisSet: arkts.ExpressionStatement = arkts.factory.createExpressionStatement( - generateGetOrSetCall(thisValue, GetSetTypes.SET) - ); - const getter: arkts.MethodDefinition = this.translateGetter(originalName, this.propertyType, thisGet); - const setter: arkts.MethodDefinition = this.translateSetter(originalName, this.propertyType, thisSet); - return [field, getter, setter]; - } - - translateGetter( - originalName: string, - typeAnnotation: arkts.TypeNode | undefined, - returnValue: arkts.Expression - ): arkts.MethodDefinition { - return createGetter(originalName, typeAnnotation, returnValue); - } - - translateSetter( - originalName: string, - typeAnnotation: arkts.TypeNode | undefined, - statement: arkts.AstNode - ): arkts.MethodDefinition { - return createSetter2(originalName, typeAnnotation, statement); - } -} - -export class StoragePropInterfaceTranslator extends InterfacePropertyTranslator { - translateProperty(): T { - if (arkts.isMethodDefinition(this.property)) { - this.modified = true; - return this.updateStateMethodInInterface(this.property) as T; - } else if (arkts.isClassProperty(this.property)) { - this.modified = true; - return this.updateStatePropertyInInterface(this.property) as T; - } - return this.property; - } - - static canBeTranslated(node: arkts.AstNode): node is InterfacePropertyTypes { - if (arkts.isMethodDefinition(node) && hasDecorator(node, DecoratorNames.STORAGE_PROP)) { - return true; - } else if (arkts.isClassProperty(node) && hasDecorator(node, DecoratorNames.STORAGE_PROP)) { - return true; - } - return false; - } - - /** - * Wrap getter's return type and setter's param type (expecting an union type with `T` and `undefined`) - * to `StoragePropDecoratedVariable | undefined`. - * - * @param method expecting getter with `@StorageProp` and a setter with `@StorageProp` in the overloads. - */ - private updateStateMethodInInterface(method: arkts.MethodDefinition): arkts.MethodDefinition { - return factory.wrapStateManagementTypeToMethodInInterface(method, DecoratorNames.STORAGE_PROP); - } - - /** - * Wrap to the type of the property (expecting an union type with `T` and `undefined`) - * to `StoragePropDecoratedVariable | undefined`. - * - * @param property expecting property with `@StorageProp`. - */ - private updateStatePropertyInInterface(property: arkts.ClassProperty): arkts.ClassProperty { - return factory.wrapStateManagementTypeToPropertyInInterface(property, DecoratorNames.STORAGE_PROP); - } -} diff --git a/arkui-plugins/ui-plugins/property-translators/utils.ts b/arkui-plugins/ui-plugins/property-translators/utils.ts index 8e4415efe359cd19db4c6b23481e129063e3c8c6..186a353bcb528ba02f6fc86199a908403bd5d3f3 100644 --- a/arkui-plugins/ui-plugins/property-translators/utils.ts +++ b/arkui-plugins/ui-plugins/property-translators/utils.ts @@ -125,7 +125,6 @@ export function needDefiniteOrOptionalModifier(st: arkts.ClassProperty): boolean hasDecoratorName(st, DecoratorNames.LINK) || hasDecoratorName(st, DecoratorNames.CONSUME) || hasDecoratorName(st, DecoratorNames.OBJECT_LINK) || - (hasDecoratorName(st, DecoratorNames.PROP) && !st.value) || (hasDecoratorName(st, DecoratorNames.PROP_REF) && !st.value) || (hasDecoratorName(st, DecoratorNames.PARAM) && !st.value) || (hasDecoratorName(st, DecoratorNames.EVENT) && !st.value) ||