sindresorhus/eslint-plugin-unicorn

`no-useless-spread` should report when passing spreading parameters to `new Set`

Closed

#2,412 opened on Jul 27, 2024

 (7 comments) (1 reaction) (0 assignees)JavaScript (468 forks)user submission
enhancementhelp wanted

Repository metrics

Stars
 (5,022 stars)
PR merge metrics
 (Avg merge 1d 16h) (399 merged PRs in 30d)

Description

1️⃣ Explain here what's wrong

function getNames() {
  return ['foo', 'bar', 'foo'];
}

const uniqueNames = [...new Set(...getNames())]; // I mistakenly spreading the `getNames()`
console.log(uniqueNames) // Print [ 'f', 'o' ]. But what I expect is ['foo', 'bar']

no-useless-spread should report error on the code new Set(...getNames()). But actually not.

Because new Set() accept one or zero parameter, we don't need to spread the parameters.

Example

const foo = ['abc'];
const set = new Set(...foo); // BAD!
const set = new Set(foo[0]); // OK

2️⃣ Specify which rule is buggy here and in the title no-useless-spread

3️⃣ More information TypeScript should report error when compiling the code above. Unfortunately, this problem havn't been fixed yet. Set https://github.com/microsoft/TypeScript/issues/59390 and https://github.com/microsoft/TypeScript/issues/48575

Contributor guide