Skip to content

no-lonely-if

JavaScript / TypeScriptstyleno fixdefault: off

Reports an if that is the only statement of an else block (else { if (b) { ... } }), also when that if has its own else. An else block with other statements, and an if inside an else if, are not reported.

else { if (b) ... } is the same as else if (b) ... and has one more level of nesting and braces for the reader to follow.

To fix, write else if.

Reported
if (a) { b(); } else { if (c) { d(); } }
if (a) { b(); } else { if (c) { d(); } e(); }
if (a) { b(); } else if (c) { d(); }
...
js:
lint:
rules:
no-lonely-if: warn # off (default), warn, error

Suppress one line with sudsy-disable-next-line no-lonely-if in a comment of the file’s language.

The same check in other tools; Sudsy reports what each upstream rule reports.

Tool Rule Match
eslint no-lonely-if full