Skip to content

Commit 2cd4c7b

Browse files
committed
feat(minifier): inline constant that is declared in normal for statement initializer (#13509)
Inline constant that is declared in normal for statement initializer. I think this is safe. Although, the `for-in` / `for-of` cases cannot be done because the values are changed.
1 parent 52f3e89 commit 2cd4c7b

File tree

2 files changed

+3
-4
lines changed

2 files changed

+3
-4
lines changed

crates/oxc_minifier/src/peephole/inline.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use oxc_ast::ast::*;
22
use oxc_ecmascript::constant_evaluation::{ConstantEvaluation, ConstantValue};
33
use oxc_span::GetSpan;
4+
use oxc_traverse::Ancestor;
45

56
use crate::ctx::Ctx;
67

@@ -21,9 +22,7 @@ impl<'a> PeepholeOptimizations {
2122
}
2223

2324
fn is_for_statement_init(ctx: &Ctx<'a, '_>) -> bool {
24-
ctx.ancestors().nth(1).is_some_and(|ancestor| {
25-
ancestor.is_parent_of_for_statement_init() || ancestor.is_parent_of_for_statement_left()
26-
})
25+
ctx.ancestors().nth(1).is_some_and(Ancestor::is_parent_of_for_statement_left)
2726
}
2827

2928
pub fn inline_identifier_reference(expr: &mut Expression<'a>, ctx: &mut Ctx<'a, '_>) {

crates/oxc_minifier/src/peephole/remove_unused_expression.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1077,7 +1077,7 @@ mod test {
10771077

10781078
// For loops
10791079
test_options("for (let i;;) i = 0", "for (;;);", &options);
1080-
test_same_options("for (let i;;) foo(i)", &options);
1080+
test_options("for (let i;;) foo(i)", "for (;;) foo(void 0)", &options);
10811081
test_same_options("for (let i;;) i = 0, foo(i)", &options);
10821082
test_same_options("for (let i in []) foo(i)", &options);
10831083
test_same_options("for (let element of list) element && (element.foo = bar)", &options);

0 commit comments

Comments
 (0)