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 { ArkField, FieldCategory } from '../../core/model/ArkField'; 17import { UnknownType } from '../../core/base/Type'; 18import { ArkMetadataKind, CommentsMetadata } from '../../core/model/ArkMetadata'; 19import { BasePrinter } from '../base/BasePrinter'; 20 21/** 22 * @category save 23 */ 24export class ArkIRFieldPrinter extends BasePrinter { 25 private field: ArkField; 26 27 public constructor(field: ArkField, indent: string = '') { 28 super(indent); 29 this.field = field; 30 } 31 32 public getLine(): number { 33 return this.field.getOriginPosition().getLineNo(); 34 } 35 public dump(): string { 36 this.printer.clear(); 37 const commentsMetadata = this.field.getMetadata(ArkMetadataKind.LEADING_COMMENTS); 38 if (commentsMetadata instanceof CommentsMetadata) { 39 this.printComments(commentsMetadata); 40 } 41 this.printDecorator(this.field.getDecorators()); 42 this.printer.writeIndent(); 43 if (this.field.getCategory() !== FieldCategory.ENUM_MEMBER) { 44 this.printer.writeSpace(this.modifiersToString(this.field.getModifiers())); 45 } 46 47 this.printer.write(this.field.getName()); 48 49 if (this.field.getQuestionToken()) { 50 this.printer.write('?'); 51 } 52 if (this.field.getExclamationToken()) { 53 this.printer.write('!'); 54 } 55 56 // property.getInitializer() PropertyAccessExpression ArrowFunction ClassExpression FirstLiteralToken StringLiteral 57 if (!(this.field.getType() instanceof UnknownType) && this.field.getCategory() !== FieldCategory.ENUM_MEMBER) { 58 this.printer.write(`: ${this.field.getType().toString()}`); 59 } 60 61 if (this.field.getCategory() === FieldCategory.ENUM_MEMBER) { 62 this.printer.writeLine(','); 63 } else { 64 this.printer.writeLine(''); 65 } 66 return this.printer.toString(); 67 } 68} 69