π Disallow if statements as the only statement in if blocks without else.
πΌ This rule is enabled in the following configs: β
recommended, βοΈ unopinionated.
π§ This rule is automatically fixable by the --fix CLI option.
This rule adds onto the built-in no-lonely-if rule, which only disallows if statements in else, not in if. It is recommended to use unicorn/no-lonely-if together with the core ESLint no-lonely-if rule.
// β
if (foo) {
if (bar) {
// β¦
}
}
// β
if (foo && bar) {
// β¦
}// β
if (foo) {
// β¦
} else if (bar) {
if (baz) {
// β¦
}
}
// β
if (foo) {
// β¦
} else if (bar && baz) {
// β¦
}// β
if (foo) {
// β¦
} else if (bar) {
if (baz) {
// β¦
}
} else {
// β¦
}// β
// Built-in rule `no-lonely-if` case https://eslint.org/docs/rules/no-lonely-if
if (foo) {
// β¦
} else {
if (bar) {
// β¦
}
}