Skip to main content

react_compiler_ast/
literals.rs

1use serde::{Deserialize, Serialize};
2
3use crate::common::BaseNode;
4
5#[derive(Debug, Clone, Serialize, Deserialize)]
6pub struct StringLiteral {
7    #[serde(flatten)]
8    pub base: BaseNode,
9    pub value: String,
10}
11
12#[derive(Debug, Clone, Serialize, Deserialize)]
13pub struct NumericLiteral {
14    #[serde(flatten)]
15    pub base: BaseNode,
16    pub value: f64,
17    /// Babel's extra field containing the raw source text.
18    /// Used to recover exact f64 values that serde_json may parse imprecisely.
19    #[serde(default, skip_serializing_if = "Option::is_none")]
20    pub extra: Option<NumericLiteralExtra>,
21}
22
23impl NumericLiteral {
24    /// Get the f64 value, preferring re-parsing from `extra.raw` when available
25    /// to avoid serde_json float parsing precision issues.
26    pub fn precise_value(&self) -> f64 {
27        if let Some(extra) = &self.extra {
28            if let Ok(v) = extra.raw.parse::<f64>() {
29                return v;
30            }
31        }
32        self.value
33    }
34}
35
36#[derive(Debug, Clone, Serialize, Deserialize)]
37pub struct NumericLiteralExtra {
38    pub raw: String,
39    #[serde(default, rename = "rawValue")]
40    pub raw_value: Option<f64>,
41}
42
43#[derive(Debug, Clone, Serialize, Deserialize)]
44pub struct BooleanLiteral {
45    #[serde(flatten)]
46    pub base: BaseNode,
47    pub value: bool,
48}
49
50#[derive(Debug, Clone, Serialize, Deserialize)]
51pub struct NullLiteral {
52    #[serde(flatten)]
53    pub base: BaseNode,
54}
55
56#[derive(Debug, Clone, Serialize, Deserialize)]
57pub struct BigIntLiteral {
58    #[serde(flatten)]
59    pub base: BaseNode,
60    pub value: String,
61}
62
63#[derive(Debug, Clone, Serialize, Deserialize)]
64pub struct RegExpLiteral {
65    #[serde(flatten)]
66    pub base: BaseNode,
67    pub pattern: String,
68    pub flags: String,
69}
70
71#[derive(Debug, Clone, Serialize, Deserialize)]
72pub struct TemplateElement {
73    #[serde(flatten)]
74    pub base: BaseNode,
75    pub value: TemplateElementValue,
76    pub tail: bool,
77}
78
79#[derive(Debug, Clone, Serialize, Deserialize)]
80pub struct TemplateElementValue {
81    pub raw: String,
82    #[serde(default, skip_serializing_if = "Option::is_none")]
83    pub cooked: Option<String>,
84}