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