• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 FUNCTION_DIR_PATH: string = 'memo/functions';
24
25const buildConfig: BuildConfig = mockBuildConfig();
26buildConfig.compileFiles = [
27    path.resolve(getRootPath(), MOCK_ENTRY_DIR_PATH, FUNCTION_DIR_PATH, 'inner-functions.ets'),
28];
29
30const pluginTester = new PluginTester('test memo function', 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() {}
35function foo(__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    {
42        __memo_scope.recache();
43        return;
44    }
45}
46function bar(__memo_context: __memo_context_type, __memo_id: __memo_id_type): void {
47    const __memo_scope = __memo_context.scope<void>(((__memo_id) + (<some_random_number>)), 0);
48    if (__memo_scope.unchanged) {
49        __memo_scope.cached;
50        return;
51    }
52    const qux = @memo() ((__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        foo(__memo_context, ((__memo_id) + (<some_random_number>)));
59        {
60            __memo_scope.recache();
61            return;
62        }
63    });
64    const other = (() => {});
65    {
66        __memo_scope.recache();
67        return;
68    }
69}
70class A {
71    public goo(__memo_context: __memo_context_type, __memo_id: __memo_id_type): void {
72        const __memo_scope = __memo_context.scope<void>(((__memo_id) + (<some_random_number>)), 0);
73        if (__memo_scope.unchanged) {
74        __memo_scope.cached;
75        return;
76        }
77        let func = (() => {});
78        let func2 = @memo() ((__memo_context: __memo_context_type, __memo_id: __memo_id_type): void => {
79            const __memo_scope = __memo_context.scope<void>(((__memo_id) + (<some_random_number>)), 0);
80            if (__memo_scope.unchanged) {
81                __memo_scope.cached;
82                return;
83            }
84            foo(__memo_context, ((__memo_id) + (<some_random_number>)));
85            {
86                __memo_scope.recache();
87                return;
88            }
89        });
90        {
91            __memo_scope.recache();
92            return;
93        }
94    }
95    public constructor() {}
96}
97`;
98
99function testMemoTransformer(this: PluginTestContext): void {
100    expect(parseDumpSrc(this.scriptSnapshot ?? '')).toBe(parseDumpSrc(expectedScript));
101}
102
103pluginTester.run(
104    'transform inner functions',
105    [memoNoRecheck],
106    {
107        'checked:memo-no-recheck': [testMemoTransformer],
108    },
109    {
110        stopAfter: 'checked',
111    }
112);
113