• 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 { builderLambdaNoRecheck, memoNoRecheck, recheck } from '../../../utils/plugins';
22
23const BUILDER_LAMBDA_DIR_PATH: string = 'builder-lambda';
24
25const buildConfig: BuildConfig = mockBuildConfig();
26buildConfig.compileFiles = [
27    path.resolve(getRootPath(), MOCK_ENTRY_DIR_PATH, BUILDER_LAMBDA_DIR_PATH, 'simple-component.ets'),
28];
29
30const pluginTester = new PluginTester('test builder-lambda simple component', buildConfig);
31
32function testBuilderLambdaTransformer(this: PluginTestContext): void {
33    const expectedScript: string = `
34import { memo as memo, __memo_context_type as __memo_context_type, __memo_id_type as __memo_id_type } from \"@ohos.arkui.stateManagement\";
35import { Column as Column, UIColumnAttribute as UIColumnAttribute } from \"arkui.component.column\";
36function main() {}
37class MyStateSample {
38    @memo() public build() {
39        Column(undefined, undefined, (() => {}));
40    }
41    public constructor() {}
42}
43`;
44    expect(parseDumpSrc(this.scriptSnapshot ?? '')).toBe(parseDumpSrc(expectedScript));
45}
46
47function testMemoTransformer(this: PluginTestContext): void {
48    const expectedScript: string = `
49import { memo as memo, __memo_context_type as __memo_context_type, __memo_id_type as __memo_id_type } from \"@ohos.arkui.stateManagement\";
50import { Column as Column, UIColumnAttribute as UIColumnAttribute } from \"arkui.component.column\";
51function main() {}
52class MyStateSample {
53    public build(__memo_context: __memo_context_type, __memo_id: __memo_id_type): void {
54        const __memo_scope = __memo_context.scope<void>(((__memo_id) + (263357132)), 0);
55        if (__memo_scope.unchanged) {
56            __memo_scope.cached;
57            return;
58        }
59        Column(__memo_context, ((__memo_id) + (65509320)), undefined, undefined, ((__memo_context: __memo_context_type, __memo_id: __memo_id_type): void => {
60            const __memo_scope = __memo_context.scope<void>(((__memo_id) + (147296800)), 0);
61            if (__memo_scope.unchanged) {
62                __memo_scope.cached;
63                return;
64            }
65            {
66                __memo_scope.recache();
67                return;
68            }
69        }));
70        {
71            __memo_scope.recache();
72            return;
73        }
74    }
75    public constructor() {}
76}
77`;
78    expect(parseDumpSrc(this.scriptSnapshot ?? '')).toBe(parseDumpSrc(expectedScript));
79}
80
81pluginTester.run(
82    'transform simple component',
83    [builderLambdaNoRecheck, recheck, memoNoRecheck],
84    {
85        'checked:builder-lambda-no-recheck': [testBuilderLambdaTransformer],
86        'checked:memo-no-recheck': [testMemoTransformer],
87    },
88    {
89        stopAfter: 'checked',
90    }
91);
92