Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix(es/minifier): take ident for ObjectPatProp::Assign with value: Some(_) #9465

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion crates/swc_ecma_minifier/src/compress/optimize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,7 @@ impl Optimizer<'_> {
Expr::Call(CallExpr {
callee: Callee::Expr(callee),
args,
ctxt,
..
}) => {
if let Expr::Fn(FnExpr {
Expand Down Expand Up @@ -875,7 +876,9 @@ impl Optimizer<'_> {
if let Expr::Ident(callee) = &**callee {
if self.options.reduce_vars && self.options.side_effects {
if let Some(usage) = self.data.vars.get(&callee.to_id()) {
if !usage.reassigned && usage.pure_fn {
if !usage.reassigned
&& (usage.pure_fn || ctxt.has_mark(self.marks.pure))
{
self.changed = true;
report_change!("Reducing function call to a variable");

Expand Down
64 changes: 58 additions & 6 deletions crates/swc_ecma_minifier/src/compress/optimize/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,53 @@ impl Optimizer<'_> {
params.retain(|p| !p.is_invalid());
}

#[cfg_attr(feature = "debug", tracing::instrument(skip_all))]
fn take_ident_of_obj_pat_if_unused_and_side_effect_free(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK.
This pr could be closed and then follow #9478.

&mut self,
i: &mut Ident,
init: Option<&mut Expr>,
) {
trace_op!("unused: Checking identifier `{}`", i);

if !self.may_remove_ident(i) {
log_abort!("unused: Preserving var `{:#?}` because it's top-level", i);
return;
}

if let Some(v) = self.data.vars.get(&i.to_id()).cloned() {
if v.is_unused() {
if let Some(init) = init {
let eft = self.ignore_return_value(init);
if let Some(eft) = eft {
*init = eft;
log_abort!(
"unused: Cannot drop ({}) because its init expression has side effects",
dump(&*i, false)
);
return;
}

self.changed = true;
report_change!(
"unused: Dropping a variable '{}{:?}' because it is not used",
i.sym,
i.ctxt
);

// This will remove variable.
i.take();
}

return;
}

log_abort!(
"unused: Cannot drop ({}) because it's used",
dump(&*i, false)
);
}
}

#[cfg_attr(feature = "debug", tracing::instrument(skip_all))]
fn take_ident_of_pat_if_unused(&mut self, i: &mut Ident, init: Option<&mut Expr>) {
trace_op!("unused: Checking identifier `{}`", i);
Expand All @@ -174,12 +221,7 @@ impl Optimizer<'_> {
}

if let Some(v) = self.data.vars.get(&i.to_id()).cloned() {
if v.ref_count == 0
&& v.usage_count == 0
&& !v.reassigned
&& v.property_mutation_count == 0
&& !v.declared_as_catch_param
{
if v.is_unused() {
self.changed = true;
report_change!(
"unused: Dropping a variable '{}{:?}' because it is not used",
Expand Down Expand Up @@ -387,6 +429,16 @@ impl Optimizer<'_> {
}) => {
self.take_ident_of_pat_if_unused(key, None);
}
ObjectPatProp::Assign(AssignPatProp {
key,
value: Some(value),
..
}) => {
self.take_ident_of_obj_pat_if_unused_and_side_effect_free(
key,
Some(value.as_mut()),
);
}
_ => {}
}
}
Expand Down
8 changes: 8 additions & 0 deletions crates/swc_ecma_minifier/src/program_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,14 @@ impl Default for VarUsageInfo {
}

impl VarUsageInfo {
pub(crate) fn is_unused(&self) -> bool {
self.ref_count == 0
&& self.usage_count == 0
&& !self.reassigned
&& self.property_mutation_count == 0
&& !self.declared_as_catch_param
}

pub(crate) fn is_infected(&self) -> bool {
!self.infects_to.is_empty()
}
Expand Down
Loading
Loading