diff --git a/crates/oxc_minifier/src/ctx.rs b/crates/oxc_minifier/src/ctx.rs index 6e8a657d5e6b8..7e590e2b9402f 100644 --- a/crates/oxc_minifier/src/ctx.rs +++ b/crates/oxc_minifier/src/ctx.rs @@ -12,6 +12,7 @@ use oxc_span::format_atom; use oxc_syntax::{ identifier::{is_identifier_part, is_identifier_start}, reference::ReferenceId, + scope::ScopeFlags, }; use oxc_traverse::Ancestor; @@ -196,9 +197,14 @@ impl<'a> Ctx<'a, '_> { } } - let scope_id = self.scoping.current_scope_id(); + let scope_id = self.scoping().symbol_scope_id(symbol_id); + let scope_flags = self.scoping().scope_flags(scope_id); + + let initialized_constant = + if scope_flags.contains(ScopeFlags::DirectEval) { None } else { constant }; + let symbol_value = SymbolValue { - initialized_constant: constant, + initialized_constant, exported, read_references_count, write_references_count, diff --git a/crates/oxc_minifier/tests/peephole/inline_single_use_variable.rs b/crates/oxc_minifier/tests/peephole/inline_single_use_variable.rs index 90fd44b633041..1e81c95b12b31 100644 --- a/crates/oxc_minifier/tests/peephole/inline_single_use_variable.rs +++ b/crates/oxc_minifier/tests/peephole/inline_single_use_variable.rs @@ -28,6 +28,17 @@ fn test_inline_single_use_variable() { test_same("async function wrapper(arg0, arg1) { await using x = foo; return x}"); test_same("function wrapper(arg0) { eval('x'); var x = arg0; return x }"); + test("{ let _; eval('_ = () => 1'); _() }", "{ let _; eval('_ = () => 1'), _() }"); + test("{ let x = 1; eval('x = 2'); log(x) }", "{ let x = 1; eval('x = 2'), log(x) }"); + test( + "function f() { let x; eval('x = 1'); return x }", + "function f() { let x; return eval('x = 1'), x }", + ); + test( + "function f() { { let y; var x; } eval('x = 1'); return x }", + "function f() { { let y; var x; } return eval('x = 1'), x }", + ); + test( " class Foo {