Skip to content

Commit f55923e

Browse files
fix(es/minifier): Restrict parameter inlining to only allow 'undefined' as unresolved identifier
Changed the safety check for parameter inlining to only allow the 'undefined' identifier as an unresolved identifier. Previously, all unresolved identifiers (like window, document, etc.) were allowed, but this is not safe because: 1. They could have side effects or vary between environments 2. Their values might not be constant across different execution contexts This change ensures that only 'undefined' (which is a true constant) can be inlined as an unresolved identifier. Addresses review feedback from #11156 (review) All tests passing. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 7c99765 commit f55923e

File tree

1 file changed

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

1 file changed

+10
-2
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,13 @@ impl Optimizer<'_> {
270270
// Allow resolved identifiers (local immutable variables)
271271
true
272272
}
273+
// Only allow 'undefined' as an unresolved identifier
274+
// Other unresolved identifiers (like window, document, etc.) are not safe
275+
// because they could have side effects or vary between environments
276+
Expr::Ident(id)
277+
if id.ctxt == self.ctx.expr_ctx.unresolved_ctxt && id.sym == "undefined" =>
278+
{
279+
true
273280
}
274281

275282
// Negated or numeric-negated literals
@@ -302,11 +309,12 @@ impl Optimizer<'_> {
302309
}
303310
(Expr::Lit(Lit::Str(a)), Expr::Lit(Lit::Str(b))) => a.value == b.value,
304311
(Expr::Lit(Lit::BigInt(a)), Expr::Lit(Lit::BigInt(b))) => a.value == b.value,
305-
// Top-level identifiers must have the same name and context
312+
// Only allow 'undefined' as an unresolved identifier
306313
(Expr::Ident(a), Expr::Ident(b))
307314
if a.ctxt == self.ctx.expr_ctx.unresolved_ctxt
308315
&& b.ctxt == self.ctx.expr_ctx.unresolved_ctxt
309-
&& a.sym == b.sym =>
316+
&& a.sym == "undefined"
317+
&& b.sym == "undefined" =>
310318
{
311319
true
312320
}

0 commit comments

Comments
 (0)