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 = [ 27 path.resolve(getRootPath(), MOCK_ENTRY_DIR_PATH, METHOD_DIR_PATH, 'void-method.ets'), 28]; 29 30const pluginTester = new PluginTester('test memo method', 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 x: int; 37 public y: int; 38 public constructor() {} 39} 40class Test { 41 public void_method(__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 public a_method_with_implicit_return_type(__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 public void_method_with_arg(__memo_context: __memo_context_type, __memo_id: __memo_id_type, arg: string): void { 64 const __memo_scope = __memo_context.scope<void>(((__memo_id) + (<some_random_number>)), 1); 65 const __memo_parameter_arg = __memo_scope.param(0, arg); 66 if (__memo_scope.unchanged) { 67 __memo_scope.cached; 68 return; 69 } 70 { 71 __memo_scope.recache(); 72 return; 73 } 74 } 75 public void_method_with_return(__memo_context: __memo_context_type, __memo_id: __memo_id_type, arg: string): void { 76 const __memo_scope = __memo_context.scope<void>(((__memo_id) + (<some_random_number>)), 1); 77 const __memo_parameter_arg = __memo_scope.param(0, arg); 78 if (__memo_scope.unchanged) { 79 __memo_scope.cached; 80 return; 81 } 82 { 83 __memo_scope.recache(); 84 return; 85 } 86 } 87 public static static_method_with_type_parameter<T>(__memo_context: __memo_context_type, __memo_id: __memo_id_type, arg: T): void { 88 const __memo_scope = __memo_context.scope<void>(((__memo_id) + (<some_random_number>)), 1); 89 const __memo_parameter_arg = __memo_scope.param(0, arg); 90 if (__memo_scope.unchanged) { 91 __memo_scope.cached; 92 return; 93 } 94 { 95 __memo_scope.recache(); 96 return; 97 } 98 } 99 public obj_arg(__memo_context: __memo_context_type, __memo_id: __memo_id_type, arg: A): void { 100 const __memo_scope = __memo_context.scope<void>(((__memo_id) + (<some_random_number>)), 1); 101 const __memo_parameter_arg = __memo_scope.param(0, arg); 102 if (__memo_scope.unchanged) { 103 __memo_scope.cached; 104 return; 105 } 106 { 107 __memo_scope.recache(); 108 return; 109 } 110 } 111 public constructor() {} 112} 113class Use { 114 public test(__memo_context: __memo_context_type, __memo_id: __memo_id_type): void { 115 const __memo_scope = __memo_context.scope<void>(((__memo_id) + (<some_random_number>)), 0); 116 if (__memo_scope.unchanged) { 117 __memo_scope.cached; 118 return; 119 } 120 const test = new Test(); 121 test.void_method(__memo_context, ((__memo_id) + (<some_random_number>))); 122 test.void_method_with_arg(__memo_context, ((__memo_id) + (<some_random_number>)), "an arg"); 123 test.void_method_with_return(__memo_context, ((__memo_id) + (<some_random_number>)), "a value"); 124 Test.static_method_with_type_parameter(__memo_context, ((__memo_id) + (<some_random_number>)), "I'm static"); 125 test.obj_arg(__memo_context, ((__memo_id) + (<some_random_number>)), { 126 x: 1, 127 y: 2, 128 }); 129 { 130 __memo_scope.recache(); 131 return; 132 } 133 } 134 public constructor() {} 135} 136`; 137 138function testMemoTransformer(this: PluginTestContext): void { 139 expect(parseDumpSrc(this.scriptSnapshot ?? '')).toBe(parseDumpSrc(expectedScript)); 140} 141 142pluginTester.run( 143 'transform methods with void return type', 144 [memoNoRecheck], 145 { 146 'checked:memo-no-recheck': [testMemoTransformer], 147 }, 148 { 149 stopAfter: 'checked', 150 } 151); 152