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 METHOD_DIR_PATH: string = 'memo/methods'; 24 25const buildConfig: BuildConfig = mockBuildConfig(); 26buildConfig.compileFiles = [path.resolve(getRootPath(), MOCK_ENTRY_DIR_PATH, METHOD_DIR_PATH, 'argument-call.ets')]; 27 28const pluginTester = new PluginTester('test memo method', 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() {} 33class Test { 34 public lambda_arg(__memo_context: __memo_context_type, __memo_id: __memo_id_type, @memo() arg: ((__memo_context: __memo_context_type, __memo_id: __memo_id_type)=> void)): void { 35 const __memo_scope = __memo_context.scope<void>(((__memo_id) + (<some_random_number>)), 1); 36 const __memo_parameter_arg = __memo_scope.param(0, arg); 37 if (__memo_scope.unchanged) { 38 __memo_scope.cached; 39 return; 40 } 41 { 42 __memo_scope.recache(); 43 return; 44 } 45 } 46 public lambda_arg_with_arg(__memo_context: __memo_context_type, __memo_id: __memo_id_type, @memo() arg: ((__memo_context: __memo_context_type, __memo_id: __memo_id_type, value: string)=> string)): void { 47 const __memo_scope = __memo_context.scope<void>(((__memo_id) + (<some_random_number>)), 1); 48 const __memo_parameter_arg = __memo_scope.param(0, arg); 49 if (__memo_scope.unchanged) { 50 __memo_scope.cached; 51 return; 52 } 53 { 54 __memo_scope.recache(); 55 return; 56 } 57 } 58 public memo_content(__memo_context: __memo_context_type, __memo_id: __memo_id_type, @memo() content: ((__memo_context: __memo_context_type, __memo_id: __memo_id_type)=> void)): void { 59 const __memo_scope = __memo_context.scope<void>(((__memo_id) + (<some_random_number>)), 1); 60 const __memo_parameter_content = __memo_scope.param(0, content); 61 if (__memo_scope.unchanged) { 62 __memo_scope.cached; 63 return; 64 } 65 __memo_parameter_content.value(__memo_context, ((__memo_id) + (<some_random_number>))); 66 { 67 __memo_scope.recache(); 68 return; 69 } 70 } 71 public compute_test(__memo_context: __memo_context_type, __memo_id: __memo_id_type, @memo() arg1: ((__memo_context: __memo_context_type, __memo_id: __memo_id_type)=> void) | undefined, arg2: (()=> void) | undefined, content: (()=> void) | undefined): void { 72 const __memo_scope = __memo_context.scope<void>(((__memo_id) + (<some_random_number>)), 3); 73 const __memo_parameter_arg1 = __memo_scope.param(0, arg1), __memo_parameter_arg2 = __memo_scope.param(1, arg2), __memo_parameter_content = __memo_scope.param(2, content); 74 if (__memo_scope.unchanged) { 75 __memo_scope.cached; 76 return; 77 } 78 { 79 __memo_scope.recache(); 80 return; 81 } 82 } 83 public constructor() {} 84} 85class Use { 86 public test(__memo_context: __memo_context_type, __memo_id: __memo_id_type): void { 87 const __memo_scope = __memo_context.scope<void>(((__memo_id) + (<some_random_number>)), 0); 88 if (__memo_scope.unchanged) { 89 __memo_scope.cached; 90 return; 91 } 92 const test = new Test(); 93 test.lambda_arg(__memo_context, ((__memo_id) + (<some_random_number>)), ((__memo_context: __memo_context_type, __memo_id: __memo_id_type): void => { 94 const __memo_scope = __memo_context.scope<void>(((__memo_id) + (<some_random_number>)), 0); 95 if (__memo_scope.unchanged) { 96 __memo_scope.cached; 97 return; 98 } 99 { 100 __memo_scope.recache(); 101 return; 102 } 103 })); 104 test.lambda_arg_with_arg(__memo_context, ((__memo_id) + (<some_random_number>)), ((__memo_context: __memo_context_type, __memo_id: __memo_id_type, value: string): string => { 105 const __memo_scope = __memo_context.scope<string>(((__memo_id) + (<some_random_number>)), 1); 106 const __memo_parameter_value = __memo_scope.param(0, value); 107 if (__memo_scope.unchanged) { 108 return __memo_scope.cached; 109 } 110 return __memo_scope.recache(__memo_parameter_value.value); 111 })); 112 test.compute_test(__memo_context, ((__memo_id) + (<some_random_number>)), ((__memo_context: __memo_context_type, __memo_id: __memo_id_type): void => { 113 const __memo_scope = __memo_context.scope<void>(((__memo_id) + (<some_random_number>)), 0); 114 if (__memo_scope.unchanged) { 115 __memo_scope.cached; 116 return; 117 } 118 { 119 __memo_scope.recache(); 120 return; 121 } 122 }), (() => {}), (() => {})); 123 { 124 __memo_scope.recache(); 125 return; 126 } 127 } 128 public constructor() {} 129} 130`; 131 132function testMemoTransformer(this: PluginTestContext): void { 133 expect(parseDumpSrc(this.scriptSnapshot ?? '')).toBe(parseDumpSrc(expectedScript)); 134} 135 136pluginTester.run( 137 'transform argument calls in methods', 138 [memoNoRecheck], 139 { 140 'checked:memo-no-recheck': [testMemoTransformer], 141 }, 142 { 143 stopAfter: 'checked', 144 } 145); 146