Skip to main content

oak_typescript/ast/
statement_node.rs

1use crate::ast::{ClassMember, Decorator, EnumMember, Expression, TypeAnnotation, TypeParameter};
2use core::range::Range;
3#[cfg(feature = "serde")]
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Clone)]
7#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
8pub enum Statement {
9    VariableDeclaration(VariableDeclaration),
10    FunctionDeclaration(FunctionDeclaration),
11    ClassDeclaration(ClassDeclaration),
12    ExpressionStatement(ExpressionStatement),
13    ImportDeclaration(ImportDeclaration),
14    ExportDeclaration(ExportDeclaration),
15    Interface(InterfaceDeclaration),
16    TypeAlias(TypeAliasDeclaration),
17    Enum(EnumDeclaration),
18    ReturnStatement(ReturnStatement),
19    IfStatement(IfStatement),
20    WhileStatement(WhileStatement),
21    DoWhileStatement(DoWhileStatement),
22    ForStatement(ForStatement),
23    ForInStatement(ForInStatement),
24    ForOfStatement(ForOfStatement),
25    SwitchStatement(SwitchStatement),
26    TryStatement(TryStatement),
27    ThrowStatement(ThrowStatement),
28    BreakStatement(BreakStatement),
29    ContinueStatement(ContinueStatement),
30    BlockStatement(BlockStatement),
31    Namespace(NamespaceDeclaration),
32}
33
34impl Statement {
35    pub fn span(&self) -> Range<usize> {
36        match self {
37            Statement::VariableDeclaration(d) => d.span.clone(),
38            Statement::FunctionDeclaration(d) => d.span.clone(),
39            Statement::ClassDeclaration(d) => d.span.clone(),
40            Statement::ExpressionStatement(s) => s.span.clone(),
41            Statement::ImportDeclaration(d) => d.span.clone(),
42            Statement::ExportDeclaration(d) => d.span.clone(),
43            Statement::Interface(d) => d.span.clone(),
44            Statement::TypeAlias(d) => d.span.clone(),
45            Statement::Enum(d) => d.span.clone(),
46            Statement::ReturnStatement(s) => s.span.clone(),
47            Statement::IfStatement(s) => s.span.clone(),
48            Statement::WhileStatement(s) => s.span.clone(),
49            Statement::DoWhileStatement(s) => s.span.clone(),
50            Statement::ForStatement(s) => s.span.clone(),
51            Statement::ForInStatement(s) => s.span.clone(),
52            Statement::ForOfStatement(s) => s.span.clone(),
53            Statement::SwitchStatement(s) => s.span.clone(),
54            Statement::TryStatement(s) => s.span.clone(),
55            Statement::ThrowStatement(s) => s.span.clone(),
56            Statement::BreakStatement(s) => s.span.clone(),
57            Statement::ContinueStatement(s) => s.span.clone(),
58            Statement::BlockStatement(s) => s.span.clone(),
59            Statement::Namespace(s) => s.span.clone(),
60        }
61    }
62}
63
64#[derive(Debug, Clone)]
65#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
66pub struct ExpressionStatement {
67    pub decorators: Vec<Decorator>,
68    pub is_declare: bool,
69    pub expression: Expression,
70    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
71    pub span: Range<usize>,
72}
73
74#[derive(Debug, Clone)]
75#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
76pub struct ReturnStatement {
77    pub decorators: Vec<Decorator>,
78    pub is_declare: bool,
79    pub argument: Option<Expression>,
80    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
81    pub span: Range<usize>,
82}
83
84#[derive(Debug, Clone)]
85#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
86pub struct ThrowStatement {
87    pub decorators: Vec<Decorator>,
88    pub is_declare: bool,
89    pub argument: Expression,
90    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
91    pub span: Range<usize>,
92}
93
94#[derive(Debug, Clone)]
95#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
96pub struct BreakStatement {
97    pub decorators: Vec<Decorator>,
98    pub is_declare: bool,
99    pub label: Option<String>,
100    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
101    pub span: Range<usize>,
102}
103
104#[derive(Debug, Clone)]
105#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
106pub struct ContinueStatement {
107    pub decorators: Vec<Decorator>,
108    pub is_declare: bool,
109    pub label: Option<String>,
110    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
111    pub span: Range<usize>,
112}
113
114#[derive(Debug, Clone)]
115#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
116pub struct NamespaceDeclaration {
117    pub decorators: Vec<Decorator>,
118    pub is_declare: bool,
119    pub name: String,
120    pub body: Vec<Statement>,
121    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
122    pub span: Range<usize>,
123}
124
125#[derive(Debug, Clone)]
126#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
127pub struct InterfaceDeclaration {
128    pub decorators: Vec<Decorator>,
129    pub is_declare: bool,
130    pub name: String,
131    pub type_params: Vec<TypeParameter>,
132    pub extends: Vec<TypeAnnotation>,
133    pub body: Vec<ClassMember>,
134    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
135    pub span: Range<usize>,
136}
137
138#[derive(Debug, Clone)]
139#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
140pub struct TypeAliasDeclaration {
141    pub decorators: Vec<Decorator>,
142    pub is_declare: bool,
143    pub name: String,
144    pub type_params: Vec<TypeParameter>,
145    pub ty: TypeAnnotation,
146    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
147    pub span: Range<usize>,
148}
149
150#[derive(Debug, Clone)]
151#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
152pub struct EnumDeclaration {
153    pub decorators: Vec<Decorator>,
154    pub is_declare: bool,
155    pub name: String,
156    pub members: Vec<EnumMember>,
157    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
158    pub span: Range<usize>,
159}
160
161#[derive(Debug, Clone)]
162#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
163pub struct IfStatement {
164    pub decorators: Vec<Decorator>,
165    pub is_declare: bool,
166    pub test: Expression,
167    pub consequent: Box<Statement>,
168    pub alternate: Option<Box<Statement>>,
169    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
170    pub span: Range<usize>,
171}
172
173#[derive(Debug, Clone)]
174#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
175pub struct WhileStatement {
176    pub decorators: Vec<Decorator>,
177    pub is_declare: bool,
178    pub test: Expression,
179    pub body: Box<Statement>,
180    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
181    pub span: Range<usize>,
182}
183
184#[derive(Debug, Clone)]
185#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
186pub struct DoWhileStatement {
187    pub decorators: Vec<Decorator>,
188    pub is_declare: bool,
189    pub body: Box<Statement>,
190    pub test: Expression,
191    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
192    pub span: Range<usize>,
193}
194
195#[derive(Debug, Clone)]
196#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
197pub struct ForStatement {
198    pub decorators: Vec<Decorator>,
199    pub is_declare: bool,
200    pub initializer: Option<Box<Statement>>,
201    pub test: Option<Expression>,
202    pub incrementor: Option<Expression>,
203    pub body: Box<Statement>,
204    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
205    pub span: Range<usize>,
206}
207
208#[derive(Debug, Clone)]
209#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
210pub struct ForInStatement {
211    pub decorators: Vec<Decorator>,
212    pub is_declare: bool,
213    pub left: Box<Statement>,
214    pub right: Expression,
215    pub body: Box<Statement>,
216    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
217    pub span: Range<usize>,
218}
219
220#[derive(Debug, Clone)]
221#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
222pub struct ForOfStatement {
223    pub decorators: Vec<Decorator>,
224    pub is_declare: bool,
225    pub left: Box<Statement>,
226    pub right: Expression,
227    pub body: Box<Statement>,
228    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
229    pub span: Range<usize>,
230}
231
232#[derive(Debug, Clone)]
233#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
234pub struct SwitchStatement {
235    pub decorators: Vec<Decorator>,
236    pub is_declare: bool,
237    pub discriminant: Expression,
238    pub cases: Vec<SwitchCase>,
239    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
240    pub span: Range<usize>,
241}
242
243#[derive(Debug, Clone)]
244#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
245pub struct SwitchCase {
246    pub test: Option<Expression>,
247    pub consequent: Vec<Statement>,
248    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
249    pub span: Range<usize>,
250}
251
252#[derive(Debug, Clone)]
253#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
254pub struct TryStatement {
255    pub decorators: Vec<Decorator>,
256    pub is_declare: bool,
257    pub block: Vec<Statement>,
258    pub handler: Option<CatchClause>,
259    pub finalizer: Option<Vec<Statement>>,
260    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
261    pub span: Range<usize>,
262}
263
264#[derive(Debug, Clone)]
265#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
266pub struct CatchClause {
267    pub param: Option<String>,
268    pub body: Vec<Statement>,
269    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
270    pub span: Range<usize>,
271}
272
273#[derive(Debug, Clone)]
274#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
275pub struct BlockStatement {
276    pub decorators: Vec<Decorator>,
277    pub is_declare: bool,
278    pub statements: Vec<Statement>,
279    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
280    pub span: Range<usize>,
281}
282
283#[derive(Debug, Clone)]
284#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
285pub enum ImportSpecifier {
286    Default(String),
287    Namespace(String),
288    Named { local: String, imported: String },
289}
290
291#[derive(Debug, Clone)]
292#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
293pub struct ExportSpecifier {
294    pub local: String,
295    pub exported: String,
296}
297
298#[derive(Debug, Clone)]
299#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
300pub struct ImportDeclaration {
301    pub module_specifier: String,
302    pub specifiers: Vec<ImportSpecifier>,
303    pub is_type_only: bool,
304    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
305    pub span: Range<usize>,
306}
307
308#[derive(Debug, Clone)]
309#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
310pub struct ExportDeclaration {
311    pub declaration: Option<Box<Statement>>,
312    pub specifiers: Vec<ExportSpecifier>,
313    pub source: Option<String>,
314    pub is_default: bool,
315    pub is_type_only: bool,
316    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
317    pub span: Range<usize>,
318}
319
320#[derive(Debug, Clone)]
321#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
322pub struct VariableDeclaration {
323    pub decorators: Vec<Decorator>,
324    pub is_declare: bool,
325    pub name: String,
326    pub ty: Option<TypeAnnotation>,
327    pub value: Option<Expression>,
328    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
329    pub span: Range<usize>,
330}
331
332#[derive(Debug, Clone)]
333#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
334pub struct FunctionDeclaration {
335    pub decorators: Vec<Decorator>,
336    pub is_declare: bool,
337    pub name: String,
338    pub type_params: Vec<TypeParameter>,
339    pub params: Vec<FunctionParam>,
340    pub return_type: Option<TypeAnnotation>,
341    pub body: Vec<Statement>,
342    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
343    pub span: Range<usize>,
344}
345
346#[derive(Debug, Clone)]
347#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
348pub struct ClassDeclaration {
349    pub decorators: Vec<Decorator>,
350    pub is_declare: bool,
351    pub name: String,
352    pub type_params: Vec<TypeParameter>,
353    pub extends: Option<TypeAnnotation>,
354    pub implements: Vec<TypeAnnotation>,
355    pub is_abstract: bool,
356    pub body: Vec<ClassMember>,
357    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
358    pub span: Range<usize>,
359}
360
361#[derive(Debug, Clone)]
362#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
363pub struct FunctionParam {
364    pub decorators: Vec<Decorator>,
365    pub name: String,
366    pub ty: Option<TypeAnnotation>,
367    pub optional: bool,
368    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
369    pub span: Range<usize>,
370}