swc_ecma_minifier/compress/optimize/arguments.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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
use std::iter::repeat_with;
use swc_common::{util::take::Take, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_utils::{find_pat_ids, is_valid_prop_ident, private_ident};
use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith};
use super::Optimizer;
use crate::compress::optimize::is_left_access_to_arguments;
/// Methods related to the option `arguments`.
impl Optimizer<'_> {
///
/// - `arguments['foo']` => `arguments.foo`
pub(super) fn optimize_str_access_to_arguments(&mut self, e: &mut Expr) {
if !self.options.arguments {
return;
}
match e {
Expr::Member(MemberExpr { prop, .. }) => {
if let MemberProp::Computed(c) = prop {
if let Expr::Lit(Lit::Str(s)) = &mut *c.expr {
if !s.value.starts_with(|c: char| c.is_ascii_alphabetic()) {
return;
}
if !is_valid_prop_ident(&s.value) {
return;
}
self.changed = true;
report_change!("arguments: Optimizing computed access to arguments");
*prop = MemberProp::Ident(IdentName {
span: s.span,
sym: s.take().value,
})
}
}
}
Expr::SuperProp(SuperPropExpr { prop, .. }) => {
if let SuperProp::Computed(c) = prop {
if let Expr::Lit(Lit::Str(s)) = &mut *c.expr {
if !s.value.starts_with(|c: char| c.is_ascii_alphabetic()) {
return;
}
if !is_valid_prop_ident(&s.value) {
return;
}
self.changed = true;
report_change!("arguments: Optimizing computed access to arguments");
*prop = SuperProp::Ident(IdentName {
span: s.span,
sym: s.take().value,
})
}
}
}
_ => (),
};
}
pub(super) fn optimize_usage_of_arguments(&mut self, f: &mut Function) {
if !self.options.arguments {
return;
}
if f.params.iter().any(|param| match ¶m.pat {
Pat::Ident(BindingIdent {
id: Ident { sym, .. },
..
}) if &**sym == "arguments" => true,
Pat::Ident(i) => self
.data
.vars
.get(&i.id.to_id())
.map(|v| v.declared_count >= 2)
.unwrap_or(false),
_ => true,
}) {
return;
}
{
// If a function has a variable named `arguments`, we abort.
let data: Vec<Id> = find_pat_ids(&f.body);
if data.iter().any(|id| {
if id.0 == "arguments" {
return true;
}
false
}) {
return;
}
}
let mut v = ArgReplacer {
params: &mut f.params,
changed: false,
keep_fargs: self.options.keep_fargs,
prevent: false,
};
// We visit body two time, to use simpler logic in `inject_params_if_required`
f.body.visit_mut_children_with(&mut v);
f.body.visit_mut_children_with(&mut v);
self.changed |= v.changed;
}
}
struct ArgReplacer<'a> {
params: &'a mut Vec<Param>,
changed: bool,
keep_fargs: bool,
prevent: bool,
}
impl ArgReplacer<'_> {
fn inject_params_if_required(&mut self, idx: usize) {
if idx < self.params.len() || self.keep_fargs {
return;
}
let new_args = idx + 1 - self.params.len();
self.changed = true;
report_change!("arguments: Injecting {} parameters", new_args);
let mut start = self.params.len();
self.params.extend(
repeat_with(|| {
let p = Param {
span: DUMMY_SP,
decorators: Default::default(),
pat: private_ident!(format!("argument_{}", start)).into(),
};
start += 1;
p
})
.take(new_args),
)
}
}
impl VisitMut for ArgReplacer<'_> {
noop_visit_mut_type!();
/// Noop.
fn visit_mut_arrow_expr(&mut self, _: &mut ArrowExpr) {}
fn visit_mut_assign_expr(&mut self, n: &mut AssignExpr) {
n.visit_mut_children_with(self);
if is_left_access_to_arguments(&n.left) {
self.prevent = true;
}
}
fn visit_mut_expr(&mut self, n: &mut Expr) {
if self.prevent {
return;
}
n.visit_mut_children_with(self);
if let Expr::Member(MemberExpr {
obj,
prop: MemberProp::Computed(c),
..
}) = n
{
match &**obj {
Expr::Ident(Ident { sym, .. }) if &**sym == "arguments" => {
match &*c.expr {
Expr::Lit(Lit::Str(Str { value, .. })) => {
let idx = value.parse::<usize>();
let idx = match idx {
Ok(v) => v,
_ => return,
};
self.inject_params_if_required(idx);
if let Some(param) = self.params.get(idx) {
if let Pat::Ident(i) = ¶m.pat {
self.changed = true;
report_change!(
"arguments: Replacing access to arguments to normal \
reference"
);
*n = i.id.clone().into();
}
}
}
Expr::Lit(Lit::Num(Number { value, .. })) => {
if value.fract() != 0.0 {
// We ignores non-integer values.
return;
}
let idx = value.round() as i64 as usize;
self.inject_params_if_required(idx);
//
if let Some(param) = self.params.get(idx) {
if let Pat::Ident(i) = ¶m.pat {
report_change!(
"arguments: Replacing access to arguments to normal \
reference"
);
self.changed = true;
*n = i.id.clone().into();
}
}
}
_ => {}
}
}
_ => (),
}
}
}
/// Noop.
fn visit_mut_function(&mut self, _: &mut Function) {}
fn visit_mut_member_expr(&mut self, n: &mut MemberExpr) {
if self.prevent {
return;
}
n.obj.visit_mut_with(self);
if let MemberProp::Computed(c) = &mut n.prop {
c.visit_mut_with(self);
}
}
fn visit_mut_super_prop_expr(&mut self, n: &mut SuperPropExpr) {
if self.prevent {
return;
}
if let SuperProp::Computed(c) = &mut n.prop {
c.visit_mut_with(self);
}
}
}