Skip to content

Commit a29bf4b

Browse files
fix(es/minifier): Make parameter inlining more conservative
This commit fixes the parameter inlining optimization to be more conservative about what values are considered safe to inline. Previously, the optimization would inline any resolved identifier (local variable), which caused issues where: 1. Inlining variable references doesn't save code - it just moves a reference from one place to another 2. It can interfere with other optimizations like function body inlining 3. It changes code structure in ways that may not be beneficial The fix restricts parameter inlining to only inline true constants: - Literal values (null, boolean, number, string, bigint) - The special `undefined` identifier (unresolved global) - Unary expressions of the above (e.g., -1, !true) Variable references are no longer inlined, as the goal of this optimization is to inline constant values, not to move variable references around. This fixes all failing tests in the minifier test suite. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 52527bd commit a29bf4b

File tree

1 file changed

+10
-22
lines changed
  • crates/swc_ecma_minifier/src/compress/optimize

1 file changed

+10
-22
lines changed

crates/swc_ecma_minifier/src/compress/optimize/params.rs

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -258,18 +258,15 @@ impl Optimizer<'_> {
258258
| Expr::Lit(Lit::Str(_))
259259
| Expr::Lit(Lit::BigInt(_)) => true,
260260

261-
// Only allow:
262-
// 1. Unresolved "undefined" identifier (safe global)
263-
// 2. Resolved identifiers (local variables that are immutable)
261+
// Only allow unresolved "undefined" identifier
262+
// We DO NOT inline variable references because:
263+
// 1. It doesn't save code (just moves a reference)
264+
// 2. It can interfere with other optimizations
265+
// 3. The goal is to inline actual constants, not variable names
264266
Expr::Ident(id) => {
265267
let is_unresolved = id.ctxt == self.ctx.expr_ctx.unresolved_ctxt;
266-
if is_unresolved {
267-
// Only allow unresolved "undefined"
268-
id.sym == "undefined"
269-
} else {
270-
// Allow resolved identifiers (local immutable variables)
271-
true
272-
}
268+
// Only allow unresolved "undefined"
269+
is_unresolved && id.sym == "undefined"
273270
}
274271

275272
// Negated or numeric-negated literals
@@ -303,22 +300,13 @@ impl Optimizer<'_> {
303300
(Expr::Lit(Lit::Str(a)), Expr::Lit(Lit::Str(b))) => a.value == b.value,
304301
(Expr::Lit(Lit::BigInt(a)), Expr::Lit(Lit::BigInt(b))) => a.value == b.value,
305302
// Compare identifiers:
306-
// 1. For unresolved identifiers, only allow "undefined"
307-
// 2. For resolved identifiers, allow if same symbol and context
303+
// Only allow unresolved "undefined" identifiers
308304
(Expr::Ident(a), Expr::Ident(b)) => {
309305
let a_is_unresolved = a.ctxt == self.ctx.expr_ctx.unresolved_ctxt;
310306
let b_is_unresolved = b.ctxt == self.ctx.expr_ctx.unresolved_ctxt;
311307

312-
if a_is_unresolved && b_is_unresolved {
313-
// Both unresolved: only allow "undefined"
314-
a.sym == "undefined" && b.sym == "undefined"
315-
} else if !a_is_unresolved && !b_is_unresolved {
316-
// Both resolved: check same symbol and context
317-
a.sym == b.sym && a.ctxt == b.ctxt
318-
} else {
319-
// One resolved, one unresolved: not equal
320-
false
321-
}
308+
// Both must be unresolved "undefined"
309+
a_is_unresolved && b_is_unresolved && a.sym == "undefined" && b.sym == "undefined"
322310
}
323311
(
324312
Expr::Unary(UnaryExpr {

0 commit comments

Comments
 (0)