Skip to main content

oak_typescript/ast/
statement_nodes.rs

1use crate::ast::{ClassMember, Decorator, EnumMember, Expression, TypeAnnotation, TypeParameter};
2use core::range::Range;
3
4/// Represents a TypeScript statement.
5#[derive(Debug, Clone)]
6#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
7pub enum Statement {
8    /// Variable declaration.
9    VariableDeclaration(VariableDeclaration),
10    /// Function declaration.
11    FunctionDeclaration(FunctionDeclaration),
12    /// Class declaration.
13    ClassDeclaration(ClassDeclaration),
14    /// Expression statement.
15    ExpressionStatement(ExpressionStatement),
16    /// Import declaration.
17    ImportDeclaration(ImportDeclaration),
18    /// Export declaration.
19    ExportDeclaration(ExportDeclaration),
20    /// Interface declaration.
21    Interface(InterfaceDeclaration),
22    /// Type alias declaration.
23    TypeAlias(TypeAliasDeclaration),
24    /// Enum declaration.
25    Enum(EnumDeclaration),
26    /// Return statement.
27    ReturnStatement(ReturnStatement),
28    /// If statement.
29    IfStatement(IfStatement),
30    /// While statement.
31    WhileStatement(WhileStatement),
32    /// Do-while statement.
33    DoWhileStatement(DoWhileStatement),
34    /// For statement.
35    ForStatement(ForStatement),
36    /// For-in statement.
37    ForInStatement(ForInStatement),
38    /// For-of statement.
39    ForOfStatement(ForOfStatement),
40    /// Switch statement.
41    SwitchStatement(SwitchStatement),
42    /// Try statement.
43    TryStatement(TryStatement),
44    /// Throw statement.
45    ThrowStatement(ThrowStatement),
46    /// Break statement.
47    BreakStatement(BreakStatement),
48    /// Continue statement.
49    ContinueStatement(ContinueStatement),
50    /// Block statement.
51    BlockStatement(BlockStatement),
52    /// Namespace declaration.
53    Namespace(NamespaceDeclaration),
54}
55
56impl Statement {
57    /// Gets the span of the statement.
58    pub fn span(&self) -> Range<usize> {
59        match self {
60            Statement::VariableDeclaration(d) => d.span.clone(),
61            Statement::FunctionDeclaration(d) => d.span.clone(),
62            Statement::ClassDeclaration(d) => d.span.clone(),
63            Statement::ExpressionStatement(s) => s.span.clone(),
64            Statement::ImportDeclaration(d) => d.span.clone(),
65            Statement::ExportDeclaration(d) => d.span.clone(),
66            Statement::Interface(d) => d.span.clone(),
67            Statement::TypeAlias(d) => d.span.clone(),
68            Statement::Enum(d) => d.span.clone(),
69            Statement::ReturnStatement(s) => s.span.clone(),
70            Statement::IfStatement(s) => s.span.clone(),
71            Statement::WhileStatement(s) => s.span.clone(),
72            Statement::DoWhileStatement(s) => s.span.clone(),
73            Statement::ForStatement(s) => s.span.clone(),
74            Statement::ForInStatement(s) => s.span.clone(),
75            Statement::ForOfStatement(s) => s.span.clone(),
76            Statement::SwitchStatement(s) => s.span.clone(),
77            Statement::TryStatement(s) => s.span.clone(),
78            Statement::ThrowStatement(s) => s.span.clone(),
79            Statement::BreakStatement(s) => s.span.clone(),
80            Statement::ContinueStatement(s) => s.span.clone(),
81            Statement::BlockStatement(s) => s.span.clone(),
82            Statement::Namespace(s) => s.span.clone(),
83        }
84    }
85}
86
87/// Represents an expression statement.
88#[derive(Debug, Clone)]
89#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
90pub struct ExpressionStatement {
91    /// Decorators associated with the statement.
92    pub decorators: Vec<Decorator>,
93    /// Whether the statement is declared with `declare`.
94    pub is_declare: bool,
95    /// The expression being evaluated.
96    pub expression: Expression,
97    /// Source span of the statement.
98    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
99    pub span: Range<usize>,
100}
101
102/// Represents a return statement.
103#[derive(Debug, Clone)]
104#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
105pub struct ReturnStatement {
106    /// Decorators associated with the statement.
107    pub decorators: Vec<Decorator>,
108    /// Whether the statement is declared with `declare`.
109    pub is_declare: bool,
110    /// The expression being returned, if any.
111    pub argument: Option<Expression>,
112    /// Source span of the statement.
113    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
114    pub span: Range<usize>,
115}
116
117/// Represents a throw statement.
118#[derive(Debug, Clone)]
119#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
120pub struct ThrowStatement {
121    /// Decorators associated with the statement.
122    pub decorators: Vec<Decorator>,
123    /// Whether the statement is declared with `declare`.
124    pub is_declare: bool,
125    /// The expression being thrown.
126    pub argument: Expression,
127    /// Source span of the statement.
128    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
129    pub span: Range<usize>,
130}
131
132/// Represents a break statement.
133#[derive(Debug, Clone)]
134#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
135pub struct BreakStatement {
136    /// Decorators associated with the statement.
137    pub decorators: Vec<Decorator>,
138    /// Whether the statement is declared with `declare`.
139    pub is_declare: bool,
140    /// The label being broken to, if any.
141    pub label: Option<String>,
142    /// Source span of the statement.
143    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
144    pub span: Range<usize>,
145}
146
147/// Represents a continue statement.
148#[derive(Debug, Clone)]
149#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
150pub struct ContinueStatement {
151    /// Decorators associated with the statement.
152    pub decorators: Vec<Decorator>,
153    /// Whether the statement is declared with `declare`.
154    pub is_declare: bool,
155    /// The label being continued to, if any.
156    pub label: Option<String>,
157    /// Source span of the statement.
158    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
159    pub span: Range<usize>,
160}
161
162/// Represents a namespace declaration.
163#[derive(Debug, Clone)]
164#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
165pub struct NamespaceDeclaration {
166    /// Decorators associated with the declaration.
167    pub decorators: Vec<Decorator>,
168    /// Whether the declaration is declared with `declare`.
169    pub is_declare: bool,
170    /// The name of the namespace.
171    pub name: String,
172    /// The statements within the namespace.
173    pub body: Vec<Statement>,
174    /// Source span of the declaration.
175    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
176    pub span: Range<usize>,
177}
178
179/// Represents an interface declaration.
180#[derive(Debug, Clone)]
181#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
182pub struct InterfaceDeclaration {
183    /// Decorators associated with the declaration.
184    pub decorators: Vec<Decorator>,
185    /// Whether the declaration is declared with `declare`.
186    pub is_declare: bool,
187    /// The name of the interface.
188    pub name: String,
189    /// Type parameters of the interface.
190    pub type_params: Vec<TypeParameter>,
191    /// Types that this interface extends.
192    pub extends: Vec<TypeAnnotation>,
193    /// Members of the interface.
194    pub body: Vec<ClassMember>,
195    /// Source span of the declaration.
196    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
197    pub span: Range<usize>,
198}
199
200/// Represents a type alias declaration.
201#[derive(Debug, Clone)]
202#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
203pub struct TypeAliasDeclaration {
204    /// Decorators associated with the declaration.
205    pub decorators: Vec<Decorator>,
206    /// Whether the declaration is declared with `declare`.
207    pub is_declare: bool,
208    /// The name of the type alias.
209    pub name: String,
210    /// Type parameters of the type alias.
211    pub type_params: Vec<TypeParameter>,
212    /// The type being aliased.
213    pub ty: TypeAnnotation,
214    /// Source span of the declaration.
215    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
216    pub span: Range<usize>,
217}
218
219/// Represents an enum declaration.
220#[derive(Debug, Clone)]
221#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
222pub struct EnumDeclaration {
223    /// Decorators associated with the declaration.
224    pub decorators: Vec<Decorator>,
225    /// Whether the declaration is declared with `declare`.
226    pub is_declare: bool,
227    /// The name of the enum.
228    pub name: String,
229    /// Members of the enum.
230    pub members: Vec<EnumMember>,
231    /// Source span of the declaration.
232    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
233    pub span: Range<usize>,
234}
235
236/// Represents an if statement.
237#[derive(Debug, Clone)]
238#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
239pub struct IfStatement {
240    /// Decorators associated with the statement.
241    pub decorators: Vec<Decorator>,
242    /// Whether the statement is declared with `declare`.
243    pub is_declare: bool,
244    /// The condition being tested.
245    pub test: Expression,
246    /// The statement to execute if the condition is true.
247    pub consequent: Box<Statement>,
248    /// The statement to execute if the condition is false.
249    pub alternate: Option<Box<Statement>>,
250    /// Source span of the statement.
251    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
252    pub span: Range<usize>,
253}
254
255/// Represents a while statement.
256#[derive(Debug, Clone)]
257#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
258pub struct WhileStatement {
259    /// Decorators associated with the statement.
260    pub decorators: Vec<Decorator>,
261    /// Whether the statement is declared with `declare`.
262    pub is_declare: bool,
263    /// The condition being tested.
264    pub test: Expression,
265    /// The statement to execute as long as the condition is true.
266    pub body: Box<Statement>,
267    /// Source span of the statement.
268    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
269    pub span: Range<usize>,
270}
271
272/// Represents a do-while statement.
273#[derive(Debug, Clone)]
274#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
275pub struct DoWhileStatement {
276    /// Decorators associated with the statement.
277    pub decorators: Vec<Decorator>,
278    /// Whether the statement is declared with `declare`.
279    pub is_declare: bool,
280    /// The statement to execute at least once.
281    pub body: Box<Statement>,
282    /// The condition being tested after each iteration.
283    pub test: Expression,
284    /// Source span of the statement.
285    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
286    pub span: Range<usize>,
287}
288
289/// Represents a for statement.
290#[derive(Debug, Clone)]
291#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
292pub struct ForStatement {
293    /// Decorators associated with the statement.
294    pub decorators: Vec<Decorator>,
295    /// Whether the statement is declared with `declare`.
296    pub is_declare: bool,
297    /// The initialization statement.
298    pub initializer: Option<Box<Statement>>,
299    /// The condition being tested before each iteration.
300    pub test: Option<Expression>,
301    /// The expression being evaluated after each iteration.
302    pub incrementor: Option<Expression>,
303    /// The statement to execute in each iteration.
304    pub body: Box<Statement>,
305    /// Source span of the statement.
306    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
307    pub span: Range<usize>,
308}
309
310/// Represents a for-in statement.
311#[derive(Debug, Clone)]
312#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
313pub struct ForInStatement {
314    /// Decorators associated with the statement.
315    pub decorators: Vec<Decorator>,
316    /// Whether the statement is declared with `declare`.
317    pub is_declare: bool,
318    /// The left-hand side of the for-in loop.
319    pub left: Box<Statement>,
320    /// The expression being iterated over.
321    pub right: Expression,
322    /// The statement to execute in each iteration.
323    pub body: Box<Statement>,
324    /// Source span of the statement.
325    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
326    pub span: Range<usize>,
327}
328
329/// Represents a for-of statement.
330#[derive(Debug, Clone)]
331#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
332pub struct ForOfStatement {
333    /// Decorators associated with the statement.
334    pub decorators: Vec<Decorator>,
335    /// Whether the statement is declared with `declare`.
336    pub is_declare: bool,
337    /// The left-hand side of the for-of loop.
338    pub left: Box<Statement>,
339    /// The expression being iterated over.
340    pub right: Expression,
341    /// The statement to execute in each iteration.
342    pub body: Box<Statement>,
343    /// Source span of the statement.
344    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
345    pub span: Range<usize>,
346}
347
348/// Represents a switch statement.
349#[derive(Debug, Clone)]
350#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
351pub struct SwitchStatement {
352    /// Decorators associated with the statement.
353    pub decorators: Vec<Decorator>,
354    /// Whether the statement is declared with `declare`.
355    pub is_declare: bool,
356    /// The expression being switched on.
357    pub discriminant: Expression,
358    /// Cases within the switch statement.
359    pub cases: Vec<SwitchCase>,
360    /// Source span of the statement.
361    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
362    pub span: Range<usize>,
363}
364
365/// Represents a case within a switch statement.
366#[derive(Debug, Clone)]
367#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
368pub struct SwitchCase {
369    /// The condition for this case. `None` for the default case.
370    pub test: Option<Expression>,
371    /// The statements to execute if the condition matches.
372    pub consequent: Vec<Statement>,
373    /// Source span of the case.
374    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
375    pub span: Range<usize>,
376}
377
378/// Represents a try statement.
379#[derive(Debug, Clone)]
380#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
381pub struct TryStatement {
382    /// Decorators associated with the statement.
383    pub decorators: Vec<Decorator>,
384    /// Whether the statement is declared with `declare`.
385    pub is_declare: bool,
386    /// The try block.
387    pub block: Vec<Statement>,
388    /// The catch clause.
389    pub handler: Option<CatchClause>,
390    /// The finally block.
391    pub finalizer: Option<Vec<Statement>>,
392    /// Source span of the statement.
393    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
394    pub span: Range<usize>,
395}
396
397/// Represents a catch clause within a try statement.
398#[derive(Debug, Clone)]
399#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
400pub struct CatchClause {
401    /// The parameter of the catch clause.
402    pub param: Option<String>,
403    /// The catch block.
404    pub body: Vec<Statement>,
405    /// Source span of the catch clause.
406    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
407    pub span: Range<usize>,
408}
409
410/// Represents a block statement.
411#[derive(Debug, Clone)]
412#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
413pub struct BlockStatement {
414    /// Decorators associated with the statement.
415    pub decorators: Vec<Decorator>,
416    /// Whether the statement is declared with `declare`.
417    pub is_declare: bool,
418    /// The statements within the block.
419    pub statements: Vec<Statement>,
420    /// Source span of the block statement.
421    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
422    pub span: Range<usize>,
423}
424
425/// Represents an import specifier.
426#[derive(Debug, Clone)]
427#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
428pub enum ImportSpecifier {
429    /// Default import: `import local from "source"`.
430    Default(String),
431    /// Namespace import: `import * as local from "source"`.
432    Namespace(String),
433    /// Named import: `import { local as imported } from "source"`.
434    Named {
435        /// Local name.
436        local: String,
437        /// Imported name.
438        imported: String,
439    },
440}
441
442/// Represents an export specifier.
443#[derive(Debug, Clone)]
444#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
445pub struct ExportSpecifier {
446    /// Local name.
447    pub local: String,
448    /// Exported name.
449    pub exported: String,
450}
451
452/// Represents an import declaration.
453#[derive(Debug, Clone)]
454#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
455pub struct ImportDeclaration {
456    /// The module specifier being imported from.
457    pub module_specifier: String,
458    /// The specifiers within the import.
459    pub specifiers: Vec<ImportSpecifier>,
460    /// Whether the import is type-only.
461    pub is_type_only: bool,
462    /// Source span of the declaration.
463    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
464    pub span: Range<usize>,
465}
466
467/// Represents an export declaration.
468#[derive(Debug, Clone)]
469#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
470pub struct ExportDeclaration {
471    /// The declaration being exported.
472    pub declaration: Option<Box<Statement>>,
473    /// The specifiers within the export.
474    pub specifiers: Vec<ExportSpecifier>,
475    /// The module specifier being exported from.
476    pub source: Option<String>,
477    /// Whether this is a default export.
478    pub is_default: bool,
479    /// Whether the export is type-only.
480    pub is_type_only: bool,
481    /// Source span of the declaration.
482    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
483    pub span: Range<usize>,
484}
485
486/// Represents a variable declaration.
487#[derive(Debug, Clone)]
488#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
489pub struct VariableDeclaration {
490    /// Decorators associated with the declaration.
491    pub decorators: Vec<Decorator>,
492    /// Whether the declaration is declared with `declare`.
493    pub is_declare: bool,
494    /// The name of the variable.
495    pub name: String,
496    /// The type annotation of the variable.
497    pub ty: Option<TypeAnnotation>,
498    /// The initial value of the variable.
499    pub value: Option<Expression>,
500    /// Source span of the declaration.
501    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
502    pub span: Range<usize>,
503}
504
505/// Represents a function declaration.
506#[derive(Debug, Clone)]
507#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
508pub struct FunctionDeclaration {
509    /// Decorators associated with the declaration.
510    pub decorators: Vec<Decorator>,
511    /// Whether the declaration is declared with `declare`.
512    pub is_declare: bool,
513    /// The name of the function.
514    pub name: String,
515    /// Type parameters of the function.
516    pub type_params: Vec<TypeParameter>,
517    /// Parameters of the function.
518    pub params: Vec<FunctionParam>,
519    /// Return type of the function.
520    pub return_type: Option<TypeAnnotation>,
521    /// Body of the function.
522    pub body: Vec<Statement>,
523    /// Source span of the declaration.
524    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
525    pub span: Range<usize>,
526}
527
528/// Represents a class declaration.
529#[derive(Debug, Clone)]
530#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
531pub struct ClassDeclaration {
532    /// Decorators associated with the declaration.
533    pub decorators: Vec<Decorator>,
534    /// Whether the declaration is declared with `declare`.
535    pub is_declare: bool,
536    /// The name of the class.
537    pub name: String,
538    /// Type parameters of the class.
539    pub type_params: Vec<TypeParameter>,
540    /// The class that this class extends.
541    pub extends: Option<TypeAnnotation>,
542    /// The interfaces that this class implements.
543    pub implements: Vec<TypeAnnotation>,
544    /// Whether the class is abstract.
545    pub is_abstract: bool,
546    /// Members of the class.
547    pub body: Vec<ClassMember>,
548    /// Source span of the declaration.
549    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
550    pub span: Range<usize>,
551}
552
553/// Represents a parameter in a function declaration.
554#[derive(Debug, Clone)]
555#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
556pub struct FunctionParam {
557    /// Decorators associated with the parameter.
558    pub decorators: Vec<Decorator>,
559    /// The name of the parameter.
560    pub name: String,
561    /// The type annotation of the parameter.
562    pub ty: Option<TypeAnnotation>,
563    /// Whether the parameter is optional.
564    pub optional: bool,
565    /// Source span of the parameter.
566    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
567    pub span: Range<usize>,
568}