Skip to content

Commit

Permalink
feat(es/minifier): Drop more patterns with PURE marker (#9478)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes #9459
 - Closes #9460
 - Closes #9465
  • Loading branch information
kdy1 committed Aug 22, 2024
1 parent b0b5e36 commit ede1a52
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 14 deletions.
6 changes: 6 additions & 0 deletions .changeset/smooth-pets-beam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
swc_ecma_minifier: patch
swc_core: patch
---

feat(es/minifier): Drop more patterns with `PURE` marker
48 changes: 34 additions & 14 deletions crates/swc_ecma_minifier/src/compress/optimize/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,21 +315,31 @@ impl Optimizer<'_> {

trace_op!("unused: take_pat_if_unused({})", dump(&*name, false));

let pure_mark = self.marks.pure;
let has_pure_ann = match init {
Some(Expr::Call(c)) => c.ctxt.has_mark(pure_mark),
Some(Expr::New(n)) => n.ctxt.has_mark(pure_mark),
Some(Expr::TaggedTpl(t)) => t.ctxt.has_mark(pure_mark),
_ => false,
};

if !name.is_ident() {
// TODO: Use smart logic
if self.options.pure_getters != PureGetterOption::Bool(true) {
if self.options.pure_getters != PureGetterOption::Bool(true) && !has_pure_ann {
return;
}

if let Some(init) = init.as_mut() {
if self.should_preserve_property_access(
init,
PropertyAccessOpts {
allow_getter: false,
only_ident: false,
},
) {
return;
if !has_pure_ann {
if let Some(init) = init.as_mut() {
if self.should_preserve_property_access(
init,
PropertyAccessOpts {
allow_getter: false,
only_ident: false,
},
) {
return;
}
}
}
}
Expand Down Expand Up @@ -361,6 +371,10 @@ impl Optimizer<'_> {
None => {}
}
}

if has_pure_ann && arr.elems.iter().all(|e| e.is_none()) {
name.take();
}
}

Pat::Object(obj) => {
Expand All @@ -382,10 +396,16 @@ impl Optimizer<'_> {

self.take_pat_if_unused(&mut p.value, None, is_var_decl);
}
ObjectPatProp::Assign(AssignPatProp {
key, value: None, ..
}) => {
self.take_ident_of_pat_if_unused(key, None);
ObjectPatProp::Assign(AssignPatProp { key, value, .. }) => {
if has_pure_ann {
if let Some(e) = value {
*value = self.ignore_return_value(e).map(Box::new);
}
}

if value.is_none() {
self.take_ident_of_pat_if_unused(key, None);
}
}
_ => {}
}
Expand Down
6 changes: 6 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/9459/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

export default function Component() {
const [state, setState] = /*#__PURE__*/ useState();
const { a, b = 'b' } = /*#__PURE__*/ call();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default function Component() {}

0 comments on commit ede1a52

Please sign in to comment.