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 = [path.resolve(getRootPath(), MOCK_ENTRY_DIR_PATH, PROPERTY_DIR_PATH, 'interfaces.ets')]; 27 28const pluginTester = new PluginTester('test memo property', buildConfig); 29 30const expectedScript: string = ` 31import { memo as memo, __memo_context_type as __memo_context_type, __memo_id_type as __memo_id_type } from \"@ohos.arkui.stateManagement\"; 32function main() {} 33@memo() ((__memo_context: __memo_context_type, __memo_id: __memo_id_type): void => { 34 const __memo_scope = __memo_context.scope<void>(((__memo_id) + (<some_random_number>)), 0); 35 if (__memo_scope.unchanged) { 36 __memo_scope.cached; 37 return; 38 } 39 let a: A = { 40 arg: (() => {}), 41 memo_arg: ((__memo_context: __memo_context_type, __memo_id: __memo_id_type): void => { 42 const __memo_scope = __memo_context.scope<void>(((__memo_id) + (<some_random_number>)), 0); 43 if (__memo_scope.unchanged) { 44 __memo_scope.cached; 45 return; 46 } 47 { 48 __memo_scope.recache(); 49 return; 50 } 51 }), 52 memo_union_arg: ((__memo_context: __memo_context_type, __memo_id: __memo_id_type): void => { 53 const __memo_scope = __memo_context.scope<void>(((__memo_id) + (<some_random_number>)), 0); 54 if (__memo_scope.unchanged) { 55 __memo_scope.cached; 56 return; 57 } 58 { 59 __memo_scope.recache(); 60 return; 61 } 62 }), 63 arg_memo_type: ((__memo_context: __memo_context_type, __memo_id: __memo_id_type): void => { 64 const __memo_scope = __memo_context.scope<void>(((__memo_id) + (<some_random_number>)), 0); 65 if (__memo_scope.unchanged) { 66 __memo_scope.cached; 67 return; 68 } 69 { 70 __memo_scope.recache(); 71 return; 72 } 73 }), 74 }; 75 { 76 __memo_scope.recache(); 77 return; 78 } 79}); 80interface A { 81 set arg(arg: (()=> void)) 82 get arg(): (()=> void) 83 @memo() set memo_arg(memo_arg: ((__memo_context: __memo_context_type, __memo_id: __memo_id_type)=> void)): void 84 @memo() get memo_arg(): ((__memo_context: __memo_context_type, __memo_id: __memo_id_type)=> void) 85 @memo() set memo_optional_arg(memo_optional_arg: ((__memo_context: __memo_context_type, __memo_id: __memo_id_type)=> void) | undefined): void 86 @memo() get memo_optional_arg(): ((__memo_context: __memo_context_type, __memo_id: __memo_id_type)=> void) | undefined 87 @memo() set memo_union_arg(memo_union_arg: ((__memo_context: __memo_context_type, __memo_id: __memo_id_type)=> void) | undefined): void 88 @memo() get memo_union_arg(): ((__memo_context: __memo_context_type, __memo_id: __memo_id_type)=> void) | undefined 89 set arg_memo_type(arg_memo_type: @memo() ((__memo_context: __memo_context_type, __memo_id: __memo_id_type)=> void)) 90 get arg_memo_type(): @memo() ((__memo_context: __memo_context_type, __memo_id: __memo_id_type)=> void) 91} 92`; 93 94function testMemoTransformer(this: PluginTestContext): void { 95 expect(parseDumpSrc(this.scriptSnapshot ?? '')).toBe(parseDumpSrc(expectedScript)); 96} 97 98pluginTester.run( 99 'transform interface properties', 100 [memoNoRecheck], 101 { 102 'checked:memo-no-recheck': [testMemoTransformer], 103 }, 104 { 105 stopAfter: 'checked', 106 } 107); 108