1/* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import * as path from 'path'; 17import { PluginTestContext, PluginTester } from '../../../utils/plugin-tester'; 18import { BuildConfig, mockBuildConfig } from '../../../utils/artkts-config'; 19import { getRootPath, MOCK_ENTRY_DIR_PATH } from '../../../utils/path-config'; 20import { parseDumpSrc } from '../../../utils/parse-string'; 21import { memoNoRecheck } from '../../../utils/plugins'; 22 23const PROPERTY_DIR_PATH: string = 'memo/properties'; 24 25const buildConfig: BuildConfig = mockBuildConfig(); 26buildConfig.compileFiles = [ 27 path.resolve(getRootPath(), MOCK_ENTRY_DIR_PATH, PROPERTY_DIR_PATH, 'class-constructor.ets'), 28]; 29 30const pluginTester = new PluginTester('test memo property', buildConfig); 31 32const expectedScript: string = ` 33import { memo as memo, __memo_context_type as __memo_context_type, __memo_id_type as __memo_id_type } from \"@ohos.arkui.stateManagement\"; 34function main() {} 35@memo() ((__memo_context: __memo_context_type, __memo_id: __memo_id_type): void => { 36 const __memo_scope = __memo_context.scope<void>(((__memo_id) + (<some_random_number>)), 0); 37 if (__memo_scope.unchanged) { 38 __memo_scope.cached; 39 return; 40 } 41 let a = new AA({ 42 a: ((__memo_context: __memo_context_type, __memo_id: __memo_id_type): void => { 43 const __memo_scope = __memo_context.scope<void>(((__memo_id) + (<some_random_number>)), 0); 44 if (__memo_scope.unchanged) { 45 __memo_scope.cached; 46 return; 47 } 48 { 49 __memo_scope.recache(); 50 return; 51 } 52 }), 53 }); 54 { 55 __memo_scope.recache(); 56 return; 57 } 58}); 59interface A { 60 @memo() set a(a: ((__memo_context: __memo_context_type, __memo_id: __memo_id_type)=> void)): void 61 @memo() get a(): ((__memo_context: __memo_context_type, __memo_id: __memo_id_type)=> void) 62} 63class AA { 64 @memo() public a: ((__memo_context: __memo_context_type, __memo_id: __memo_id_type)=> void) | undefined; 65 constructor() { 66 this(undefined); 67 } 68 public constructor(arg: A | undefined) { 69 this.a = ({let gensym%%_<some_random_number> = arg; 70 (((gensym%%_<some_random_number>) == (null)) ? undefined : gensym%%_<some_random_number>.a)}); 71 } 72 public build(__memo_context: __memo_context_type, __memo_id: __memo_id_type): void { 73 const __memo_scope = __memo_context.scope<void>(((__memo_id) + (<some_random_number>)), 0); 74 if (__memo_scope.unchanged) { 75 __memo_scope.cached; 76 return; 77 } 78 ({let gensym%%_<some_random_number> = this.a; 79 (((gensym%%_<some_random_number>) == (null)) ? undefined : gensym%%_<some_random_number>(__memo_context, ((__memo_id) + (<some_random_number>))))}); 80 { 81 __memo_scope.recache(); 82 return; 83 } 84 } 85} 86`; 87 88function testMemoTransformer(this: PluginTestContext): void { 89 expect(parseDumpSrc(this.scriptSnapshot ?? '')).toBe(parseDumpSrc(expectedScript)); 90} 91 92pluginTester.run( 93 'transform properties in class constructor', 94 [memoNoRecheck], 95 { 96 'checked:memo-no-recheck': [testMemoTransformer], 97 }, 98 { 99 stopAfter: 'checked', 100 } 101); 102