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 { Decorator } from '../../core/base/Decorator'; 17import { modifiers2stringArray } from '../../core/model/ArkBaseModel'; 18import { ClassCategory } from '../../core/model/ArkClass'; 19import { CommentsMetadata } from '../../core/model/ArkMetadata'; 20import { Printer } from '../Printer'; 21import { PrinterUtils } from './PrinterUtils'; 22 23export interface Dump { 24 getLine(): number; 25 dump(): string; 26} 27 28export interface PrinterOptions { 29 pureTs: boolean; // struct 转成class 30 noMethodBody: boolean; // 仅输出函数原型,不输出函数体 31} 32 33let printerOptions: PrinterOptions = { pureTs: false, noMethodBody: false }; 34export function setPrinterOptions(options: PrinterOptions): void { 35 printerOptions = { ...printerOptions, ...options }; 36} 37 38export abstract class BasePrinter extends Printer implements Dump { 39 public constructor(indent: string) { 40 super(indent); 41 } 42 abstract getLine(): number; 43 44 protected printDecorator(docorator: Decorator[]): void { 45 docorator.forEach(value => { 46 this.printer.writeIndent().writeLine(value.toString()); 47 }); 48 } 49 50 protected printComments(commentsMetadata: CommentsMetadata): void { 51 const comments = commentsMetadata.getComments(); 52 comments.forEach(comment => { 53 this.printer.writeIndent().writeLine(comment.content); 54 }); 55 } 56 57 protected modifiersToString(modifiers: number): string { 58 let modifiersStr: string[] = modifiers2stringArray(modifiers); 59 return modifiersStr.join(' '); 60 } 61 62 protected resolveMethodName(name: string): string { 63 if (name === '_Constructor') { 64 return 'constructor'; 65 } 66 if (name.startsWith('Get-')) { 67 return name.replace('Get-', 'get '); 68 } 69 if (name.startsWith('Set-')) { 70 return name.replace('Set-', 'set '); 71 } 72 return name; 73 } 74 75 protected classOriginTypeToString(clsCategory: ClassCategory): string { 76 if (printerOptions.pureTs) { 77 if (clsCategory === ClassCategory.STRUCT) { 78 clsCategory = ClassCategory.CLASS; 79 } 80 } 81 82 return PrinterUtils.classOriginTypeToString.get(clsCategory)!; 83 } 84 85 public static getPrinterOptions(): PrinterOptions { 86 return printerOptions; 87 } 88} 89