swc_estree_compat/swcify/
lit.rs

1use swc_ecma_ast::{BigInt, Bool, Expr, Lit, Null, Number, Regex, Str, Tpl, TplElement};
2use swc_estree_ast::{
3    BigIntLiteral, BooleanLiteral, DecimalLiteral, Literal, NullLiteral, NumberLiteral,
4    NumericLiteral, RegExpLiteral, StringLiteral, TemplateElement, TemplateLiteral,
5    TemplateLiteralExpr,
6};
7
8use super::Context;
9use crate::swcify::Swcify;
10
11impl Swcify for Literal {
12    type Output = Lit;
13
14    fn swcify(self, ctx: &Context) -> Self::Output {
15        match self {
16            Literal::String(v) => v.swcify(ctx).into(),
17            Literal::Numeric(v) => v.swcify(ctx).into(),
18            Literal::Null(v) => v.swcify(ctx).into(),
19            Literal::Boolean(v) => v.swcify(ctx).into(),
20            Literal::RegExp(v) => v.swcify(ctx).into(),
21            Literal::Template(..) => unreachable!(),
22            Literal::BigInt(v) => v.swcify(ctx).into(),
23            Literal::Decimal(v) => v.swcify(ctx).into(),
24        }
25    }
26}
27
28impl Swcify for StringLiteral {
29    type Output = Str;
30
31    fn swcify(self, ctx: &Context) -> Self::Output {
32        Str {
33            span: ctx.span(&self.base),
34            value: self.value,
35            raw: Some(self.raw),
36        }
37    }
38}
39
40impl Swcify for NumberLiteral {
41    type Output = Number;
42
43    fn swcify(self, ctx: &Context) -> Self::Output {
44        Number {
45            span: ctx.span(&self.base),
46            value: self.value,
47            // TODO improve me
48            raw: None,
49        }
50    }
51}
52
53impl Swcify for NumericLiteral {
54    type Output = Number;
55
56    fn swcify(self, ctx: &Context) -> Self::Output {
57        Number {
58            span: ctx.span(&self.base),
59            value: self.value,
60            // TODO improve me
61            raw: None,
62        }
63    }
64}
65
66impl Swcify for NullLiteral {
67    type Output = Null;
68
69    fn swcify(self, ctx: &Context) -> Self::Output {
70        Null {
71            span: ctx.span(&self.base),
72        }
73    }
74}
75
76impl Swcify for BooleanLiteral {
77    type Output = Bool;
78
79    fn swcify(self, ctx: &Context) -> Self::Output {
80        Bool {
81            span: ctx.span(&self.base),
82            value: self.value,
83        }
84    }
85}
86
87impl Swcify for RegExpLiteral {
88    type Output = Regex;
89
90    fn swcify(self, ctx: &Context) -> Self::Output {
91        Regex {
92            span: ctx.span(&self.base),
93            exp: self.pattern,
94            flags: self.flags,
95        }
96    }
97}
98
99impl Swcify for TemplateLiteral {
100    type Output = Tpl;
101
102    fn swcify(self, ctx: &Context) -> Self::Output {
103        Tpl {
104            span: ctx.span(&self.base),
105            exprs: self.expressions.swcify(ctx),
106            quasis: self.quasis.swcify(ctx),
107        }
108    }
109}
110
111impl Swcify for TemplateLiteralExpr {
112    type Output = Box<Expr>;
113
114    fn swcify(self, ctx: &Context) -> Self::Output {
115        match self {
116            TemplateLiteralExpr::TSType(..) => todo!(),
117            TemplateLiteralExpr::Expr(v) => v.swcify(ctx),
118        }
119    }
120}
121
122impl Swcify for TemplateElement {
123    type Output = TplElement;
124
125    fn swcify(self, ctx: &Context) -> Self::Output {
126        TplElement {
127            span: ctx.span(&self.base),
128            tail: self.tail,
129            cooked: self.value.cooked,
130            raw: self.value.raw,
131        }
132    }
133}
134
135impl Swcify for BigIntLiteral {
136    type Output = BigInt;
137
138    fn swcify(self, ctx: &Context) -> Self::Output {
139        BigInt {
140            span: ctx.span(&self.base),
141            value: self
142                .value
143                .parse()
144                .map(Box::new)
145                .expect("failed to parse the value of BigIntLiteral"),
146            // TODO improve me
147            raw: None,
148        }
149    }
150}
151
152impl Swcify for DecimalLiteral {
153    type Output = Number;
154
155    fn swcify(self, ctx: &Context) -> Self::Output {
156        Number {
157            span: ctx.span(&self.base),
158            value: self
159                .value
160                .parse()
161                .expect("failed to parse the value of DecimalLiteral"),
162            // TODO improve me
163            raw: None,
164        }
165    }
166}