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