• 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, structNoRecheck, uiNoRecheck } from '../../../utils/plugins';
22import { uiTransform } from '../../../../ui-plugins';
23import { Plugins } from '../../../../common/plugin-context';
24
25const BUILDER_LAMBDA_DIR_PATH: string = 'builder-lambda';
26
27const buildConfig: BuildConfig = mockBuildConfig();
28buildConfig.compileFiles = [
29    path.resolve(getRootPath(), MOCK_ENTRY_DIR_PATH, BUILDER_LAMBDA_DIR_PATH, 'style-with-receiver.ets'),
30];
31
32const pluginTester = new PluginTester('test function with receiver style transformstion', buildConfig);
33
34const parsedTransform: Plugins = {
35    name: 'style-with-receiver',
36    parsed: uiTransform().parsed
37};
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 { UIColumnAttribute as UIColumnAttribute } from "@ohos.arkui.component";
43import { UITextAttribute as UITextAttribute } from "@ohos.arkui.component";
44import { CustomComponent as CustomComponent } from "arkui.component.customComponent";
45import { memo as memo } from "@ohos.arkui.stateManagement";
46import { Text as Text, UITextAttribute as UITextAttribute, Column as Column, Component as Component } from "@ohos.arkui.component";
47import hilog from "@ohos.hilog";
48
49function main() {}
50
51@memo() function cardStyle(this: UITextAttribute, num: number, str: string): UITextAttribute {
52  this.fontSize(num);
53  this.backgroundColor(num);
54  return this;
55}
56
57@memo() function style22(this: UITextAttribute): UITextAttribute {
58  this.fontWeight(700);
59  return this;
60}
61
62@Component({freezeWhenInactive:false}) final class MM extends CustomComponent<MM, __Options_MM> {
63  public __initializeStruct(initializers: __Options_MM | undefined, @memo() content: (()=> void) | undefined): void {}
64
65  public __updateStruct(initializers: __Options_MM | undefined): void {}
66
67  @memo() public _build(@memo() style: ((instance: MM)=> MM) | undefined, @memo() content: (()=> void) | undefined, initializers: __Options_MM | undefined): void {
68    Column(undefined, undefined, (() => {
69      Text(@memo() ((instance: UITextAttribute): void => {
70        style22(cardStyle(instance.height(200).fontColor("#000000"), 600, "#eeeeee").fontSize(60).fontWeight(400)).width(900);
71        return;
72      }), "hello world", undefined, undefined);
73      Text(@memo() ((instance: UITextAttribute): void => {
74        cardStyle(instance, 600, "#eeeeee");
75        return;
76      }), "hello world", undefined, undefined);
77    }));
78  }
79
80  public constructor() {}
81
82}
83
84interface __Options_MM {
85
86}
87`;
88
89function testParsedAndCheckedTransformer(this: PluginTestContext): void {
90    expect(parseDumpSrc(this.scriptSnapshot ?? '')).toBe(parseDumpSrc(expectedScript));
91}
92
93pluginTester.run(
94    'test function with receiver style transformstion',
95    [parsedTransform, uiNoRecheck],
96    {
97        checked: [testParsedAndCheckedTransformer],
98    },
99    {
100        stopAfter: 'checked',
101    }
102);
103