|
| 1 | +use oxc_allocator::TakeIn; |
| 2 | +use oxc_ast::ast::*; |
| 3 | +use oxc_ecmascript::side_effects::MayHaveSideEffects; |
| 4 | + |
| 5 | +use crate::{CompressOptionsUnused, ctx::Ctx}; |
| 6 | + |
| 7 | +use super::{PeepholeOptimizations, State}; |
| 8 | + |
| 9 | +impl<'a> PeepholeOptimizations { |
| 10 | + pub fn should_remove_unused_declarator( |
| 11 | + decl: &VariableDeclarator<'a>, |
| 12 | + ctx: &mut Ctx<'a, '_>, |
| 13 | + ) -> bool { |
| 14 | + if ctx.state.options.unused == CompressOptionsUnused::Keep { |
| 15 | + return false; |
| 16 | + } |
| 17 | + if let BindingPatternKind::BindingIdentifier(ident) = &decl.id.kind { |
| 18 | + if Self::keep_top_level_var_in_script_mode(ctx) { |
| 19 | + return false; |
| 20 | + } |
| 21 | + // It is unsafe to remove if direct eval is involved. |
| 22 | + if ctx.scoping().root_scope_flags().contains_direct_eval() { |
| 23 | + return false; |
| 24 | + } |
| 25 | + if let Some(symbol_id) = ident.symbol_id.get() { |
| 26 | + return ctx.scoping().symbol_is_unused(symbol_id); |
| 27 | + } |
| 28 | + } |
| 29 | + false |
| 30 | + } |
| 31 | + |
| 32 | + pub fn remove_unused_function_declaration( |
| 33 | + f: &Function<'a>, |
| 34 | + ctx: &mut Ctx<'a, '_>, |
| 35 | + ) -> Option<Statement<'a>> { |
| 36 | + if ctx.state.options.unused == CompressOptionsUnused::Keep { |
| 37 | + return None; |
| 38 | + } |
| 39 | + if Self::keep_top_level_var_in_script_mode(ctx) { |
| 40 | + return None; |
| 41 | + } |
| 42 | + let id = f.id.as_ref()?; |
| 43 | + let symbol_id = id.symbol_id.get()?; |
| 44 | + if ctx.scoping().symbol_is_unused(symbol_id) { |
| 45 | + return Some(ctx.ast.statement_empty(f.span)); |
| 46 | + } |
| 47 | + None |
| 48 | + } |
| 49 | + |
| 50 | + pub fn remove_unused_assignment_expression( |
| 51 | + &self, |
| 52 | + e: &mut Expression<'a>, |
| 53 | + state: &mut State, |
| 54 | + ctx: &mut Ctx<'a, '_>, |
| 55 | + ) -> bool { |
| 56 | + let Expression::AssignmentExpression(assign_expr) = e else { return false }; |
| 57 | + if matches!( |
| 58 | + ctx.state.options.unused, |
| 59 | + CompressOptionsUnused::Keep | CompressOptionsUnused::KeepAssign |
| 60 | + ) { |
| 61 | + return false; |
| 62 | + } |
| 63 | + let Some(SimpleAssignmentTarget::AssignmentTargetIdentifier(ident)) = |
| 64 | + assign_expr.left.as_simple_assignment_target() |
| 65 | + else { |
| 66 | + return false; |
| 67 | + }; |
| 68 | + if Self::keep_top_level_var_in_script_mode(ctx) { |
| 69 | + return false; |
| 70 | + } |
| 71 | + let Some(reference_id) = ident.reference_id.get() else { return false }; |
| 72 | + let Some(symbol_id) = ctx.scoping().get_reference(reference_id).symbol_id() else { |
| 73 | + return false; |
| 74 | + }; |
| 75 | + // Keep error for assigning to `const foo = 1; foo = 2`. |
| 76 | + if ctx.scoping().symbol_flags(symbol_id).is_const_variable() { |
| 77 | + return false; |
| 78 | + } |
| 79 | + if !ctx.scoping().get_resolved_references(symbol_id).all(|r| !r.flags().is_read()) { |
| 80 | + return false; |
| 81 | + } |
| 82 | + state.changed = true; |
| 83 | + if assign_expr.right.may_have_side_effects(ctx) { |
| 84 | + *e = assign_expr.right.take_in(ctx.ast); |
| 85 | + false |
| 86 | + } else { |
| 87 | + true |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + /// Do remove top level vars in script mode. |
| 92 | + fn keep_top_level_var_in_script_mode(ctx: &Ctx<'a, '_>) -> bool { |
| 93 | + ctx.scoping.current_scope_id() == ctx.scoping().root_scope_id() |
| 94 | + && ctx.source_type().is_script() |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +#[cfg(test)] |
| 99 | +mod test { |
| 100 | + use oxc_span::SourceType; |
| 101 | + |
| 102 | + use crate::{ |
| 103 | + CompressOptions, |
| 104 | + tester::{ |
| 105 | + test_options, test_options_source_type, test_same_options, |
| 106 | + test_same_options_source_type, |
| 107 | + }, |
| 108 | + }; |
| 109 | + |
| 110 | + #[test] |
| 111 | + fn remove_unused_variable_declaration() { |
| 112 | + let options = CompressOptions::smallest(); |
| 113 | + test_options("var x", "", &options); |
| 114 | + test_options("var x = 1", "", &options); |
| 115 | + test_options("var x = foo", "foo", &options); |
| 116 | + test_same_options("var x; foo(x)", &options); |
| 117 | + test_same_options("export var x", &options); |
| 118 | + } |
| 119 | + |
| 120 | + #[test] |
| 121 | + fn remove_unused_function_declaration() { |
| 122 | + let options = CompressOptions::smallest(); |
| 123 | + test_options("function foo() {}", "", &options); |
| 124 | + test_same_options("function foo() {} foo()", &options); |
| 125 | + test_same_options("export function foo() {} foo()", &options); |
| 126 | + } |
| 127 | + |
| 128 | + #[test] |
| 129 | + fn remove_unused_assignment_expression() { |
| 130 | + let options = CompressOptions::smallest(); |
| 131 | + test_options("var x = 1; x = 2;", "", &options); |
| 132 | + test_options("var x = 1; x = 2;", "", &options); |
| 133 | + test_options("var x = 1; x = foo();", "foo()", &options); |
| 134 | + test_same_options("var x = 1; x = 2, foo(x)", &options); |
| 135 | + test_same_options("function foo() { var t; return t = x(); } foo();", &options); |
| 136 | + } |
| 137 | + |
| 138 | + #[test] |
| 139 | + fn keep_in_script_mode() { |
| 140 | + let options = CompressOptions::smallest(); |
| 141 | + let source_type = SourceType::cjs(); |
| 142 | + test_same_options_source_type("var x = 1; x = 2;", source_type, &options); |
| 143 | + test_same_options_source_type("var x = 1; x = 2, foo(x)", source_type, &options); |
| 144 | + |
| 145 | + test_options_source_type( |
| 146 | + "function foo() { var x = 1; x = 2; bar() } foo()", |
| 147 | + "function foo() { bar() } foo()", |
| 148 | + source_type, |
| 149 | + &options, |
| 150 | + ); |
| 151 | + } |
| 152 | +} |
0 commit comments