prefer-boolean-return
Added in v0.10.1Configuration
Rule details
Prefer returning a boolean expression directly when an if and its else or
the immediately following return return opposite boolean literals.
The rule keeps consecutive guard clauses that return the same value together.
Incorrect code
Correct code
Automatic fixes
The fix returns conditions that already evaluate to booleans directly. It uses
Boolean(...) to coerce other positive conditions and ! for inverted
conditions. A report has no fix when the edit would discard a comment or when
the replacement needs Boolean(...) and a local value shadows the global.
Options
This rule has no options.
Differences from upstream
- rslint keeps the diagnostic and withholds a fix that relies on
Booleanwhen the global is disabled in the lint configuration. A TypeScriptimport typethat introduces the nameBooleanalso prevents inserting a call, since TypeScript can reject the new call. - Inside a JavaScript
withblock, the object can provide its ownBooleanorArray. rslint only applies fixes that can preserve a boolean return without relying on those built-ins.
An enclosing variable or parameter named Boolean prevents inserting
Boolean(...) even when an inner TypeScript type alias has the same name.
Calling the parameter in this example could return a string.