# curly

> Require braces around the bodies of `if`, `else` and loops

Without braces, only the next statement belongs to the `if` or the loop. If you add a second line below it with the same indentation, that line runs every time, no matter what the condition says.

`--fix` adds the missing braces, or removes the ones your style doesn't want.

## Configure

Off by default. To turn it on:

```toml bunfig.toml
[lint.rules]
"curly" = "error" # or "warn"
```

Every option, set to its default:

```toml bunfig.toml
[lint.rules]
"curly" = ["error", "all"] # "all", "multi", "multi-line" or "multi-or-nest"
```

## Examples

Reported:

```js
if (!user) redirect("/login");

for (const item of items) total += item.price;
```

Not reported:

```js
if (!user) {
  redirect("/login");
}

for (const item of items) {
  total += item.price;
}
```

## Options

```toml bunfig.toml
[lint.rules]
curly = ["error", "multi-line", "consistent"]
```

| Option | What it does |
| --- | --- |
| `"all"` (default) | Braces everywhere. |
| `"multi"` | Braces only around more than one statement. Braces around a single statement are reported. |
| `"multi-line"` | You can leave the braces out if the body is on the same line as the `if` or the loop. |
| `"multi-or-nest"` | No braces around a single statement that fits on one line. Braces around a body that takes several lines. |

```js
if (!user) {
  redirect("/login");
}
```

Braces that the code needs are never reported, for example around a `const`:

```js
if (!user) redirect("/login");

if (debug) {
  const started = Date.now();
}
```

With `"multi-line"`, the first `if` is allowed and the second one is reported:

```js
if (!user) redirect("/login");

if (!user.active)
  redirect("/activate");
```

With `"multi-or-nest"`, the body below is one statement, but it takes three lines:

```js
if (!user)
  save({
    name: "guest",
  });
```

```js
if (!user)
  redirect("/login");

if (user.admin) {
  save({
    role: "admin",
  });
}
```

With any option except `"all"`, you can add `"consistent"` as a second option. It makes all branches of an `if` / `else` chain look the same. If one of them needs braces, they all get braces.

```js
if (user.admin) {
  grant(user);
  notify(user);
} else
  deny(user);
```

```js
if (user.admin) {
  grant(user);
  notify(user);
} else {
  deny(user);
}
```
