Skip to content

prefer-early-return

Flags functions whose whole body is a single if statement with no else.

This 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.

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);
}
OptionDefaultWhat it does
maximumStatements1Reports when the wrapped if block contains more than this many statements. Set it to 0 to also report if (condition) doThing();.
eslint.config.js
import ceaseNonsense from "eslint-plugin-cease-nonsense";
export default [
{
plugins: {
"cease-nonsense": ceaseNonsense,
},
rules: {
"cease-nonsense/prefer-early-return": ["error", { maximumStatements: 0 }],
},
},
];