• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2024-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 { ArkField, FieldCategory } from '../../core/model/ArkField';
17import { SourceBase } from './SourceBase';
18import { SourceTransformer } from './SourceTransformer';
19import { UnknownType } from '../../core/base/Type';
20import { ArkMetadataKind, CommentsMetadata } from '../../core/model/ArkMetadata';
21
22/**
23 * @category save
24 */
25export class SourceField extends SourceBase {
26    private field: ArkField;
27    private transformer: SourceTransformer;
28    private initializer: Map<string, string>;
29
30    public constructor(field: ArkField, indent: string = '', initializer: Map<string, string>) {
31        super(field.getDeclaringArkClass().getDeclaringArkFile(), indent);
32        this.field = field;
33        this.transformer = new SourceTransformer(this);
34        this.initializer = initializer;
35    }
36
37    public getLine(): number {
38        return this.field.getOriginPosition().getLineNo();
39    }
40    public dump(): string {
41        this.printer.clear();
42        const commentsMetadata = this.field.getMetadata(ArkMetadataKind.LEADING_COMMENTS);
43        if (commentsMetadata instanceof CommentsMetadata) {
44            const comments = commentsMetadata.getComments();
45            comments.forEach(comment => {
46                this.printer.writeIndent().writeLine(comment.content);
47            });
48        }
49        this.printDecorator(this.field.getDecorators());
50        this.printer.writeIndent();
51        if (this.field.getCategory() !== FieldCategory.ENUM_MEMBER) {
52            this.printer.writeSpace(this.modifiersToString(this.field.getModifiers()));
53        }
54
55        this.printer.write(this.field.getName());
56
57        if (this.field.getQuestionToken()) {
58            this.printer.write('?');
59        }
60        if (this.field.getExclamationToken()) {
61            this.printer.write('!');
62        }
63
64        // property.getInitializer() PropertyAccessExpression ArrowFunction ClassExpression FirstLiteralToken StringLiteral
65        if (!(this.field.getType() instanceof UnknownType) && this.field.getCategory() !== FieldCategory.ENUM_MEMBER) {
66            this.printer.write(`: ${this.transformer.typeToString(this.field.getType())}`);
67        }
68
69        if (this.initializer.has(this.field.getName())) {
70            this.printer.write(` = ${this.initializer.get(this.field.getName())}`);
71        }
72
73        if (this.field.getCategory() === FieldCategory.ENUM_MEMBER) {
74            this.printer.writeLine(',');
75        } else {
76            this.printer.writeLine(';');
77        }
78        return this.printer.toString();
79    }
80}
81