no-lonely-if

March 27, 2026 ยท View on GitHub

๐Ÿ“ 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.

Examples

// โŒ
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) {
		// โ€ฆ
	}
}