Skip to main content

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?

  1. Prevents accidental variable leaks between cases that share the same function scope.
  2. Makes each case self-contained so reordering or removing one won't break another.
  3. 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.

OptionTypeDefaultDescription
ignoreFilesstring[][]File patterns to skip.
requireDefaultbooleantrueRequire 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.