sindresorhus/eslint-plugin-unicorn

Rule proposal: `no-useless-else`

Closed

#987 opened on Jan 2, 2021

 (3 comments) (9 reactions) (0 assignees)JavaScript (468 forks)user submission
help wantednew rule

Repository metrics

Stars
 (5,022 stars)
PR merge metrics
 (Avg merge 1d 16h) (399 merged PRs in 30d)

Description

If the body of if contains one of these keywords, the else is moot:

  • throw
  • break
  • continue
  • return

This slightly overlaps with https://eslint.org/docs/rules/no-else-return

This was inspired by https://github.com/eslint/eslint/issues/13067.

This could be auto-fixed, but we should take care to properly move any comments.

Fail

if (foo) {
	throw new Error();
} else {
	console.log('🦄');
}

Pass

if (foo) {
	throw new Error();
} 

console.log('🦄');

Contributor guide