oak_typescript/ast/expression_nodes.rs
1use crate::ast::{FunctionParam, JsxElement, JsxFragment, JsxSelfClosingElement, Statement, TypeAnnotation, TypeParameter};
2use core::range::Range;
3
4/// Represents a TypeScript expression.
5#[derive(Debug, Clone)]
6#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
7pub struct Expression {
8 /// The kind of expression.
9 pub kind: Box<ExpressionKind>,
10 /// Source span of the expression.
11 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
12 pub span: Range<usize>,
13}
14
15impl Expression {
16 /// Creates a new `Expression`.
17 pub fn new(kind: ExpressionKind, span: Range<usize>) -> Self {
18 Self { kind: Box::new(kind), span }
19 }
20
21 /// Gets the span of the expression.
22 pub fn span(&self) -> Range<usize> {
23 self.span.clone()
24 }
25}
26
27/// Represents the different kinds of expressions in TypeScript.
28#[derive(Debug, Clone)]
29#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
30pub enum ExpressionKind {
31 /// An identifier.
32 Identifier(String),
33 /// A numeric literal.
34 NumericLiteral(f64),
35 /// A string literal.
36 StringLiteral(String),
37 /// A BigInt literal.
38 BigIntLiteral(String),
39 /// A boolean literal.
40 BooleanLiteral(bool),
41 /// A null literal.
42 NullLiteral,
43 /// A regular expression literal.
44 RegexLiteral(String),
45 /// A template string literal.
46 TemplateString(String),
47 /// A unary expression.
48 UnaryExpression {
49 /// Unary operator.
50 operator: String,
51 /// Argument expression.
52 argument: Box<Expression>,
53 },
54 /// An update expression.
55 UpdateExpression {
56 /// Update operator.
57 operator: String,
58 /// Argument expression.
59 argument: Box<Expression>,
60 /// Whether the operator is a prefix.
61 prefix: bool,
62 },
63 /// A binary expression.
64 BinaryExpression {
65 /// Left-hand side expression.
66 left: Box<Expression>,
67 /// Binary operator.
68 operator: String,
69 /// Right-hand side expression.
70 right: Box<Expression>,
71 },
72 /// A conditional (ternary) expression.
73 ConditionalExpression {
74 /// Condition expression.
75 test: Box<Expression>,
76 /// Expression to evaluate if the condition is true.
77 consequent: Box<Expression>,
78 /// Expression to evaluate if the condition is false.
79 alternate: Box<Expression>,
80 },
81 /// A member expression.
82 MemberExpression {
83 /// Object expression.
84 object: Box<Expression>,
85 /// Property expression.
86 property: Box<Expression>,
87 /// Whether the property is computed (using `[]`).
88 computed: bool,
89 /// Whether the access is optional (using `?.`).
90 optional: bool,
91 },
92 /// A call expression.
93 CallExpression {
94 /// Function to call.
95 func: Box<Expression>,
96 /// Arguments to the function call.
97 args: Vec<Expression>,
98 },
99 /// A new expression.
100 NewExpression {
101 /// Constructor function to call.
102 func: Box<Expression>,
103 /// Arguments to the constructor call.
104 args: Vec<Expression>,
105 },
106 /// An assignment expression.
107 AssignmentExpression {
108 /// Left-hand side expression.
109 left: Box<Expression>,
110 /// Assignment operator.
111 operator: String,
112 /// Right-hand side expression.
113 right: Box<Expression>,
114 },
115 /// An 'as' expression (type assertion).
116 AsExpression {
117 /// Expression being asserted.
118 expression: Box<Expression>,
119 /// Type annotation being asserted to.
120 type_annotation: TypeAnnotation,
121 },
122 /// An arrow function expression.
123 ArrowFunction {
124 /// Type parameters of the arrow function.
125 type_params: Vec<TypeParameter>,
126 /// Parameters of the arrow function.
127 params: Vec<FunctionParam>,
128 /// Return type of the arrow function.
129 return_type: Option<TypeAnnotation>,
130 /// Body of the arrow function.
131 body: Box<Statement>,
132 /// Whether the arrow function is async.
133 async_: bool,
134 },
135 /// An object literal.
136 ObjectLiteral {
137 /// Properties within the object literal.
138 properties: Vec<ObjectProperty>,
139 },
140 /// An array literal.
141 ArrayLiteral {
142 /// Elements within the array literal.
143 elements: Vec<Expression>,
144 },
145 /// A spread element.
146 SpreadElement(Box<Expression>),
147 /// An await expression.
148 AwaitExpression(Box<Expression>),
149 /// A yield expression.
150 YieldExpression(Option<Box<Expression>>),
151 /// An import expression.
152 ImportExpression {
153 /// Module specifier being imported.
154 module_specifier: Box<Expression>,
155 },
156 /// A function expression.
157 FunctionExpression {
158 /// Optional name of the function.
159 name: Option<String>,
160 /// Type parameters of the function.
161 type_params: Vec<TypeParameter>,
162 /// Parameters of the function.
163 params: Vec<FunctionParam>,
164 /// Return type of the function.
165 return_type: Option<TypeAnnotation>,
166 /// Body of the function.
167 body: Vec<Statement>,
168 /// Whether the function is async.
169 async_: bool,
170 /// Whether the function is a generator.
171 generator: bool,
172 },
173 /// A tagged template expression.
174 TaggedTemplateExpression {
175 /// Tag function expression.
176 tag: Box<Expression>,
177 /// Template literal expression.
178 template: Box<Expression>,
179 },
180 /// A type assertion expression (e.g., `<Type>expression`).
181 TypeAssertionExpression {
182 /// Expression being asserted.
183 expression: Box<Expression>,
184 /// Type annotation being asserted to.
185 type_annotation: TypeAnnotation,
186 },
187 /// A non-null expression (e.g., `expression!`).
188 NonNullExpression(Box<Expression>),
189 /// A JSX element.
190 JsxElement(Box<JsxElement>),
191 /// A JSX fragment.
192 JsxFragment(Box<JsxFragment>),
193 /// A self-closing JSX element.
194 JsxSelfClosingElement(Box<JsxSelfClosingElement>),
195}
196
197/// Represents a property in an object literal.
198#[derive(Debug, Clone)]
199#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
200pub enum ObjectProperty {
201 /// A property declaration.
202 Property {
203 /// Name of the property.
204 name: String,
205 /// Value of the property.
206 value: Expression,
207 /// Whether the property is shorthand.
208 shorthand: bool,
209 /// Source span of the property.
210 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
211 span: Range<usize>,
212 },
213 /// A spread property.
214 Spread(Expression),
215}
216
217/// Represents a literal type.
218#[derive(Debug, Clone)]
219#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
220pub enum LiteralType {
221 /// A string literal type.
222 String(String),
223 /// A numeric literal type.
224 Number(f64),
225 /// A boolean literal type.
226 Boolean(bool),
227 /// A BigInt literal type.
228 BigInt(String),
229}