Skip to main content

oak_typescript/ast/
statement_nodes.rs

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