sindresorhus/eslint-plugin-unicorn

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

Fechada

#2.412 aberto em 27 de jul. de 2024

 (7 comentários) (1 reação) (0 responsável)JavaScript (468 forks)user submission
enhancementhelp wanted

Métricas do repositório

Stars
 (5.022 estrelas)
Métricas de merge de PR
 (Mesclagem média 4h 30m) (26 fundiu PRs em 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

Guia do colaborador