swc_ecma_minifier/compress/pure/
properties.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
use swc_atoms::js_word;
use swc_ecma_ast::*;

use super::Pure;
use crate::compress::util::is_valid_identifier;

impl Pure<'_> {
    pub(super) fn optimize_property_of_member_expr(
        &mut self,
        obj: Option<&Expr>,
        c: &mut ComputedPropName,
    ) -> Option<IdentName> {
        if !self.options.props {
            return None;
        }
        if let Some(Expr::Array(..) | Expr::Await(..) | Expr::Yield(..) | Expr::Lit(..)) = obj {
            return None;
        }

        match &*c.expr {
            Expr::Lit(Lit::Str(s))
                if s.value.is_reserved()
                    || s.value.is_reserved_in_es3()
                    || is_valid_identifier(&s.value, true) =>
            {
                self.changed = true;
                report_change!(
                    "properties: Computed member => member expr with identifier as a prop"
                );

                Some(IdentName {
                    span: s.span,
                    sym: s.value.clone(),
                })
            }

            _ => None,
        }
    }

    /// If a key of is `'str'` (like `{ 'str': 1 }`) change it to [Ident] like
    /// (`{ str: 1, }`)
    pub(super) fn optimize_computed_prop_name_as_normal(&mut self, p: &mut PropName) {
        if !self.options.computed_props {
            return;
        }

        if let PropName::Computed(c) = p {
            match &mut *c.expr {
                Expr::Lit(Lit::Str(s)) => {
                    if s.value == *"constructor" || s.value == *"__proto__" {
                        return;
                    }

                    if s.value.is_reserved()
                        || s.value.is_reserved_in_es3()
                        || is_valid_identifier(&s.value, false)
                    {
                        *p = PropName::Ident(IdentName::new(s.value.clone(), s.span));
                    } else {
                        *p = PropName::Str(s.clone());
                    }
                }
                Expr::Lit(Lit::Num(n)) => {
                    if n.value.is_sign_positive() {
                        *p = PropName::Num(n.clone());
                    }
                }
                _ => {}
            }
        }
    }

    pub(super) fn optimize_prop_name(&mut self, name: &mut PropName) {
        if let PropName::Str(s) = name {
            if s.value.is_reserved()
                || s.value.is_reserved_in_es3()
                || is_valid_identifier(&s.value, false)
            {
                self.changed = true;
                report_change!("misc: Optimizing string property name");
                *name = PropName::Ident(IdentName {
                    span: s.span,
                    sym: s.value.clone(),
                });
                return;
            }

            if (!s.value.starts_with('0') && !s.value.starts_with('+')) || s.value.len() <= 1 {
                if let Ok(v) = s.value.parse::<u32>() {
                    self.changed = true;
                    report_change!("misc: Optimizing numeric property name");
                    *name = PropName::Num(Number {
                        span: s.span,
                        value: v as _,
                        raw: None,
                    });
                }
            }
        }
    }

    pub(super) fn handle_known_computed_member_expr(
        &mut self,
        c: &mut ComputedPropName,
    ) -> Option<IdentName> {
        if !self.options.props || !self.options.evaluate {
            return None;
        }

        match &*c.expr {
            Expr::Lit(Lit::Str(s)) => {
                if s.value == js_word!("")
                    || s.value.starts_with(|c: char| c.is_ascii_digit())
                    || s.value
                        .contains(|c: char| !matches!(c, '0'..='9' | 'a'..='z' | 'A'..='Z' | '$'))
                {
                    return None;
                }

                self.changed = true;

                Some(IdentName::new(s.value.clone(), s.span))
            }
            _ => None,
        }
    }
}