mirror of
https://github.com/swc-project/swc.git
synced 2024-11-29 11:47:21 +03:00
feat(es/minifier): Drop more patterns with PURE
marker (#9478)
**Related issue:** - Closes https://github.com/swc-project/swc/issues/9459 - Closes https://github.com/swc-project/swc/issues/9460 - Closes https://github.com/swc-project/swc/pull/9465
This commit is contained in:
parent
b0b5e36675
commit
ede1a52cb8
6
.changeset/smooth-pets-beam.md
Normal file
6
.changeset/smooth-pets-beam.md
Normal file
@ -0,0 +1,6 @@
|
||||
---
|
||||
swc_ecma_minifier: patch
|
||||
swc_core: patch
|
||||
---
|
||||
|
||||
feat(es/minifier): Drop more patterns with `PURE` marker
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -361,6 +371,10 @@ impl Optimizer<'_> {
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
|
||||
if has_pure_ann && arr.elems.iter().all(|e| e.is_none()) {
|
||||
name.take();
|
||||
}
|
||||
}
|
||||
|
||||
Pat::Object(obj) => {
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
@ -0,0 +1,6 @@
|
||||
|
||||
export default function Component() {
|
||||
const [state, setState] = /*#__PURE__*/ useState();
|
||||
const { a, b = 'b' } = /*#__PURE__*/ call();
|
||||
|
||||
}
|
@ -0,0 +1 @@
|
||||
export default function Component() {}
|
Loading…
Reference in New Issue
Block a user