Skip to content

unicorn/switch-case-braces Style

🛠️ An auto-fix is available for this rule.

What it does

Requires empty switch cases to omit braces, while non-empty cases must use braces. This reduces visual clutter for empty cases and enforces proper scoping for non-empty ones.

Why is this bad?

Using braces unnecessarily for empty cases adds visual noise, while omitting braces in non-empty cases can lead to scoping issues.

Examples

Examples of incorrect code for this rule:

javascript
switch (num) {
  case 1: {}
  case 2:
    console.log("Case 2");
    break;
}

Examples of correct code for this rule:

javascript
switch (num) {
  case 1:
  case 2: {
    console.log("Case 2");
    break;
  }
}

Options

{ type: "always" | "avoid", default: "always" }

  • "always" Always report when clause is not a BlockStatement.

  • "avoid" Allows braces only when needed for scoping (e.g., variable or function declarations).

Example:

json
"unicorn/switch-case-braces": ["error", "avoid"]

How to use

To enable this rule in the CLI or using the config file, you can use:

bash
oxlint --deny unicorn/switch-case-braces
json
{
  "rules": {
    "unicorn/switch-case-braces": "error"
  }
}

References

Released under the MIT License.