1use crate::generate_ts::GenerateTs;
2use crate::{Result, SourceId, SourceSpan, TsGenerator};
3use arcstr::ArcStr;
4use serde::{Deserialize, Serialize};
5
6mod expressions;
7pub use self::expressions::*;
8
9#[derive(Debug, Clone, PartialEq)]
10pub struct ProgramNode {
11 pub statements: Vec<StatementNode>,
12 pub span: SourceSpan,
13 pub source_id: SourceId,
14 pub target: String,
15}
16
17impl GenerateTs for ProgramNode {
18 fn generate_ts(&self, context: &mut TsGenerator) -> Result<()> {
19 for stmt in &self.statements {
20 stmt.generate_ts(context)?;
21 context.append_newline();
22 }
23 context.append_text(&format!("//# sourceMappingURL={}", self.target));
24 Ok(())
25 }
26}
27
28#[derive(Debug, Clone, PartialEq)]
29pub enum StatementNode {
30 If(Box<IfStatement>),
31 Class(Box<ClassStatement>),
32}
33
34impl GenerateTs for StatementNode {
35 fn generate_ts(&self, context: &mut TsGenerator) -> Result<()> {
36 match self {
37 StatementNode::If(node) => node.generate_ts(context),
38 StatementNode::Class(node) => node.generate_ts(context),
39 }
40 }
41}
42
43#[derive(Debug, Clone, PartialEq)]
52pub struct IfStatement {
53 pub condition: ExpressionNode,
54 pub consequent: ExpressionNode,
55 pub alternates: Vec<ElseIfStatement>,
56 pub default: Option<ExpressionNode>,
57 pub span: SourceSpan,
58 pub source_id: SourceId,
59}
60#[derive(Debug, Clone, PartialEq)]
61pub struct ElseIfStatement {
62 pub condition: ExpressionNode,
63 pub consequent: ExpressionNode,
64 pub span: SourceSpan,
65 pub source_id: SourceId,
66}
67
68#[derive(Debug, Clone, PartialEq)]
69pub struct ClassStatement {
70 pub class_name: Identifier,
71 pub class_body: String,
72 pub export: bool,
73}
74impl GenerateTs for ClassStatement {
75 fn generate_ts(&self, context: &mut TsGenerator) -> Result<()> {
76 if self.export {
77 context.append_text("export ");
78 }
79 context.append_text("class ");
80 self.class_name.generate_ts(context)?;
81 context.indent(" {");
82 context.append_text(&self.class_body);
83 context.dedent("}");
84 Ok(())
85 }
86}
87
88#[derive(Debug, Clone, PartialEq)]
89pub struct BlockStatement {
90 pub span: SourceSpan,
91 pub source_id: SourceId,
92 pub body: Vec<ExpressionNode>,
93}
94
95impl GenerateTs for IfStatement {
100 fn generate_ts(&self, context: &mut TsGenerator) -> Result<()> {
101 context.append_text("if (");
102 self.condition.generate_ts(context)?;
103 context.append_text(") ");
104 context.indent("{");
105 context.append_newline();
106 self.consequent.generate_ts(context)?;
107 context.dedent("}");
108 Ok(())
109 }
110}
111
112impl GenerateTs for BlockStatement {
113 fn generate_ts(&self, context: &mut TsGenerator) -> Result<()> {
114 context.indent("{");
115 for stmt in &self.body {
116 stmt.generate_ts(context)?;
117 context.append_text(";\n"); }
119 context.dedent("}");
120 Ok(())
121 }
122}
123