unicorn/prefer-early-return
eslint-plugin-unicorn
Safe autofix
eslint.config.js
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/prefer-early-return": "error",
},
},
];Prefer early returns over conditionals wrapping the remainder of the function body.
function foo() {
doSomethingBefore();
if (condition) {
doSomething();
doSomethingElse();
}
}function foo() {
doSomethingBefore();
if (!condition) {
return;
}
doSomething();
doSomethingElse();
}function foo() {
if (condition) {
doSomething();
}
}function foo() {
if (condition) {
doSomething();
doSomethingElse();
}
finish();
}Examples from the ESLint documentation.
unicorn/prefer-early-return
eslint-plugin-unicorn
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/prefer-early-return": "error",
},
},
];Oxlint does not have this rule.
Biome does not have this rule.
Prefer .replaceChildren() when replacing DOM children.
Prefer escape sequences for irregular whitespace characters.
Prefer direct iteration over default iterator method calls.
Prefer global numeric constants over Number static properties.