• 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 { ExportInfo, ExportType } from '../../core/model/ArkExport';
17import { ArkMetadataKind, CommentsMetadata } from '../../core/model/ArkMetadata';
18import { BasePrinter } from './BasePrinter';
19
20export class ExportPrinter extends BasePrinter {
21    info: ExportInfo;
22
23    public constructor(info: ExportInfo, indent: string = '') {
24        super(indent);
25        this.info = info;
26    }
27
28    public getLine(): number {
29        return this.info.getOriginTsPosition().getLineNo();
30    }
31
32    public dump(): string {
33        this.printer.clear();
34        const commentsMetadata = this.info.getMetadata(ArkMetadataKind.LEADING_COMMENTS);
35        if (commentsMetadata instanceof CommentsMetadata) {
36            this.printComments(commentsMetadata);
37        }
38
39        if (
40            !this.info.getFrom() &&
41            (this.info.isExport() || this.info.getExportClauseType() === ExportType.LOCAL || this.info.getExportClauseType() === ExportType.TYPE)
42        ) {
43            return this.printer.toString();
44        }
45
46        if (this.info.getExportClauseName() === '*') {
47            // just like: export * as xx from './yy'
48            if (this.info.getNameBeforeAs() && this.info.getNameBeforeAs() !== '*') {
49                this.printer.writeIndent().write(`export ${this.info.getNameBeforeAs()} as ${this.info.getExportClauseName()}`);
50            } else {
51                this.printer.writeIndent().write(`export ${this.info.getExportClauseName()}`);
52            }
53        } else {
54            // just like: export {xxx as x} from './yy'
55            if (this.info.getNameBeforeAs()) {
56                this.printer.write(`export {${this.info.getNameBeforeAs()} as ${this.info.getExportClauseName()}}`);
57            } else {
58                this.printer.write(`export {${this.info.getExportClauseName()}}`);
59            }
60        }
61        if (this.info.getFrom()) {
62            this.printer.write(` from '${this.info.getFrom() as string}'`);
63        }
64        this.printer.writeLine(';');
65
66        return this.printer.toString();
67    }
68}
69