eslint-plugin-unicorn
eslint-plugin-unicorn copied to clipboard
Rule proposal: Have `prefer-spread` handle `for..of`
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]
Have you actually seen code like this in the wild?
@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.
Accepted