Skip to content

Commit

Permalink
fix(minifier): prevent removing if when side effected
Browse files Browse the repository at this point in the history
  • Loading branch information
7086cmd committed Nov 10, 2024
1 parent e2101f9 commit 7771afc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
12 changes: 12 additions & 0 deletions crates/oxc_minifier/src/ast_passes/peephole_remove_dead_code.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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))
}
Expand Down Expand Up @@ -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)) {}");
}
}
4 changes: 2 additions & 2 deletions tasks/minsize/minsize.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 7771afc

Please sign in to comment.