Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(es/minifier): Remove optimization for array pattern #9241

Merged
merged 3 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading