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

feat(minifier): remove label statement with empty body #8333

Merged
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
15 changes: 8 additions & 7 deletions crates/oxc_minifier/src/ast_passes/peephole_remove_dead_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,21 +251,21 @@ impl<'a, 'b> PeepholeRemoveDeadCode {
/// ```js
/// a: break a;
/// ```
fn try_fold_labeled(
labeled: &mut LabeledStatement<'a>,
ctx: Ctx<'a, 'b>,
) -> Option<Statement<'a>> {
let id = labeled.label.name.as_str();
fn try_fold_labeled(s: &mut LabeledStatement<'a>, ctx: Ctx<'a, 'b>) -> Option<Statement<'a>> {
let id = s.label.name.as_str();
// Check the first statement in the block, or just the `break [id] ` statement.
// Check if we need to remove the whole block.
match &mut labeled.body {
match &mut s.body {
Statement::BreakStatement(break_stmt)
if break_stmt.label.as_ref().is_some_and(|l| l.name.as_str() == id) => {}
Statement::BlockStatement(block) if block.body.first().is_some_and(|first| matches!(first, Statement::BreakStatement(break_stmt) if break_stmt.label.as_ref().is_some_and(|l| l.name.as_str() == id))) => {}
Statement::EmptyStatement(_) => {
return Some(ctx.ast.statement_empty(s.span))
}
_ => return None,
}
let mut var = KeepVar::new(ctx.ast);
var.visit_statement(&labeled.body);
var.visit_statement(&s.body);
let var_decl = var.get_variable_declaration_statement();
var_decl.unwrap_or(ctx.ast.statement_empty(SPAN)).into()
}
Expand Down Expand Up @@ -587,6 +587,7 @@ mod test {

fold_same("b: { var x = 1; } x = 2;");
fold_same("a: b: { var x = 1; } x = 2;");
fold("foo:;", "");
}

#[test]
Expand Down
Loading