1use serde::{Deserialize, Serialize};
2use swc_common::ast_serde;
3
4use crate::{
5 class::ClassDeclaration,
6 common::{BaseNode, Directive, Identifier, LVal},
7 decl::{EnumDeclaration, FunctionDeclaration, VariableDeclaration},
8 expr::Expression,
9 flow::{
10 DeclareClass, DeclareExportAllDeclaration, DeclareExportDeclaration, DeclareFunction,
11 DeclareInterface, DeclareModule, DeclareModuleExports, DeclareOpaqueType, DeclareTypeAlias,
12 DeclareVariable, InterfaceDeclaration, OpaqueType, TypeAlias,
13 },
14 module::{
15 ExportAllDeclaration, ExportDefaultDeclaration, ExportNamedDeclaration, ImportDeclaration,
16 },
17 pat::{ArrayPattern, ObjectPattern},
18 typescript::{
19 TSDeclareFunction, TSEnumDeclaration, TSExportAssignment, TSImportEqualsDeclaration,
20 TSInterfaceDeclaration, TSModuleDeclaration, TSNamespaceExportDeclaration,
21 TSTypeAliasDeclaration,
22 },
23 UsingDeclaration,
24};
25
26#[derive(Debug, Clone, PartialEq)]
27#[ast_serde]
28pub enum Statement {
29 #[tag("BlockStatement")]
30 Block(BlockStatement),
31 #[tag("BreakStatement")]
32 Break(BreakStatement),
33 #[tag("ContinueStatement")]
34 Continue(ContinueStatement),
35 #[tag("DebuggerStatement")]
36 Debugger(DebuggerStatement),
37 #[tag("DoWhileStatement")]
38 DoWhile(DoWhileStatement),
39 #[tag("EmptyStatement")]
40 Empty(EmptyStatement),
41 #[tag("ExpressionStatement")]
42 Expr(ExpressionStatement),
43 #[tag("ForInStatement")]
44 ForIn(ForInStatement),
45 #[tag("ForStatement")]
46 For(ForStatement),
47 #[tag("FunctionDeclaration")]
48 FuncDecl(FunctionDeclaration),
49 #[tag("IfStatement")]
50 If(IfStatement),
51 #[tag("LabeledStatement")]
52 Labeled(LabeledStatement),
53 #[tag("ReturnStatement")]
54 Return(ReturnStatement),
55 #[tag("SwitchStatement")]
56 Switch(SwitchStatement),
57 #[tag("ThrowStatement")]
58 Throw(ThrowStatement),
59 #[tag("TryStatement")]
60 Try(TryStatement),
61 #[tag("VariableDeclaration")]
62 VarDecl(VariableDeclaration),
63 #[tag("WhileStatement")]
64 While(WhileStatement),
65 #[tag("WithStatement")]
66 With(WithStatement),
67 #[tag("ClassDeclaration")]
68 ClassDecl(ClassDeclaration),
69 #[tag("ExportAllDeclaration")]
70 ExportAllDecl(ExportAllDeclaration),
71 #[tag("ExportDefaultDeclaration")]
72 ExportDefaultDecl(ExportDefaultDeclaration),
73 #[tag("ExportNamedDeclaration")]
74 ExportNamedDecl(ExportNamedDeclaration),
75 #[tag("ForOfStatement")]
76 ForOf(ForOfStatement),
77 #[tag("ImportDeclaration")]
78 ImportDecl(ImportDeclaration),
79 #[tag("DeclareClass")]
80 DeclClass(DeclareClass),
81 #[tag("DeclareFunction")]
82 DeclFunc(DeclareFunction),
83 #[tag("DeclareInterface")]
84 DeclInterface(DeclareInterface),
85 #[tag("DeclareModule")]
86 DeclModule(DeclareModule),
87 #[tag("DeclareModuleExports")]
88 DeclareModuleExports(DeclareModuleExports),
89 #[tag("DeclareTypeAlias")]
90 DeclTypeAlias(DeclareTypeAlias),
91 #[tag("DeclareOpaqueType")]
92 DeclOpaqueType(DeclareOpaqueType),
93 #[tag("DeclareVariable")]
94 DeclVar(DeclareVariable),
95 #[tag("DeclareExportDeclaration")]
96 DeclExportDeclaration(DeclareExportDeclaration),
97 #[tag("DeclareExportAllDeclaration")]
98 DeclExportAllDeclaration(DeclareExportAllDeclaration),
99 #[tag("InterfaceDeclaration")]
100 InterfaceDecl(InterfaceDeclaration),
101 #[tag("OpaqueType")]
102 OpaqueType(OpaqueType),
103 #[tag("TypeAlias")]
104 TypeAlias(TypeAlias),
105 #[tag("EnumDeclaration")]
106 EnumDecl(EnumDeclaration),
107 #[tag("UsingDeclaration")]
108 UsingDecl(UsingDeclaration),
109 #[tag("TSDeclareFunction")]
110 TSDeclFunc(TSDeclareFunction),
111 #[tag("TSInterfaceDeclaration")]
112 TSInterfaceDecl(TSInterfaceDeclaration),
113 #[tag("TSTypeAliasDeclaration")]
114 TSTypeAliasDecl(TSTypeAliasDeclaration),
115 #[tag("TSEnumDeclaration")]
116 TSEnumDecl(TSEnumDeclaration),
117 #[tag("TSModuleDeclaration")]
118 TSModuleDecl(TSModuleDeclaration),
119 #[tag("TSImportEqualsDeclaration")]
120 TSImportEqualsDecl(TSImportEqualsDeclaration),
121 #[tag("TSExportAssignment")]
122 TSExportAssignment(TSExportAssignment),
123 #[tag("TSNamespaceExportDeclaration")]
124 TSNamespaceExportDecl(TSNamespaceExportDeclaration),
125}
126
127#[derive(Debug, Clone, PartialEq)]
128#[ast_serde]
129pub enum CompletionStatement {
130 #[tag("BreakStatement")]
131 Break(BreakStatement),
132 #[tag("ContinueStatement")]
133 Continue(ContinueStatement),
134 #[tag("ReturnStatement")]
135 Return(ReturnStatement),
136 #[tag("ThrowStatement")]
137 Throw(ThrowStatement),
138}
139
140#[derive(Debug, Clone, PartialEq)]
141#[ast_serde]
142pub enum Loop {
143 #[tag("DoWhileStatement")]
144 DoWhile(DoWhileStatement),
145 #[tag("ForInStatement")]
146 ForIn(ForInStatement),
147 #[tag("ForStatement")]
148 For(ForStatement),
149 #[tag("WhileStatement")]
150 While(WhileStatement),
151 #[tag("ForOfStatement")]
152 ForOf(ForOfStatement),
153}
154
155#[derive(Debug, Clone, PartialEq)]
156#[ast_serde]
157pub enum For {
158 #[tag("ForInStatement")]
159 InStmt(ForInStatement),
160 #[tag("ForStatement")]
161 Stmt(ForStatement),
162 #[tag("ForOfStatement")]
163 OfStmt(ForOfStatement),
164}
165
166#[derive(Debug, Clone, PartialEq)]
167#[ast_serde]
168pub enum ForXStatement {
169 #[tag("ForInStatement")]
170 ForIn(ForInStatement),
171 #[tag("ForOfStatement")]
172 ForOf(ForOfStatement),
173}
174
175#[derive(Debug, Clone, PartialEq)]
176#[ast_serde]
177pub enum While {
178 #[tag("DoWhileStatement")]
179 DoWhile(DoWhileStatement),
180 #[tag("WhileStatement")]
181 While(WhileStatement),
182}
183
184#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
185#[serde(tag = "type")]
186pub struct BlockStatement {
187 #[serde(flatten)]
188 pub base: BaseNode,
189 #[serde(default)]
190 pub body: Vec<Statement>,
191 #[serde(default, skip_serializing_if = "crate::flavor::Flavor::skip_empty")]
192 pub directives: Vec<Directive>,
193}
194
195#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
196#[serde(tag = "type")]
197pub struct BreakStatement {
198 #[serde(flatten)]
199 pub base: BaseNode,
200 #[serde(default)]
201 pub label: Option<Identifier>,
202}
203
204#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
205#[serde(tag = "type")]
206pub struct ContinueStatement {
207 #[serde(flatten)]
208 pub base: BaseNode,
209 #[serde(default)]
210 pub label: Option<Identifier>,
211}
212
213#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
214#[serde(tag = "type")]
215pub struct DebuggerStatement {
216 #[serde(flatten)]
217 pub base: BaseNode,
218}
219
220#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
221#[serde(tag = "type")]
222pub struct DoWhileStatement {
223 #[serde(flatten)]
224 pub base: BaseNode,
225 pub test: Box<Expression>,
226 pub body: Box<Statement>,
227}
228
229#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
230#[serde(tag = "type")]
231pub struct EmptyStatement {
232 #[serde(flatten)]
233 pub base: BaseNode,
234}
235
236#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
237#[serde(tag = "type")]
238pub struct ExpressionStatement {
239 #[serde(flatten)]
240 pub base: BaseNode,
241 pub expression: Box<Expression>,
242}
243
244#[derive(Debug, Clone, PartialEq)]
245#[ast_serde]
246pub enum ForStmtInit {
247 #[tag("VariableDeclaration")]
248 VarDecl(VariableDeclaration),
249 #[tag("*")]
250 Expr(Box<Expression>),
251}
252
253#[derive(Debug, Clone, PartialEq)]
254#[ast_serde]
255pub enum ForStmtLeft {
256 #[tag("VariableDeclaration")]
257 VarDecl(VariableDeclaration),
258 #[tag("*")]
259 LVal(LVal),
260}
261
262#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
263#[serde(tag = "type")]
264pub struct ForInStatement {
265 #[serde(flatten)]
266 pub base: BaseNode,
267 pub left: ForStmtLeft,
268 pub right: Box<Expression>,
269 pub body: Box<Statement>,
270}
271
272#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
273#[serde(tag = "type")]
274pub struct ForStatement {
275 #[serde(flatten)]
276 pub base: BaseNode,
277 #[serde(default)]
278 pub init: Option<ForStmtInit>,
279 #[serde(default)]
280 pub test: Option<Box<Expression>>,
281 #[serde(default)]
282 pub update: Option<Box<Expression>>,
283 pub body: Box<Statement>,
284}
285
286#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
287#[serde(tag = "type")]
288pub struct ForOfStatement {
289 #[serde(flatten)]
290 pub base: BaseNode,
291 pub left: ForStmtLeft,
292 pub right: Box<Expression>,
293 pub body: Box<Statement>,
294}
295
296#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
297#[serde(tag = "type")]
298pub struct IfStatement {
299 #[serde(flatten)]
300 pub base: BaseNode,
301 pub test: Box<Expression>,
302 pub consequent: Box<Statement>,
303 #[serde(default)]
304 pub alternate: Option<Box<Statement>>,
305}
306
307#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
308#[serde(tag = "type")]
309pub struct LabeledStatement {
310 #[serde(flatten)]
311 pub base: BaseNode,
312 pub label: Identifier,
313 pub body: Box<Statement>,
314}
315
316#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
317#[serde(tag = "type")]
318pub struct ReturnStatement {
319 #[serde(flatten)]
320 pub base: BaseNode,
321 #[serde(default)]
322 pub argument: Option<Box<Expression>>,
323}
324
325#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
326#[serde(tag = "type")]
327pub struct SwitchCase {
328 #[serde(flatten)]
329 pub base: BaseNode,
330 #[serde(default)]
331 pub test: Option<Box<Expression>>,
332 #[serde(default)]
333 pub consequent: Vec<Statement>,
334}
335
336#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
337#[serde(tag = "type")]
338pub struct SwitchStatement {
339 #[serde(flatten)]
340 pub base: BaseNode,
341 pub discriminant: Box<Expression>,
342 #[serde(default)]
343 pub cases: Vec<SwitchCase>,
344}
345
346#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
347#[serde(tag = "type")]
348pub struct ThrowStatement {
349 #[serde(flatten)]
350 pub base: BaseNode,
351 pub argument: Box<Expression>,
352}
353
354#[derive(Debug, Clone, PartialEq)]
355#[ast_serde]
356pub enum CatchClauseParam {
357 #[tag("Identifier")]
358 Id(Identifier),
359 #[tag("ArrayPattern")]
360 Array(ArrayPattern),
361 #[tag("ObjectPattern")]
362 Object(ObjectPattern),
363}
364
365#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
366#[serde(tag = "type")]
367pub struct CatchClause {
368 #[serde(flatten)]
369 pub base: BaseNode,
370 #[serde(default)]
371 pub param: Option<CatchClauseParam>,
372 pub body: BlockStatement,
373}
374
375#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
376#[serde(tag = "type")]
377pub struct TryStatement {
378 #[serde(flatten)]
379 pub base: BaseNode,
380 pub block: BlockStatement,
381 #[serde(default)]
382 pub handler: Option<CatchClause>,
383 #[serde(default)]
384 pub finalizer: Option<BlockStatement>,
385}
386
387#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
388#[serde(tag = "type")]
389pub struct WhileStatement {
390 #[serde(flatten)]
391 pub base: BaseNode,
392 pub test: Box<Expression>,
393 pub body: Box<Statement>,
394}
395
396#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
397#[serde(tag = "type")]
398pub struct WithStatement {
399 #[serde(flatten)]
400 pub base: BaseNode,
401 pub object: Box<Expression>,
402 pub body: Box<Statement>,
403}