swc_typescript/fast_dts/
enum.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
use core::f64;

use rustc_hash::FxHashMap;
use swc_atoms::Atom;
use swc_common::{Spanned, SyntaxContext, DUMMY_SP};
use swc_ecma_ast::{
    BinExpr, BinaryOp, Expr, Ident, Lit, Number, Str, TsEnumDecl, TsEnumMemberId, UnaryExpr,
    UnaryOp,
};
use swc_ecma_utils::number::JsNumber;

use super::{util::ast_ext::MemberPropExt, FastDts};

#[derive(Debug, Clone)]
enum ConstantValue {
    Number(f64),
    String(String),
}

impl FastDts {
    pub(crate) fn transform_enum(&mut self, decl: &mut TsEnumDecl) {
        let mut prev_init_value = Some(ConstantValue::Number(-1.0));
        let mut prev_members = FxHashMap::default();
        for member in &mut decl.members {
            let value = if let Some(init_expr) = &member.init {
                let computed_value = self.evaluate(init_expr, &decl.id.sym, &prev_members);
                if computed_value.is_none() {
                    self.enum_member_initializers(member.id.span());
                }
                computed_value
            } else if let Some(ConstantValue::Number(v)) = prev_init_value {
                Some(ConstantValue::Number(v + 1.0))
            } else {
                None
            };

            prev_init_value = value.clone();
            if let Some(value) = &value {
                let member_name = match &member.id {
                    TsEnumMemberId::Ident(ident) => &ident.sym,
                    TsEnumMemberId::Str(s) => &s.value,
                };
                prev_members.insert(member_name.clone(), value.clone());
            }

            member.init = value.map(|value| {
                Box::new(match value {
                    ConstantValue::Number(v) => {
                        let is_neg = v < 0.0;
                        let expr = if v.is_infinite() {
                            Expr::Ident(Ident {
                                span: DUMMY_SP,
                                sym: "Infinity".into(),
                                ctxt: SyntaxContext::empty(),
                                optional: false,
                            })
                        } else {
                            Expr::Lit(Lit::Num(Number {
                                span: DUMMY_SP,
                                value: v,
                                raw: Some(v.to_string().into()),
                            }))
                        };

                        if is_neg {
                            Expr::Unary(UnaryExpr {
                                span: DUMMY_SP,
                                arg: Box::new(expr),
                                op: UnaryOp::Minus,
                            })
                        } else {
                            expr
                        }
                    }
                    ConstantValue::String(s) => Expr::Lit(Lit::Str(Str {
                        span: DUMMY_SP,
                        value: s.clone().into(),
                        raw: None,
                    })),
                })
            });
        }
    }

    fn evaluate(
        &self,
        expr: &Expr,
        enum_name: &Atom,
        prev_members: &FxHashMap<Atom, ConstantValue>,
    ) -> Option<ConstantValue> {
        match expr {
            Expr::Lit(lit) => match lit {
                Lit::Str(s) => Some(ConstantValue::String(s.value.to_string())),
                Lit::Num(number) => Some(ConstantValue::Number(number.value)),
                Lit::Null(_) | Lit::BigInt(_) | Lit::Bool(_) | Lit::Regex(_) | Lit::JSXText(_) => {
                    None
                }
            },
            Expr::Tpl(tpl) => {
                let mut value = String::new();
                for part in &tpl.quasis {
                    value.push_str(&part.raw);
                }
                Some(ConstantValue::String(value))
            }
            Expr::Paren(expr) => self.evaluate(&expr.expr, enum_name, prev_members),
            Expr::Bin(bin_expr) => self.evaluate_binary_expr(bin_expr, enum_name, prev_members),
            Expr::Unary(unary_expr) => {
                self.evaluate_unary_expr(unary_expr, enum_name, prev_members)
            }
            Expr::Ident(ident) => {
                if ident.sym == "Infinity" {
                    Some(ConstantValue::Number(f64::INFINITY))
                } else if ident.sym == "NaN" {
                    Some(ConstantValue::Number(f64::NAN))
                } else {
                    prev_members.get(&ident.sym).cloned()
                }
            }
            Expr::Member(member) => {
                let ident = member.obj.as_ident()?;
                if &ident.sym == enum_name {
                    let name = member.prop.static_name()?;
                    prev_members.get(name).cloned()
                } else {
                    None
                }
            }
            _ => None,
        }
    }

    fn evaluate_unary_expr(
        &self,
        expr: &UnaryExpr,
        enum_name: &Atom,
        prev_members: &FxHashMap<Atom, ConstantValue>,
    ) -> Option<ConstantValue> {
        let value = self.evaluate(&expr.arg, enum_name, prev_members)?;
        let value = match value {
            ConstantValue::Number(n) => n,
            ConstantValue::String(_) => {
                let value = if expr.op == UnaryOp::Minus {
                    ConstantValue::Number(f64::NAN)
                } else if expr.op == UnaryOp::Tilde {
                    ConstantValue::Number(-1.0)
                } else {
                    value
                };
                return Some(value);
            }
        };

        match expr.op {
            UnaryOp::Minus => Some(ConstantValue::Number(-value)),
            UnaryOp::Plus => Some(ConstantValue::Number(value)),
            UnaryOp::Tilde => Some(ConstantValue::Number((!JsNumber::from(value)).into())),
            _ => None,
        }
    }

    fn evaluate_binary_expr(
        &self,
        expr: &BinExpr,
        enum_name: &Atom,
        prev_members: &FxHashMap<Atom, ConstantValue>,
    ) -> Option<ConstantValue> {
        let left = self.evaluate(&expr.left, enum_name, prev_members)?;
        let right = self.evaluate(&expr.right, enum_name, prev_members)?;

        if expr.op == BinaryOp::Add
            && (matches!(left, ConstantValue::String(_))
                || matches!(right, ConstantValue::String(_)))
        {
            let left_string = match left {
                ConstantValue::Number(number) => number.to_string(),
                ConstantValue::String(s) => s,
            };

            let right_string = match right {
                ConstantValue::Number(number) => number.to_string(),
                ConstantValue::String(s) => s,
            };

            return Some(ConstantValue::String(format!(
                "{left_string}{right_string}"
            )));
        }

        let left = JsNumber::from(match left {
            ConstantValue::Number(n) => n,
            ConstantValue::String(_) => return None,
        });

        let right = JsNumber::from(match right {
            ConstantValue::Number(n) => n,
            ConstantValue::String(_) => return None,
        });

        match expr.op {
            BinaryOp::LShift => Some(left << right),
            BinaryOp::RShift => Some(left >> right),
            BinaryOp::ZeroFillRShift => Some(left.unsigned_shr(right)),
            BinaryOp::Add => Some(left + right),
            BinaryOp::Sub => Some(left - right),
            BinaryOp::Mul => Some(left * right),
            BinaryOp::Div => Some(left / right),
            BinaryOp::Mod => Some(left & right),
            BinaryOp::BitOr => Some(left | right),
            BinaryOp::BitXor => Some(left ^ right),
            BinaryOp::BitAnd => Some(left & right),
            BinaryOp::Exp => Some(left.pow(right)),
            _ => None,
        }
        .map(|number| ConstantValue::Number(number.into()))
    }
}