Skip to content

Commit b5fed8c

Browse files
authored
feat(es/minifier): Remove unused parts of destructuring (#11024)
**Description:** If `pure_getters` is `true`, remove unused vars of destructuring **while `init` is `Expr::Ident`**. We should follow user's configuration at least in simple scenarios. **Related issue:** - Closes #10962
1 parent bc83875 commit b5fed8c

File tree

8 files changed

+83
-8
lines changed

8 files changed

+83
-8
lines changed

.changeset/five-rockets-deny.md

Lines changed: 6 additions & 0 deletions
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+
Remove unused vars of destructuring while 'init' is identifier

crates/swc_ecma_minifier/src/compress/optimize/unused.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -356,13 +356,15 @@ impl Optimizer<'_> {
356356

357357
if !has_pure_ann {
358358
if let Some(init) = init.as_mut() {
359-
if self.should_preserve_property_access(
360-
init,
361-
PropertyAccessOpts {
362-
allow_getter: false,
363-
only_ident: false,
364-
},
365-
) {
359+
if !matches!(init, Expr::Ident(_))
360+
&& self.should_preserve_property_access(
361+
init,
362+
PropertyAccessOpts {
363+
allow_getter: false,
364+
only_ident: false,
365+
},
366+
)
367+
{
366368
return;
367369
}
368370
}

crates/swc_ecma_minifier/tests/exec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2740,7 +2740,7 @@ extract({
27402740
b: 4,
27412741
});"#;
27422742
let config = r#"{
2743-
"pure_getters": true,
2743+
"pure_getters": false,
27442744
"unused": true
27452745
}"#;
27462746

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"pure_getters": true,
3+
"side_effects": true,
4+
"toplevel": true,
5+
"unused": true,
6+
"passes": 1
7+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import declare from "phantom";
2+
const declared = declare();
3+
const { a1, b1, c1 = "c" } = declared;
4+
const { a2, b2, c2 = "c" } = undeclared;
5+
const { a3, b3, c3 = "c" } = window;
6+
function fn({ a, b, c = "c", d }) {
7+
console.log(d);
8+
}
9+
fn({
10+
a: "a",
11+
get b() {
12+
console.log("side effect of b");
13+
return "b";
14+
},
15+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import declare from "phantom";
2+
const declared = declare();
3+
const { c1 = "c" } = declared;
4+
const { c2 = "c" } = undeclared;
5+
const { c3 = "c" } = window;
6+
function fn({ c = "c", d }) {
7+
console.log(d);
8+
}
9+
fn({
10+
a: "a",
11+
get b () {
12+
console.log("side effect of b");
13+
return "b";
14+
}
15+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import o from "phantom";
2+
const c = o();
3+
const { a1: n, b1: t, c1: e = "c" } = c;
4+
const { a2: s, b2: f, c2: l = "c" } = undeclared;
5+
const { a3: r, b3: a, c3: d = "c" } = window;
6+
function i({ a: o, b: c, c: n = "c", d: t }) {
7+
console.log(t);
8+
}
9+
i({
10+
a: "a",
11+
get b () {
12+
console.log("side effect of b");
13+
return "b";
14+
}
15+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import declare from "phantom";
2+
const declared = declare();
3+
const { c1 = "c" } = declared;
4+
const { c2 = "c" } = undeclared;
5+
const { c3 = "c" } = window;
6+
function fn({ c = "c", d }) {
7+
console.log(d);
8+
}
9+
fn({
10+
a: "a",
11+
get b () {
12+
console.log("side effect of b");
13+
return "b";
14+
}
15+
});

0 commit comments

Comments
 (0)