prefer-early-return
Flags functions whose whole body is a single if statement with no else.
Rule details Suggestion
Section titled “Rule details ”SuggestionThis rule reports a function when its block body has exactly one statement, that statement is an if, and the if does
not have an else branch.
With the default options, the if block must contain more than one statement before the rule reports it. That keeps
small guard-style wrappers out of the way.
Examples
Section titled “Examples” Incorrect
function process(data: Data) { if (data.isValid) { transform(data); save(data); }} Correct
function process(data: Data) { if (!data.isValid) return;
transform(data); save(data);}Options
Section titled “Options”| Option | Default | What it does |
|---|---|---|
maximumStatements | 1 | Reports when the wrapped if block contains more than this many statements. Set it to 0 to also report if (condition) doThing();. |
import ceaseNonsense from "eslint-plugin-cease-nonsense";
export default [ { plugins: { "cease-nonsense": ceaseNonsense, }, rules: { "cease-nonsense/prefer-early-return": ["error", { maximumStatements: 0 }], }, },];Related rules
Section titled “Related rules” no-god-components Keeps large React components from growing unchecked