sindresorhus/eslint-plugin-unicorn

Rule proposal: `no-set-duplicates`

Geschlossen

#1.559 geöffnet am 25.10.2021

 (14 Kommentare) (7 Reaktionen) (0 zugewiesene Personen)JavaScript (468 Forks)user submission
help wantednew rule

Repository-Metriken

Stars
 (5.022 Sterne)
PR-Merge-Metriken
 (Durchschn. Merge 1T 16h) (399 gemergte PRs in 30 T)

Beschreibung

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',
]);

Contributor Guide