Skip to content

Commit

Permalink
fix(es/minifier): Remove optimization for array pattern (#9241)
Browse files Browse the repository at this point in the history
**Description:**

This could be an unsound optimization. The issue shows a bad case. 

**Related issue:**

 - Closes #8918
  • Loading branch information
CPunisher authored Jul 15, 2024
1 parent 0406178 commit 521161e
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{borrow::Cow, cmp::min, iter::once, mem::take};
use std::{borrow::Cow, iter::once, mem::take};

use swc_common::{
pass::{CompilerPass, Repeated},
Expand Down Expand Up @@ -75,32 +75,6 @@ impl Parallel for Remover {
impl VisitMut for Remover {
standard_only_visit_mut!();

fn visit_mut_array_pat(&mut self, p: &mut ArrayPat) {
p.visit_mut_children_with(self);

let mut preserved = None;
let len = p.elems.len();
for (i, p) in p.elems.iter().enumerate() {
let can_be_removed = match p {
Some(Pat::Array(ref p)) if p.elems.is_empty() => true,
Some(Pat::Object(ref p)) if p.props.is_empty() => true,
_ => false,
};

if !can_be_removed {
preserved = Some(min(i + 1, len))
}
}

if let Some(i) = preserved {
if cfg!(feature = "debug") {
debug!("Removing elements of an array pattern");
}

p.elems.drain(i..);
}
}

fn visit_mut_expr(&mut self, e: &mut Expr) {
e.visit_mut_children_with(self);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1432,16 +1432,6 @@ fn test_empty_pattern_in_for_of_loop_not_removed() {
test_same("for ({} of foo());");
}

#[test]
fn test_empty_slot_in_array_pattern_removed() {
test("[,,] = foo();", "foo()");
test("[a,b,,] = foo();", "[a,b] = foo();");
test("[a,[],b,[],[]] = foo();", "[a,[],b] = foo();");
test("[a,{},b,{},{}] = foo();", "[a,{},b] = foo();");
test("function f([,,,]) {}", "function f([]) {}");
test_same("[[], [], [], ...rest] = foo()");
}

#[test]
#[ignore]
fn test_empty_slot_in_array_pattern_with_default_value_maybe_removed_1() {
Expand Down

0 comments on commit 521161e

Please sign in to comment.