eslint-plugin-unicorn icon indicating copy to clipboard operation
eslint-plugin-unicorn copied to clipboard

Rule proposal: `no-set-duplicates`

Open futpib opened this issue 3 years ago ā€¢ 14 comments

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

futpib avatar Oct 25 '21 15:10 futpib

Couldn't it apply to Array too? And maybe also Object and Map keys?

sindresorhus avatar Oct 25 '21 16:10 sindresorhus

Array

We have prefer-set-has to report Arrays that are used like Sets and suggest converting them into Sets

Object

A similar built-in rule exists for objects https://eslint.org/docs/rules/no-dupe-keys

Map

šŸ‘šŸ»

futpib avatar Oct 25 '21 16:10 futpib

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.

sindresorhus avatar Oct 25 '21 17:10 sindresorhus

How about naming it no-duplicate-literal-elements?

sindresorhus avatar Nov 02 '21 06:11 sindresorhus

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 avatar Nov 09 '21 14:11 quirimmo

@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.

sindresorhus avatar Nov 12 '21 04:11 sindresorhus

This is now accepted.

sindresorhus avatar Nov 12 '21 04:11 sindresorhus

I am looking at this issue.

SidStraw avatar Dec 12 '21 14:12 SidStraw

I have added a rule about Set and Map.

PTAL, thx.

SidStraw avatar Dec 14 '21 15:12 SidStraw

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~

SidStraw avatar Dec 18 '21 14:12 SidStraw

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.

SidStraw avatar Dec 20 '21 01:12 SidStraw

Let's focus on Set?

fisker avatar Mar 30 '22 12:03 fisker

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

fisker avatar Mar 30 '22 12:03 fisker

If anyone wants to work on this, see the initial attempt and feedback in https://github.com/sindresorhus/eslint-plugin-unicorn/pull/1635.

sindresorhus avatar Sep 20 '22 08:09 sindresorhus