# unicorn/switch-case-braces

> Enforce braces, or no braces, around the body of each `case`

All the `case` clauses in a `switch` share one scope. A `const` declared in one clause is in scope in the next, and declaring the same name in two clauses is a syntax error. Braces give each clause its own scope.

A `case` without a body, which falls through to the next one, is never reported. Empty braces, as in `case 1: {}`, are always reported.

`--fix` adds or removes the braces.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

Off by default. To turn it on:

```toml bunfig.toml
[lint.rules]
"unicorn/switch-case-braces" = "error" # or "warn"
```

Every option, set to its default:

```toml bunfig.toml
[lint.rules]
"unicorn/switch-case-braces" = ["error", "always"] # "always" or "avoid"
```

{/* End of the generated section. */}

## Examples

Reported:

{/* bad {"count": 2} */}
```js
switch (event.type) {
  case "open":
    show(event.target);
    break;
  default:
    ignore(event);
}
```

Not reported:

{/* good */}
```js
switch (event.type) {
  case "click":
  case "open": {
    show(event.target);
    break;
  }
  default: {
    ignore(event);
  }
}
```

## Options

| Option | What it does |
| --- | --- |
| `"always"` (default) | Require braces around the whole body of every `case`. |
| `"avoid"` | Report braces unless the block declares a variable or a function. |

{/* bad {"options": ["avoid"]} */}
```js
switch (event.type) {
  case "open": {
    show(event.target);
    break;
  }
}
```

{/* good {"options": ["avoid"]} */}
```js
switch (event.type) {
  case "open":
    show(event.target);
    break;
  case "close": {
    const panel = find(event.target);
    hide(panel);
    break;
  }
}
```
