Skip to content
NewBun 1.4.5 is out. Release notes
AphrodyBun

Linter · Rules

unicorn/no-lonely-if

Disallow an if as the only statement in another if when neither has an else

Two nested if statements with nothing else between them test one combined condition. Join them with && and you lose a level of indentation and get the whole condition in one place.

no-lonely-if without the unicorn/ prefix is a different rule. It reports an if that's the only thing in an else block.

Configure

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"unicorn/no-lonely-if" = "error" # or "warn"

Examples

Reported:

if (user) {
  if (user.isAdmin) {
    showSettings();
  }
}

Not reported:

if (user && user.isAdmin) {
  showSettings();
}

You can't merge the two when either one has an else, or when the outer block does something else as well:

if (user) {
  if (user.isAdmin) {
    showSettings();
  }
} else {
  showLogin();
}

if (user) {
  greet(user);
  if (user.isAdmin) {
    showSettings();
  }
}