Skip to content

Commit d93f4c8

Browse files
authored
Migrate to VisitMut (#86347)
This is generally better for performance
1 parent 40dd4ce commit d93f4c8

File tree

3 files changed

+121
-192
lines changed

3 files changed

+121
-192
lines changed

crates/next-custom-transforms/src/transforms/dynamic.rs

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use swc_core::{
1515
PropName, PropOrSpread, Stmt, Str, Tpl, UnaryExpr, UnaryOp,
1616
},
1717
utils::{private_ident, quote_ident, ExprFactory},
18-
visit::{fold_pass, Fold, FoldWith, VisitMut, VisitMutWith},
18+
visit::{visit_mut_pass, VisitMut, VisitMutWith},
1919
},
2020
quote,
2121
};
@@ -34,7 +34,7 @@ pub fn next_dynamic(
3434
filename: Arc<FileName>,
3535
pages_or_app_dir: Option<PathBuf>,
3636
) -> impl Pass {
37-
fold_pass(NextDynamicPatcher {
37+
visit_mut_pass(NextDynamicPatcher {
3838
is_development,
3939
is_server_compiler,
4040
is_react_server_layer,
@@ -117,33 +117,24 @@ enum TurbopackImport {
117117
},
118118
}
119119

120-
impl Fold for NextDynamicPatcher {
121-
fn fold_module_items(&mut self, mut items: Vec<ModuleItem>) -> Vec<ModuleItem> {
122-
items = items.fold_children_with(self);
120+
impl VisitMut for NextDynamicPatcher {
121+
fn visit_mut_module_items(&mut self, items: &mut Vec<ModuleItem>) {
122+
items.visit_mut_children_with(self);
123123

124-
self.maybe_add_dynamically_imported_specifier(&mut items);
125-
126-
items
124+
self.maybe_add_dynamically_imported_specifier(items);
127125
}
128126

129-
fn fold_import_decl(&mut self, decl: ImportDecl) -> ImportDecl {
130-
let ImportDecl {
131-
ref src,
132-
ref specifiers,
133-
..
134-
} = decl;
135-
if &src.value == "next/dynamic" {
136-
for specifier in specifiers {
127+
fn visit_mut_import_decl(&mut self, decl: &mut ImportDecl) {
128+
if &decl.src.value == "next/dynamic" {
129+
for specifier in &decl.specifiers {
137130
if let ImportSpecifier::Default(default_specifier) = specifier {
138131
self.dynamic_bindings.push(default_specifier.local.to_id());
139132
}
140133
}
141134
}
142-
143-
decl
144135
}
145136

146-
fn fold_call_expr(&mut self, expr: CallExpr) -> CallExpr {
137+
fn visit_mut_call_expr(&mut self, expr: &mut CallExpr) {
147138
if self.is_next_dynamic_first_arg {
148139
if let Callee::Import(..) = &expr.callee {
149140
match &*expr.args[0].expr {
@@ -157,9 +148,12 @@ impl Fold for NextDynamicPatcher {
157148
_ => {}
158149
}
159150
}
160-
return expr.fold_children_with(self);
151+
expr.visit_mut_children_with(self);
152+
return;
161153
}
162-
let mut expr = expr.fold_children_with(self);
154+
155+
expr.visit_mut_children_with(self);
156+
163157
if let Callee::Expr(i) = &expr.callee {
164158
if let Expr::Ident(identifier) = &**i {
165159
if self.dynamic_bindings.contains(&identifier.to_id()) {
@@ -172,7 +166,7 @@ impl Fold for NextDynamicPatcher {
172166
)
173167
.emit()
174168
});
175-
return expr;
169+
return;
176170
} else if expr.args.len() > 2 {
177171
HANDLER.with(|handler| {
178172
handler
@@ -182,7 +176,7 @@ impl Fold for NextDynamicPatcher {
182176
)
183177
.emit()
184178
});
185-
return expr;
179+
return;
186180
}
187181
if expr.args.len() == 2 {
188182
match &*expr.args[1].expr {
@@ -196,19 +190,19 @@ impl Fold for NextDynamicPatcher {
196190
)
197191
.emit();
198192
});
199-
return expr;
193+
return;
200194
}
201195
}
202196
}
203197

204198
self.is_next_dynamic_first_arg = true;
205-
expr.args[0].expr = expr.args[0].expr.clone().fold_with(self);
199+
expr.args[0].expr.visit_mut_with(self);
206200
self.is_next_dynamic_first_arg = false;
207201

208202
let Some((dynamically_imported_specifier, dynamically_imported_specifier_span)) =
209203
self.dynamically_imported_specifier.take()
210204
else {
211-
return expr;
205+
return;
212206
};
213207

214208
let project_dir = match self.pages_or_app_dir.as_deref() {
@@ -419,7 +413,6 @@ impl Fold for NextDynamicPatcher {
419413
}
420414
}
421415
}
422-
expr
423416
}
424417
}
425418

crates/next-custom-transforms/src/transforms/named_import_transform.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use swc_core::{
55
common::DUMMY_SP,
66
ecma::{
77
ast::*,
8-
visit::{fold_pass, Fold},
8+
visit::{visit_mut_pass, VisitMut},
99
},
1010
};
1111

@@ -14,8 +14,8 @@ pub struct Config {
1414
pub packages: Vec<String>,
1515
}
1616

17-
pub fn named_import_transform(config: Config) -> impl Pass {
18-
fold_pass(NamedImportTransform {
17+
pub fn named_import_transform(config: Config) -> impl Pass + VisitMut {
18+
visit_mut_pass(NamedImportTransform {
1919
packages: config.packages,
2020
})
2121
}
@@ -25,9 +25,8 @@ struct NamedImportTransform {
2525
packages: Vec<String>,
2626
}
2727

28-
/// TODO: Implement this as a [Pass] instead of a full visitor ([Fold])
29-
impl Fold for NamedImportTransform {
30-
fn fold_import_decl(&mut self, decl: ImportDecl) -> ImportDecl {
28+
impl VisitMut for NamedImportTransform {
29+
fn visit_mut_import_decl(&mut self, decl: &mut ImportDecl) {
3130
// Match named imports and check if it's included in the packages
3231
let src_value = &decl.src.value;
3332

@@ -75,18 +74,12 @@ impl Fold for NamedImportTransform {
7574
src_value.to_string_lossy()
7675
);
7776

78-
// Create a new import declaration, keep everything the same except the source
79-
let mut new_decl = decl.clone();
80-
*new_decl.src = Str {
77+
*decl.src = Str {
8178
span: DUMMY_SP,
8279
value: new_src.into(),
8380
raw: None,
8481
};
85-
86-
return new_decl;
8782
}
8883
}
89-
90-
decl
9184
}
9285
}

0 commit comments

Comments
 (0)