Disallow else blocks after return statements in if statements

Proprietary

Removes the unnecessary else when every branch of the preceding if/else if chain ends in a return, unwrapping the else body into the enclosing statement list. For example, if (x) { return y; } else { return z; } becomes if (x) { return y; } return z;. As ESLint does, the unwrapped statements keep their indentation, and the else is left where its block-scoped names would collide with names of the enclosing scope, where it holds a lone function declaration, where automatic semicolon insertion could change the parse, and where the if is itself the unbraced body of another statement. Unlike ESLint, it is also left where it declares block-scoped names and the enclosing scope calls eval, which may read them. Replicates the autofix of the ESLint rule no-else-return under its default options.

io.moderne.javascript.cleanup.NoElseReturn
mod run . --recipe=io.moderne.javascript.cleanup.NoElseReturn

Install it once, then run it on every repository under the current directory. New to the Moderne CLI? Set it up.