1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3use swc_atoms::Atom;
4use swc_common::ast_serde;
5
6use crate::{
7 class::ClassDeclaration,
8 comment::Comment,
9 common::{BaseNode, Directive, IdOrString, Identifier},
10 decl::{Declaration, FunctionDeclaration},
11 expr::Expression,
12 lit::StringLiteral,
13 stmt::Statement,
14 typescript::TSDeclareFunction,
15};
16
17#[derive(Debug, Clone, PartialEq)]
18#[ast_serde]
19pub enum ModuleDeclaration {
20 #[tag("ExportAllDeclaration")]
21 ExportAll(ExportAllDeclaration),
22 #[tag("ExportDefaultDeclaration")]
23 ExportDefault(ExportDefaultDeclaration),
24 #[tag("ExportNamedDeclaration")]
25 ExportNamed(ExportNamedDeclaration),
26 #[tag("ImportDeclaration")]
27 Import(ImportDeclaration),
28}
29
30#[derive(Debug, Clone, PartialEq)]
31#[ast_serde]
32pub enum ExportDeclaration {
33 #[tag("ExportAllDeclaration")]
34 ExportAll(ExportAllDeclaration),
35 #[tag("ExportDefaultDeclaration")]
36 ExportDefault(ExportDefaultDeclaration),
37 #[tag("ExportNamedDeclaration")]
38 ExportNamed(ExportNamedDeclaration),
39}
40
41#[derive(Debug, Clone, PartialEq)]
42#[ast_serde]
43pub enum ModuleSpecifier {
44 #[tag("ExportSpecifier")]
45 Export(ExportSpecifier),
46 #[tag("ImportDefaultSpecifier")]
47 ImportDefault(ImportDefaultSpecifier),
48 #[tag("ImportNamespaceSpecifier")]
49 ImportNamespace(ImportNamespaceSpecifier),
50 #[tag("ImportSpecifier")]
51 Import(ImportSpecifier),
52 #[tag("ExportNamespaceSpecifier")]
53 ExportNamespace(ExportNamespaceSpecifier),
54 #[tag("ExportDefaultSpecifier")]
55 ExportDefault(ExportDefaultSpecifier),
56}
57
58#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
59#[serde(tag = "type")]
60pub struct File {
61 #[serde(flatten)]
62 pub base: BaseNode,
63 pub program: Program,
64 #[serde(default)]
65 pub comments: Option<Vec<Comment>>,
66 #[serde(default)]
67 pub tokens: Option<Vec<Value>>, }
69
70#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
71#[serde(tag = "type")]
72pub struct InterpreterDirective {
73 #[serde(flatten)]
74 pub base: BaseNode,
75 #[serde(default)]
76 pub value: Atom,
77}
78
79#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
80#[serde(rename_all = "lowercase")]
81pub enum SrcType {
82 Script,
83 Module,
84}
85
86#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
87#[serde(rename_all = "camelCase")]
88#[serde(tag = "type")]
89pub struct Program {
90 #[serde(flatten)]
91 pub base: BaseNode,
92 pub body: Vec<Statement>,
93 #[serde(default, skip_serializing_if = "crate::ser::skip_comments_on_program")]
94 pub comments: Vec<Comment>,
95 #[serde(default, skip_serializing_if = "crate::flavor::Flavor::skip_empty")]
96 pub directives: Vec<Directive>,
97 pub source_type: SrcType,
98 #[serde(default, skip_serializing_if = "crate::ser::skip_interpreter")]
99 pub interpreter: Option<InterpreterDirective>,
100 #[serde(default, skip_serializing_if = "crate::flavor::Flavor::skip_empty")]
101 pub source_file: String,
102}
103
104#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
105#[serde(rename_all = "lowercase")]
106pub enum ExportKind {
107 Type,
108 Value,
109}
110
111#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
112#[serde(rename_all = "camelCase")]
113#[serde(tag = "type")]
114pub struct ExportSpecifier {
115 #[serde(flatten)]
116 pub base: BaseNode,
117 pub local: ModuleExportNameType,
118 pub exported: ModuleExportNameType,
119 pub export_kind: ExportKind,
120}
121
122#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
123#[serde(tag = "type")]
124pub struct ExportDefaultSpecifier {
125 #[serde(flatten)]
126 pub base: BaseNode,
127 pub exported: Identifier,
128}
129
130#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
131#[serde(tag = "type")]
132pub struct ExportNamespaceSpecifier {
133 #[serde(flatten)]
134 pub base: BaseNode,
135 pub exported: ModuleExportNameType,
136}
137
138#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
139#[serde(rename_all = "camelCase")]
140#[serde(tag = "type")]
141pub struct ExportAllDeclaration {
142 #[serde(flatten)]
143 pub base: BaseNode,
144 pub source: StringLiteral,
145 #[serde(default)]
146 pub with: Option<Vec<ImportAttribute>>,
147 #[serde(default)]
148 pub export_kind: Option<ExportKind>,
149}
150
151#[derive(Debug, Clone, PartialEq)]
152#[ast_serde]
153pub enum ExportDefaultDeclType {
154 #[tag("FunctionDeclaration")]
155 Func(FunctionDeclaration),
156 #[tag("TSDeclareFunction")]
157 TSFunc(TSDeclareFunction),
158 #[tag("ClassDeclaration")]
159 Class(ClassDeclaration),
160 #[tag("*")]
161 Expr(Box<Expression>),
162}
163
164#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
165#[serde(tag = "type")]
166pub struct ExportDefaultDeclaration {
167 #[serde(flatten)]
168 pub base: BaseNode,
169 pub declaration: ExportDefaultDeclType,
170}
171
172#[derive(Debug, Clone, PartialEq)]
173#[ast_serde]
174pub enum ExportSpecifierType {
175 #[tag("ExportSpecifier")]
176 Export(ExportSpecifier),
177 #[tag("ExportDefaultSpecifier")]
178 Default(ExportDefaultSpecifier),
179 #[tag("ExportNamespaceSpecifier")]
180 Namespace(ExportNamespaceSpecifier),
181}
182
183#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
184#[serde(tag = "type")]
185pub struct ExportNamedDeclaration {
186 #[serde(flatten)]
187 pub base: BaseNode,
188 #[serde(default)]
189 pub declaration: Option<Box<Declaration>>,
190 #[serde(default)]
191 pub specifiers: Vec<ExportSpecifierType>,
192 #[serde(default)]
193 pub source: Option<StringLiteral>,
194 #[serde(default)]
195 pub with: Option<Vec<ImportAttribute>>,
196 #[serde(default)]
197 pub export_kind: Option<ExportKind>,
198}
199
200#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
201#[serde(tag = "type")]
202pub struct Import {
203 #[serde(flatten)]
204 pub base: BaseNode,
205}
206
207#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
208#[serde(tag = "type")]
209pub struct ImportAttribute {
210 #[serde(flatten)]
211 pub base: BaseNode,
212 pub key: IdOrString,
213 pub value: StringLiteral,
214}
215
216#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
217#[serde(rename_all = "camelCase")]
218#[serde(tag = "type")]
219pub struct ImportSpecifier {
220 #[serde(flatten)]
221 pub base: BaseNode,
222 pub local: Identifier,
223 pub imported: ModuleExportNameType,
224 #[serde(default)]
225 pub import_kind: Option<ImportKind>,
226}
227
228#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
229#[serde(tag = "type")]
230pub struct ImportDefaultSpecifier {
231 #[serde(flatten)]
232 pub base: BaseNode,
233 pub local: Identifier,
234}
235
236#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
237#[serde(tag = "type")]
238pub struct ImportNamespaceSpecifier {
239 #[serde(flatten)]
240 pub base: BaseNode,
241 pub local: Identifier,
242}
243
244#[derive(Debug, Clone, PartialEq)]
245#[ast_serde]
246pub enum ImportSpecifierType {
247 #[tag("ImportSpecifier")]
248 Import(ImportSpecifier),
249 #[tag("ImportDefaultSpecifier")]
250 Default(ImportDefaultSpecifier),
251 #[tag("ImportNamespaceSpecifier")]
252 Namespace(ImportNamespaceSpecifier),
253}
254
255#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
256#[serde(rename_all = "lowercase")]
257pub enum ImportKind {
258 Type,
259 Typeof,
260 Value,
261}
262
263#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
264#[serde(rename_all = "camelCase")]
265#[serde(tag = "type")]
266pub struct ImportDeclaration {
267 #[serde(flatten)]
268 pub base: BaseNode,
269 #[serde(default)]
270 pub specifiers: Vec<ImportSpecifierType>,
271 pub source: StringLiteral,
272 #[serde(default)]
273 pub with: Option<Vec<ImportAttribute>>,
274 #[serde(default)]
275 pub import_kind: Option<ImportKind>,
276 pub phase: Option<ImportPhase>,
277}
278
279#[derive(Debug, Clone, PartialEq)]
280#[ast_serde]
281pub enum ModuleExportNameType {
282 #[tag("Identifier")]
283 Ident(Identifier),
284 #[tag("StringLiteral")]
285 Str(StringLiteral),
286}
287
288#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
289#[serde(rename_all = "lowercase")]
290pub enum ImportPhase {
291 Source,
292 Defer,
293}