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 { ArkMetadataKind, CommentsMetadata } from '../../core/model/ArkMetadata'; 17import { ArkNamespace } from '../../core/model/ArkNamespace'; 18import { SourceBase } from './SourceBase'; 19import { SourceClass } from './SourceClass'; 20import { SourceMethod } from './SourceMethod'; 21import { PrinterUtils } from '../base/PrinterUtils'; 22import { Dump } from '../base/BasePrinter'; 23import { ExportPrinter } from '../base/ExportPrinter'; 24import { ArkClass } from '../../core/model/ArkClass'; 25 26/** 27 * @category save 28 */ 29export class SourceNamespace extends SourceBase { 30 ns: ArkNamespace; 31 32 public constructor(ns: ArkNamespace, indent: string = '') { 33 super(ns.getDeclaringArkFile(), indent); 34 this.ns = ns; 35 } 36 37 public getLine(): number { 38 return this.ns.getLine(); 39 } 40 41 private printDefaultClassInNamespace(items: Dump[], cls: ArkClass): void { 42 for (let method of cls.getMethods()) { 43 if (method.isDefaultArkMethod()) { 44 items.push(...new SourceMethod(method, this.printer.getIndent()).dumpDefaultMethod()); 45 } else if (!PrinterUtils.isAnonymousMethod(method.getName())) { 46 items.push(new SourceMethod(method, this.printer.getIndent())); 47 } 48 } 49 } 50 51 public dump(): string { 52 const commentsMetadata = this.ns.getMetadata(ArkMetadataKind.LEADING_COMMENTS); 53 if (commentsMetadata instanceof CommentsMetadata) { 54 const comments = commentsMetadata.getComments(); 55 comments.forEach(comment => { 56 this.printer.writeIndent().writeLine(comment.content); 57 }); 58 } 59 this.printer.writeIndent().writeSpace(this.modifiersToString(this.ns.getModifiers())).writeLine(`namespace ${this.ns.getName()} {`); 60 this.printer.incIndent(); 61 62 let items: Dump[] = []; 63 // print class 64 for (let cls of this.ns.getClasses()) { 65 if (PrinterUtils.isAnonymousClass(cls.getName())) { 66 continue; 67 } 68 if (cls.isDefaultArkClass()) { 69 this.printDefaultClassInNamespace(items, cls); 70 } else { 71 items.push(new SourceClass(cls, this.printer.getIndent())); 72 } 73 } 74 // print namespace 75 for (let childNs of this.ns.getNamespaces()) { 76 items.push(new SourceNamespace(childNs, this.printer.getIndent())); 77 } 78 // print exportInfos 79 for (let exportInfo of this.ns.getExportInfos()) { 80 items.push(new ExportPrinter(exportInfo, this.printer.getIndent())); 81 } 82 83 items.sort((a, b) => a.getLine() - b.getLine()); 84 items.forEach((v): void => { 85 this.printer.write(v.dump()); 86 }); 87 this.printer.decIndent(); 88 this.printer.writeIndent().writeLine('}'); 89 return this.printer.toString(); 90 } 91} 92