sindresorhus/eslint-plugin-unicorn

Rule proposal: `no-set-duplicates`

已關閉

#1,559 建立於 2021年10月25日

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

倉庫指標

星標
 (5,022 顆星)
PR 合併指標
 (平均合併 4小時 30分鐘) (30 天內合併 26 個 PR)

描述

Sometimes a hardcoded Set grows large enough for it to be hard to see if there is already a value in it. One could miss that a value one wants to add is already there and add a duplicate.

This rule should at minimum report hardcoded sets of literals and consts with duplicates.

This rule may also report consecutive .add calls with same arguments (this is probably much harder to implement). Or maybe this could be a separate rule like unicorn/no-array-push-push.

Fail

const allowed = new Set([
	'foo',
	'bar',
	'buz',
	'qux',
	'bar', // ← Remove duplicate value `'bar'` from the Set
]);

Pass

const allowed = new Set([
	'foo',
	'bar',
	'buz',
	'qux',
]);

貢獻者指南