1use crate::ast::{ClassMember, Decorator, EnumMember, Expression, TypeAnnotation, TypeParameter};
2use core::range::Range;
3
4#[derive(Debug, Clone)]
6#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
7pub enum Statement {
8 VariableDeclaration(VariableDeclaration),
10 FunctionDeclaration(FunctionDeclaration),
12 ClassDeclaration(ClassDeclaration),
14 ExpressionStatement(ExpressionStatement),
16 ImportDeclaration(ImportDeclaration),
18 ExportDeclaration(ExportDeclaration),
20 Interface(InterfaceDeclaration),
22 TypeAlias(TypeAliasDeclaration),
24 Enum(EnumDeclaration),
26 ReturnStatement(ReturnStatement),
28 IfStatement(IfStatement),
30 WhileStatement(WhileStatement),
32 DoWhileStatement(DoWhileStatement),
34 ForStatement(ForStatement),
36 ForInStatement(ForInStatement),
38 ForOfStatement(ForOfStatement),
40 SwitchStatement(SwitchStatement),
42 TryStatement(TryStatement),
44 ThrowStatement(ThrowStatement),
46 BreakStatement(BreakStatement),
48 ContinueStatement(ContinueStatement),
50 BlockStatement(BlockStatement),
52 Namespace(NamespaceDeclaration),
54}
55
56impl Statement {
57 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#[derive(Debug, Clone)]
89#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
90pub struct ExpressionStatement {
91 pub decorators: Vec<Decorator>,
93 pub is_declare: bool,
95 pub expression: Expression,
97 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
99 pub span: Range<usize>,
100}
101
102#[derive(Debug, Clone)]
104#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
105pub struct ReturnStatement {
106 pub decorators: Vec<Decorator>,
108 pub is_declare: bool,
110 pub argument: Option<Expression>,
112 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
114 pub span: Range<usize>,
115}
116
117#[derive(Debug, Clone)]
119#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
120pub struct ThrowStatement {
121 pub decorators: Vec<Decorator>,
123 pub is_declare: bool,
125 pub argument: Expression,
127 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
129 pub span: Range<usize>,
130}
131
132#[derive(Debug, Clone)]
134#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
135pub struct BreakStatement {
136 pub decorators: Vec<Decorator>,
138 pub is_declare: bool,
140 pub label: Option<String>,
142 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
144 pub span: Range<usize>,
145}
146
147#[derive(Debug, Clone)]
149#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
150pub struct ContinueStatement {
151 pub decorators: Vec<Decorator>,
153 pub is_declare: bool,
155 pub label: Option<String>,
157 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
159 pub span: Range<usize>,
160}
161
162#[derive(Debug, Clone)]
164#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
165pub struct NamespaceDeclaration {
166 pub decorators: Vec<Decorator>,
168 pub is_declare: bool,
170 pub name: String,
172 pub body: Vec<Statement>,
174 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
176 pub span: Range<usize>,
177}
178
179#[derive(Debug, Clone)]
181#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
182pub struct InterfaceDeclaration {
183 pub decorators: Vec<Decorator>,
185 pub is_declare: bool,
187 pub name: String,
189 pub type_params: Vec<TypeParameter>,
191 pub extends: Vec<TypeAnnotation>,
193 pub body: Vec<ClassMember>,
195 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
197 pub span: Range<usize>,
198}
199
200#[derive(Debug, Clone)]
202#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
203pub struct TypeAliasDeclaration {
204 pub decorators: Vec<Decorator>,
206 pub is_declare: bool,
208 pub name: String,
210 pub type_params: Vec<TypeParameter>,
212 pub ty: TypeAnnotation,
214 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
216 pub span: Range<usize>,
217}
218
219#[derive(Debug, Clone)]
221#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
222pub struct EnumDeclaration {
223 pub decorators: Vec<Decorator>,
225 pub is_declare: bool,
227 pub name: String,
229 pub members: Vec<EnumMember>,
231 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
233 pub span: Range<usize>,
234}
235
236#[derive(Debug, Clone)]
238#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
239pub struct IfStatement {
240 pub decorators: Vec<Decorator>,
242 pub is_declare: bool,
244 pub test: Expression,
246 pub consequent: Box<Statement>,
248 pub alternate: Option<Box<Statement>>,
250 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
252 pub span: Range<usize>,
253}
254
255#[derive(Debug, Clone)]
257#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
258pub struct WhileStatement {
259 pub decorators: Vec<Decorator>,
261 pub is_declare: bool,
263 pub test: Expression,
265 pub body: Box<Statement>,
267 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
269 pub span: Range<usize>,
270}
271
272#[derive(Debug, Clone)]
274#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
275pub struct DoWhileStatement {
276 pub decorators: Vec<Decorator>,
278 pub is_declare: bool,
280 pub body: Box<Statement>,
282 pub test: Expression,
284 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
286 pub span: Range<usize>,
287}
288
289#[derive(Debug, Clone)]
291#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
292pub struct ForStatement {
293 pub decorators: Vec<Decorator>,
295 pub is_declare: bool,
297 pub initializer: Option<Box<Statement>>,
299 pub test: Option<Expression>,
301 pub incrementor: Option<Expression>,
303 pub body: Box<Statement>,
305 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
307 pub span: Range<usize>,
308}
309
310#[derive(Debug, Clone)]
312#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
313pub struct ForInStatement {
314 pub decorators: Vec<Decorator>,
316 pub is_declare: bool,
318 pub left: Box<Statement>,
320 pub right: Expression,
322 pub body: Box<Statement>,
324 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
326 pub span: Range<usize>,
327}
328
329#[derive(Debug, Clone)]
331#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
332pub struct ForOfStatement {
333 pub decorators: Vec<Decorator>,
335 pub is_declare: bool,
337 pub left: Box<Statement>,
339 pub right: Expression,
341 pub body: Box<Statement>,
343 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
345 pub span: Range<usize>,
346}
347
348#[derive(Debug, Clone)]
350#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
351pub struct SwitchStatement {
352 pub decorators: Vec<Decorator>,
354 pub is_declare: bool,
356 pub discriminant: Expression,
358 pub cases: Vec<SwitchCase>,
360 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
362 pub span: Range<usize>,
363}
364
365#[derive(Debug, Clone)]
367#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
368pub struct SwitchCase {
369 pub test: Option<Expression>,
371 pub consequent: Vec<Statement>,
373 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
375 pub span: Range<usize>,
376}
377
378#[derive(Debug, Clone)]
380#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
381pub struct TryStatement {
382 pub decorators: Vec<Decorator>,
384 pub is_declare: bool,
386 pub block: Vec<Statement>,
388 pub handler: Option<CatchClause>,
390 pub finalizer: Option<Vec<Statement>>,
392 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
394 pub span: Range<usize>,
395}
396
397#[derive(Debug, Clone)]
399#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
400pub struct CatchClause {
401 pub param: Option<String>,
403 pub body: Vec<Statement>,
405 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
407 pub span: Range<usize>,
408}
409
410#[derive(Debug, Clone)]
412#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
413pub struct BlockStatement {
414 pub decorators: Vec<Decorator>,
416 pub is_declare: bool,
418 pub statements: Vec<Statement>,
420 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
422 pub span: Range<usize>,
423}
424
425#[derive(Debug, Clone)]
427#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
428pub enum ImportSpecifier {
429 Default(String),
431 Namespace(String),
433 Named {
435 local: String,
437 imported: String,
439 },
440}
441
442#[derive(Debug, Clone)]
444#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
445pub struct ExportSpecifier {
446 pub local: String,
448 pub exported: String,
450}
451
452#[derive(Debug, Clone)]
454#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
455pub struct ImportDeclaration {
456 pub module_specifier: String,
458 pub specifiers: Vec<ImportSpecifier>,
460 pub is_type_only: bool,
462 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
464 pub span: Range<usize>,
465}
466
467#[derive(Debug, Clone)]
469#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
470pub struct ExportDeclaration {
471 pub declaration: Option<Box<Statement>>,
473 pub specifiers: Vec<ExportSpecifier>,
475 pub source: Option<String>,
477 pub is_default: bool,
479 pub is_type_only: bool,
481 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
483 pub span: Range<usize>,
484}
485
486#[derive(Debug, Clone)]
488#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
489pub struct VariableDeclaration {
490 pub decorators: Vec<Decorator>,
492 pub is_declare: bool,
494 pub name: String,
496 pub ty: Option<TypeAnnotation>,
498 pub value: Option<Expression>,
500 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
502 pub span: Range<usize>,
503}
504
505#[derive(Debug, Clone)]
507#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
508pub struct FunctionDeclaration {
509 pub decorators: Vec<Decorator>,
511 pub is_declare: bool,
513 pub name: String,
515 pub type_params: Vec<TypeParameter>,
517 pub params: Vec<FunctionParam>,
519 pub return_type: Option<TypeAnnotation>,
521 pub body: Vec<Statement>,
523 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
525 pub span: Range<usize>,
526}
527
528#[derive(Debug, Clone)]
530#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
531pub struct ClassDeclaration {
532 pub decorators: Vec<Decorator>,
534 pub is_declare: bool,
536 pub name: String,
538 pub type_params: Vec<TypeParameter>,
540 pub extends: Option<TypeAnnotation>,
542 pub implements: Vec<TypeAnnotation>,
544 pub is_abstract: bool,
546 pub body: Vec<ClassMember>,
548 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
550 pub span: Range<usize>,
551}
552
553#[derive(Debug, Clone)]
555#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
556pub struct FunctionParam {
557 pub decorators: Vec<Decorator>,
559 pub name: String,
561 pub ty: Option<TypeAnnotation>,
563 pub optional: bool,
565 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
567 pub span: Range<usize>,
568}