diff --git a/crates/oxc_minifier/src/ast_passes/peephole_remove_dead_code.rs b/crates/oxc_minifier/src/ast_passes/peephole_remove_dead_code.rs index 3f9c1d1d30b02a..86293a9f82c462 100644 --- a/crates/oxc_minifier/src/ast_passes/peephole_remove_dead_code.rs +++ b/crates/oxc_minifier/src/ast_passes/peephole_remove_dead_code.rs @@ -1,6 +1,7 @@ use oxc_allocator::Vec; use oxc_ast::{ast::*, Visit}; use oxc_ecmascript::constant_evaluation::{ConstantEvaluation, IsLiteralValue}; +use oxc_ecmascript::side_effects::MayHaveSideEffects; use oxc_span::SPAN; use oxc_traverse::{Ancestor, Traverse, TraverseCtx}; @@ -177,6 +178,11 @@ impl<'a, 'b> PeepholeRemoveDeadCode { match ctx.get_boolean_value(&if_stmt.test) { Some(true) => { + // FIXME we should eliminate the statement, but need to prevent removing side effect statements. + // so firstly I just avoid removing the statement if it has side effects. + if if_stmt.test.may_have_side_effects() { + return None; + } // self.changed = true; Some(ctx.ast.move_statement(&mut if_stmt.consequent)) } @@ -555,4 +561,10 @@ mod test { fold("([...a, b, ...c])", "([...a, ...c])"); fold_same("([...b, ...c])"); // It would also be fine if the spreads were split apart. } + + #[test] + fn test_issue_7209() { + // TODO + fold_same("if (((() => console.log('effect'))(), true)) {}"); + } } diff --git a/tasks/minsize/minsize.snap b/tasks/minsize/minsize.snap index 375432cb7a48d6..5bdfad8113e4be 100644 --- a/tasks/minsize/minsize.snap +++ b/tasks/minsize/minsize.snap @@ -18,9 +18,9 @@ Original | Minified | esbuild | Gzip | esbuild 2.14 MB | 741.57 kB | 724.14 kB | 181.45 kB | 181.07 kB | victory.js -3.20 MB | 1.02 MB | 1.01 MB | 332.01 kB | 331.56 kB | echarts.js +3.20 MB | 1.02 MB | 1.01 MB | 332.08 kB | 331.56 kB | echarts.js -6.69 MB | 2.39 MB | 2.31 MB | 496.10 kB | 488.28 kB | antd.js +6.69 MB | 2.39 MB | 2.31 MB | 496.12 kB | 488.28 kB | antd.js 10.95 MB | 3.56 MB | 3.49 MB | 911.23 kB | 915.50 kB | typescript.js