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(minifier): do not fold if statement block with lexical declaration #7519

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
11 changes: 5 additions & 6 deletions crates/oxc_allocator/src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,11 @@ impl<'alloc, T> ops::Index<usize> for Vec<'alloc, T> {
}
}

// Unused right now.
// impl<'alloc, T> ops::IndexMut<usize> for Vec<'alloc, T> {
// fn index_mut(&mut self, index: usize) -> &mut Self::Output {
// self.0.index_mut(index)
// }
// }
impl<'alloc, T> ops::IndexMut<usize> for Vec<'alloc, T> {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
self.0.index_mut(index)
}
}

#[cfg(any(feature = "serialize", test))]
impl<'alloc, T> Serialize for Vec<'alloc, T>
Expand Down
21 changes: 17 additions & 4 deletions crates/oxc_minifier/src/ast_passes/peephole_minimize_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,19 @@ impl<'a> PeepholeMinimizeConditions {
/// Duplicate logic to DCE part.
fn try_fold_if_block_one(&mut self, if_stmt: &mut IfStatement<'a>, ctx: &mut TraverseCtx<'a>) {
if let Statement::BlockStatement(block) = &mut if_stmt.consequent {
if block.body.len() == 1 {
if block.body.len() == 1
&& !matches!(&block.body[0], Statement::VariableDeclaration(decl) if !decl.kind.is_var())
{
self.changed = true;
if_stmt.consequent = ctx.ast.move_statement(block.body.first_mut().unwrap());
if_stmt.consequent = ctx.ast.move_statement(&mut block.body[0]);
}
}
if let Some(Statement::BlockStatement(block)) = &mut if_stmt.alternate {
if block.body.len() == 1 {
if block.body.len() == 1
&& !matches!(&block.body[0], Statement::VariableDeclaration(decl) if !decl.kind.is_var())
{
self.changed = true;
if_stmt.alternate = Some(ctx.ast.move_statement(block.body.first_mut().unwrap()));
if_stmt.alternate = Some(ctx.ast.move_statement(&mut block.body[0]));
}
}
}
Expand Down Expand Up @@ -241,6 +245,15 @@ mod test {
fold_same("function f(){foo()}");
fold_same("switch(x){case y: foo()}");
fold_same("try{foo()}catch(ex){bar()}finally{baz()}");

// Dot not fold `let` and `const`.
// Lexical declaration cannot appear in a single-statement context.
fold_same("if (foo) { const bar = 1 } else { const baz = 1 }");
fold_same("if (foo) { let bar = 1 } else { let baz = 1 }");
fold(
"if (foo) { var bar = 1 } else { var baz = 1 }",
"if (foo) var bar = 1; else var baz = 1;",
);
}

/** Try to minimize returns */
Expand Down
Loading