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
13 changes: 13 additions & 0 deletions crates/oxc_minifier/src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,17 @@ impl<'a> Ctx<'a, '_> {
})
.unwrap_or_default()
}

/// Whether the assignment expression needs to be kept to preserve the name
pub fn is_expression_whose_name_needs_to_be_kept(&self, expr: &Expression) -> bool {
let options = &self.options().keep_names;
if !options.class && !options.function {
return false;
}
if !expr.is_anonymous_function_definition() {
return false;
}
let is_class = matches!(expr.without_parentheses(), Expression::ClassExpression(_));
(options.class && is_class) || (options.function && !is_class)
}
}
3 changes: 3 additions & 0 deletions crates/oxc_minifier/src/peephole/minimize_statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,9 @@ impl<'a> PeepholeOptimizations {
let BindingPatternKind::BindingIdentifier(prev_decl_id) = &prev_decl.id.kind else {
return true;
};
if ctx.is_expression_whose_name_needs_to_be_kept(prev_decl_init) {
return true;
}
let Some(symbol_value) =
ctx.state.symbol_values.get_symbol_value(prev_decl_id.symbol_id())
else {
Expand Down
49 changes: 48 additions & 1 deletion crates/oxc_minifier/tests/peephole/inline_single_use_variable.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use oxc_minifier::{CompressOptions, CompressOptionsKeepNames};
use oxc_span::SourceType;

use crate::{default_options, test, test_options_source_type, test_same};
use crate::{default_options, test, test_options, test_options_source_type, test_same};

#[track_caller]
fn test_script_same(source_text: &str) {
Expand All @@ -12,6 +13,15 @@ fn test_script(source_text: &str, expected: &str) {
test_options_source_type(source_text, expected, SourceType::cjs(), &default_options());
}

#[track_caller]
fn test_keep_names(source_text: &str, expected: &str) {
test_options(
source_text,
expected,
&CompressOptions { keep_names: CompressOptionsKeepNames::all_true(), ..default_options() },
);
}

#[test]
fn test_inline_single_use_variable() {
test_same("function wrapper(arg0, arg1) {using x = foo; return x}");
Expand Down Expand Up @@ -146,6 +156,43 @@ fn keep_exposed_variables() {
test_script("{ let x = foo; x() }", "foo()");
}

#[test]
fn keep_names() {
test(
"var x = function() {}; var y = x; console.log(y.name)",
"var y = function() {}; console.log(y.name)",
);
test_keep_names(
"var x = function() {}; var y = x; console.log(y.name)",
"var x = function() {}, y = x; console.log(y.name)",
);
test_keep_names(
"var x = (function() {}); var y = x; console.log(y.name)",
"var x = (function() {}), y = x; console.log(y.name)",
);
test_keep_names(
"var x = function foo() {}; var y = x; console.log(y.name)",
"var y = function foo() {}; console.log(y.name)",
);

test(
"var x = class {}; var y = x; console.log(y.name)",
"var y = class {}; console.log(y.name)",
);
test_keep_names(
"var x = class {}; var y = x; console.log(y.name)",
"var x = class {}, y = x; console.log(y.name)",
);
test_keep_names(
"var x = (class {}); var y = x; console.log(y.name)",
"var x = (class {}), y = x; console.log(y.name)",
);
test_keep_names(
"var x = class Foo {}; var y = x; console.log(y.name)",
"var y = class Foo {}; console.log(y.name)",
);
}

#[test]
fn integration() {
test(
Expand Down
Loading