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

Rule proposal: Have `prefer-spread` handle `for..of`

Open Pyrolistical opened this issue 2 years ago • 3 comments

Description

Often no-for-loop results in something that is a simple spread, which prefer-spread should handle

This works for both arrays and iterators.

Fail

const result = []
for (const element of array) {
  result.push(element)
}
const result = []
for (const [index, element] of array.entries()) {
  result[index] = element
}

Pass

const result = [...array]

Pyrolistical avatar Feb 03 '22 19:02 Pyrolistical

Have you actually seen code like this in the wild?

sindresorhus avatar Feb 04 '22 05:02 sindresorhus

@sindresorhus I'm embarrassed to say, yes, but in my own code. I don't set out to write a trivial spread, but often as I clean up for loops and delete code, it ends up being a trivial spread.

Pyrolistical avatar Feb 07 '22 22:02 Pyrolistical

Accepted

sindresorhus avatar Mar 01 '22 13:03 sindresorhus