diff --git a/arkui-plugins/common/predefines.ts b/arkui-plugins/common/predefines.ts index 7cc1eb969e477b8563be2895e9eb2e9c948e733f..f7474ba77a6dfc8ab47f3ae7645e1778619eaf04 100644 --- a/arkui-plugins/common/predefines.ts +++ b/arkui-plugins/common/predefines.ts @@ -126,9 +126,9 @@ export enum StateManagementTypes { LINK_DECORATED = 'ILinkDecoratedVariable', LINK_SOURCE_TYPE = 'LinkSourceType', STORAGE_LINK_DECORATED = 'IStorageLinkDecoratedVariable', - STORAGE_PROP_DECORATED = 'IStoragePropDecoratedVariable', + STORAGE_PROP_REF_DECORATED = 'IStoragePropRefDecoratedVariable', + LOCAL_STORAGE_LINK_DECORATED = 'ILocalStorageLinkDecoratedVariable', PROP_DECORATED = 'IPropDecoratedVariable', - MUTABLE_STATE = 'MutableState', SYNCED_PROPERTY = 'SyncedProperty', PROVIDE_DECORATED = 'IProvideDecoratedVariable', CONSUME_DECORATED = 'IConsumeDecoratedVariable', @@ -147,8 +147,9 @@ export enum StateManagementTypes { MAKE_STATE = 'makeState', MAKE_LINK = 'makeLink', MAKE_PROP = 'makeProp', - MAKE_STORAGE_PROP = 'makeStorageProp', + MAKE_STORAGE_PROP_REF = 'makeStoragePropRef', MAKE_STORAGE_LINK = 'makeStorageLink', + MAKE_LOCAL_STORAGE_LINK = 'makeLocalStorageLink', MAKE_PROVIDE = 'makeProvide', MAKE_CONSUME = 'makeConsume', MAKE_OBJECT_LINK = 'makeObjectLink', @@ -193,9 +194,9 @@ export const DECORATOR_TYPE_MAP = new Map( [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], + [DecoratorNames.STORAGE_PROP, StateManagementTypes.STORAGE_PROP_REF_DECORATED], [DecoratorNames.LOCAL_STORAGE_PROP, StateManagementTypes.SYNCED_PROPERTY], - [DecoratorNames.LOCAL_STORAGE_LINK, StateManagementTypes.MUTABLE_STATE], + [DecoratorNames.LOCAL_STORAGE_LINK, StateManagementTypes.LOCAL_STORAGE_LINK_DECORATED], [DecoratorNames.OBJECT_LINK, StateManagementTypes.OBJECT_LINK_DECORATED], [DecoratorNames.PROVIDE, StateManagementTypes.PROVIDE_DECORATED], [DecoratorNames.CONSUME, StateManagementTypes.CONSUME_DECORATED], @@ -210,17 +211,10 @@ 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.MUTABLE_STATE, 'arkui.stateManagement.runtime'], [StateManagementTypes.SYNCED_PROPERTY, 'arkui.stateManagement.runtime'], [StateManagementTypes.STORAGE_LINK_STATE, 'arkui.stateManagement.runtime'], [StateManagementTypes.OBSERVABLE_PROXY, 'arkui.stateManagement.runtime'], diff --git a/arkui-plugins/test/demo/mock/decorators/localstoragelink/localstoragelink-complex-type.ets b/arkui-plugins/test/demo/mock/decorators/localstoragelink/localstoragelink-complex-type.ets new file mode 100644 index 0000000000000000000000000000000000000000..4a1099b23f4a7f05e2293c4b53baa2ddac583ac1 --- /dev/null +++ b/arkui-plugins/test/demo/mock/decorators/localstoragelink/localstoragelink-complex-type.ets @@ -0,0 +1,43 @@ +/* + * 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 { Component, Entry } from "@ohos.arkui.component" +import { LocalStorageLink } from "@ohos.arkui.stateManagement" + +class Person{ + name: string = '' + constructor(name: string){} +} + +enum Status { + Success = 200, + NotFound = 404, + ServerError = 500 +} + +@Entry +@Component +struct MyStateSample { + @LocalStorageLink('Prop1') arrayA: number[] = [1,2,3]; + @LocalStorageLink('Prop2') objectA: Object = {}; + @LocalStorageLink('Prop3') dateA: Date = new Date('2021-08-08'); + @LocalStorageLink('Prop4') setA: Set = new Set(); + @LocalStorageLink('Prop5') mapA: Map = new Map(); + //@LocalStorageLink('Prop6') unionA: string | undefined = ""; + @LocalStorageLink('Prop7') classA: Person = new Person("John"); + @LocalStorageLink('Prop8') enumA: Status = Status.NotFound; + + build() {} +} \ No newline at end of file diff --git a/arkui-plugins/test/demo/mock/decorators/localstoragelink/localstoragelink-primitive-type.ets b/arkui-plugins/test/demo/mock/decorators/localstoragelink/localstoragelink-primitive-type.ets new file mode 100644 index 0000000000000000000000000000000000000000..8bc882e51e4005925ef19ce4f0e73539725c79cf --- /dev/null +++ b/arkui-plugins/test/demo/mock/decorators/localstoragelink/localstoragelink-primitive-type.ets @@ -0,0 +1,27 @@ +/* + * 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 { Component, Entry } from "@ohos.arkui.component" +import { LocalStorageLink } from "@ohos.arkui.stateManagement" + +@Entry +@Component +struct MyStateSample { + @LocalStorageLink('Prop1') numA: number = 33; + @LocalStorageLink('Prop2') stringA: string = 'AA'; + @LocalStorageLink('Prop3') booleanA: boolean = true; + + build() {} +} \ No newline at end of file diff --git a/arkui-plugins/test/demo/mock/decorators/storagelink/storagelink-complex-type.ets b/arkui-plugins/test/demo/mock/decorators/storagelink/storagelink-complex-type.ets index bf7b42ab65af2a3c8d902dc8106dda8ea19bdace..08b9113960050e21e0a8ec028dcf9983bec52514 100644 --- a/arkui-plugins/test/demo/mock/decorators/storagelink/storagelink-complex-type.ets +++ b/arkui-plugins/test/demo/mock/decorators/storagelink/storagelink-complex-type.ets @@ -35,7 +35,6 @@ struct MyStateSample { @StorageLink('Prop3') dateA: Date = new Date('2021-08-08'); @StorageLink('Prop4') setA: Set = new Set(); @StorageLink('Prop5') mapA: Map = new Map(); - @StorageLink('Prop6') unionA: string | undefined = ""; @StorageLink('Prop7') classA: Person = new Person("John"); @StorageLink('Prop8') enumA: Status = Status.NotFound; diff --git a/arkui-plugins/test/demo/mock/decorators/storageprop/storageprop-complex-type.ets b/arkui-plugins/test/demo/mock/decorators/storageprop/storageprop-complex-type.ets index 6136d1bd38a2eeb942fb647d8e7fafcd5d287e0c..50c2ea128a534ea463a12c780b1fa27ab051fe10 100644 --- a/arkui-plugins/test/demo/mock/decorators/storageprop/storageprop-complex-type.ets +++ b/arkui-plugins/test/demo/mock/decorators/storageprop/storageprop-complex-type.ets @@ -35,7 +35,6 @@ struct MyStateSample { @StorageProp('Prop3') dateB: Date = new Date('2021-09-09'); @StorageProp('Prop4') setB: Set = new Set(); @StorageProp('Prop5') mapB: Map = new Map(); - @StorageProp('Prop6') unionB: string | undefined = ""; @StorageProp('Prop7') classB: Person = new Person("Kevin"); @StorageProp('Prop8') enumB: Status = Status.NotFound; diff --git a/arkui-plugins/test/ut/ui-plugins/decorators/localstoragelink/localstoragelink-complex-type.test.ts b/arkui-plugins/test/ut/ui-plugins/decorators/localstoragelink/localstoragelink-complex-type.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..8e928771a740540a6c79487b1faf2713a57efa8d --- /dev/null +++ b/arkui-plugins/test/ut/ui-plugins/decorators/localstoragelink/localstoragelink-complex-type.test.ts @@ -0,0 +1,309 @@ +/* + * 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 path from 'path'; +import { PluginTester } from '../../../../utils/plugin-tester'; +import { mockBuildConfig } from '../../../../utils/artkts-config'; +import { getRootPath, MOCK_ENTRY_DIR_PATH } from '../../../../utils/path-config'; +import { parseDumpSrc } from '../../../../utils/parse-string'; +import { uiNoRecheck, recheck } from '../../../../utils/plugins'; +import { BuildConfig, PluginTestContext } from '../../../../utils/shared-types'; +import { uiTransform } from '../../../../../ui-plugins'; +import { Plugins } from '../../../../../common/plugin-context'; + +const LOCAL_STORAGELINK_DIR_PATH: string = 'decorators/localstoragelink'; + +const buildConfig: BuildConfig = mockBuildConfig(); +buildConfig.compileFiles = [ + path.resolve(getRootPath(), MOCK_ENTRY_DIR_PATH, LOCAL_STORAGELINK_DIR_PATH, 'localstoragelink-complex-type.ets'), +]; + +const localStorageLinkTransform: Plugins = { + name: 'localStorageLink', + parsed: uiTransform().parsed, +} + +const pluginTester = new PluginTester('test LocalStorageLink complex type transform', buildConfig); + +const expectedScript: string = ` + +import { memo as memo } from "arkui.stateManagement.runtime"; + +import { STATE_MGMT_FACTORY as STATE_MGMT_FACTORY } from "arkui.stateManagement.decorator"; + +import { ILocalStorageLinkDecoratedVariable as ILocalStorageLinkDecoratedVariable } from "arkui.stateManagement.decorator"; + +import { NavInterface as NavInterface } from "arkui.UserView"; + +import { PageLifeCycle as PageLifeCycle } from "arkui.component.customComponent"; + +import { EntryPoint as EntryPoint } from "arkui.UserView"; + +import { LayoutCallback as LayoutCallback } from "arkui.component.customComponent"; + +import { CustomComponentV2 as CustomComponentV2 } from "arkui.component.customComponent"; + +import { CustomComponent as CustomComponent } from "arkui.component.customComponent"; + +import { Component as Component, Entry as Entry } from "@ohos.arkui.component"; + +import { LocalStorageLink as LocalStorageLink } from "@ohos.arkui.stateManagement"; + +function main() {} + +__EntryWrapper.RegisterNamedRouter("", new __EntryWrapper(), ({ + bundleName: "com.example.mock", + moduleName: "entry", + pagePath: "../../../decorators/localstoragelink/localstoragelink-complex-type", + pageFullPath: "test/demo/mock/decorators/localstoragelink/localstoragelink-complex-type", + integratedHsp: "false", +} as NavInterface)); + +class Person { + public name: string = ""; + + public constructor(name: string) {} + +} + +final class Status extends BaseEnum { + private readonly #ordinal: int; + + private static () {} + + public constructor(ordinal: int, value: int) { + super(value); + this.#ordinal = ordinal; + } + + public static readonly Success: Status = new Status(0, 200); + + public static readonly NotFound: Status = new Status(1, 404); + + public static readonly ServerError: Status = new Status(2, 500); + + private static readonly #NamesArray: String[] = ["Success", "NotFound", "ServerError"]; + + private static readonly #ValuesArray: int[] = [200, 404, 500]; + + private static readonly #StringValuesArray: String[] = ["200", "404", "500"]; + + private static readonly #ItemsArray: Status[] = [Status.Success, Status.NotFound, Status.ServerError]; + + public getName(): String { + return Status.#NamesArray[this.#ordinal]; + } + + public static getValueOf(name: String): Status { + for (let i = 0;((i) < (Status.#NamesArray.length));(++i)) { + if (((name) == (Status.#NamesArray[i]))) { + return Status.#ItemsArray[i]; + } + } + throw new Error((("No enum constant Status.") + (name))); + } + + public static fromValue(value: int): Status { + for (let i = 0;((i) < (Status.#ValuesArray.length));(++i)) { + if (((value) == (Status.#ValuesArray[i]))) { + return Status.#ItemsArray[i]; + } + } + throw new Error((("No enum Status with value ") + (value))); + } + + public valueOf(): int { + return Status.#ValuesArray[this.#ordinal]; + } + + public toString(): String { + return Status.#StringValuesArray[this.#ordinal]; + } + + public static values(): Status[] { + return Status.#ItemsArray; + } + + public getOrdinal(): int { + return this.#ordinal; + } + + public static $_get(e: Status): String { + return e.getName(); + } + +} + +@Entry({useSharedStorage:false,storage:"",routeName:""}) @Component() final struct MyStateSample extends CustomComponent implements PageLifeCycle { + public __initializeStruct(initializers: __Options_MyStateSample | undefined, @memo() content: (()=> void) | undefined): void { + this.__backing_arrayA = STATE_MGMT_FACTORY.makeLocalStorageLink>(this, "Prop1", "arrayA", [1, 2, 3], Type.from>()) + this.__backing_objectA = STATE_MGMT_FACTORY.makeLocalStorageLink(this, "Prop2", "objectA", {}, Type.from()) + this.__backing_dateA = STATE_MGMT_FACTORY.makeLocalStorageLink(this, "Prop3", "dateA", new Date("2021-08-08"), Type.from()) + this.__backing_setA = STATE_MGMT_FACTORY.makeLocalStorageLink>(this, "Prop4", "setA", new Set(), Type.from>()) + this.__backing_mapA = STATE_MGMT_FACTORY.makeLocalStorageLink>(this, "Prop5", "mapA", new Map(), Type.from>()) + this.__backing_classA = STATE_MGMT_FACTORY.makeLocalStorageLink(this, "Prop7", "classA", new Person("John"), Type.from()) + this.__backing_enumA = STATE_MGMT_FACTORY.makeLocalStorageLink(this, "Prop8", "enumA", Status.NotFound, Type.from()) + } + + public __updateStruct(initializers: __Options_MyStateSample | undefined): void {} + + private __backing_arrayA?: ILocalStorageLinkDecoratedVariable>; + + public get arrayA(): Array { + return this.__backing_arrayA!.get(); + } + + public set arrayA(value: Array) { + this.__backing_arrayA!.set(value); + } + + private __backing_objectA?: ILocalStorageLinkDecoratedVariable; + + public get objectA(): Object { + return this.__backing_objectA!.get(); + } + + public set objectA(value: Object) { + this.__backing_objectA!.set(value); + } + + private __backing_dateA?: ILocalStorageLinkDecoratedVariable; + + public get dateA(): Date { + return this.__backing_dateA!.get(); + } + + public set dateA(value: Date) { + this.__backing_dateA!.set(value); + } + + private __backing_setA?: ILocalStorageLinkDecoratedVariable>; + + public get setA(): Set { + return this.__backing_setA!.get(); + } + + public set setA(value: Set) { + this.__backing_setA!.set(value); + } + + private __backing_mapA?: ILocalStorageLinkDecoratedVariable>; + + public get mapA(): Map { + return this.__backing_mapA!.get(); + } + + public set mapA(value: Map) { + this.__backing_mapA!.set(value); + } + + private __backing_classA?: ILocalStorageLinkDecoratedVariable; + + public get classA(): Person { + return this.__backing_classA!.get(); + } + + public set classA(value: Person) { + this.__backing_classA!.set(value); + } + + private __backing_enumA?: ILocalStorageLinkDecoratedVariable; + + public get enumA(): Status { + return this.__backing_enumA!.get(); + } + + public set enumA(value: Status) { + this.__backing_enumA!.set(value); + } + + @memo() public build() {} + + private constructor() {} + +} + +@Entry({useSharedStorage:false,storage:"",routeName:""}) @Component() export interface __Options_MyStateSample { + set arrayA(arrayA: Array | undefined) + + get arrayA(): Array | undefined + set __backing_arrayA(__backing_arrayA: ILocalStorageLinkDecoratedVariable> | undefined) + + get __backing_arrayA(): ILocalStorageLinkDecoratedVariable> | undefined + set objectA(objectA: Object | undefined) + + get objectA(): Object | undefined + set __backing_objectA(__backing_objectA: ILocalStorageLinkDecoratedVariable | undefined) + + get __backing_objectA(): ILocalStorageLinkDecoratedVariable | undefined + set dateA(dateA: Date | undefined) + + get dateA(): Date | undefined + set __backing_dateA(__backing_dateA: ILocalStorageLinkDecoratedVariable | undefined) + + get __backing_dateA(): ILocalStorageLinkDecoratedVariable | undefined + set setA(setA: Set | undefined) + + get setA(): Set | undefined + set __backing_setA(__backing_setA: ILocalStorageLinkDecoratedVariable> | undefined) + + get __backing_setA(): ILocalStorageLinkDecoratedVariable> | undefined + set mapA(mapA: Map | undefined) + + get mapA(): Map | undefined + set __backing_mapA(__backing_mapA: ILocalStorageLinkDecoratedVariable> | undefined) + + get __backing_mapA(): ILocalStorageLinkDecoratedVariable> | undefined + set classA(classA: Person | undefined) + + get classA(): Person | undefined + set __backing_classA(__backing_classA: ILocalStorageLinkDecoratedVariable | undefined) + + get __backing_classA(): ILocalStorageLinkDecoratedVariable | undefined + set enumA(enumA: Status | undefined) + + get enumA(): Status | undefined + set __backing_enumA(__backing_enumA: ILocalStorageLinkDecoratedVariable | undefined) + + get __backing_enumA(): ILocalStorageLinkDecoratedVariable | undefined + +} + +class __EntryWrapper extends EntryPoint { + @memo() public entry(): void { + MyStateSample._instantiateImpl(undefined, (() => { + return new MyStateSample(); + })); + } + + public constructor() {} + +} +`; + +function testLocalStorageLinkTransformer(this: PluginTestContext): void { + expect(parseDumpSrc(this.scriptSnapshot ?? '')).toBe(parseDumpSrc(expectedScript)); +} + +pluginTester.run( + 'test LocalStorageLink complex type transform', + [localStorageLinkTransform, uiNoRecheck, recheck], + { + 'checked:ui-no-recheck': [testLocalStorageLinkTransformer], + }, + { + stopAfter: 'checked', + } +); diff --git a/arkui-plugins/test/ut/ui-plugins/decorators/localstoragelink/localstoragelink-primitive-type.test.ts b/arkui-plugins/test/ut/ui-plugins/decorators/localstoragelink/localstoragelink-primitive-type.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..5a3ed79ba0ac50056159791a24551a6d935cb8cc --- /dev/null +++ b/arkui-plugins/test/ut/ui-plugins/decorators/localstoragelink/localstoragelink-primitive-type.test.ts @@ -0,0 +1,165 @@ +/* + * 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 path from 'path'; +import { PluginTester } from '../../../../utils/plugin-tester'; +import { mockBuildConfig } from '../../../../utils/artkts-config'; +import { getRootPath, MOCK_ENTRY_DIR_PATH } from '../../../../utils/path-config'; +import { parseDumpSrc } from '../../../../utils/parse-string'; +import { uiNoRecheck, recheck } from '../../../../utils/plugins'; +import { BuildConfig, PluginTestContext } from '../../../../utils/shared-types'; +import { uiTransform } from '../../../../../ui-plugins'; +import { Plugins } from '../../../../../common/plugin-context'; + +const LOCAL_STORAGELINK_DIR_PATH: string = 'decorators/localstoragelink'; + +const buildConfig: BuildConfig = mockBuildConfig(); +buildConfig.compileFiles = [ + path.resolve(getRootPath(), MOCK_ENTRY_DIR_PATH, LOCAL_STORAGELINK_DIR_PATH, 'localstoragelink-primitive-type.ets'), +]; + +const localStorageLinkTransform: Plugins = { + name: 'localStorageLink', + parsed: uiTransform().parsed, +} + +const pluginTester = new PluginTester('test LocalStorageLink primitive type transform', buildConfig); + +const expectedScript: string = ` +import { memo as memo } from "arkui.stateManagement.runtime"; + +import { STATE_MGMT_FACTORY as STATE_MGMT_FACTORY } from "arkui.stateManagement.decorator"; + +import { ILocalStorageLinkDecoratedVariable as ILocalStorageLinkDecoratedVariable } from "arkui.stateManagement.decorator"; + +import { NavInterface as NavInterface } from "arkui.UserView"; + +import { PageLifeCycle as PageLifeCycle } from "arkui.component.customComponent"; + +import { EntryPoint as EntryPoint } from "arkui.UserView"; + +import { LayoutCallback as LayoutCallback } from "arkui.component.customComponent"; + +import { CustomComponentV2 as CustomComponentV2 } from "arkui.component.customComponent"; + +import { CustomComponent as CustomComponent } from "arkui.component.customComponent"; + +import { Component as Component, Entry as Entry } from "@ohos.arkui.component"; + +import { LocalStorageLink as LocalStorageLink } from "@ohos.arkui.stateManagement"; + +function main() {} + +__EntryWrapper.RegisterNamedRouter("", new __EntryWrapper(), ({ + bundleName: "com.example.mock", + moduleName: "entry", + pagePath: "../../../decorators/localstoragelink/localstoragelink-primitive-type", + pageFullPath: "test/demo/mock/decorators/localstoragelink/localstoragelink-primitive-type", + integratedHsp: "false", +} as NavInterface)); + +@Entry({useSharedStorage:false,storage:"",routeName:""}) @Component() final struct MyStateSample extends CustomComponent implements PageLifeCycle { + public __initializeStruct(initializers: __Options_MyStateSample | undefined, @memo() content: (()=> void) | undefined): void { + this.__backing_numA = STATE_MGMT_FACTORY.makeLocalStorageLink(this, "Prop1", "numA", 33, Type.from()) + this.__backing_stringA = STATE_MGMT_FACTORY.makeLocalStorageLink(this, "Prop2", "stringA", "AA", Type.from()) + this.__backing_booleanA = STATE_MGMT_FACTORY.makeLocalStorageLink(this, "Prop3", "booleanA", true, Type.from()) + } + + public __updateStruct(initializers: __Options_MyStateSample | undefined): void {} + + private __backing_numA?: ILocalStorageLinkDecoratedVariable; + + public get numA(): number { + return this.__backing_numA!.get(); + } + + public set numA(value: number) { + this.__backing_numA!.set(value); + } + + private __backing_stringA?: ILocalStorageLinkDecoratedVariable; + + public get stringA(): string { + return this.__backing_stringA!.get(); + } + + public set stringA(value: string) { + this.__backing_stringA!.set(value); + } + + private __backing_booleanA?: ILocalStorageLinkDecoratedVariable; + + public get booleanA(): boolean { + return this.__backing_booleanA!.get(); + } + + public set booleanA(value: boolean) { + this.__backing_booleanA!.set(value); + } + + @memo() public build() {} + + private constructor() {} + +} + +@Entry({useSharedStorage:false,storage:"",routeName:""}) @Component() export interface __Options_MyStateSample { + set numA(numA: number | undefined) + + get numA(): number | undefined + set __backing_numA(__backing_numA: ILocalStorageLinkDecoratedVariable | undefined) + + get __backing_numA(): ILocalStorageLinkDecoratedVariable | undefined + set stringA(stringA: string | undefined) + + get stringA(): string | undefined + set __backing_stringA(__backing_stringA: ILocalStorageLinkDecoratedVariable | undefined) + + get __backing_stringA(): ILocalStorageLinkDecoratedVariable | undefined + set booleanA(booleanA: boolean | undefined) + + get booleanA(): boolean | undefined + set __backing_booleanA(__backing_booleanA: ILocalStorageLinkDecoratedVariable | undefined) + + get __backing_booleanA(): ILocalStorageLinkDecoratedVariable | undefined + +} + +class __EntryWrapper extends EntryPoint { + @memo() public entry(): void { + MyStateSample._instantiateImpl(undefined, (() => { + return new MyStateSample(); + })); + } + + public constructor() {} + +} +`; + +function testLocalStorageLinkTransformer(this: PluginTestContext): void { + expect(parseDumpSrc(this.scriptSnapshot ?? '')).toBe(parseDumpSrc(expectedScript)); +} + +pluginTester.run( + 'test LocalStorageLink primitive type transform', + [localStorageLinkTransform, uiNoRecheck, recheck], + { + 'checked:ui-no-recheck': [testLocalStorageLinkTransformer], + }, + { + stopAfter: 'checked', + } +); diff --git a/arkui-plugins/test/ut/ui-plugins/decorators/storagelink/storagelink-appstorage.test.ts b/arkui-plugins/test/ut/ui-plugins/decorators/storagelink/storagelink-appstorage.test.ts index 474e6776a36eb93853ff253d7a6e9880078c874f..95dc2f1df460863d8c5d110a2275cc6d692d831c 100644 --- a/arkui-plugins/test/ut/ui-plugins/decorators/storagelink/storagelink-appstorage.test.ts +++ b/arkui-plugins/test/ut/ui-plugins/decorators/storagelink/storagelink-appstorage.test.ts @@ -38,29 +38,17 @@ const storageLinkTransform: Plugins = { const pluginTester = new PluginTester('test storagelink with appstorage', buildConfig); const expectedScript: string = ` - import { memo as memo } from "arkui.stateManagement.runtime"; - import { STATE_MGMT_FACTORY as STATE_MGMT_FACTORY } from "arkui.stateManagement.decorator"; - import { IStorageLinkDecoratedVariable as IStorageLinkDecoratedVariable } from "arkui.stateManagement.decorator"; - import { TextAttribute as TextAttribute } from "arkui.component.text"; - import { NavInterface as NavInterface } from "arkui.UserView"; - import { PageLifeCycle as PageLifeCycle } from "arkui.component.customComponent"; - import { EntryPoint as EntryPoint } from "arkui.UserView"; - import { LayoutCallback as LayoutCallback } from "arkui.component.customComponent"; - import { CustomComponentV2 as CustomComponentV2 } from "arkui.component.customComponent"; - import { CustomComponent as CustomComponent } from "arkui.component.customComponent"; - import { Component as Component, Entry as Entry, Column as Column, Text as Text, ClickEvent as ClickEvent } from "@ohos.arkui.component"; - import { StorageLink as StorageLink, AppStorage as AppStorage } from "@ohos.arkui.stateManagement"; function main() {} @@ -78,17 +66,15 @@ __EntryWrapper.RegisterNamedRouter("", new __EntryWrapper(), ({ class Data { public code: number; - public constructor(code: number) { this.code = code; } - } @Entry({useSharedStorage:false,storage:"",routeName:""}) @Component() final struct Index extends CustomComponent implements PageLifeCycle { public __initializeStruct(initializers: __Options_Index | undefined, @memo() content: (()=> void) | undefined): void { - this.__backing_storageLink = STATE_MGMT_FACTORY.makeStorageLink(this, "PropA", "storageLink", 1) - this.__backing_storageLinkObject = STATE_MGMT_FACTORY.makeStorageLink(this, "PropB", "storageLinkObject", new Data(1)) + this.__backing_storageLink = STATE_MGMT_FACTORY.makeStorageLink(this, "PropA", "storageLink", 1, Type.from()) + this.__backing_storageLinkObject = STATE_MGMT_FACTORY.makeStorageLink(this, "PropB", "storageLinkObject", new Data(1), Type.from()) } public __updateStruct(initializers: __Options_Index | undefined): void {} @@ -136,18 +122,13 @@ class Data { @Entry({useSharedStorage:false,storage:"",routeName:""}) @Component() export interface __Options_Index { set storageLink(storageLink: number | undefined) - get storageLink(): number | undefined set __backing_storageLink(__backing_storageLink: IStorageLinkDecoratedVariable | undefined) - get __backing_storageLink(): IStorageLinkDecoratedVariable | undefined set storageLinkObject(storageLinkObject: Data | undefined) - get storageLinkObject(): Data | undefined set __backing_storageLinkObject(__backing_storageLinkObject: IStorageLinkDecoratedVariable | undefined) - get __backing_storageLinkObject(): IStorageLinkDecoratedVariable | undefined - } class __EntryWrapper extends EntryPoint { diff --git a/arkui-plugins/test/ut/ui-plugins/decorators/storagelink/storagelink-complex-type.test.ts b/arkui-plugins/test/ut/ui-plugins/decorators/storagelink/storagelink-complex-type.test.ts index 7f256cbe8ab5e5a1d531dfe3a8c8e899260d77fb..1dbc9932a1fc96081dcdf5b961fae014ef36e448 100644 --- a/arkui-plugins/test/ut/ui-plugins/decorators/storagelink/storagelink-complex-type.test.ts +++ b/arkui-plugins/test/ut/ui-plugins/decorators/storagelink/storagelink-complex-type.test.ts @@ -38,27 +38,16 @@ const storageLinkTransform: Plugins = { const pluginTester = new PluginTester('test storagelink complex type transform', buildConfig); const expectedScript: string = ` - import { memo as memo } from "arkui.stateManagement.runtime"; - import { STATE_MGMT_FACTORY as STATE_MGMT_FACTORY } from "arkui.stateManagement.decorator"; - import { IStorageLinkDecoratedVariable as IStorageLinkDecoratedVariable } from "arkui.stateManagement.decorator"; - import { NavInterface as NavInterface } from "arkui.UserView"; - import { PageLifeCycle as PageLifeCycle } from "arkui.component.customComponent"; - import { EntryPoint as EntryPoint } from "arkui.UserView"; - import { LayoutCallback as LayoutCallback } from "arkui.component.customComponent"; - import { CustomComponentV2 as CustomComponentV2 } from "arkui.component.customComponent"; - import { CustomComponent as CustomComponent } from "arkui.component.customComponent"; - import { Component as Component, Entry as Entry } from "@ohos.arkui.component"; - import { StorageLink as StorageLink } from "@ohos.arkui.stateManagement"; function main() {} @@ -148,14 +137,13 @@ final class Status extends BaseEnum { @Entry({useSharedStorage:false,storage:"",routeName:""}) @Component() final struct MyStateSample extends CustomComponent implements PageLifeCycle { public __initializeStruct(initializers: __Options_MyStateSample | undefined, @memo() content: (()=> void) | undefined): void { - this.__backing_arrayA = STATE_MGMT_FACTORY.makeStorageLink>(this, "Prop1", "arrayA", [1, 2, 3]) - this.__backing_objectA = STATE_MGMT_FACTORY.makeStorageLink(this, "Prop2", "objectA", {}) - this.__backing_dateA = STATE_MGMT_FACTORY.makeStorageLink(this, "Prop3", "dateA", new Date("2021-08-08")) - this.__backing_setA = STATE_MGMT_FACTORY.makeStorageLink>(this, "Prop4", "setA", new Set()) - this.__backing_mapA = STATE_MGMT_FACTORY.makeStorageLink>(this, "Prop5", "mapA", new Map()) - this.__backing_unionA = STATE_MGMT_FACTORY.makeStorageLink(this, "Prop6", "unionA", "") - this.__backing_classA = STATE_MGMT_FACTORY.makeStorageLink(this, "Prop7", "classA", new Person("John")) - this.__backing_enumA = STATE_MGMT_FACTORY.makeStorageLink(this, "Prop8", "enumA", Status.NotFound) + this.__backing_arrayA = STATE_MGMT_FACTORY.makeStorageLink>(this, "Prop1", "arrayA", [1, 2, 3], Type.from>()) + this.__backing_objectA = STATE_MGMT_FACTORY.makeStorageLink(this, "Prop2", "objectA", {}, Type.from()) + this.__backing_dateA = STATE_MGMT_FACTORY.makeStorageLink(this, "Prop3", "dateA", new Date("2021-08-08"), Type.from()) + this.__backing_setA = STATE_MGMT_FACTORY.makeStorageLink>(this, "Prop4", "setA", new Set(), Type.from>()) + this.__backing_mapA = STATE_MGMT_FACTORY.makeStorageLink>(this, "Prop5", "mapA", new Map(), Type.from>()) + this.__backing_classA = STATE_MGMT_FACTORY.makeStorageLink(this, "Prop7", "classA", new Person("John"), Type.from()) + this.__backing_enumA = STATE_MGMT_FACTORY.makeStorageLink(this, "Prop8", "enumA", Status.NotFound, Type.from()) } public __updateStruct(initializers: __Options_MyStateSample | undefined): void {} @@ -210,16 +198,6 @@ final class Status extends BaseEnum { this.__backing_mapA!.set(value); } - private __backing_unionA?: IStorageLinkDecoratedVariable; - - public get unionA(): string | undefined { - return this.__backing_unionA!.get(); - } - - public set unionA(value: string | undefined) { - this.__backing_unionA!.set(value); - } - private __backing_classA?: IStorageLinkDecoratedVariable; public get classA(): Person { @@ -277,12 +255,6 @@ final class Status extends BaseEnum { set __backing_mapA(__backing_mapA: IStorageLinkDecoratedVariable> | undefined) get __backing_mapA(): IStorageLinkDecoratedVariable> | undefined - set unionA(unionA: string | undefined | undefined) - - get unionA(): string | undefined | undefined - set __backing_unionA(__backing_unionA: IStorageLinkDecoratedVariable | undefined) - - get __backing_unionA(): IStorageLinkDecoratedVariable | undefined set classA(classA: Person | undefined) get classA(): Person | undefined diff --git a/arkui-plugins/test/ut/ui-plugins/decorators/storagelink/storagelink-primitive-type.test.ts b/arkui-plugins/test/ut/ui-plugins/decorators/storagelink/storagelink-primitive-type.test.ts index f613eea39ca42f8d8d952bab0419cff1c24687c7..d401ae41ecde317926ceb00f4715c7c96741945e 100644 --- a/arkui-plugins/test/ut/ui-plugins/decorators/storagelink/storagelink-primitive-type.test.ts +++ b/arkui-plugins/test/ut/ui-plugins/decorators/storagelink/storagelink-primitive-type.test.ts @@ -38,27 +38,16 @@ const storageLinkTransform: Plugins = { const pluginTester = new PluginTester('test storagelink primitive type transform', buildConfig); const expectedScript: string = ` - import { memo as memo } from "arkui.stateManagement.runtime"; - import { STATE_MGMT_FACTORY as STATE_MGMT_FACTORY } from "arkui.stateManagement.decorator"; - import { IStorageLinkDecoratedVariable as IStorageLinkDecoratedVariable } from "arkui.stateManagement.decorator"; - import { NavInterface as NavInterface } from "arkui.UserView"; - import { PageLifeCycle as PageLifeCycle } from "arkui.component.customComponent"; - import { EntryPoint as EntryPoint } from "arkui.UserView"; - import { LayoutCallback as LayoutCallback } from "arkui.component.customComponent"; - import { CustomComponentV2 as CustomComponentV2 } from "arkui.component.customComponent"; - import { CustomComponent as CustomComponent } from "arkui.component.customComponent"; - import { Component as Component, Entry as Entry } from "@ohos.arkui.component"; - import { StorageLink as StorageLink } from "@ohos.arkui.stateManagement"; function main() {} @@ -73,9 +62,9 @@ __EntryWrapper.RegisterNamedRouter("", new __EntryWrapper(), ({ @Entry({useSharedStorage:false,storage:"",routeName:""}) @Component() final struct MyStateSample extends CustomComponent implements PageLifeCycle { public __initializeStruct(initializers: __Options_MyStateSample | undefined, @memo() content: (()=> void) | undefined): void { - this.__backing_numA = STATE_MGMT_FACTORY.makeStorageLink(this, "Prop1", "numA", 33) - this.__backing_stringA = STATE_MGMT_FACTORY.makeStorageLink(this, "Prop2", "stringA", "AA") - this.__backing_booleanA = STATE_MGMT_FACTORY.makeStorageLink(this, "Prop3", "booleanA", true) + this.__backing_numA = STATE_MGMT_FACTORY.makeStorageLink(this, "Prop1", "numA", 33, Type.from()) + this.__backing_stringA = STATE_MGMT_FACTORY.makeStorageLink(this, "Prop2", "stringA", "AA", Type.from()) + this.__backing_booleanA = STATE_MGMT_FACTORY.makeStorageLink(this, "Prop3", "booleanA", true, Type.from()) } public __updateStruct(initializers: __Options_MyStateSample | undefined): void {} diff --git a/arkui-plugins/test/ut/ui-plugins/decorators/storageprop/storageprop-appstorage.test.ts b/arkui-plugins/test/ut/ui-plugins/decorators/storageprop/storageprop-appstorage.test.ts index de222f2cc1a9a8b2e168b169c1bd3040cdb085a4..645a2dde9a4cf9a43d6e4a76e0cab83ade7928d1 100644 --- a/arkui-plugins/test/ut/ui-plugins/decorators/storageprop/storageprop-appstorage.test.ts +++ b/arkui-plugins/test/ut/ui-plugins/decorators/storageprop/storageprop-appstorage.test.ts @@ -38,12 +38,11 @@ const storagePropTransform: Plugins = { const pluginTester = new PluginTester('test storageprop with appstorage', buildConfig); const expectedScript: string = ` - import { memo as memo } from "arkui.stateManagement.runtime"; import { STATE_MGMT_FACTORY as STATE_MGMT_FACTORY } from "arkui.stateManagement.decorator"; -import { IStoragePropDecoratedVariable as IStoragePropDecoratedVariable } from "arkui.stateManagement.decorator"; +import { IStoragePropRefDecoratedVariable as IStoragePropRefDecoratedVariable } from "arkui.stateManagement.decorator"; import { TextAttribute as TextAttribute } from "arkui.component.text"; @@ -87,13 +86,13 @@ class Data { @Entry({useSharedStorage:false,storage:"",routeName:""}) @Component() final struct Index extends CustomComponent implements PageLifeCycle { public __initializeStruct(initializers: __Options_Index | undefined, @memo() content: (()=> void) | undefined): void { - this.__backing_storageProp = STATE_MGMT_FACTORY.makeStorageProp(this, "PropA", "storageProp", 1) - this.__backing_storagePropObject = STATE_MGMT_FACTORY.makeStorageProp(this, "PropB", "storagePropObject", new Data(1)) + this.__backing_storageProp = STATE_MGMT_FACTORY.makeStoragePropRef(this, "PropA", "storageProp", 1, Type.from()) + this.__backing_storagePropObject = STATE_MGMT_FACTORY.makeStoragePropRef(this, "PropB", "storagePropObject", new Data(1), Type.from()) } public __updateStruct(initializers: __Options_Index | undefined): void {} - private __backing_storageProp?: IStoragePropDecoratedVariable; + private __backing_storageProp?: IStoragePropRefDecoratedVariable; public get storageProp(): number { return this.__backing_storageProp!.get(); @@ -103,7 +102,7 @@ class Data { this.__backing_storageProp!.set(value); } - private __backing_storagePropObject?: IStoragePropDecoratedVariable; + private __backing_storagePropObject?: IStoragePropRefDecoratedVariable; public get storagePropObject(): Data { return this.__backing_storagePropObject!.get(); @@ -138,15 +137,15 @@ class Data { set storageProp(storageProp: number | undefined) get storageProp(): number | undefined - set __backing_storageProp(__backing_storageProp: IStoragePropDecoratedVariable | undefined) + set __backing_storageProp(__backing_storageProp: IStoragePropRefDecoratedVariable | undefined) - get __backing_storageProp(): IStoragePropDecoratedVariable | undefined + get __backing_storageProp(): IStoragePropRefDecoratedVariable | undefined set storagePropObject(storagePropObject: Data | undefined) get storagePropObject(): Data | undefined - set __backing_storagePropObject(__backing_storagePropObject: IStoragePropDecoratedVariable | undefined) + set __backing_storagePropObject(__backing_storagePropObject: IStoragePropRefDecoratedVariable | undefined) - get __backing_storagePropObject(): IStoragePropDecoratedVariable | undefined + get __backing_storagePropObject(): IStoragePropRefDecoratedVariable | undefined } diff --git a/arkui-plugins/test/ut/ui-plugins/decorators/storageprop/storageprop-complex-type.test.ts b/arkui-plugins/test/ut/ui-plugins/decorators/storageprop/storageprop-complex-type.test.ts index 57c5dbb37f0f8a0cf32a42456e8261021c5c4e5c..b98a64d9bbdb516b5162591d8a09b5a6efb27d2e 100644 --- a/arkui-plugins/test/ut/ui-plugins/decorators/storageprop/storageprop-complex-type.test.ts +++ b/arkui-plugins/test/ut/ui-plugins/decorators/storageprop/storageprop-complex-type.test.ts @@ -38,27 +38,16 @@ const storagePropTransform: Plugins = { const pluginTester = new PluginTester('test storageprop complex type transform', buildConfig); const expectedScript: string = ` - import { memo as memo } from "arkui.stateManagement.runtime"; - import { STATE_MGMT_FACTORY as STATE_MGMT_FACTORY } from "arkui.stateManagement.decorator"; - -import { IStoragePropDecoratedVariable as IStoragePropDecoratedVariable } from "arkui.stateManagement.decorator"; - +import { IStoragePropRefDecoratedVariable as IStoragePropRefDecoratedVariable } from "arkui.stateManagement.decorator"; import { NavInterface as NavInterface } from "arkui.UserView"; - import { PageLifeCycle as PageLifeCycle } from "arkui.component.customComponent"; - import { EntryPoint as EntryPoint } from "arkui.UserView"; - import { LayoutCallback as LayoutCallback } from "arkui.component.customComponent"; - import { CustomComponentV2 as CustomComponentV2 } from "arkui.component.customComponent"; - import { CustomComponent as CustomComponent } from "arkui.component.customComponent"; - import { Component as Component, Entry as Entry } from "@ohos.arkui.component"; - import { StorageProp as StorageProp } from "@ohos.arkui.stateManagement"; function main() {} @@ -148,19 +137,18 @@ final class Status extends BaseEnum { @Entry({useSharedStorage:false,storage:"",routeName:""}) @Component() final struct MyStateSample extends CustomComponent implements PageLifeCycle { public __initializeStruct(initializers: __Options_MyStateSample | undefined, @memo() content: (()=> void) | undefined): void { - this.__backing_arrayB = STATE_MGMT_FACTORY.makeStorageProp>(this, "Prop1", "arrayB", [1, 2, 3]) - this.__backing_objectB = STATE_MGMT_FACTORY.makeStorageProp(this, "Prop2", "objectB", {}) - this.__backing_dateB = STATE_MGMT_FACTORY.makeStorageProp(this, "Prop3", "dateB", new Date("2021-09-09")) - this.__backing_setB = STATE_MGMT_FACTORY.makeStorageProp>(this, "Prop4", "setB", new Set()) - this.__backing_mapB = STATE_MGMT_FACTORY.makeStorageProp>(this, "Prop5", "mapB", new Map()) - this.__backing_unionB = STATE_MGMT_FACTORY.makeStorageProp(this, "Prop6", "unionB", "") - this.__backing_classB = STATE_MGMT_FACTORY.makeStorageProp(this, "Prop7", "classB", new Person("Kevin")) - this.__backing_enumB = STATE_MGMT_FACTORY.makeStorageProp(this, "Prop8", "enumB", Status.NotFound) + this.__backing_arrayB = STATE_MGMT_FACTORY.makeStoragePropRef>(this, "Prop1", "arrayB", [1, 2, 3], Type.from>()) + this.__backing_objectB = STATE_MGMT_FACTORY.makeStoragePropRef(this, "Prop2", "objectB", {}, Type.from()) + this.__backing_dateB = STATE_MGMT_FACTORY.makeStoragePropRef(this, "Prop3", "dateB", new Date("2021-09-09"), Type.from()) + this.__backing_setB = STATE_MGMT_FACTORY.makeStoragePropRef>(this, "Prop4", "setB", new Set(), Type.from>()) + this.__backing_mapB = STATE_MGMT_FACTORY.makeStoragePropRef>(this, "Prop5", "mapB", new Map(), Type.from>()) + this.__backing_classB = STATE_MGMT_FACTORY.makeStoragePropRef(this, "Prop7", "classB", new Person("Kevin"), Type.from()) + this.__backing_enumB = STATE_MGMT_FACTORY.makeStoragePropRef(this, "Prop8", "enumB", Status.NotFound, Type.from()) } public __updateStruct(initializers: __Options_MyStateSample | undefined): void {} - private __backing_arrayB?: IStoragePropDecoratedVariable>; + private __backing_arrayB?: IStoragePropRefDecoratedVariable>; public get arrayB(): Array { return this.__backing_arrayB!.get(); @@ -170,7 +158,7 @@ final class Status extends BaseEnum { this.__backing_arrayB!.set(value); } - private __backing_objectB?: IStoragePropDecoratedVariable; + private __backing_objectB?: IStoragePropRefDecoratedVariable; public get objectB(): Object { return this.__backing_objectB!.get(); @@ -180,7 +168,7 @@ final class Status extends BaseEnum { this.__backing_objectB!.set(value); } - private __backing_dateB?: IStoragePropDecoratedVariable; + private __backing_dateB?: IStoragePropRefDecoratedVariable; public get dateB(): Date { return this.__backing_dateB!.get(); @@ -190,7 +178,7 @@ final class Status extends BaseEnum { this.__backing_dateB!.set(value); } - private __backing_setB?: IStoragePropDecoratedVariable>; + private __backing_setB?: IStoragePropRefDecoratedVariable>; public get setB(): Set { return this.__backing_setB!.get(); @@ -200,7 +188,7 @@ final class Status extends BaseEnum { this.__backing_setB!.set(value); } - private __backing_mapB?: IStoragePropDecoratedVariable>; + private __backing_mapB?: IStoragePropRefDecoratedVariable>; public get mapB(): Map { return this.__backing_mapB!.get(); @@ -210,17 +198,7 @@ final class Status extends BaseEnum { this.__backing_mapB!.set(value); } - private __backing_unionB?: IStoragePropDecoratedVariable; - - public get unionB(): string | undefined { - return this.__backing_unionB!.get(); - } - - public set unionB(value: string | undefined) { - this.__backing_unionB!.set(value); - } - - private __backing_classB?: IStoragePropDecoratedVariable; + private __backing_classB?: IStoragePropRefDecoratedVariable; public get classB(): Person { return this.__backing_classB!.get(); @@ -230,7 +208,7 @@ final class Status extends BaseEnum { this.__backing_classB!.set(value); } - private __backing_enumB?: IStoragePropDecoratedVariable; + private __backing_enumB?: IStoragePropRefDecoratedVariable; public get enumB(): Status { return this.__backing_enumB!.get(); @@ -250,51 +228,45 @@ final class Status extends BaseEnum { set arrayB(arrayB: Array | undefined) get arrayB(): Array | undefined - set __backing_arrayB(__backing_arrayB: IStoragePropDecoratedVariable> | undefined) + set __backing_arrayB(__backing_arrayB: IStoragePropRefDecoratedVariable> | undefined) - get __backing_arrayB(): IStoragePropDecoratedVariable> | undefined + get __backing_arrayB(): IStoragePropRefDecoratedVariable> | undefined set objectB(objectB: Object | undefined) get objectB(): Object | undefined - set __backing_objectB(__backing_objectB: IStoragePropDecoratedVariable | undefined) + set __backing_objectB(__backing_objectB: IStoragePropRefDecoratedVariable | undefined) - get __backing_objectB(): IStoragePropDecoratedVariable | undefined + get __backing_objectB(): IStoragePropRefDecoratedVariable | undefined set dateB(dateB: Date | undefined) get dateB(): Date | undefined - set __backing_dateB(__backing_dateB: IStoragePropDecoratedVariable | undefined) + set __backing_dateB(__backing_dateB: IStoragePropRefDecoratedVariable | undefined) - get __backing_dateB(): IStoragePropDecoratedVariable | undefined + get __backing_dateB(): IStoragePropRefDecoratedVariable | undefined set setB(setB: Set | undefined) get setB(): Set | undefined - set __backing_setB(__backing_setB: IStoragePropDecoratedVariable> | undefined) + set __backing_setB(__backing_setB: IStoragePropRefDecoratedVariable> | undefined) - get __backing_setB(): IStoragePropDecoratedVariable> | undefined + get __backing_setB(): IStoragePropRefDecoratedVariable> | undefined set mapB(mapB: Map | undefined) get mapB(): Map | undefined - set __backing_mapB(__backing_mapB: IStoragePropDecoratedVariable> | undefined) - - get __backing_mapB(): IStoragePropDecoratedVariable> | undefined - set unionB(unionB: string | undefined | undefined) - - get unionB(): string | undefined | undefined - set __backing_unionB(__backing_unionB: IStoragePropDecoratedVariable | undefined) + set __backing_mapB(__backing_mapB: IStoragePropRefDecoratedVariable> | undefined) - get __backing_unionB(): IStoragePropDecoratedVariable | undefined + get __backing_mapB(): IStoragePropRefDecoratedVariable> | undefined set classB(classB: Person | undefined) get classB(): Person | undefined - set __backing_classB(__backing_classB: IStoragePropDecoratedVariable | undefined) + set __backing_classB(__backing_classB: IStoragePropRefDecoratedVariable | undefined) - get __backing_classB(): IStoragePropDecoratedVariable | undefined + get __backing_classB(): IStoragePropRefDecoratedVariable | undefined set enumB(enumB: Status | undefined) get enumB(): Status | undefined - set __backing_enumB(__backing_enumB: IStoragePropDecoratedVariable | undefined) + set __backing_enumB(__backing_enumB: IStoragePropRefDecoratedVariable | undefined) - get __backing_enumB(): IStoragePropDecoratedVariable | undefined + get __backing_enumB(): IStoragePropRefDecoratedVariable | undefined } diff --git a/arkui-plugins/test/ut/ui-plugins/decorators/storageprop/storageprop-primitive-type.test.ts b/arkui-plugins/test/ut/ui-plugins/decorators/storageprop/storageprop-primitive-type.test.ts index 14122a95beafda969e621b455d3d9c5c36492d53..7aa7347a3610ac933953600322c1fda572739b39 100644 --- a/arkui-plugins/test/ut/ui-plugins/decorators/storageprop/storageprop-primitive-type.test.ts +++ b/arkui-plugins/test/ut/ui-plugins/decorators/storageprop/storageprop-primitive-type.test.ts @@ -38,12 +38,11 @@ const storagePropTransform: Plugins = { const pluginTester = new PluginTester('test storageprop primitive type transform', buildConfig); const expectedScript: string = ` - import { memo as memo } from "arkui.stateManagement.runtime"; import { STATE_MGMT_FACTORY as STATE_MGMT_FACTORY } from "arkui.stateManagement.decorator"; -import { IStoragePropDecoratedVariable as IStoragePropDecoratedVariable } from "arkui.stateManagement.decorator"; +import { IStoragePropRefDecoratedVariable as IStoragePropRefDecoratedVariable } from "arkui.stateManagement.decorator"; import { NavInterface as NavInterface } from "arkui.UserView"; @@ -73,14 +72,14 @@ __EntryWrapper.RegisterNamedRouter("", new __EntryWrapper(), ({ @Entry({useSharedStorage:false,storage:"",routeName:""}) @Component() final struct MyStateSample extends CustomComponent implements PageLifeCycle { public __initializeStruct(initializers: __Options_MyStateSample | undefined, @memo() content: (()=> void) | undefined): void { - this.__backing_numB = STATE_MGMT_FACTORY.makeStorageProp(this, "Prop1", "numB", 43) - this.__backing_stringB = STATE_MGMT_FACTORY.makeStorageProp(this, "Prop2", "stringB", "BB") - this.__backing_booleanB = STATE_MGMT_FACTORY.makeStorageProp(this, "Prop3", "booleanB", false) + this.__backing_numB = STATE_MGMT_FACTORY.makeStoragePropRef(this, "Prop1", "numB", 43, Type.from()) + this.__backing_stringB = STATE_MGMT_FACTORY.makeStoragePropRef(this, "Prop2", "stringB", "BB", Type.from()) + this.__backing_booleanB = STATE_MGMT_FACTORY.makeStoragePropRef(this, "Prop3", "booleanB", false, Type.from()) } public __updateStruct(initializers: __Options_MyStateSample | undefined): void {} - private __backing_numB?: IStoragePropDecoratedVariable; + private __backing_numB?: IStoragePropRefDecoratedVariable; public get numB(): number { return this.__backing_numB!.get(); @@ -90,7 +89,7 @@ __EntryWrapper.RegisterNamedRouter("", new __EntryWrapper(), ({ this.__backing_numB!.set(value); } - private __backing_stringB?: IStoragePropDecoratedVariable; + private __backing_stringB?: IStoragePropRefDecoratedVariable; public get stringB(): string { return this.__backing_stringB!.get(); @@ -100,7 +99,7 @@ __EntryWrapper.RegisterNamedRouter("", new __EntryWrapper(), ({ this.__backing_stringB!.set(value); } - private __backing_booleanB?: IStoragePropDecoratedVariable; + private __backing_booleanB?: IStoragePropRefDecoratedVariable; public get booleanB(): boolean { return this.__backing_booleanB!.get(); @@ -120,21 +119,21 @@ __EntryWrapper.RegisterNamedRouter("", new __EntryWrapper(), ({ set numB(numB: number | undefined) get numB(): number | undefined - set __backing_numB(__backing_numB: IStoragePropDecoratedVariable | undefined) + set __backing_numB(__backing_numB: IStoragePropRefDecoratedVariable | undefined) - get __backing_numB(): IStoragePropDecoratedVariable | undefined + get __backing_numB(): IStoragePropRefDecoratedVariable | undefined set stringB(stringB: string | undefined) get stringB(): string | undefined - set __backing_stringB(__backing_stringB: IStoragePropDecoratedVariable | undefined) + set __backing_stringB(__backing_stringB: IStoragePropRefDecoratedVariable | undefined) - get __backing_stringB(): IStoragePropDecoratedVariable | undefined + get __backing_stringB(): IStoragePropRefDecoratedVariable | undefined set booleanB(booleanB: boolean | undefined) get booleanB(): boolean | undefined - set __backing_booleanB(__backing_booleanB: IStoragePropDecoratedVariable | undefined) + set __backing_booleanB(__backing_booleanB: IStoragePropRefDecoratedVariable | undefined) - get __backing_booleanB(): IStoragePropDecoratedVariable | undefined + get __backing_booleanB(): IStoragePropRefDecoratedVariable | 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 aa6c5f3d661114f432b473b9d1e8578621c88bea..1d7cdf50889e4f2a9049fd0ab270b79233fc55fa 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 @@ -47,7 +47,7 @@ import { IProvideDecoratedVariable as IProvideDecoratedVariable } from "arkui.st import { IObjectLinkDecoratedVariable as IObjectLinkDecoratedVariable } from "arkui.stateManagement.decorator"; -import { IStoragePropDecoratedVariable as IStoragePropDecoratedVariable } from "arkui.stateManagement.decorator"; +import { IStoragePropRefDecoratedVariable as IStoragePropRefDecoratedVariable } from "arkui.stateManagement.decorator"; import { IStorageLinkDecoratedVariable as IStorageLinkDecoratedVariable } from "arkui.stateManagement.decorator"; @@ -165,10 +165,10 @@ __EntryWrapper.RegisterNamedRouter("", new __EntryWrapper(), ({ this.linkOnChange(_); })); }; - this.__backing_storagelinkvar = STATE_MGMT_FACTORY.makeStorageLink(this, "prop1", "storagelinkvar", "Hello World", ((_: string): void => { + this.__backing_storagelinkvar = STATE_MGMT_FACTORY.makeStorageLink(this, "prop1", "storagelinkvar", "Hello World", Type.from(), ((_: string): void => { this.storageLinkOnChange(_); })) - this.__backing_storagepropvar = STATE_MGMT_FACTORY.makeStorageProp(this, "prop2", "storagepropvar", "Hello World", ((_: string): void => { + this.__backing_storagepropvar = STATE_MGMT_FACTORY.makeStoragePropRef(this, "prop2", "storagepropvar", "Hello World", Type.from(), ((_: string): void => { this.storagePropOnChange(_); })) this.__backing_objectlinkvar = STATE_MGMT_FACTORY.makeObjectLink(this, "objectlinkvar", ({let gensym___172556967 = initializers; @@ -232,7 +232,7 @@ __EntryWrapper.RegisterNamedRouter("", new __EntryWrapper(), ({ this.__backing_storagelinkvar!.set(value); } - private __backing_storagepropvar?: IStoragePropDecoratedVariable; + private __backing_storagepropvar?: IStoragePropRefDecoratedVariable; public get storagepropvar(): string { return this.__backing_storagepropvar!.get(); @@ -339,9 +339,9 @@ __EntryWrapper.RegisterNamedRouter("", new __EntryWrapper(), ({ set storagepropvar(storagepropvar: string | undefined) get storagepropvar(): string | undefined - set __backing_storagepropvar(__backing_storagepropvar: IStoragePropDecoratedVariable | undefined) + set __backing_storagepropvar(__backing_storagepropvar: IStoragePropRefDecoratedVariable | undefined) - get __backing_storagepropvar(): IStoragePropDecoratedVariable | undefined + get __backing_storagepropvar(): IStoragePropRefDecoratedVariable | undefined set objectlinkvar(objectlinkvar: A | undefined) get objectlinkvar(): A | undefined diff --git a/arkui-plugins/ui-plugins/property-translators/factory.ts b/arkui-plugins/ui-plugins/property-translators/factory.ts index 882a7b4ba4b20d7bee139341a333b4a6014a7848..378b5ff53c9f8c8a159db9b52bf1d7e69e457247 100644 --- a/arkui-plugins/ui-plugins/property-translators/factory.ts +++ b/arkui-plugins/ui-plugins/property-translators/factory.ts @@ -780,4 +780,18 @@ export class factory { } return property; } + + static createTypeFrom(typeAnnotation: arkts.TypeNode | undefined): arkts.CallExpression { + return arkts.factory.createCallExpression( + arkts.factory.createMemberExpression( + arkts.factory.createIdentifier('Type'), + arkts.factory.createIdentifier('from'), + arkts.Es2pandaMemberExpressionKind.MEMBER_EXPRESSION_KIND_PROPERTY_ACCESS, + false, + false + ), + typeAnnotation ? [typeAnnotation] : undefined, + undefined + ); + } } diff --git a/arkui-plugins/ui-plugins/property-translators/localstoragelink.ts b/arkui-plugins/ui-plugins/property-translators/localstoragelink.ts index cc1e0ba115a15e7ac71b385117c668dbc6686cbf..be59f9e6b9cd3c5d4817b5f9502f72077045d063 100755 --- a/arkui-plugins/ui-plugins/property-translators/localstoragelink.ts +++ b/arkui-plugins/ui-plugins/property-translators/localstoragelink.ts @@ -16,12 +16,16 @@ 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 { - collectStateManagementTypeImport, generateToRecord, + createGetter, + createSetter2, + generateThisBacking, + generateGetOrSetCall, + collectStateManagementTypeImport, hasDecorator, PropertyCache, } from './utils'; @@ -29,14 +33,15 @@ import { factory } from './factory'; function getLocalStorageLinkValueStr(node: arkts.AstNode): string | undefined { if (!arkts.isClassProperty(node) || !node.value) return undefined; + return arkts.isStringLiteral(node.value) ? node.value.str : undefined; } function getLocalStorageLinkAnnotationValue(anno: arkts.AnnotationUsage): string | undefined { - const isStorageLinkAnnotation: boolean = + const isLocalStorageLinkAnnotation: boolean = !!anno.expr && arkts.isIdentifier(anno.expr) && anno.expr.name === DecoratorNames.LOCAL_STORAGE_LINK; - if (isStorageLinkAnnotation && anno.properties.length === 1) { + if (isLocalStorageLinkAnnotation && anno.properties.length === 1) { return getLocalStorageLinkValueStr(anno.properties.at(0)!); } return undefined; @@ -52,7 +57,6 @@ function getLocalStorageLinkValueInAnnotation(node: arkts.ClassProperty): string return str; } } - return undefined; } @@ -74,78 +78,71 @@ export class LocalStorageLinkTranslator extends PropertyTranslator implements In } } + generateInitializeStruct(newName: string, originalName: string): arkts.AstNode { + const localStorageLinkValueStr: string | undefined = getLocalStorageLinkValueInAnnotation(this.property); + if (!localStorageLinkValueStr) { + throw new Error('LocalStorageLink required only one value!!'); + } + + const args: arkts.Expression[] = [ + arkts.factory.createStringLiteral(localStorageLinkValueStr), + arkts.factory.create1StringLiteral(originalName), + this.property.value ?? arkts.factory.createUndefinedLiteral(), + factory.createTypeFrom(this.property.typeAnnotation) + ]; + factory.judgeIfAddWatchFunc(args, this.property); + collectStateManagementTypeImport(StateManagementTypes.LOCAL_STORAGE_LINK_DECORATED); + return arkts.factory.createAssignmentExpression( + generateThisBacking(newName), + arkts.Es2pandaTokenType.TOKEN_TYPE_PUNCTUATOR_SUBSTITUTION, + factory.generateStateMgmtFactoryCall( + StateManagementTypes.MAKE_LOCAL_STORAGE_LINK, + this.property.typeAnnotation, + args, + true + ) + ); + } + translateWithoutInitializer(newName: string, originalName: string): arkts.AstNode[] { const field = factory.createOptionalClassProperty( newName, this.property, - StateManagementTypes.MUTABLE_STATE, + StateManagementTypes.LOCAL_STORAGE_LINK_DECORATED, 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.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 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.property.typeAnnotation, - thisValue + thisGet ); const setter: arkts.MethodDefinition = this.translateSetter( originalName, this.property.typeAnnotation, - thisValue + thisSet ); return [field, getter, setter]; } - generateInitializeStruct(newName: string, originalName: string): arkts.AstNode { - const localStorageLinkValueStr: string | undefined = getLocalStorageLinkValueInAnnotation(this.property); - if (!localStorageLinkValueStr) { - throw new Error('LocalStorageLink required only one value!!'); - } - - const call = arkts.factory.createCallExpression( - arkts.factory.createIdentifier(StateManagementTypes.STORAGE_LINK_STATE), - this.property.typeAnnotation ? [this.property.typeAnnotation] : [], - [ - arkts.factory.createMemberExpression( - arkts.factory.createThisExpression(), - arkts.factory.createIdentifier('_entry_local_storage_'), - arkts.Es2pandaMemberExpressionKind.MEMBER_EXPRESSION_KIND_PROPERTY_ACCESS, - false, - false - ), - arkts.factory.createStringLiteral(localStorageLinkValueStr), - this.property.value ?? arkts.factory.createUndefinedLiteral(), - ] - ); - collectStateManagementTypeImport(StateManagementTypes.STORAGE_LINK_STATE); + translateGetter( + originalName: string, + typeAnnotation: arkts.TypeNode | undefined, + returnValue: arkts.Expression + ): arkts.MethodDefinition { + return createGetter(originalName, typeAnnotation, returnValue); + } - 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 - ); + translateSetter( + originalName: string, + typeAnnotation: arkts.TypeNode | undefined, + statement: arkts.AstNode + ): arkts.MethodDefinition { + return createSetter2(originalName, typeAnnotation, statement); } } diff --git a/arkui-plugins/ui-plugins/property-translators/storageProp.ts b/arkui-plugins/ui-plugins/property-translators/storageProp.ts index ecec9362b047ad0e779b5010d1612fbd12fcf165..8d3cf948a104b13fafed06bbb7f5c5fba9b92a7d 100644 --- a/arkui-plugins/ui-plugins/property-translators/storageProp.ts +++ b/arkui-plugins/ui-plugins/property-translators/storageProp.ts @@ -88,15 +88,16 @@ export class StoragePropTranslator extends PropertyTranslator implements Initial arkts.factory.createStringLiteral(storagePropValueStr), arkts.factory.create1StringLiteral(originalName), this.property.value ?? arkts.factory.createUndefinedLiteral(), + factory.createTypeFrom(this.property.typeAnnotation) ]; factory.judgeIfAddWatchFunc(args, this.property); - collectStateManagementTypeImport(StateManagementTypes.STORAGE_PROP_DECORATED); + collectStateManagementTypeImport(StateManagementTypes.STORAGE_PROP_REF_DECORATED); return arkts.factory.createAssignmentExpression( generateThisBacking(newName), arkts.Es2pandaTokenType.TOKEN_TYPE_PUNCTUATOR_SUBSTITUTION, factory.generateStateMgmtFactoryCall( - StateManagementTypes.MAKE_STORAGE_PROP, + StateManagementTypes.MAKE_STORAGE_PROP_REF, this.property.typeAnnotation, args, true @@ -108,7 +109,7 @@ export class StoragePropTranslator extends PropertyTranslator implements Initial const field = factory.createOptionalClassProperty( newName, this.property, - StateManagementTypes.STORAGE_PROP_DECORATED, + StateManagementTypes.STORAGE_PROP_REF_DECORATED, arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_PRIVATE ); const thisValue: arkts.Expression = generateThisBacking(newName, false, true); diff --git a/arkui-plugins/ui-plugins/property-translators/storagelink.ts b/arkui-plugins/ui-plugins/property-translators/storagelink.ts index 8e3256a90b80d8550fcaef2e114aefdd2d730146..8c43bd631281a5166607bf3d38c188b8cc338833 100644 --- a/arkui-plugins/ui-plugins/property-translators/storagelink.ts +++ b/arkui-plugins/ui-plugins/property-translators/storagelink.ts @@ -88,6 +88,7 @@ export class StorageLinkTranslator extends PropertyTranslator implements Initial arkts.factory.createStringLiteral(storageLinkValueStr), arkts.factory.create1StringLiteral(originalName), this.property.value ?? arkts.factory.createUndefinedLiteral(), + factory.createTypeFrom(this.property.typeAnnotation) ]; factory.judgeIfAddWatchFunc(args, this.property); collectStateManagementTypeImport(StateManagementTypes.STORAGE_LINK_DECORATED);