swc_estree_compat/babelify/
lit.rs1use serde::{Deserialize, Serialize};
2use swc_atoms::atom;
3use swc_ecma_ast::{BigInt, Bool, Lit, Null, Number, Regex, Str};
4use swc_estree_ast::{
5 BigIntLiteral, BooleanLiteral, JSXText as BabelJSXText, Literal, NullLiteral, NumericLiteral,
6 RegExpLiteral, StringLiteral,
7};
8
9use crate::babelify::{Babelify, Context};
10
11#[derive(Debug, Clone, Serialize, Deserialize)]
12pub enum LitOutput {
13 Lit(Literal),
14 JSX(BabelJSXText),
15}
16
17impl Babelify for Lit {
18 type Output = LitOutput;
19
20 fn babelify(self, ctx: &Context) -> Self::Output {
21 match self {
22 Lit::Str(s) => LitOutput::Lit(Literal::String(s.babelify(ctx))),
23 Lit::Bool(b) => LitOutput::Lit(Literal::Boolean(b.babelify(ctx))),
24 Lit::Null(n) => LitOutput::Lit(Literal::Null(n.babelify(ctx))),
25 Lit::Num(n) => LitOutput::Lit(Literal::Numeric(n.babelify(ctx))),
26 Lit::BigInt(i) => LitOutput::Lit(Literal::BigInt(i.babelify(ctx))),
27 Lit::Regex(r) => LitOutput::Lit(Literal::RegExp(r.babelify(ctx))),
28 Lit::JSXText(t) => LitOutput::JSX(t.babelify(ctx)),
29 }
30 }
31}
32
33impl Babelify for Str {
34 type Output = StringLiteral;
35
36 fn babelify(self, ctx: &Context) -> Self::Output {
37 StringLiteral {
38 base: ctx.base(self.span),
39 value: self.value,
40 raw: match self.raw {
42 Some(value) => value,
43 _ => swc_atoms::atom!(""),
44 },
45 }
46 }
47}
48
49impl Babelify for Bool {
50 type Output = BooleanLiteral;
51
52 fn babelify(self, ctx: &Context) -> Self::Output {
53 BooleanLiteral {
54 base: ctx.base(self.span),
55 value: self.value,
56 }
57 }
58}
59
60impl Babelify for Null {
61 type Output = NullLiteral;
62
63 fn babelify(self, ctx: &Context) -> Self::Output {
64 NullLiteral {
65 base: ctx.base(self.span),
66 }
67 }
68}
69
70impl Babelify for Number {
71 type Output = NumericLiteral;
72
73 fn babelify(self, ctx: &Context) -> Self::Output {
74 NumericLiteral {
75 base: ctx.base(self.span),
76 value: self.value,
77 }
78 }
79}
80
81impl Babelify for BigInt {
82 type Output = BigIntLiteral;
83
84 fn babelify(self, ctx: &Context) -> Self::Output {
85 BigIntLiteral {
86 base: ctx.base(self.span),
87 value: self.value.to_string(),
88 raw: match self.raw {
90 Some(value) => value,
91 _ => atom!(""),
92 },
93 }
94 }
95}
96
97impl Babelify for Regex {
98 type Output = RegExpLiteral;
99
100 fn babelify(self, ctx: &Context) -> Self::Output {
101 RegExpLiteral {
102 base: ctx.base(self.span),
103 pattern: self.exp,
104 flags: self.flags,
105 }
106 }
107}