Skip to content

Commit 98da644

Browse files
authoredJan 3, 2023
perf(es/preset-env): Use VisitMut instead of Fold for polyfill (#6734)
1 parent 2e32faa commit 98da644

File tree

3 files changed

+26
-50
lines changed

3 files changed

+26
-50
lines changed
 

‎crates/swc_ecma_preset_env/src/corejs2/entry.rs

+12-23
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use indexmap::IndexSet;
22
use preset_env_base::{version::should_enable, Versions};
33
use swc_atoms::js_word;
4-
use swc_common::{util::move_map::MoveMap, DUMMY_SP};
4+
use swc_common::DUMMY_SP;
55
use swc_ecma_ast::*;
6-
use swc_ecma_visit::{Fold, FoldWith};
6+
use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith};
77

88
use super::builtin::BUILTINS;
99

@@ -64,31 +64,21 @@ impl Entry {
6464
}
6565
}
6666

67-
impl Fold for Entry {
68-
fn fold_import_decl(&mut self, i: ImportDecl) -> ImportDecl {
69-
let i: ImportDecl = i.fold_children_with(self);
67+
impl VisitMut for Entry {
68+
noop_visit_mut_type!();
7069

70+
fn visit_mut_import_decl(&mut self, i: &mut ImportDecl) {
7171
let remove = i.specifiers.is_empty() && self.add_all(&i.src.value);
7272

7373
if remove {
74-
ImportDecl {
75-
src: Str {
76-
span: DUMMY_SP,
77-
value: js_word!(""),
78-
..*i.src
79-
}
80-
.into(),
81-
..i
82-
}
83-
} else {
84-
i
74+
i.src.value = js_word!("");
75+
i.src.span = DUMMY_SP;
8576
}
8677
}
8778

88-
fn fold_module_items(&mut self, items: Vec<ModuleItem>) -> Vec<ModuleItem> {
89-
items.move_flat_map(|item| {
90-
let item: ModuleItem = item.fold_with(self);
91-
79+
fn visit_mut_module_items(&mut self, items: &mut Vec<ModuleItem>) {
80+
items.retain_mut(|item| {
81+
item.visit_mut_children_with(self);
9282
if let ModuleItem::Stmt(Stmt::Expr(ExprStmt { expr, .. })) = &item {
9383
if let Expr::Call(CallExpr {
9484
callee: Callee::Expr(callee),
@@ -115,13 +105,12 @@ impl Fold for Entry {
115105
}
116106
&& self.add_all("@swc/polyfill")
117107
{
118-
return None;
108+
return false;
119109
}
120110
}
121111
}
122112
}
123-
124-
Some(item)
113+
true
125114
})
126115
}
127116
}

‎crates/swc_ecma_preset_env/src/corejs3/entry.rs

+5-16
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use preset_env_base::{
77
use swc_atoms::js_word;
88
use swc_common::{collections::AHashMap, DUMMY_SP};
99
use swc_ecma_ast::*;
10-
use swc_ecma_visit::{Fold, FoldWith};
10+
use swc_ecma_visit::VisitMut;
1111

1212
use super::{compat::DATA as CORE_JS_COMPAT_DATA, data::MODULES_BY_VERSION};
1313

@@ -91,24 +91,13 @@ impl Entry {
9191
}
9292
}
9393

94-
impl Fold for Entry {
95-
fn fold_import_decl(&mut self, i: ImportDecl) -> ImportDecl {
96-
let i: ImportDecl = i.fold_children_with(self);
97-
94+
impl VisitMut for Entry {
95+
fn visit_mut_import_decl(&mut self, i: &mut ImportDecl) {
9896
let remove = i.specifiers.is_empty() && self.add(&i.src.value);
9997

10098
if remove {
101-
ImportDecl {
102-
src: Str {
103-
span: DUMMY_SP,
104-
value: js_word!(""),
105-
..*i.src
106-
}
107-
.into(),
108-
..i
109-
}
110-
} else {
111-
i
99+
i.src.span = DUMMY_SP;
100+
i.src.value = js_word!("");
112101
}
113102
}
114103
}

‎crates/swc_ecma_preset_env/src/lib.rs

+9-11
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use swc_ecma_transforms::{
2222
Assumptions,
2323
};
2424
use swc_ecma_utils::prepend_stmts;
25-
use swc_ecma_visit::{Fold, FoldWith, VisitWith};
25+
use swc_ecma_visit::{as_folder, Fold, VisitMut, VisitMutWith, VisitWith};
2626

2727
pub use self::transform_data::Feature;
2828

@@ -311,7 +311,7 @@ where
311311

312312
chain!(
313313
pass,
314-
Polyfills {
314+
as_folder(Polyfills {
315315
mode: c.mode,
316316
regenerator: should_enable!(Regenerator, true),
317317
corejs: c.core_js.unwrap_or(Version {
@@ -323,7 +323,7 @@ where
323323
targets,
324324
includes: included_modules,
325325
excludes: excluded_modules,
326-
}
326+
})
327327
)
328328
}
329329

@@ -338,8 +338,8 @@ struct Polyfills {
338338
excludes: AHashSet<String>,
339339
}
340340

341-
impl Fold for Polyfills {
342-
fn fold_module(&mut self, mut m: Module) -> Module {
341+
impl VisitMut for Polyfills {
342+
fn visit_mut_module(&mut self, m: &mut Module) {
343343
let span = m.span;
344344

345345
let required = match self.mode {
@@ -365,7 +365,7 @@ impl Fold for Polyfills {
365365
_ => unimplemented!("corejs version other than 2 / 3"),
366366
};
367367

368-
if regenerator::is_required(&m) {
368+
if regenerator::is_required(m) {
369369
r.insert("regenerator-runtime/runtime.js");
370370
}
371371

@@ -374,13 +374,13 @@ impl Fold for Polyfills {
374374
Some(Mode::Entry) => match self.corejs {
375375
Version { major: 2, .. } => {
376376
let mut v = corejs2::Entry::new(self.targets, self.regenerator);
377-
m = m.fold_with(&mut v);
377+
m.visit_mut_with(&mut v);
378378
v.imports
379379
}
380380

381381
Version { major: 3, .. } => {
382382
let mut v = corejs3::Entry::new(self.targets, self.corejs, !self.regenerator);
383-
m = m.fold_with(&mut v);
383+
m.visit_mut_with(&mut v);
384384
v.imports
385385
}
386386

@@ -450,11 +450,9 @@ impl Fold for Polyfills {
450450
}
451451

452452
m.body.retain(|item| !matches!(item, ModuleItem::ModuleDecl(ModuleDecl::Import(ImportDecl { src, .. })) if src.span == DUMMY_SP && src.value == js_word!("")));
453-
454-
m
455453
}
456454

457-
fn fold_script(&mut self, _: Script) -> Script {
455+
fn visit_mut_script(&mut self, _: &mut Script) {
458456
unimplemented!("automatic polyfill for scripts")
459457
}
460458
}

0 commit comments

Comments
 (0)
Please sign in to comment.