• 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';
22import { Plugins } from '../../../../common/plugin-context';
23import { uiTransform } from '../../../../ui-plugins';
24
25const LAMBDA_DIR_PATH: string = 'memo/lambdas';
26
27const buildConfig: BuildConfig = mockBuildConfig();
28buildConfig.compileFiles = [
29    path.resolve(getRootPath(), MOCK_ENTRY_DIR_PATH, LAMBDA_DIR_PATH, 'function-with-receiver.ets'),
30];
31
32const parsedTransform: Plugins = {
33    name: 'state-complex-type',
34    parsed: uiTransform().parsed
35};
36
37const pluginTester = new PluginTester('test memo lambda', buildConfig);
38
39const expectedScript: string = `
40import { __memo_id_type as __memo_id_type } from "arkui.stateManagement.runtime";
41import { __memo_context_type as __memo_context_type } from "arkui.stateManagement.runtime";
42import { memo as memo } from "@ohos.arkui.stateManagement";
43
44function main() {}
45
46function foo1(this: B, __memo_context: __memo_context_type, __memo_id: __memo_id_type, str: string): void {
47  const __memo_scope = __memo_context.scope<void>(((__memo_id) + (38567515)), 2);
48  const __memo_parameter_this = __memo_scope.param(0, this), __memo_parameter_str = __memo_scope.param(1, str);
49  if (__memo_scope.unchanged) {
50    __memo_scope.cached;
51  return;
52  }
53  console.log("Good", __memo_parameter_str.value);
54  {
55    __memo_scope.recache();
56    return;
57  }
58}
59
60function foo2(this: B, __memo_context: __memo_context_type, __memo_id: __memo_id_type, str: string): B {
61  const __memo_scope = __memo_context.scope<B>(((__memo_id) + (167482260)), 2);
62  const __memo_parameter_this = __memo_scope.param(0, this), __memo_parameter_str = __memo_scope.param(1, str);
63  if (__memo_scope.unchanged) {
64    return __memo_scope.cached;
65  }
66  console.log("Good", __memo_parameter_str.value);
67  return __memo_scope.recache(this);
68}
69
70class B {
71  public internal_call(__memo_context: __memo_context_type, __memo_id: __memo_id_type): B {
72    const __memo_scope = __memo_context.scope<B>(((__memo_id) + (146437675)), 0);
73    if (__memo_scope.unchanged) {
74      return __memo_scope.cached;
75    }
76    foo1(this, __memo_context, ((__memo_id) + (119664703)), "morning");
77    return __memo_scope.recache(foo2(this, __memo_context, ((__memo_id) + (181969214)), "afternoon"));
78  }
79
80  public constructor() {}
81}
82`;
83
84function testMemoTransformer(this: PluginTestContext): void {
85    expect(parseDumpSrc(this.scriptSnapshot ?? '')).toBe(parseDumpSrc(expectedScript));
86}
87
88pluginTester.run(
89    'transform lambdas about function with receiver feature',
90    [parsedTransform, memoNoRecheck],
91    {
92        'checked:memo-no-recheck': [testMemoTransformer],
93    },
94    {
95        stopAfter: 'checked',
96    }
97);
98