Skip to content

Commit 521161e

Browse files
authoredJul 15, 2024··
fix(es/minifier): Remove optimization for array pattern (#9241)
**Description:** This could be an unsound optimization. The issue shows a bad case. **Related issue:** - Closes #8918
1 parent 0406178 commit 521161e

File tree

2 files changed

+1
-37
lines changed
  • crates/swc_ecma_transforms_optimization/src/simplify/branch

2 files changed

+1
-37
lines changed
 

‎crates/swc_ecma_transforms_optimization/src/simplify/branch/mod.rs

+1-27
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{borrow::Cow, cmp::min, iter::once, mem::take};
1+
use std::{borrow::Cow, iter::once, mem::take};
22

33
use swc_common::{
44
pass::{CompilerPass, Repeated},
@@ -75,32 +75,6 @@ impl Parallel for Remover {
7575
impl VisitMut for Remover {
7676
standard_only_visit_mut!();
7777

78-
fn visit_mut_array_pat(&mut self, p: &mut ArrayPat) {
79-
p.visit_mut_children_with(self);
80-
81-
let mut preserved = None;
82-
let len = p.elems.len();
83-
for (i, p) in p.elems.iter().enumerate() {
84-
let can_be_removed = match p {
85-
Some(Pat::Array(ref p)) if p.elems.is_empty() => true,
86-
Some(Pat::Object(ref p)) if p.props.is_empty() => true,
87-
_ => false,
88-
};
89-
90-
if !can_be_removed {
91-
preserved = Some(min(i + 1, len))
92-
}
93-
}
94-
95-
if let Some(i) = preserved {
96-
if cfg!(feature = "debug") {
97-
debug!("Removing elements of an array pattern");
98-
}
99-
100-
p.elems.drain(i..);
101-
}
102-
}
103-
10478
fn visit_mut_expr(&mut self, e: &mut Expr) {
10579
e.visit_mut_children_with(self);
10680

‎crates/swc_ecma_transforms_optimization/src/simplify/branch/tests.rs

-10
Original file line numberDiff line numberDiff line change
@@ -1432,16 +1432,6 @@ fn test_empty_pattern_in_for_of_loop_not_removed() {
14321432
test_same("for ({} of foo());");
14331433
}
14341434

1435-
#[test]
1436-
fn test_empty_slot_in_array_pattern_removed() {
1437-
test("[,,] = foo();", "foo()");
1438-
test("[a,b,,] = foo();", "[a,b] = foo();");
1439-
test("[a,[],b,[],[]] = foo();", "[a,[],b] = foo();");
1440-
test("[a,{},b,{},{}] = foo();", "[a,{},b] = foo();");
1441-
test("function f([,,,]) {}", "function f([]) {}");
1442-
test_same("[[], [], [], ...rest] = foo()");
1443-
}
1444-
14451435
#[test]
14461436
#[ignore]
14471437
fn test_empty_slot_in_array_pattern_with_default_value_maybe_removed_1() {

0 commit comments

Comments
 (0)
Please sign in to comment.