eslint-plugin-unicorn
eslint-plugin-unicorn copied to clipboard
Rule proposal: `no-set-duplicates`
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 const
s 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',
]);
Couldn't it apply to Array
too? And maybe also Object
and Map
keys?
Array
We have prefer-set-has
to report Array
s that are used like Set
s and suggest converting them into Set
s
Object
A similar built-in rule exists for objects https://eslint.org/docs/rules/no-dupe-keys
Map
šš»
We have prefer-set-has to report Arrays that are used like Sets and suggest converting them into Sets
Sure, but someone may disable that rule, but still have this one enabled.
How about naming it no-duplicate-literal-elements
?
Hey guys,
Sorry for jumping into the thread, i Was just having a short break at work after dealing with unicorns as everyday. Although this rule looks cool, but won't be ok to have duplicates explicitly in a Set when you pass static values? Cause maybe as the other guy was pointing out correctly, maybe usually when you have st as tic values and you use a set, you know the possibility that the input data has duplicates?
Maybe that s why you want a set then .
Thanks foe your great job Highly Appreciated Looking forward to yhe new rules upcomhin!
@quirimmo This is already answered in the issue body:
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 is now accepted.
I am looking at this issue.
I have added a rule about Set
and Map
.
PTAL, thx.
Hello @futpib @sindresorhus
There are two questions here that I need to ask you to confirm.
https://github.com/sindresorhus/eslint-plugin-unicorn/pull/1635#discussion_r771135771 https://github.com/sindresorhus/eslint-plugin-unicorn/pull/1635#issuecomment-996478812
Thx~
Actually, Iām not sure why I need to check the arrays.
There are many cases where the contents of the array are duplicated.
like this: ((x, y) => Math.pow(x, y)).apply(null, [2, 2])
https://github.com/sindresorhus/eslint-plugin-unicorn/pull/1635#discussion_r772027700
I should understand exactly. To know how to describe.
Let's focus on Set
?
And let's try to statically analyze the AST too, not only literals.
Fail
new Set([-1, -1]);
const foo = 2;
new Set([foo, 2]);
new Set([foo.bar, foo.bar]);
If anyone wants to work on this, see the initial attempt and feedback in https://github.com/sindresorhus/eslint-plugin-unicorn/pull/1635.