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
use crate::jsx::JSXText;
use num_bigint::BigInt as BigIntValue;
use std::fmt::{self, Display, Formatter};
use swc_atoms::JsWord;
use swc_common::{ast_node, Span};

#[ast_node]
pub enum Lit {
    #[tag("StringLiteral")]
    Str(Str),

    #[tag("BooleanLiteral")]
    Bool(Bool),

    #[tag("NullLiteral")]
    Null(Null),

    #[tag("NumericLiteral")]
    Num(Number),

    #[tag("BigIntLiteral")]
    BigInt(BigInt),

    #[tag("RegExpLiteral")]
    Regex(Regex),

    #[tag("JSXText")]
    JSXText(JSXText),
}

#[ast_node("BigIntLiteral")]
pub struct BigInt {
    pub span: Span,
    #[cfg_attr(feature = "fold", fold(ignore))]
    pub value: BigIntValue,
}

#[ast_node("StringLiteral")]
pub struct Str {
    pub span: Span,

    pub value: JsWord,

    /// This includes line escape.
    #[serde(default)]
    pub has_escape: bool,
}
impl Str {
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.value.is_empty()
    }
}

#[ast_node("BooleanLiteral")]
#[derive(Copy)]
pub struct Bool {
    pub span: Span,
    pub value: bool,
}

#[ast_node("NullLiteral")]
#[derive(Copy)]
pub struct Null {
    pub span: Span,
}

#[ast_node("RegExpLiteral")]
pub struct Regex {
    pub span: Span,

    #[serde(rename = "pattern")]
    pub exp: JsWord,

    #[serde(default)]
    pub flags: JsWord,
}

#[ast_node("NumericLiteral")]
#[derive(Copy)]
pub struct Number {
    pub span: Span,
    pub value: f64,
}

impl Display for Number {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        if self.value.is_infinite() {
            if self.value.is_sign_positive() {
                Display::fmt("Infinity", f)
            } else {
                Display::fmt("-Infinity", f)
            }
        } else {
            Display::fmt(&self.value, f)
        }
    }
}