feat(minifier): convert switch with one case to if statement#24611
feat(minifier): convert switch with one case to if statement#24611armano2 wants to merge 15 commits into
Conversation
Merging this PR will not alter performance
Comparing Footnotes
|
| found_unlabelled_break: bool, | ||
| } | ||
|
|
||
| impl<'a> VisitJs<'a> for FindNestedBreak { |
There was a problem hiding this comment.
similar check is going to be required for loops, as currently we do not inline/remove them, tho in that case we also have to check for continue
eg. while(true) break;
I'm also not sure where to put this visitor
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: aea932d191
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| SPAN, | ||
| switch_stmt.discriminant.take_in(ctx), | ||
| BinaryOperator::StrictEquality, | ||
| test, |
There was a problem hiding this comment.
Preserve the switch case lexical scope for case tests
When a single-case switch contains a lexical declaration that shadows the case expression, switch evaluates the case test inside the switch's case-block scope, but this if condition is built outside the new block_stmt scope. For let x = 1; switch (0) { case x: let x = 2; }, the original reads the switch-scoped x and throws a TDZ ReferenceError, while the transformed if (0 === x) { let x = 2; } reads the outer x and continues, so this inlining changes runtime behavior.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Minifier assumes that the input has not TDZ violations so this isn't supported at this point.
oxc/crates/oxc_minifier/docs/ASSUMPTIONS.md
Lines 59 to 67 in 6ce51d3
| vec.pop().unwrap() | ||
| } else { | ||
| Statement::new_block_statement_with_scope_id( | ||
| SPAN, |
There was a problem hiding this comment.
maybe we should take switch case span in here
Inline switch with 1 case to if statement or in case of default to statement expressions + block statement
https://github.com/oxc-project/oxc/actions/runs/29759622291#summary-88410679091
change extracted from #17523
ref #17544