Skip to content

Commit 52f3e89

Browse files
committed
feat(minifier): remove unused variables in for init (#13508)
Remove unused variables in for initializer.
1 parent b4dfddd commit 52f3e89

File tree

5 files changed

+21
-3
lines changed

5 files changed

+21
-3
lines changed

crates/oxc_minifier/src/peephole/minimize_statements.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,20 @@ impl<'a> PeepholeOptimizations {
817817

818818
ctx: &mut Ctx<'a, '_>,
819819
) {
820+
if let Some(ForStatementInit::VariableDeclaration(var_decl)) = &mut for_stmt.init {
821+
let old_len = var_decl.declarations.len();
822+
var_decl.declarations.retain(|decl| {
823+
!Self::should_remove_unused_declarator(decl, ctx)
824+
|| decl.init.as_ref().is_some_and(|init| init.may_have_side_effects(ctx))
825+
});
826+
if old_len != var_decl.declarations.len() {
827+
if var_decl.declarations.is_empty() {
828+
for_stmt.init = None;
829+
}
830+
ctx.state.changed = true;
831+
}
832+
}
833+
820834
if ctx.options().sequences {
821835
match result.last_mut() {
822836
Some(Statement::ExpressionStatement(prev_expr_stmt)) => {

crates/oxc_minifier/src/peephole/remove_unused_declaration.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ mod test {
103103
test_same_options("export var x", &options);
104104
test_same_options("using x = foo", &options);
105105
test_same_options("await using x = foo", &options);
106+
107+
test_options("for (var x; ; );", "for (; ;);", &options);
108+
test_options("for (var x = 1; ; );", "for (; ;);", &options);
109+
test_same_options("for (var x = foo; ; );", &options); // can be improved
106110
}
107111

108112
#[test]

crates/oxc_minifier/src/peephole/remove_unused_expression.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1076,7 +1076,7 @@ mod test {
10761076
test_same_options("function foo(t) { return t = x(); } foo();", &options);
10771077

10781078
// For loops
1079-
test_options("for (let i;;) i = 0", "for (let i;;);", &options);
1079+
test_options("for (let i;;) i = 0", "for (;;);", &options);
10801080
test_same_options("for (let i;;) foo(i)", &options);
10811081
test_same_options("for (let i;;) i = 0, foo(i)", &options);
10821082
test_same_options("for (let i in []) foo(i)", &options);

tasks/minsize/minsize.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ Original | minified | minified | gzip | gzip | Iterations | Fi
2323

2424
6.69 MB | 2.22 MB | 2.31 MB | 459.28 kB | 488.28 kB | 4 | antd.js
2525

26-
10.95 MB | 3.34 MB | 3.49 MB | 855.34 kB | 915.50 kB | 4 | typescript.js
26+
10.95 MB | 3.34 MB | 3.49 MB | 855.24 kB | 915.50 kB | 4 | typescript.js
2727

tasks/track_memory_allocations/allocs_minifier.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ RadixUIAdoptionSection.jsx | 2.52 kB || 75 | 4 |
44

55
pdf.mjs | 567.30 kB || 19577 | 2898 || 47403 | 7782 | 1.624 MB
66

7-
antd.js | 6.69 MB || 99856 | 13518 || 331725 | 70354 | 17.408 MB
7+
antd.js | 6.69 MB || 99854 | 13518 || 331725 | 70354 | 17.408 MB
88

0 commit comments

Comments
 (0)