Skip to content

Commit

Permalink
Do not recurse
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 committed Jan 26, 2025
1 parent 28b03ea commit 74393af
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 20 deletions.
8 changes: 3 additions & 5 deletions crates/swc_ecma_minifier/src/compress/optimize/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -685,11 +685,9 @@ impl Optimizer<'_> {

for i in collect_infects_from(
&f.function,
AliasConfig {
marks: Some(self.marks),
ignore_nested: false,
need_all: true,
},
AliasConfig::default()
.marks(Some(self.marks))
.need_all(true),
) {
if let Some(usage) = self.data.vars.get_mut(&i.0) {
usage.ref_count += 1;
Expand Down
27 changes: 12 additions & 15 deletions crates/swc_ecma_minifier/src/compress/optimize/sequences.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1150,33 +1150,30 @@ impl Optimizer<'_> {
Mergable::Var(a) => a.init.as_ref().map(|init| {
collect_infects_from(
init,
AliasConfig {
marks: Some(self.marks),
ignore_nested: true,
need_all: true,
},
AliasConfig::default()
.marks(Some(self.marks))
.ignore_nested(true)
.need_all(true),
)
}),
Mergable::Expr(a) => match a {
Expr::Assign(a) if a.is_simple_assign() => Some(collect_infects_from(
&a.right,
AliasConfig {
marks: Some(self.marks),
ignore_nested: true,
need_all: true,
},
AliasConfig::default()
.marks(Some(self.marks))
.ignore_nested(true)
.need_all(true),
)),

_ => None,
},

Mergable::FnDecl(a) => Some(collect_infects_from(
&a.function,
AliasConfig {
marks: Some(self.marks),
ignore_nested: true,
need_all: true,
},
AliasConfig::default()
.marks(Some(self.marks))
.ignore_nested(true)
.need_all(true),
)),

Mergable::Drop => return false,
Expand Down
57 changes: 57 additions & 0 deletions crates/swc_ecma_usage_analyzer/src/alias/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,36 @@ use crate::{marks::Marks, util::is_global_var_with_pure_property_access};
mod ctx;

#[derive(Default)]
#[non_exhaustive]
pub struct AliasConfig {
pub marks: Option<Marks>,
pub ignore_nested: bool,
/// TODO(kdy1): This field is used for sequential inliner.
/// It should be renamed to some correct name.
pub need_all: bool,

pub ignore_named_child_scope: bool,
}
impl AliasConfig {
pub fn marks(mut self, arg: Option<Marks>) -> Self {
self.marks = arg;
self
}

pub fn ignore_nested(mut self, arg: bool) -> Self {
self.ignore_nested = arg;
self
}

pub fn ignore_named_child_scope(mut self, arg: bool) -> Self {
self.ignore_named_child_scope = arg;
self
}

pub fn need_all(mut self, arg: bool) -> Self {
self.need_all = arg;
self
}
}

pub trait InfectableNode {
Expand Down Expand Up @@ -293,4 +317,37 @@ impl Visit for InfectionCollector<'_> {
};
n.visit_children_with(&mut *self.with_ctx(ctx));
}

fn visit_fn_decl(&mut self, n: &FnDecl) {
if self.config.ignore_named_child_scope {
return;
}

n.visit_children_with(self);
}

fn visit_fn_expr(&mut self, n: &FnExpr) {
if self.config.ignore_named_child_scope && n.ident.is_some() {
return;
}
n.visit_children_with(self);
}

fn visit_assign_expr(&mut self, n: &AssignExpr) {
if self.config.ignore_named_child_scope && n.op == op!("=") && n.left.is_simple() {
return;
}

n.visit_children_with(self);
}

fn visit_var_declarator(&mut self, n: &VarDeclarator) {
if self.config.ignore_named_child_scope {
if let (Pat::Ident(..), Some(..)) = (&n.name, n.init.as_deref()) {
return;
}
}

n.visit_children_with(self);
}
}
4 changes: 4 additions & 0 deletions crates/swc_ecma_usage_analyzer/src/analyzer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ where
&n.right,
AliasConfig {
marks: self.marks,
ignore_named_child_scope: true,
..Default::default()
},
) {
Expand Down Expand Up @@ -758,6 +759,7 @@ where
&n.function,
AliasConfig {
marks: self.marks,
ignore_named_child_scope: true,
..Default::default()
},
) {
Expand Down Expand Up @@ -788,6 +790,7 @@ where
&n.function,
AliasConfig {
marks: self.marks,
ignore_named_child_scope: true,
..Default::default()
},
) {
Expand Down Expand Up @@ -1285,6 +1288,7 @@ where
init,
AliasConfig {
marks: self.marks,
ignore_named_child_scope: true,
..Default::default()
},
) {
Expand Down

0 comments on commit 74393af

Please sign in to comment.