Skip to content

Latest commit

Β 

History

History
71 lines (59 loc) Β· 1.26 KB

File metadata and controls

71 lines (59 loc) Β· 1.26 KB

no-lonely-if

πŸ“ 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) {
		// …
	}
}