• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 { ArkFile } from '../../core/model/ArkFile';
17import { Dump } from '../base/BasePrinter';
18import { Printer } from '../Printer';
19import { ArkIRClassPrinter } from './ArkIRClassPrinter';
20import { ExportPrinter } from '../base/ExportPrinter';
21import { printImports } from '../base/ImportPrinter';
22import { ArkIRNamespacePrinter } from './ArkIRNamespacePrinter';
23
24/**
25 * @category save
26 */
27export class ArkIRFilePrinter extends Printer {
28    arkFile: ArkFile;
29    items: Dump[] = [];
30
31    constructor(arkFile: ArkFile) {
32        super();
33        this.arkFile = arkFile;
34    }
35
36    public dump(): string {
37        // print imports
38        this.items.push(...printImports(this.arkFile.getImportInfos(), this.printer.getIndent()));
39
40        // print namespace
41        for (let ns of this.arkFile.getNamespaces()) {
42            this.items.push(new ArkIRNamespacePrinter(ns));
43        }
44        // print class
45        for (let cls of this.arkFile.getClasses()) {
46            this.items.push(new ArkIRClassPrinter(cls));
47        }
48
49        // print export
50        for (let info of this.arkFile.getExportInfos()) {
51            this.items.push(new ExportPrinter(info));
52        }
53
54        this.items.sort((a, b) => a.getLine() - b.getLine());
55        this.items.forEach((v): void => {
56            this.printer.write(v.dump());
57        });
58
59        return this.printer.toString();
60    }
61}
62