javascript-no-else-after-return-in-if
Flattens redundant else and else if branches when the preceding if branch unconditionally exits with return, throw, continue, or break.
The codemod is intentionally conservative. It only rewrites if statements that are already inside a statement list, skips labeled break/continue, skips cases involving try/finally, and skips alternates that would hoist top-level declarations out of an else block.
What It Rewrites
js
becomes:
js
It also handles chained else if branches by repeatedly flattening the chain until no redundant else remains.
Target Files
**/*.{js,jsx,mjs,cjs}via the JavaScript parser**/*.{ts,mts,cts}via the TypeScript parser**/*.tsxvia the TSX parser
Preserve / No-op Cases
- The consequent does not exit on every path
- The
elsebody would hoist top-level declarations whose scope would change - Labeled
breakorcontinue ifstatements insidetry/finallyflows where the codemod cannot safely prove equivalenceifstatements that already have noelse
Development
bash
License
MIT