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.
nursery/useElseIf
nursery · since 2.6.10
{
"linter": {
"rules": {
"nursery": {
"useElseIf": "error"
}
}
}
}Prefer early continues over conditionals wrapping the remainder of the loop body.
Prefer Iterator.zip() over parallel-array indexing.
Disallow simple recursive function calls that can be replaced with a loop.
Disallow checking a Map key before accessing a different key.
Enforce consistent class references in static methods.