Skip to content

Commit 5642b29

Browse files
committed
fix(minifier): initialize constant value in DCE (#12610)
1 parent 75cf797 commit 5642b29

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

crates/oxc_minifier/src/peephole/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,15 @@ impl<'a> DeadCodeElimination {
335335
}
336336

337337
impl<'a> Traverse<'a, MinifierState<'a>> for DeadCodeElimination {
338+
fn exit_variable_declarator(
339+
&mut self,
340+
decl: &mut VariableDeclarator<'a>,
341+
ctx: &mut TraverseCtx<'a>,
342+
) {
343+
let mut ctx = Ctx::new(ctx);
344+
self.inner.init_symbol_value(decl, &mut ctx);
345+
}
346+
338347
fn exit_statement(&mut self, stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) {
339348
let mut ctx = Ctx::new(ctx);
340349
self.inner.remove_dead_code_exit_statement(stmt, &mut ctx);

crates/oxc_minifier/tests/peephole/dead_code_elimination.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ fn dce_logical_expression() {
140140

141141
test("foo = false && bar()", "foo = false");
142142
test("foo = true && bar()", "foo = bar()");
143+
144+
test(
145+
"const x = 'keep'; const y = 'remove'; foo(x || y), foo(y && x)",
146+
"const x = 'keep'; const y = 'remove'; foo(x), foo(x);",
147+
);
143148
}
144149

145150
#[test]

0 commit comments

Comments
 (0)