unicorn/prefer-else-if
eslint-plugin-unicorn
Safe autofix
eslint.config.js
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/prefer-else-if": "error",
},
},
];Prefer else if over adjacent if statements with related conditions.
if (foo === 1) {
one();
}
if (foo === 2) {
two();
}if (foo === 1) {
one();
} else if (foo === 2) {
two();
}Examples from the ESLint documentation.
unicorn/prefer-else-if
eslint-plugin-unicorn
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/prefer-else-if": "error",
},
},
];Oxlint does not have this rule.
Biome does not have this rule.
Prefer .getHTML() and .setHTML() over .innerHTML.
Prefer .getOrInsertComputed() when the default value has side effects.
Prefer combining consecutive guards with identical exit statements.
Prefer Object.groupBy() or Map.groupBy() over manual grouping.
Prefer Array#slice() over Array#splice() when reading from the returned array.
Prefer moving code shared by all branches of an if statement out of the branches.