Skip to main content

switch-case-spacing

Enforce consistent spacing and break placement in switch cases.

Targeted pattern scope

This rule inspects each SwitchCase body and enforces a specific structure for non-empty cases.

For non-empty cases, it expects:

  • the first consequent statement starts on a later line than case ...:; and
  • the case ends with break, unless the case body starts with a block statement.

What this rule reports

This rule reports switch case bodies that do not match the expected spacing/break style.

Why this rule exists

This rule enforces a strict legacy switch layout policy to keep case bodies visually consistent.

❌ Incorrect

switch (x) {
case 1:
foo();
}

✅ Correct

switch (x) {
case 1: {
foo();
}
}

Deprecated

Behavior and migration notes

This rule is deprecated in favor of @stylistic/switch-colon-spacing.

It reports only and does not provide an autofix.

Options

This rule has no options.

Status

Use the Deprecated section above for lifecycle details.

Additional examples

switch (status) {
case "ok":
doWork();
break;
// ❌ first statement is on same line as `case`

case "done":
doWork();
break;
// ✅ valid
}

ESLint flat config example

import etcMisc from "eslint-plugin-etc-misc";

export default [
{
plugins: { "etc-misc": etcMisc },
rules: {
"etc-misc/switch-case-spacing": "error",
},
},
];

When not to use it

Disable this rule if your switch formatting is handled by a different style policy.

Package documentation

Rule catalog ID: R072

Further reading

Adoption resources

  • Start at warning level in CI, then move to error after cleanup.
  • Use focused codemods/autofix batches per package or directory.