feat(minifier): prune empty case before trailing default#17994
feat(minifier): prune empty case before trailing default#17994graphite-app[bot] merged 1 commit intomainfrom
case before trailing default#17994Conversation
Merge activity
|
There was a problem hiding this comment.
Pull request overview
This PR adds a minification optimization that removes empty case clauses containing only primitive literals when they immediately precede a trailing default clause in switch statements. This is a port from esbuild that reduces code size without changing behavior.
Changes:
- Added optimization logic to prune empty literal cases before trailing defaults
- Added comprehensive test coverage for various literal types and edge cases
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/minimize_statements.rs | Implements the optimization to remove empty literal cases before trailing default |
| crates/oxc_minifier/tests/peephole/esbuild.rs | Adds comprehensive test cases covering literals, non-literals, and edge cases |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
CodSpeed Performance ReportMerging this PR will not alter performanceComparing Summary
Footnotes
|
## Summary - Adds minification optimization to remove empty `case` clauses that precede a trailing `default` clause when those cases contain only primitive literals - Port of evanw/esbuild@add452e ### Example ```javascript // Before: switch (x) { case 0: foo(); break; case 1: default: bar() } // After: switch (x) { case 0: foo(); break; default: bar() } ``` ## Test plan - [x] Added tests for basic case with empty literal before default - [x] Added tests for multiple empty literal cases - [x] Added tests for non-literal identifiers (should NOT be removed) - [x] Added tests for default not at end (preserved) - [x] Added tests for string, null, and BigInt literals - [x] All existing minifier tests pass 🤖 Generated with [Claude Code](https://claude.com/claude-code)
683f7f9 to
bbda204
Compare
## Summary - Adds minification optimization to remove empty `case` clauses that precede a trailing `default` clause when those cases contain only primitive literals - Port of evanw/esbuild@add452e ### Example ```javascript // Before: switch (x) { case 0: foo(); break; case 1: default: bar() } // After: switch (x) { case 0: foo(); break; default: bar() } ``` ## Test plan - [x] Added tests for basic case with empty literal before default - [x] Added tests for multiple empty literal cases - [x] Added tests for non-literal identifiers (should NOT be removed) - [x] Added tests for default not at end (preserved) - [x] Added tests for string, null, and BigInt literals - [x] All existing minifier tests pass 🤖 Generated with [Claude Code](https://claude.com/claude-code)
bbda204 to
9c6e344
Compare
| if first_empty_idx < default_idx { | ||
| let default_case = switch_stmt.cases.pop().unwrap(); | ||
| switch_stmt.cases.truncate(first_empty_idx); | ||
| switch_stmt.cases.push(default_case); |
There was a problem hiding this comment.
default case should be ommited when its empty, either empty block or no stmt
| while first_empty_idx > 0 { | ||
| let case = &switch_stmt.cases[first_empty_idx - 1]; | ||
| // Only remove empty cases with primitive literal tests | ||
| if case.consequent.is_empty() |
There was a problem hiding this comment.
case that contains only break without label, block stmt, block with break could be also be considered empty
Summary
caseclauses that precede a trailingdefaultclause when those cases contain only primitive literalsExample
Test plan
🤖 Generated with Claude Code