• 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 arkts from '@koalaui/libarkts';
17import { createGetter, createSetter, getStageManagementIdent } from './utils';
18import { createOptionalClassProperty } from '../utils';
19
20export abstract class PropertyTranslator {
21    constructor(
22        protected property: arkts.ClassProperty,
23        protected structName: string
24    ) {}
25
26    abstract translateMember(): arkts.AstNode[];
27
28    translateWithoutInitializer(newName: string, originalName: string): arkts.AstNode[] {
29        const field = createOptionalClassProperty(
30            newName,
31            this.property,
32            getStageManagementIdent(this.property),
33            arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_PRIVATE
34        );
35
36        const member = arkts.factory.createTSNonNullExpression(
37            arkts.factory.createMemberExpression(
38                arkts.factory.createThisExpression(),
39                arkts.factory.createIdentifier(newName),
40                arkts.Es2pandaMemberExpressionKind.MEMBER_EXPRESSION_KIND_PROPERTY_ACCESS,
41                false,
42                false
43            )
44        );
45        const thisValue: arkts.MemberExpression = arkts.factory.createMemberExpression(
46            member,
47            arkts.factory.createIdentifier('value'),
48            arkts.Es2pandaMemberExpressionKind.MEMBER_EXPRESSION_KIND_PROPERTY_ACCESS,
49            false,
50            false
51        );
52
53        const getter: arkts.MethodDefinition = this.translateGetter(
54            originalName,
55            this.property.typeAnnotation,
56            thisValue
57        );
58        const setter: arkts.MethodDefinition = this.translateSetter(
59            originalName,
60            this.property.typeAnnotation,
61            thisValue
62        );
63        return [field, getter, setter];
64    }
65
66    translateGetter(
67        originalName: string,
68        typeAnnotation: arkts.TypeNode | undefined,
69        returnValue: arkts.MemberExpression
70    ): arkts.MethodDefinition {
71        return createGetter(originalName, typeAnnotation, returnValue);
72    }
73
74    translateSetter(
75        originalName: string,
76        typeAnnotation: arkts.TypeNode | undefined,
77        left: arkts.MemberExpression
78    ): arkts.MethodDefinition {
79        const right: arkts.CallExpression = arkts.factory.createCallExpression(
80            arkts.factory.createIdentifier('observableProxy'),
81            undefined,
82            [arkts.factory.createIdentifier('value')]
83        );
84        return createSetter(originalName, typeAnnotation, left, right);
85    }
86}
87