Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions crates/oxc_minifier/src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl<'a> oxc_ecmascript::GlobalContext<'a> for Ctx<'a, '_> {
.get_reference(reference_id)
.symbol_id()
.and_then(|symbol_id| self.state.symbol_values.get_symbol_value(symbol_id))
.filter(|sv| sv.write_references_count == 0 && !sv.for_statement_init)
.filter(|sv| sv.write_references_count == 0)
.and_then(|sv| sv.initialized_constant.as_ref())
.cloned()
}
Expand Down Expand Up @@ -182,10 +182,6 @@ impl<'a> Ctx<'a, '_> {
}
}

let for_statement_init = self.ancestors().nth(1).is_some_and(|ancestor| {
ancestor.is_parent_of_for_statement_init() || ancestor.is_parent_of_for_statement_left()
});

let mut read_references_count = 0;
let mut write_references_count = 0;
for r in self.scoping().get_resolved_references(symbol_id) {
Expand All @@ -201,7 +197,6 @@ impl<'a> Ctx<'a, '_> {
let symbol_value = SymbolValue {
initialized_constant: constant,
exported,
for_statement_init,
read_references_count,
write_references_count,
scope_id,
Expand Down
14 changes: 9 additions & 5 deletions crates/oxc_minifier/src/peephole/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,22 @@ impl<'a> PeepholeOptimizations {
pub fn init_symbol_value(decl: &VariableDeclarator<'a>, ctx: &mut Ctx<'a, '_>) {
let BindingPatternKind::BindingIdentifier(ident) = &decl.id.kind else { return };
let Some(symbol_id) = ident.symbol_id.get() else { return };
let value = if decl.kind.is_var() {
// Skip constant value inlining for `var` declarations, due to TDZ problems.
let value = if decl.kind.is_var() || Self::is_for_statement_init(ctx) {
// - Skip constant value inlining for `var` declarations, due to TDZ problems.
// - Set None for for statement initializers as the value of these are set by the for statement.
None
} else {
decl.init.as_ref().map_or(Some(ConstantValue::Undefined), |e| e.evaluate_value(ctx))
};
ctx.init_value(symbol_id, value);
}

fn is_for_statement_init(ctx: &Ctx<'a, '_>) -> bool {
ctx.ancestors().nth(1).is_some_and(|ancestor| {
ancestor.is_parent_of_for_statement_init() || ancestor.is_parent_of_for_statement_left()
})
}

pub fn inline_identifier_reference(expr: &mut Expression<'a>, ctx: &mut Ctx<'a, '_>) {
let Expression::Identifier(ident) = expr else { return };
let reference_id = ident.reference_id();
Expand All @@ -30,9 +37,6 @@ impl<'a> PeepholeOptimizations {
if symbol_value.write_references_count > 0 {
return;
}
if symbol_value.for_statement_init {
return;
}
let Some(cv) = &symbol_value.initialized_constant else { return };
if symbol_value.read_references_count == 1
|| match cv {
Expand Down
5 changes: 2 additions & 3 deletions crates/oxc_minifier/src/peephole/remove_unused_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -642,9 +642,6 @@ impl<'a> PeepholeOptimizations {
if symbol_value.read_references_count > 0 {
return false;
}
if symbol_value.for_statement_init {
return false;
}
*e = assign_expr.right.take_in(ctx.ast);
ctx.state.changed = true;
false
Expand Down Expand Up @@ -1079,7 +1076,9 @@ mod test {
test_same_options("function foo(t) { return t = x(); } foo();", &options);

// For loops
test_options("for (let i;;) i = 0", "for (let i;;);", &options);
test_same_options("for (let i;;) foo(i)", &options);
test_same_options("for (let i;;) i = 0, foo(i)", &options);
test_same_options("for (let i in []) foo(i)", &options);
test_same_options("for (let element of list) element && (element.foo = bar)", &options);
test_same_options("for (let key in obj) key && (obj[key] = bar)", &options);
Expand Down
3 changes: 0 additions & 3 deletions crates/oxc_minifier/src/symbol_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ pub struct SymbolValue<'a> {
/// Symbol is exported.
pub exported: bool,

/// Inside for statement initializer.
pub for_statement_init: bool,

pub read_references_count: u32,
pub write_references_count: u32,

Expand Down
4 changes: 2 additions & 2 deletions tasks/minsize/minsize.snap
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Original | minified | minified | gzip | gzip | Iterations | Fi

3.20 MB | 1.00 MB | 1.01 MB | 323.12 kB | 331.56 kB | 3 | echarts.js

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

10.95 MB | 3.34 MB | 3.49 MB | 855.39 kB | 915.50 kB | 4 | typescript.js
10.95 MB | 3.34 MB | 3.49 MB | 855.34 kB | 915.50 kB | 4 | typescript.js

2 changes: 1 addition & 1 deletion tasks/track_memory_allocations/allocs_minifier.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ RadixUIAdoptionSection.jsx | 2.52 kB || 75 | 4 |

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

antd.js | 6.69 MB || 99857 | 13518 || 331724 | 70354 | 17.408 MB
antd.js | 6.69 MB || 99856 | 13518 || 331725 | 70354 | 17.408 MB

Loading