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-properties.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() {} 35class A { 36 public arg: (()=> void); 37 @memo() public memo_arg: ((__memo_context: __memo_context_type, __memo_id: __memo_id_type)=> void); 38 @memo() public memo_optional_arg?: ((__memo_context: __memo_context_type, __memo_id: __memo_id_type)=> void) | undefined; 39 @memo() public memo_union_arg: ((__memo_context: __memo_context_type, __memo_id: __memo_id_type)=> void) | undefined = ((__memo_context: __memo_context_type, __memo_id: __memo_id_type): void => { 40 const __memo_scope = __memo_context.scope<void>(((__memo_id) + (<some_random_number>)), 0); 41 if (__memo_scope.unchanged) { 42 __memo_scope.cached; 43 return; 44 } 45 { 46 __memo_scope.recache(); 47 return; 48 } 49 }); 50 public arg_memo_type: @memo() ((__memo_context: __memo_context_type, __memo_id: __memo_id_type)=> void); 51 public constructor() { 52 this.arg = (() => {}); 53 this.memo_arg = ((__memo_context: __memo_context_type, __memo_id: __memo_id_type): void => { 54 const __memo_scope = __memo_context.scope<void>(((__memo_id) + (<some_random_number>)), 0); 55 if (__memo_scope.unchanged) { 56 __memo_scope.cached; 57 return; 58 } 59 { 60 __memo_scope.recache(); 61 return; 62 } 63 }); 64 this.arg_memo_type = ((__memo_context: __memo_context_type, __memo_id: __memo_id_type): void => { 65 const __memo_scope = __memo_context.scope<void>(((__memo_id) + (<some_random_number>)), 0); 66 if (__memo_scope.unchanged) { 67 __memo_scope.cached; 68 return; 69 } 70 { 71 __memo_scope.recache(); 72 return; 73 } 74 }); 75 } 76 public build(__memo_context: __memo_context_type, __memo_id: __memo_id_type): void { 77 const __memo_scope = __memo_context.scope<void>(((__memo_id) + (<some_random_number>)), 0); 78 if (__memo_scope.unchanged) { 79 __memo_scope.cached; 80 return; 81 } 82 this.arg(); 83 this.memo_arg(__memo_context, ((__memo_id) + (<some_random_number>))); 84 this.arg_memo_type(__memo_context, ((__memo_id) + (<some_random_number>))); 85 { 86 __memo_scope.recache(); 87 return; 88 } 89 } 90} 91`; 92 93function testMemoTransformer(this: PluginTestContext): void { 94 expect(parseDumpSrc(this.scriptSnapshot ?? '')).toBe(parseDumpSrc(expectedScript)); 95} 96 97pluginTester.run( 98 'transform properties in class', 99 [memoNoRecheck], 100 { 101 'checked:memo-no-recheck': [testMemoTransformer], 102 }, 103 { 104 stopAfter: 'checked', 105 } 106); 107