Skip to content
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
4 changes: 3 additions & 1 deletion crates/oxc_minifier/src/peephole/minimize_statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ impl<'a> PeepholeOptimizations {
break;
}
}
if let Some(stmt) = keep_var.get_variable_declaration_statement() {
if let Some(stmt) = keep_var.get_variable_declaration_statement()
&& let Some(stmt) = Self::remove_unused_variable_declaration(stmt, ctx)
{
stmts.push(stmt);
}

Expand Down
20 changes: 17 additions & 3 deletions crates/oxc_minifier/src/peephole/remove_dead_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ impl<'a> PeepholeOptimizations {
} else {
keep_var.visit_statement(&if_stmt.consequent);
}
let var_stmt = keep_var.get_variable_declaration_statement();
let var_stmt = keep_var
.get_variable_declaration_statement()
.and_then(|stmt| Self::remove_unused_variable_declaration(stmt, ctx));
let has_var_stmt = var_stmt.is_some();
if let Some(var_stmt) = var_stmt {
if boolean {
Expand Down Expand Up @@ -508,10 +510,17 @@ impl<'a> PeepholeOptimizations {
#[cfg(test)]
mod test {
use crate::{
CompressOptions,
tester::{test, test_options, test_same, test_same_options},
CompressOptions, CompressOptionsUnused,
tester::{default_options, test, test_options, test_same, test_same_options},
};

#[track_caller]
fn test_unused(source_text: &str, expected: &str) {
let options =
CompressOptions { unused: CompressOptionsUnused::Remove, ..default_options() };
test_options(source_text, expected, &options);
}

#[test]
fn test_fold_block() {
test("{{foo()}}", "foo()");
Expand Down Expand Up @@ -629,6 +638,8 @@ mod test {
test("if (foo) {} else {}", "foo");
test("if (false) {}", "");
test("if (true) {}", "");
test("if (false) { var a; console.log(a) }", "if (0) var a");
test_unused("if (false) { var a; console.log(a) }", "");
}

#[test]
Expand Down Expand Up @@ -668,6 +679,9 @@ mod test {
test("while(true) { continue a; unreachable;}", "for(;;) continue a");
test("while(true) { throw a; unreachable;}", "for(;;) throw a");
test("while(true) { return a; unreachable;}", "for(;;) return a");

test("(function () { return; var a })()", "(function () { return; var a })()");
test_unused("(function () { return; var a })()", "");
}

#[test]
Expand Down
30 changes: 22 additions & 8 deletions crates/oxc_minifier/src/peephole/remove_unused_declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,46 @@ use crate::{CompressOptionsUnused, ctx::Ctx};
use super::PeepholeOptimizations;

impl<'a> PeepholeOptimizations {
fn can_remove_unused_declarators(ctx: &Ctx<'a, '_>) -> bool {
ctx.state.options.unused != CompressOptionsUnused::Keep
&& !Self::keep_top_level_var_in_script_mode(ctx)
&& !ctx.scoping().root_scope_flags().contains_direct_eval()
}

pub fn should_remove_unused_declarator(
decl: &VariableDeclarator<'a>,
ctx: &Ctx<'a, '_>,
) -> bool {
if ctx.state.options.unused == CompressOptionsUnused::Keep {
if !Self::can_remove_unused_declarators(ctx) {
return false;
}
if let BindingPatternKind::BindingIdentifier(ident) = &decl.id.kind {
// Unsafe to remove `using`, unable to statically determine usage of [Symbol.dispose].
if decl.kind.is_using() {
return false;
}
if Self::keep_top_level_var_in_script_mode(ctx) {
return false;
}
// It is unsafe to remove if direct eval is involved.
if ctx.scoping().root_scope_flags().contains_direct_eval() {
return false;
}
if let Some(symbol_id) = ident.symbol_id.get() {
return ctx.scoping().symbol_is_unused(symbol_id);
}
}
false
}

pub fn remove_unused_variable_declaration(
mut stmt: Statement<'a>,
ctx: &Ctx<'a, '_>,
) -> Option<Statement<'a>> {
let Statement::VariableDeclaration(var_decl) = &mut stmt else { return Some(stmt) };
if !Self::can_remove_unused_declarators(ctx) {
return Some(stmt);
}
var_decl.declarations.retain(|decl| !Self::should_remove_unused_declarator(decl, ctx));
if var_decl.declarations.is_empty() {
return None;
}
Some(stmt)
}

pub fn remove_unused_function_declaration(stmt: &mut Statement<'a>, ctx: &mut Ctx<'a, '_>) {
let Statement::FunctionDeclaration(f) = stmt else { return };
if ctx.state.options.unused == CompressOptionsUnused::Keep {
Expand Down
Loading