Unverified Commit d93f4c80 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Migrate to VisitMut (#86347)

This is generally better for performance
parent 40dd4ce4
......@@ -15,7 +15,7 @@ use swc_core::{
PropName, PropOrSpread, Stmt, Str, Tpl, UnaryExpr, UnaryOp,
},
utils::{private_ident, quote_ident, ExprFactory},
visit::{fold_pass, Fold, FoldWith, VisitMut, VisitMutWith},
visit::{visit_mut_pass, VisitMut, VisitMutWith},
},
quote,
};
......@@ -34,7 +34,7 @@ pub fn next_dynamic(
filename: Arc<FileName>,
pages_or_app_dir: Option<PathBuf>,
) -> impl Pass {
fold_pass(NextDynamicPatcher {
visit_mut_pass(NextDynamicPatcher {
is_development,
is_server_compiler,
is_react_server_layer,
......@@ -117,33 +117,24 @@ enum TurbopackImport {
},
}
impl Fold for NextDynamicPatcher {
fn fold_module_items(&mut self, mut items: Vec<ModuleItem>) -> Vec<ModuleItem> {
items = items.fold_children_with(self);
impl VisitMut for NextDynamicPatcher {
fn visit_mut_module_items(&mut self, items: &mut Vec<ModuleItem>) {
items.visit_mut_children_with(self);
self.maybe_add_dynamically_imported_specifier(&mut items);
items
self.maybe_add_dynamically_imported_specifier(items);
}
fn fold_import_decl(&mut self, decl: ImportDecl) -> ImportDecl {
let ImportDecl {
ref src,
ref specifiers,
..
} = decl;
if &src.value == "next/dynamic" {
for specifier in specifiers {
fn visit_mut_import_decl(&mut self, decl: &mut ImportDecl) {
if &decl.src.value == "next/dynamic" {
for specifier in &decl.specifiers {
if let ImportSpecifier::Default(default_specifier) = specifier {
self.dynamic_bindings.push(default_specifier.local.to_id());
}
}
}
decl
}
fn fold_call_expr(&mut self, expr: CallExpr) -> CallExpr {
fn visit_mut_call_expr(&mut self, expr: &mut CallExpr) {
if self.is_next_dynamic_first_arg {
if let Callee::Import(..) = &expr.callee {
match &*expr.args[0].expr {
......@@ -157,9 +148,12 @@ impl Fold for NextDynamicPatcher {
_ => {}
}
}
return expr.fold_children_with(self);
expr.visit_mut_children_with(self);
return;
}
let mut expr = expr.fold_children_with(self);
expr.visit_mut_children_with(self);
if let Callee::Expr(i) = &expr.callee {
if let Expr::Ident(identifier) = &**i {
if self.dynamic_bindings.contains(&identifier.to_id()) {
......@@ -172,7 +166,7 @@ impl Fold for NextDynamicPatcher {
)
.emit()
});
return expr;
return;
} else if expr.args.len() > 2 {
HANDLER.with(|handler| {
handler
......@@ -182,7 +176,7 @@ impl Fold for NextDynamicPatcher {
)
.emit()
});
return expr;
return;
}
if expr.args.len() == 2 {
match &*expr.args[1].expr {
......@@ -196,19 +190,19 @@ impl Fold for NextDynamicPatcher {
)
.emit();
});
return expr;
return;
}
}
}
self.is_next_dynamic_first_arg = true;
expr.args[0].expr = expr.args[0].expr.clone().fold_with(self);
expr.args[0].expr.visit_mut_with(self);
self.is_next_dynamic_first_arg = false;
let Some((dynamically_imported_specifier, dynamically_imported_specifier_span)) =
self.dynamically_imported_specifier.take()
else {
return expr;
return;
};
let project_dir = match self.pages_or_app_dir.as_deref() {
......@@ -419,7 +413,6 @@ impl Fold for NextDynamicPatcher {
}
}
}
expr
}
}
......
......@@ -5,7 +5,7 @@ use swc_core::{
common::DUMMY_SP,
ecma::{
ast::*,
visit::{fold_pass, Fold},
visit::{visit_mut_pass, VisitMut},
},
};
......@@ -14,8 +14,8 @@ pub struct Config {
pub packages: Vec<String>,
}
pub fn named_import_transform(config: Config) -> impl Pass {
fold_pass(NamedImportTransform {
pub fn named_import_transform(config: Config) -> impl Pass + VisitMut {
visit_mut_pass(NamedImportTransform {
packages: config.packages,
})
}
......@@ -25,9 +25,8 @@ struct NamedImportTransform {
packages: Vec<String>,
}
/// TODO: Implement this as a [Pass] instead of a full visitor ([Fold])
impl Fold for NamedImportTransform {
fn fold_import_decl(&mut self, decl: ImportDecl) -> ImportDecl {
impl VisitMut for NamedImportTransform {
fn visit_mut_import_decl(&mut self, decl: &mut ImportDecl) {
// Match named imports and check if it's included in the packages
let src_value = &decl.src.value;
......@@ -75,18 +74,12 @@ impl Fold for NamedImportTransform {
src_value.to_string_lossy()
);
// Create a new import declaration, keep everything the same except the source
let mut new_decl = decl.clone();
*new_decl.src = Str {
*decl.src = Str {
span: DUMMY_SP,
value: new_src.into(),
raw: None,
};
return new_decl;
}
}
decl
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment