• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 { LineColPosition } from '../base/Position';
17import { ArkFile, Language } from './ArkFile';
18import { ArkSignature, ClassSignature, LocalSignature, MethodSignature, NamespaceSignature } from './ArkSignature';
19import { DEFAULT } from '../common/TSConst';
20import { ArkBaseModel, ModifierType } from './ArkBaseModel';
21import { ArkError } from '../common/ArkError';
22import { ArkMetadataKind, CommentsMetadata } from './ArkMetadata';
23import { ArkNamespace } from './ArkNamespace';
24
25export type ExportSignature = NamespaceSignature | ClassSignature | MethodSignature | LocalSignature;
26
27export enum ExportType {
28    NAME_SPACE = 0,
29    CLASS = 1,
30    METHOD = 2,
31    LOCAL = 3,
32    TYPE = 4,
33    UNKNOWN = 9,
34}
35
36export interface ArkExport extends ArkSignature {
37    getModifiers(): number;
38    containsModifier(modifierType: ModifierType): boolean;
39
40    getName(): string;
41
42    getExportType(): ExportType;
43}
44
45export interface FromInfo {
46    isDefault(): boolean;
47
48    getOriginName(): string;
49
50    getFrom(): string | undefined;
51
52    getDeclaringArkFile(): ArkFile;
53}
54
55/**
56 * @category core/model
57 */
58export class ExportInfo extends ArkBaseModel implements FromInfo {
59    private _default?: boolean;
60    private nameBeforeAs?: string;
61    private exportClauseName: string = '';
62
63    private exportClauseType: ExportType = ExportType.UNKNOWN;
64    private arkExport?: ArkExport | null;
65    private exportFrom?: string;
66
67    private originTsPosition?: LineColPosition;
68    private tsSourceCode?: string;
69    private declaringArkFile!: ArkFile;
70    private declaringArkNamespace?: ArkNamespace;
71    private constructor() {
72        super();
73    }
74
75    /**
76     * Returns the program language of the file where this export info defined.
77     */
78    public getLanguage(): Language {
79        return this.getDeclaringArkFile().getLanguage();
80    }
81
82    public getFrom(): string | undefined {
83        return this.exportFrom;
84    }
85
86    public getOriginName(): string {
87        return this.nameBeforeAs ?? this.exportClauseName;
88    }
89
90    public getExportClauseName(): string {
91        return this.exportClauseName;
92    }
93
94    public setExportClauseType(exportClauseType: ExportType): void {
95        this.exportClauseType = exportClauseType;
96    }
97
98    public getExportClauseType(): ExportType {
99        return this.exportClauseType;
100    }
101
102    public getNameBeforeAs(): string | undefined {
103        return this.nameBeforeAs;
104    }
105
106    public setArkExport(value: ArkExport | null): void {
107        this.arkExport = value;
108    }
109
110    public getArkExport(): ArkExport | undefined | null {
111        return this.arkExport;
112    }
113
114    public isDefault(): boolean {
115        if (this.exportFrom) {
116            return this.nameBeforeAs === DEFAULT;
117        }
118        if (this._default === undefined) {
119            this._default = this.containsModifier(ModifierType.DEFAULT);
120        }
121        return this._default;
122    }
123
124    public getOriginTsPosition(): LineColPosition {
125        return this.originTsPosition ?? LineColPosition.DEFAULT;
126    }
127
128    public getTsSourceCode(): string {
129        return this.tsSourceCode ?? '';
130    }
131
132    public getDeclaringArkFile(): ArkFile {
133        return this.declaringArkFile;
134    }
135
136    public getDeclaringArkNamespace(): ArkNamespace | undefined {
137        return this.declaringArkNamespace;
138    }
139
140    public static Builder = class ArkExportBuilder {
141        exportInfo: ExportInfo = new ExportInfo();
142
143        public exportClauseName(exportClauseName: string): ArkExportBuilder {
144            this.exportInfo.exportClauseName = exportClauseName;
145            return this;
146        }
147
148        public exportClauseType(exportClauseType: ExportType): ArkExportBuilder {
149            this.exportInfo.setExportClauseType(exportClauseType);
150            return this;
151        }
152
153        public nameBeforeAs(nameBeforeAs: string): ArkExportBuilder {
154            this.exportInfo.nameBeforeAs = nameBeforeAs;
155            return this;
156        }
157
158        public modifiers(modifiers: number): ArkExportBuilder {
159            this.exportInfo.modifiers = modifiers;
160            return this;
161        }
162
163        public originTsPosition(originTsPosition: LineColPosition): ArkExportBuilder {
164            this.exportInfo.originTsPosition = originTsPosition;
165            return this;
166        }
167
168        public tsSourceCode(tsSourceCode: string): ArkExportBuilder {
169            this.exportInfo.tsSourceCode = tsSourceCode;
170            return this;
171        }
172
173        public declaringArkFile(value: ArkFile): ArkExportBuilder {
174            this.exportInfo.declaringArkFile = value;
175            return this;
176        }
177
178        public declaringArkNamespace(value: ArkNamespace): ArkExportBuilder {
179            this.exportInfo.declaringArkNamespace = value;
180            return this;
181        }
182
183        public arkExport(value: ArkExport): ArkExportBuilder {
184            this.exportInfo.arkExport = value;
185            return this;
186        }
187
188        public exportFrom(exportFrom: string): ArkExportBuilder {
189            if (exportFrom !== '') {
190                this.exportInfo.exportFrom = exportFrom;
191            }
192            return this;
193        }
194
195        public setLeadingComments(commentsMetadata: CommentsMetadata): ArkExportBuilder {
196            if (commentsMetadata.getComments().length > 0) {
197                this.exportInfo.setMetadata(ArkMetadataKind.LEADING_COMMENTS, commentsMetadata);
198            }
199            return this;
200        }
201
202        public setTrailingComments(commentsMetadata: CommentsMetadata): ArkExportBuilder {
203            if (commentsMetadata.getComments().length > 0) {
204                this.exportInfo.setMetadata(ArkMetadataKind.TRAILING_COMMENTS, commentsMetadata);
205            }
206            return this;
207        }
208
209        public build(): ExportInfo {
210            return this.exportInfo;
211        }
212    };
213
214    public validate(): ArkError {
215        return this.validateFields(['declaringArkFile']);
216    }
217}
218