Skip to content

Commit d553318

Browse files
authored
feat(minifier): complete MangleIf (#8810)
Co-authored-by: Boshen <1430279+Boshen@users.noreply.github.com>
1 parent 0afbc7b commit d553318

File tree

8 files changed

+243
-187
lines changed

8 files changed

+243
-187
lines changed

crates/oxc_ast/src/ast_impl/js.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -953,13 +953,15 @@ impl Statement<'_> {
953953
/// - `continue` => `true`
954954
/// - `if (true) { }` => `false`
955955
pub fn is_jump_statement(&self) -> bool {
956-
matches!(
957-
self,
958-
Self::ReturnStatement(_)
959-
| Self::ThrowStatement(_)
960-
| Self::BreakStatement(_)
961-
| Self::ContinueStatement(_)
962-
)
956+
self.get_one_child().is_some_and(|stmt| {
957+
matches!(
958+
stmt,
959+
Self::ReturnStatement(_)
960+
| Self::ThrowStatement(_)
961+
| Self::BreakStatement(_)
962+
| Self::ContinueStatement(_)
963+
)
964+
})
963965
}
964966

965967
/// Returns the single statement from block statement, or self

crates/oxc_minifier/src/peephole/minimize_conditions.rs

+206-124
Large diffs are not rendered by default.

crates/oxc_minifier/src/peephole/mod.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,19 @@ impl<'a> Traverse<'a> for PeepholeOptimizations {
141141
self.minimize_statements(stmts, ctx);
142142
}
143143

144-
fn exit_statement(&mut self, stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) {
144+
fn exit_statement(&mut self, stmt: &mut Statement<'a>, traverse_ctx: &mut TraverseCtx<'a>) {
145145
if !self.is_prev_function_changed() {
146146
return;
147147
}
148-
let ctx = Ctx(ctx);
149-
self.minimize_conditions_exit_statement(stmt, ctx);
150-
self.remove_dead_code_exit_statement(stmt, ctx);
151-
self.substitute_exit_statement(stmt, ctx);
148+
Self::minimize_conditions_exit_statement(stmt, Ctx(traverse_ctx));
149+
self.remove_dead_code_exit_statement(stmt, Ctx(traverse_ctx));
150+
if let Statement::IfStatement(if_stmt) = stmt {
151+
if let Some(folded_stmt) = self.try_minimize_if(if_stmt, traverse_ctx) {
152+
*stmt = folded_stmt;
153+
self.mark_current_function_as_changed();
154+
}
155+
}
156+
self.substitute_exit_statement(stmt, Ctx(traverse_ctx));
152157
}
153158

154159
fn exit_return_statement(&mut self, stmt: &mut ReturnStatement<'a>, ctx: &mut TraverseCtx<'a>) {

crates/oxc_minifier/src/peephole/remove_dead_code.rs

+10-12
Original file line numberDiff line numberDiff line change
@@ -177,19 +177,17 @@ impl<'a, 'b> PeepholeOptimizations {
177177
_ => {}
178178
}
179179

180-
// `if (test) {}` -> `test`
181-
if if_stmt.alternate.is_none()
182-
&& match &if_stmt.consequent {
183-
Statement::EmptyStatement(_) => true,
184-
Statement::BlockStatement(s) => s.body.is_empty(),
185-
_ => false,
186-
}
187-
{
188-
let expr = ctx.ast.move_expression(&mut if_stmt.test);
189-
return Some(ctx.ast.statement_expression(if_stmt.span, expr));
190-
}
191-
192180
if let Some(boolean) = ctx.get_side_free_boolean_value(&if_stmt.test) {
181+
// Use "1" and "0" instead of "true" and "false" to be shorter.
182+
// And also prevent swapping consequent and alternate when `!0` is encourtnered.
183+
if let Expression::BooleanLiteral(b) = &if_stmt.test {
184+
if_stmt.test = ctx.ast.expression_numeric_literal(
185+
b.span,
186+
if b.value { 1.0 } else { 0.0 },
187+
None,
188+
NumberBase::Decimal,
189+
);
190+
}
193191
let mut keep_var = KeepVar::new(ctx.ast);
194192
if boolean {
195193
if let Some(alternate) = &if_stmt.alternate {

crates/oxc_minifier/src/peephole/statement_fusion.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ mod test {
1818
#[test]
1919
fn fold_block_into_if() {
2020
test("a;b;c;if(x){}", "a,b,c,x");
21-
// FIXME: remove last `!`
22-
test("a;b;c;if(x,y){}else{}", "a, b, c, x, !y");
21+
test("a;b;c;if(x,y){}else{}", "a, b, c, x, y");
2322
test("a;b;c;if(x,y){}", "a, b, c, x, y");
2423
test("a;b;c;if(x,y,z){}", "a, b, c, x, y, z");
2524
test("a();if(a()){}a()", "a(), a(), a()");

crates/oxc_minifier/tests/peephole/dead_code_elimination.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ fn dce_if_statement() {
6060
);
6161
test(
6262
"if (xxx) { foo } else if (false) { var a; var b; } else if (false) { var c; var d; }",
63-
"if (xxx) foo; else if (false) var a, b; else if (false) var c, d;",
63+
"if (xxx) foo; else if (0) var a, b; else if (0) var c, d;",
6464
);
6565

6666
test("if (!false) { foo }", "foo");

crates/oxc_minifier/tests/peephole/mod.rs

-30
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,6 @@ fn test_same(source_text: &str) {
1616

1717
#[test]
1818
fn integration() {
19-
// FIXME
20-
// test(
21-
// "function writeInteger(int) {
22-
// if (int >= 0)
23-
// if (int <= 0xffffffff)
24-
// return this.u32(int);
25-
// else if (int > -0x80000000)
26-
// return this.n32(int);
27-
// }",
28-
// "function writeInteger(int) {
29-
// if (int >= 0) {
30-
// if (int <= 4294967295) return this.u32(int);
31-
// if (int > -2147483648) return this.n32(int);
32-
// }
33-
// }",
34-
// );
35-
3619
test(
3720
"require('./index.js')(function (e, os) {
3821
if (e) return console.log(e)
@@ -76,19 +59,6 @@ fn integration() {
7659
console.log(c, d);
7760
",
7861
);
79-
80-
test(
81-
"function _() {
82-
if (currentChar === '\\n')
83-
return pos + 1;
84-
else if (currentChar !== ' ' && currentChar !== '\\t')
85-
return pos + 1;
86-
}",
87-
"function _() {
88-
if (currentChar === '\\n' || currentChar !== ' ' && currentChar !== '\\t')
89-
return pos + 1;
90-
}",
91-
);
9262
}
9363

9464
#[test]

tasks/minsize/minsize.snap

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@ Original | minified | minified | gzip | gzip | Fixture
77

88
287.63 kB | 89.48 kB | 90.07 kB | 30.95 kB | 31.95 kB | jquery.js
99

10-
342.15 kB | 117.68 kB | 118.14 kB | 43.57 kB | 44.37 kB | vue.js
10+
342.15 kB | 117.68 kB | 118.14 kB | 43.56 kB | 44.37 kB | vue.js
1111

1212
544.10 kB | 71.43 kB | 72.48 kB | 25.87 kB | 26.20 kB | lodash.js
1313

1414
555.77 kB | 271.25 kB | 270.13 kB | 88.35 kB | 90.80 kB | d3.js
1515

16-
1.01 MB | 440.96 kB | 458.89 kB | 122.50 kB | 126.71 kB | bundle.min.js
16+
1.01 MB | 440.97 kB | 458.89 kB | 122.50 kB | 126.71 kB | bundle.min.js
1717

1818
1.25 MB | 650.36 kB | 646.76 kB | 161.01 kB | 163.73 kB | three.js
1919

20-
2.14 MB | 718.61 kB | 724.14 kB | 162.13 kB | 181.07 kB | victory.js
20+
2.14 MB | 718.69 kB | 724.14 kB | 162.15 kB | 181.07 kB | victory.js
2121

22-
3.20 MB | 1.01 MB | 1.01 MB | 324.31 kB | 331.56 kB | echarts.js
22+
3.20 MB | 1.01 MB | 1.01 MB | 324.44 kB | 331.56 kB | echarts.js
2323

24-
6.69 MB | 2.30 MB | 2.31 MB | 468.52 kB | 488.28 kB | antd.js
24+
6.69 MB | 2.30 MB | 2.31 MB | 468.54 kB | 488.28 kB | antd.js
2525

26-
10.95 MB | 3.36 MB | 3.49 MB | 862.11 kB | 915.50 kB | typescript.js
26+
10.95 MB | 3.36 MB | 3.49 MB | 862.14 kB | 915.50 kB | typescript.js
2727

0 commit comments

Comments
 (0)