javascript-eslint-no-lonely-if
Normalize JavaScript-family else { if (...) ... } blocks into else if (...) ... chains when the else block contains only that nested if, matching ESLint's no-lonely-if intent without changing runtime behavior.
What It Rewrites
Before:
js
After:
js
The codemod also normalizes nested chains in one run, so else { if (...) ... else { if (...) ... } } becomes a flat else if chain.
Scope
- Targets
.js,.jsx,.mjs,.cjs,.ts, and.tsx - Preserves inner
elsebranches - Preserves comments around the removed
elseblock and innerif - Preserves multiline literal contents by keeping inner branch text intact instead of reindenting it
- Skips files under
dist/**,build/**,vendor/**, and*.min.* - Skips files containing
codemod:ignore no-lonely-ifby default
No-Op Cases
elseblocks with more than one non-comment statementelseblocks whose single statement is not anififstatements without anelse- Code already written as
else if - Excluded or pragma-marked files
- Files with syntax errors are skipped by the codemod and reported with a diagnostic
Usage
bash
Override the ignore pragma at runtime:
bash
Development
bash
License
MIT