unicorn/no-lonely-if
eslint-plugin-unicorn
Safe autofix
eslint.config.js
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/no-lonely-if": "error",
},
},
];Disallow if statements as the only statement in if blocks without else.
if (foo) {
if (bar) {
// …
}
}if (foo) {
// …
} else if (bar) {
if (baz) {
// …
}
}if (foo && bar) {
// …
}if (foo) {
// …
} else if (bar && baz) {
// …
}if (foo) {
// …
} else if (bar) {
if (baz) {
// …
}
} else {
// …
}Examples from the ESLint documentation.
unicorn/no-lonely-if
eslint-plugin-unicorn
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/no-lonely-if": "error",
},
},
];unicorn/no-lonely-if
unicorn plugin · pedantic
{
"rules": {
"unicorn/no-lonely-if": "error"
}
}style/useCollapsedIf
style · since 1.9.4
{
"linter": {
"rules": {
"style": {
"useCollapsedIf": "error"
}
}
}
}Disallow if statements as the only statement in else blocks
Enforce the style of numeric separators by correctly grouping digits.
Prefer .some(…) over .filter(…).length check and .{find,findLast,findIndex,findLastIndex}(…).
Prefer .at() method for index access and String#charAt().
Prefer JavaScript modules (ESM) over CommonJS.
Prefer RegExp#test() over String#match(), String#search(), and RegExp#exec().