Skip to content
← Back to rules

eslint/no-lonely-if Pedantic

🚧 An auto-fix is planned for this rule, but not implemented at this time.

What it does ​

Disallow if statements as the only statement in else blocks.

Why is this bad? ​

When an if statement is the only statement in an else block, it is often clearer to use an else if instead.

Examples ​

Examples of incorrect code for this rule:

js
if (condition) {
  // ...
} else {
  if (anotherCondition) {
    // ...
  }
}
js
if (condition) {
  // ...
} else {
  if (anotherCondition) {
    // ...
  } else {
    // ...
  }
}

Examples of correct code for this rule:

js
if (condition) {
  // ...
} else if (anotherCondition) {
  // ...
}
js
if (condition) {
  // ...
} else if (anotherCondition) {
  // ...
} else {
  // ...
}
js
if (condition) {
  // ...
} else {
  if (anotherCondition) {
    // ...
  }
  doSomething();
}

How to use ​

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

json
{
  "rules": {
    "no-lonely-if": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "no-lonely-if": "error",
  },
});
bash
oxlint --deny no-lonely-if

Version ​

This rule was added in v0.16.0.

References ​