# no-lonely-if

> Require `else if` instead of an `if` alone in an `else` block

An `else` block that holds nothing but an `if` is an `else if` with one more level of indentation. Each extra condition pushes the code further to the right.

`--fix` rewrites it to `else if`. It leaves the code alone if the `else` block also has a comment, which would be lost.

{/* 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]
"no-lonely-if" = "error" # or "warn"
```

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

## Examples

Reported:

{/* bad */}
```js
if (user.isAdmin) {
  showAdminPage();
} else {
  if (user.isMember) {
    showMemberPage();
  } else {
    showLogin();
  }
}
```

Not reported:

{/* good */}
```js
if (user.isAdmin) {
  showAdminPage();
} else if (user.isMember) {
  showMemberPage();
} else {
  showLogin();
}
```

Not reported, because the `else` block does something else as well:

{/* good */}
```js
if (user.isAdmin) {
  showAdminPage();
} else {
  if (user.isMember) {
    showMemberPage();
  }
  trackVisit(user);
}
```
