Skip to content

Commit ede1a52

Browse files
authored
feat(es/minifier): Drop more patterns with PURE marker (#9478)
**Related issue:** - Closes #9459 - Closes #9460 - Closes #9465
1 parent b0b5e36 commit ede1a52

File tree

4 files changed

+47
-14
lines changed

4 files changed

+47
-14
lines changed

Diff for: .changeset/smooth-pets-beam.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
swc_ecma_minifier: patch
3+
swc_core: patch
4+
---
5+
6+
feat(es/minifier): Drop more patterns with `PURE` marker

Diff for: crates/swc_ecma_minifier/src/compress/optimize/unused.rs

+34-14
Original file line numberDiff line numberDiff line change
@@ -315,21 +315,31 @@ impl Optimizer<'_> {
315315

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

318+
let pure_mark = self.marks.pure;
319+
let has_pure_ann = match init {
320+
Some(Expr::Call(c)) => c.ctxt.has_mark(pure_mark),
321+
Some(Expr::New(n)) => n.ctxt.has_mark(pure_mark),
322+
Some(Expr::TaggedTpl(t)) => t.ctxt.has_mark(pure_mark),
323+
_ => false,
324+
};
325+
318326
if !name.is_ident() {
319327
// TODO: Use smart logic
320-
if self.options.pure_getters != PureGetterOption::Bool(true) {
328+
if self.options.pure_getters != PureGetterOption::Bool(true) && !has_pure_ann {
321329
return;
322330
}
323331

324-
if let Some(init) = init.as_mut() {
325-
if self.should_preserve_property_access(
326-
init,
327-
PropertyAccessOpts {
328-
allow_getter: false,
329-
only_ident: false,
330-
},
331-
) {
332-
return;
332+
if !has_pure_ann {
333+
if let Some(init) = init.as_mut() {
334+
if self.should_preserve_property_access(
335+
init,
336+
PropertyAccessOpts {
337+
allow_getter: false,
338+
only_ident: false,
339+
},
340+
) {
341+
return;
342+
}
333343
}
334344
}
335345
}
@@ -361,6 +371,10 @@ impl Optimizer<'_> {
361371
None => {}
362372
}
363373
}
374+
375+
if has_pure_ann && arr.elems.iter().all(|e| e.is_none()) {
376+
name.take();
377+
}
364378
}
365379

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

383397
self.take_pat_if_unused(&mut p.value, None, is_var_decl);
384398
}
385-
ObjectPatProp::Assign(AssignPatProp {
386-
key, value: None, ..
387-
}) => {
388-
self.take_ident_of_pat_if_unused(key, None);
399+
ObjectPatProp::Assign(AssignPatProp { key, value, .. }) => {
400+
if has_pure_ann {
401+
if let Some(e) = value {
402+
*value = self.ignore_return_value(e).map(Box::new);
403+
}
404+
}
405+
406+
if value.is_none() {
407+
self.take_ident_of_pat_if_unused(key, None);
408+
}
389409
}
390410
_ => {}
391411
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
export default function Component() {
3+
const [state, setState] = /*#__PURE__*/ useState();
4+
const { a, b = 'b' } = /*#__PURE__*/ call();
5+
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default function Component() {}

0 commit comments

Comments
 (0)