fix(minifier): preserve catch binding with direct eval#22221
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes an unsafe minification case where the peephole optimization that removes an unused catch (e) binding could change runtime semantics when the catch scope contains a direct eval, since eval can reference the catch parameter dynamically even if static analysis reports it as unused.
Changes:
- Prevent
catchparameter removal when the catch scope is flagged as containing directeval. - Add regression tests covering direct
eval(including inside a nested function) vs. indirectevalvia optional chaining (eval?.(...)).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| crates/oxc_minifier/src/peephole/substitute_alternate_syntax.rs | Adds a safety guard to the optional catch binding optimization to preserve semantics in the presence of direct eval. |
| crates/oxc_minifier/tests/peephole/substitute_alternate_syntax.rs | Adds regression tests ensuring catch bindings are preserved for direct eval, while still allowing removal for indirect eval (eval?.). |
Merging this PR will not alter performance
Comparing Footnotes
|
Contributor
Author
Merge activity
|
Given:
```
try {
throw "error"
} catch (e) {
eval('console.log(e)')
}
```
The expected runtime behaviour is
```
"error"
```
instead, runtime behaviour is:
```
Uncaught ReferenceError: e is not defined
at eval (eval at <anonymous> (REPL5:4:3), <anonymous>:1:13)
```
This is because we remove the catch parameter, as we see no references to it, despite it being referenced in the `eval` call.
This PR adds a check to see if we are in a direct eval scope, and bails removing the catch parameter if this is the case.
9ee1100 to
73b4f40
Compare
camc314
added a commit
that referenced
this pull request
May 11, 2026
### 🚀 Features - 66c9b01 transformer/typescript: Debug_assert that `enum_eval` ran in semantic (#22252) (Dunqing) - ffe6475 minifier: Fold `Array` constructor with safe spreads (#22215) (camc314) ### 🐛 Bug Fixes - d3d0b18 traverse: Handle `ChainElement::TSNonNullExpression` in `GatherNodeParts` (#22247) (leaysgur) - 4e880de transformer/object-rest-spread: Declare temp vars for computed keys (#22284) (camc314) - a7c3e22 semantic: Clear member write target for computed keys (#22302) (camc314) - 6a8852d codegen: Emit newline after legal-comment orphan flush (#22304) (Dunqing) - 5da9fda transformer/explicit-resource-management: Preserve class names (#22306) (Dunqing) - b5d970f transformer/explicit-resource-management: Preserve class names (#22290) (camc314) - bc54fd4 minifier: Keep function / class names if direct eval is present in the scope (#22241) (sapphi-red) - 7a810c0 minifier: Refresh direct eval flags after DCE (#21787) (Dunqing) - dd88726 transformer/legacy-decorator: Preserve accessor type annotation for emitDecoratorMetadata (#21966) (Dunqing) - 29a3cd7 codegen: Swap mapping/indent order for top-level decls (#22206) (Dunqing) - 73b4f40 minifier: Preserve catch binding with direct eval (#22221) (camc314) - 0e13d17 minifier: Preserve optional chain base side effects (#22219) (camc314) - 0c7c01c transformer/typescript: Inline optional-chain enum member access (#21834) (Dunqing) - a6aff7e codegen: Emit block/array/object end mapping at close char (#22200) (Dunqing) - a099b03 codegen: Emit call end mapping at `)` position, not past it (#22199) (Dunqing) - 5753774 minifier: Cap if-return ternary collapse for firefox (#21841) (Gurupungav Narayanan) - 2493bdd codegen: Correct sourcemap end mappings for closing delimiters (#22001) (Mark Dalgleish) - 3b385e2 minifier: Bail optimizing `Array` with unknown arg count (#22188) (camc314) - 9fa2122 parser: Parse array computed class keys (#22159) (camc314) ### 📚 Documentation - a4a6892 napi/parser: Correct code comment (#22278) (overlookmotel) - 9305373 oxc: Update README (#22178) (camc314) Co-authored-by: Cameron <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Given:
The expected runtime behaviour is
instead, runtime behaviour is:
This is because we remove the catch parameter, as we see no references to it, despite it being referenced in the
evalcall.This PR adds a check to see if we are in a direct eval scope, and bails removing the catch parameter if this is the case.