Switch Case Blocks
Enforce block-wrapped switch cases so each branch is self-contained and declarations never leak across cases.
Summary
The switch-case-blocks rule requires every non-empty switch case to wrap its body in a block ({ }). Empty cases used for intentional fallthrough are allowed.
Why Use This Rule?
- Prevents accidental variable leaks between cases that share the same function scope.
- Makes each case self-contained so reordering or removing one won't break another.
- Gives a consistent visual boundary that's easier to scan.
Examples
Configuration
Options
warning
ignoreFiles is an escape hatch for files where this rule genuinely does not apply. It is not intended for routine use.
| Option | Type | Default | Description |
|---|---|---|---|
ignoreFiles | string[] | [] | File patterns to skip. |
requireDefault | boolean | true | Require a default case in switch blocks. |
Autofix
Autofix is not available for this rule. Flagged code must be updated manually.
Troubleshooting
- False positive on an empty case — Empty cases (no statements at all) are allowed for intentional fallthrough. If the case has any statement, it needs a block.
- Conflicts with another formatter — Some formatters strip braces from single-statement cases. Configure the formatter to preserve block statements inside switch cases.