Skip to main content

oak_typescript/ast/
expression_node.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#[derive(Debug, Clone)]
7#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
8pub struct Expression {
9    pub kind: Box<ExpressionKind>,
10    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
11    pub span: Range<usize>,
12}
13
14impl Expression {
15    pub fn new(kind: ExpressionKind, span: Range<usize>) -> Self {
16        Self { kind: Box::new(kind), span }
17    }
18
19    pub fn span(&self) -> Range<usize> {
20        self.span.clone()
21    }
22}
23
24#[derive(Debug, Clone)]
25#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
26pub enum ExpressionKind {
27    Identifier(String),
28    NumericLiteral(f64),
29    StringLiteral(String),
30    BigIntLiteral(String),
31    BooleanLiteral(bool),
32    NullLiteral,
33    RegexLiteral(String),
34    TemplateString(String),
35    UnaryExpression { operator: String, argument: Box<Expression> },
36    UpdateExpression { operator: String, argument: Box<Expression>, prefix: bool },
37    BinaryExpression { left: Box<Expression>, operator: String, right: Box<Expression> },
38    ConditionalExpression { test: Box<Expression>, consequent: Box<Expression>, alternate: Box<Expression> },
39    MemberExpression { object: Box<Expression>, property: Box<Expression>, computed: bool, optional: bool },
40    CallExpression { func: Box<Expression>, args: Vec<Expression> },
41    NewExpression { func: Box<Expression>, args: Vec<Expression> },
42    AssignmentExpression { left: Box<Expression>, operator: String, right: Box<Expression> },
43    AsExpression { expression: Box<Expression>, type_annotation: TypeAnnotation },
44    ArrowFunction { type_params: Vec<TypeParameter>, params: Vec<FunctionParam>, return_type: Option<TypeAnnotation>, body: Box<Statement>, async_: bool },
45    ObjectLiteral { properties: Vec<ObjectProperty> },
46    ArrayLiteral { elements: Vec<Expression> },
47    SpreadElement(Box<Expression>),
48    AwaitExpression(Box<Expression>),
49    YieldExpression(Option<Box<Expression>>),
50    ImportExpression { module_specifier: Box<Expression> },
51    FunctionExpression { name: Option<String>, type_params: Vec<TypeParameter>, params: Vec<FunctionParam>, return_type: Option<TypeAnnotation>, body: Vec<Statement>, async_: bool, generator: bool },
52    TaggedTemplateExpression { tag: Box<Expression>, template: Box<Expression> },
53    TypeAssertionExpression { expression: Box<Expression>, type_annotation: TypeAnnotation },
54    NonNullExpression(Box<Expression>),
55    JsxElement(Box<JsxElement>),
56    JsxFragment(Box<JsxFragment>),
57    JsxSelfClosingElement(Box<JsxSelfClosingElement>),
58}
59
60#[derive(Debug, Clone)]
61#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
62pub enum ObjectProperty {
63    Property {
64        name: String,
65        value: Expression,
66        shorthand: bool,
67        #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
68        span: Range<usize>,
69    },
70    Spread(Expression),
71}
72
73#[derive(Debug, Clone)]
74#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
75pub enum LiteralType {
76    String(String),
77    Number(f64),
78    Boolean(bool),
79    BigInt(String),
80}