Skip to content

Commit

Permalink
fix(es/minifier): Fix removal of array pattern bindings (#8730)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes #8670
  • Loading branch information
kdy1 committed Mar 12, 2024
1 parent e46dd5a commit 312f0d8
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 11 deletions.
15 changes: 6 additions & 9 deletions crates/swc_ecma_minifier/src/compress/optimize/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,25 +345,22 @@ impl Optimizer<'_> {
}

Pat::Array(arr) => {
for (idx, elem) in arr.elems.iter_mut().enumerate() {
match elem {
for (idx, arr_elem) in arr.elems.iter_mut().enumerate() {
match arr_elem {
Some(p) => {
if p.is_ident() {
continue;
}

let elem = init
.as_mut()
.and_then(|expr| self.access_numeric_property(expr, idx));

self.take_pat_if_unused(p, elem, is_var_decl);

if p.is_invalid() {
*arr_elem = None;
}
}
None => {}
}
}

arr.elems
.retain(|elem| !matches!(elem, Some(Pat::Invalid(..))))
}

Pat::Object(obj) => {
Expand Down
1 change: 0 additions & 1 deletion crates/swc_ecma_minifier/tests/TODO.txt
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ harmony/issue_2874_2/input.js
harmony/module_enabled/input.js
harmony/module_mangle_scope/input.js
harmony/regression_cannot_use_of/input.js
hoist_props/contains_this_3/input.js
hoist_props/hoist_function_with_call/input.js
if_return/if_return_same_value/input.js
if_return/if_var_return/input.js
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"defaults": true,
"pure_getters": true,
"toplevel": true
}
2 changes: 2 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/8670/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const [a, b, c, d, e] = [1, 2, 3, 4, 5]
console.log(c)
8 changes: 8 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/8670/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const [, , c, , ] = [
1,
2,
3,
4,
5
];
console.log(c);
1 change: 1 addition & 0 deletions crates/swc_ecma_minifier/tests/passing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,7 @@ hoist/hoist_no_destructurings/input.js
hoist/hoist_vars/input.js
hoist_props/contains_this_1/input.js
hoist_props/contains_this_2/input.js
hoist_props/contains_this_3/input.js
hoist_props/direct_access_1/input.js
hoist_props/direct_access_2/input.js
hoist_props/direct_access_3/input.js
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
const [, w, , x, { z: z }] = [1, 2, 3, 4, { z: 5 }];
const [, , , x, { z: z }] = [
1,
2,
3,
4,
{
z: 5
}
];
console.log(x, z);

0 comments on commit 312f0d8

Please sign in to comment.