Skip to main content

react_compiler_ast/
statements.rs

1use serde::{Deserialize, Serialize};
2
3use crate::common::BaseNode;
4
5use crate::expressions::{Expression, Identifier};
6use crate::patterns::PatternLike;
7
8fn is_false(v: &bool) -> bool {
9    !v
10}
11
12#[derive(Debug, Clone, Serialize, Deserialize)]
13#[serde(tag = "type")]
14pub enum Statement {
15    // Statements
16    BlockStatement(BlockStatement),
17    ReturnStatement(ReturnStatement),
18    IfStatement(IfStatement),
19    ForStatement(ForStatement),
20    WhileStatement(WhileStatement),
21    DoWhileStatement(DoWhileStatement),
22    ForInStatement(ForInStatement),
23    ForOfStatement(ForOfStatement),
24    SwitchStatement(SwitchStatement),
25    ThrowStatement(ThrowStatement),
26    TryStatement(TryStatement),
27    BreakStatement(BreakStatement),
28    ContinueStatement(ContinueStatement),
29    LabeledStatement(LabeledStatement),
30    ExpressionStatement(ExpressionStatement),
31    EmptyStatement(EmptyStatement),
32    DebuggerStatement(DebuggerStatement),
33    WithStatement(WithStatement),
34    // Declarations are also statements
35    VariableDeclaration(VariableDeclaration),
36    FunctionDeclaration(FunctionDeclaration),
37    ClassDeclaration(ClassDeclaration),
38    // Import/export declarations
39    ImportDeclaration(crate::declarations::ImportDeclaration),
40    ExportNamedDeclaration(crate::declarations::ExportNamedDeclaration),
41    ExportDefaultDeclaration(crate::declarations::ExportDefaultDeclaration),
42    ExportAllDeclaration(crate::declarations::ExportAllDeclaration),
43    // TypeScript declarations
44    TSTypeAliasDeclaration(crate::declarations::TSTypeAliasDeclaration),
45    TSInterfaceDeclaration(crate::declarations::TSInterfaceDeclaration),
46    TSEnumDeclaration(crate::declarations::TSEnumDeclaration),
47    TSModuleDeclaration(crate::declarations::TSModuleDeclaration),
48    TSDeclareFunction(crate::declarations::TSDeclareFunction),
49    // Flow declarations
50    TypeAlias(crate::declarations::TypeAlias),
51    OpaqueType(crate::declarations::OpaqueType),
52    InterfaceDeclaration(crate::declarations::InterfaceDeclaration),
53    DeclareVariable(crate::declarations::DeclareVariable),
54    DeclareFunction(crate::declarations::DeclareFunction),
55    DeclareClass(crate::declarations::DeclareClass),
56    DeclareModule(crate::declarations::DeclareModule),
57    DeclareModuleExports(crate::declarations::DeclareModuleExports),
58    DeclareExportDeclaration(crate::declarations::DeclareExportDeclaration),
59    DeclareExportAllDeclaration(crate::declarations::DeclareExportAllDeclaration),
60    DeclareInterface(crate::declarations::DeclareInterface),
61    DeclareTypeAlias(crate::declarations::DeclareTypeAlias),
62    DeclareOpaqueType(crate::declarations::DeclareOpaqueType),
63    EnumDeclaration(crate::declarations::EnumDeclaration),
64}
65
66#[derive(Debug, Clone, Serialize, Deserialize)]
67pub struct BlockStatement {
68    #[serde(flatten)]
69    pub base: BaseNode,
70    pub body: Vec<Statement>,
71    #[serde(default)]
72    pub directives: Vec<Directive>,
73}
74
75#[derive(Debug, Clone, Serialize, Deserialize)]
76pub struct Directive {
77    #[serde(flatten)]
78    pub base: BaseNode,
79    pub value: DirectiveLiteral,
80}
81
82#[derive(Debug, Clone, Serialize, Deserialize)]
83pub struct DirectiveLiteral {
84    #[serde(flatten)]
85    pub base: BaseNode,
86    pub value: String,
87}
88
89#[derive(Debug, Clone, Serialize, Deserialize)]
90pub struct ReturnStatement {
91    #[serde(flatten)]
92    pub base: BaseNode,
93    pub argument: Option<Box<Expression>>,
94}
95
96#[derive(Debug, Clone, Serialize, Deserialize)]
97pub struct ExpressionStatement {
98    #[serde(flatten)]
99    pub base: BaseNode,
100    pub expression: Box<Expression>,
101}
102
103#[derive(Debug, Clone, Serialize, Deserialize)]
104pub struct IfStatement {
105    #[serde(flatten)]
106    pub base: BaseNode,
107    pub test: Box<Expression>,
108    pub consequent: Box<Statement>,
109    pub alternate: Option<Box<Statement>>,
110}
111
112#[derive(Debug, Clone, Serialize, Deserialize)]
113pub struct ForStatement {
114    #[serde(flatten)]
115    pub base: BaseNode,
116    pub init: Option<Box<ForInit>>,
117    pub test: Option<Box<Expression>>,
118    pub update: Option<Box<Expression>>,
119    pub body: Box<Statement>,
120}
121
122#[derive(Debug, Clone, Serialize, Deserialize)]
123#[serde(tag = "type")]
124pub enum ForInit {
125    VariableDeclaration(VariableDeclaration),
126    #[serde(untagged)]
127    Expression(Box<Expression>),
128}
129
130#[derive(Debug, Clone, Serialize, Deserialize)]
131pub struct WhileStatement {
132    #[serde(flatten)]
133    pub base: BaseNode,
134    pub test: Box<Expression>,
135    pub body: Box<Statement>,
136}
137
138#[derive(Debug, Clone, Serialize, Deserialize)]
139pub struct DoWhileStatement {
140    #[serde(flatten)]
141    pub base: BaseNode,
142    pub test: Box<Expression>,
143    pub body: Box<Statement>,
144}
145
146#[derive(Debug, Clone, Serialize, Deserialize)]
147pub struct ForInStatement {
148    #[serde(flatten)]
149    pub base: BaseNode,
150    pub left: Box<ForInOfLeft>,
151    pub right: Box<Expression>,
152    pub body: Box<Statement>,
153}
154
155#[derive(Debug, Clone, Serialize, Deserialize)]
156pub struct ForOfStatement {
157    #[serde(flatten)]
158    pub base: BaseNode,
159    pub left: Box<ForInOfLeft>,
160    pub right: Box<Expression>,
161    pub body: Box<Statement>,
162    #[serde(default, rename = "await")]
163    pub is_await: bool,
164}
165
166#[derive(Debug, Clone, Serialize, Deserialize)]
167#[serde(tag = "type")]
168pub enum ForInOfLeft {
169    VariableDeclaration(VariableDeclaration),
170    #[serde(untagged)]
171    Pattern(Box<PatternLike>),
172}
173
174#[derive(Debug, Clone, Serialize, Deserialize)]
175pub struct SwitchStatement {
176    #[serde(flatten)]
177    pub base: BaseNode,
178    pub discriminant: Box<Expression>,
179    pub cases: Vec<SwitchCase>,
180}
181
182#[derive(Debug, Clone, Serialize, Deserialize)]
183pub struct SwitchCase {
184    #[serde(flatten)]
185    pub base: BaseNode,
186    pub test: Option<Box<Expression>>,
187    pub consequent: Vec<Statement>,
188}
189
190#[derive(Debug, Clone, Serialize, Deserialize)]
191pub struct ThrowStatement {
192    #[serde(flatten)]
193    pub base: BaseNode,
194    pub argument: Box<Expression>,
195}
196
197#[derive(Debug, Clone, Serialize, Deserialize)]
198pub struct TryStatement {
199    #[serde(flatten)]
200    pub base: BaseNode,
201    pub block: BlockStatement,
202    pub handler: Option<CatchClause>,
203    pub finalizer: Option<BlockStatement>,
204}
205
206#[derive(Debug, Clone, Serialize, Deserialize)]
207pub struct CatchClause {
208    #[serde(flatten)]
209    pub base: BaseNode,
210    pub param: Option<PatternLike>,
211    pub body: BlockStatement,
212}
213
214#[derive(Debug, Clone, Serialize, Deserialize)]
215pub struct BreakStatement {
216    #[serde(flatten)]
217    pub base: BaseNode,
218    pub label: Option<Identifier>,
219}
220
221#[derive(Debug, Clone, Serialize, Deserialize)]
222pub struct ContinueStatement {
223    #[serde(flatten)]
224    pub base: BaseNode,
225    pub label: Option<Identifier>,
226}
227
228#[derive(Debug, Clone, Serialize, Deserialize)]
229pub struct LabeledStatement {
230    #[serde(flatten)]
231    pub base: BaseNode,
232    pub label: Identifier,
233    pub body: Box<Statement>,
234}
235
236#[derive(Debug, Clone, Serialize, Deserialize)]
237pub struct EmptyStatement {
238    #[serde(flatten)]
239    pub base: BaseNode,
240}
241
242#[derive(Debug, Clone, Serialize, Deserialize)]
243pub struct DebuggerStatement {
244    #[serde(flatten)]
245    pub base: BaseNode,
246}
247
248#[derive(Debug, Clone, Serialize, Deserialize)]
249pub struct WithStatement {
250    #[serde(flatten)]
251    pub base: BaseNode,
252    pub object: Box<Expression>,
253    pub body: Box<Statement>,
254}
255
256#[derive(Debug, Clone, Serialize, Deserialize)]
257pub struct VariableDeclaration {
258    #[serde(flatten)]
259    pub base: BaseNode,
260    pub declarations: Vec<VariableDeclarator>,
261    pub kind: VariableDeclarationKind,
262    #[serde(default, skip_serializing_if = "Option::is_none")]
263    pub declare: Option<bool>,
264}
265
266#[derive(Debug, Clone, Serialize, Deserialize)]
267#[serde(rename_all = "lowercase")]
268pub enum VariableDeclarationKind {
269    Var,
270    Let,
271    Const,
272    Using,
273}
274
275#[derive(Debug, Clone, Serialize, Deserialize)]
276pub struct VariableDeclarator {
277    #[serde(flatten)]
278    pub base: BaseNode,
279    pub id: PatternLike,
280    pub init: Option<Box<Expression>>,
281    #[serde(default, skip_serializing_if = "Option::is_none")]
282    pub definite: Option<bool>,
283}
284
285#[derive(Debug, Clone, Serialize, Deserialize)]
286pub struct FunctionDeclaration {
287    #[serde(flatten)]
288    pub base: BaseNode,
289    pub id: Option<Identifier>,
290    pub params: Vec<PatternLike>,
291    pub body: BlockStatement,
292    #[serde(default)]
293    pub generator: bool,
294    #[serde(default, rename = "async")]
295    pub is_async: bool,
296    #[serde(default, skip_serializing_if = "Option::is_none")]
297    pub declare: Option<bool>,
298    #[serde(
299        default,
300        skip_serializing_if = "Option::is_none",
301        rename = "returnType"
302    )]
303    pub return_type: Option<Box<serde_json::Value>>,
304    #[serde(
305        default,
306        skip_serializing_if = "Option::is_none",
307        rename = "typeParameters"
308    )]
309    pub type_parameters: Option<Box<serde_json::Value>>,
310    #[serde(
311        default,
312        skip_serializing_if = "Option::is_none",
313        rename = "predicate"
314    )]
315    pub predicate: Option<Box<serde_json::Value>>,
316    /// Set by the Hermes parser for Flow `component Foo(...) { ... }` syntax
317    #[serde(
318        default,
319        skip_serializing_if = "is_false",
320        rename = "__componentDeclaration"
321    )]
322    pub component_declaration: bool,
323    /// Set by the Hermes parser for Flow `hook useFoo(...) { ... }` syntax
324    #[serde(
325        default,
326        skip_serializing_if = "is_false",
327        rename = "__hookDeclaration"
328    )]
329    pub hook_declaration: bool,
330}
331
332#[derive(Debug, Clone, Serialize, Deserialize)]
333pub struct ClassDeclaration {
334    #[serde(flatten)]
335    pub base: BaseNode,
336    pub id: Option<Identifier>,
337    #[serde(rename = "superClass")]
338    pub super_class: Option<Box<Expression>>,
339    pub body: crate::expressions::ClassBody,
340    #[serde(default, skip_serializing_if = "Option::is_none")]
341    pub decorators: Option<Vec<serde_json::Value>>,
342    #[serde(default, skip_serializing_if = "Option::is_none", rename = "abstract")]
343    pub is_abstract: Option<bool>,
344    #[serde(default, skip_serializing_if = "Option::is_none")]
345    pub declare: Option<bool>,
346    #[serde(
347        default,
348        skip_serializing_if = "Option::is_none",
349        rename = "implements"
350    )]
351    pub implements: Option<Vec<serde_json::Value>>,
352    #[serde(
353        default,
354        skip_serializing_if = "Option::is_none",
355        rename = "superTypeParameters"
356    )]
357    pub super_type_parameters: Option<Box<serde_json::Value>>,
358    #[serde(
359        default,
360        skip_serializing_if = "Option::is_none",
361        rename = "typeParameters"
362    )]
363    pub type_parameters: Option<Box<serde_json::Value>>,
364    #[serde(default, skip_serializing_if = "Option::is_none")]
365    pub mixins: Option<Vec<serde_json::Value>>,
366}