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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use swc_atoms::Atom;
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)) => {
let value = s.value.as_str()?;
if value.is_reserved()
|| value.is_reserved_in_es3()
|| is_valid_identifier(value, true)
{
self.changed = true;
report_change!(
"properties: Computed member => member expr with identifier as a prop"
);
Some(IdentName {
span: s.span,
// SAFETY: We just checked that s.value is valid UTF-8.
sym: unsafe { Atom::from_wtf8_unchecked(s.value.clone()) },
})
} else {
None
}
}
_ => 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)) => {
let Some(value) = s.value.as_str() else {
return;
};
if value == "constructor" || value == "__proto__" {
return;
}
if value.is_reserved()
|| value.is_reserved_in_es3()
|| is_valid_identifier(value, false)
{
*p = PropName::Ident(IdentName::new(
// SAFETY: reserved words and valid identifiers are valid UTF-8.
unsafe { Atom::from_wtf8_unchecked(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 {
let Some(value) = s.value.as_str() else {
return;
};
if value.is_reserved()
|| value.is_reserved_in_es3()
|| is_valid_identifier(value, false)
{
self.changed = true;
report_change!("misc: Optimizing string property name");
*name = PropName::Ident(IdentName {
span: s.span,
// SAFETY: reserved words and valid identifiers are valid UTF-8.
sym: unsafe { Atom::from_wtf8_unchecked(s.value.clone()) },
});
return;
}
if (!value.starts_with('0') && !value.starts_with('+')) || value.len() <= 1 {
if let Ok(v) = 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)) => {
let value = s.value.as_str()?;
if value.is_empty()
|| value.starts_with(|c: char| c.is_ascii_digit())
|| value
.contains(|c: char| !matches!(c, '0'..='9' | 'a'..='z' | 'A'..='Z' | '$'))
{
return None;
}
self.changed = true;
Some(IdentName::new(
// SAFETY: We just checked that s.value is valid UTF-8.
unsafe { Atom::from_wtf8_unchecked(s.value.clone()) },
s.span,
))
}
_ => None,
}
}
}