sindresorhus/eslint-plugin-unicorn

Rule proposal: `no-useless-else`

已關閉

#987 建立於 2021年1月2日

 (3 則留言) (9 個反應) (0 位負責人)JavaScript (468 個分叉)user submission
help wantednew rule

倉庫指標

星標
 (5,022 顆星)
PR 合併指標
 (平均合併 1天 16小時) (30 天內合併 399 個 PR)

描述

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('🦄');

貢獻者指南