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
- Correct
- Incorrect
switch (action) {
case 'start': {
const id = getId();
run(id);
break;
}
case 'stop': {
cleanup();
break;
}
// Empty cases for intentional fallthrough are fine.
case 'pause':
case 'resume':
default: {
log('unhandled action');
break;
}
}
switch (action) {
case 'start':
const id = getId();
run(id);
break;
case 'stop':
cleanup();
break;
case 'pause':
case 'resume':
default:
log('unhandled action');
break;
}
Configuration
Enable the rule in your ESLint flat config:
import { switchCaseBlocks } from '@cbnventures/nova/rules/eslint';
export default [
{
plugins: {
'@cbnventures/nova': {
rules: {
'switch-case-blocks': switchCaseBlocks,
},
},
},
rules: {
'@cbnventures/nova/switch-case-blocks': ['error'],
},
},
];
This rule has no additional options.
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.