• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2024 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 { ClassProperty, Expression, TypeNode } from '../../generated';
17import { isSameNativeObject } from '../peers/ArktsObject';
18import { updateThenAttach } from '../utilities/private';
19import { classPropertySetOptional, hasModifierFlag } from '../utilities/public';
20import { Es2pandaModifierFlags } from '../../generated/Es2pandaEnums';
21
22export function updateClassProperty(
23    original: ClassProperty,
24    key: Expression | undefined,
25    value: Expression | undefined,
26    typeAnnotation: TypeNode | undefined,
27    modifiers: Es2pandaModifierFlags,
28    isComputed: boolean
29): ClassProperty {
30    if (
31        isSameNativeObject(key, original.key) &&
32        isSameNativeObject(value, original.value) &&
33        isSameNativeObject(typeAnnotation, original.typeAnnotation) &&
34        isSameNativeObject(modifiers, original.modifiers) &&
35        isSameNativeObject(isComputed, original.isComputed)
36    ) {
37        return original;
38    }
39
40    const update = updateThenAttach(
41        ClassProperty.updateClassProperty,
42        (node: ClassProperty, original: ClassProperty) => node.setAnnotations(original.annotations),
43        (node: ClassProperty, original: ClassProperty) => {
44            if (hasModifierFlag(original, Es2pandaModifierFlags.MODIFIER_FLAGS_OPTIONAL)) {
45                return classPropertySetOptional(node, true);
46            }
47            return node;
48        }
49    );
50    return update(original, key, value, typeAnnotation, modifiers, isComputed);
51}
52