π Prefer Iterator.concat(β¦) over temporary spread arrays.
π« Disabled by default.
π§π‘ This rule is automatically fixable by the --fix CLI option and manually fixable by editor suggestions.
Prefer Iterator.concat(β¦) over temporary spread arrays.
Iterator.concat() creates one iterator from multiple iterables. In places that accept an iterable, this avoids materializing a temporary array.
// β
const set = new Set([...foo, ...bar]);
// β
const set = new Set(Iterator.concat(foo, bar));// β
for (const item of [...foo, ...bar]);
// β
for (const item of Iterator.concat(foo, bar));This rule only reports temporary arrays made entirely from spreads. Mixed arrays are intentionally ignored.
// β
new Set([first, ...rest]);Iterator.concat() evaluates all its arguments first and then consumes the iterables lazily. The conversion is autofixed when the array is passed to new Map(β¦), new WeakMap(β¦), new Set(β¦), new WeakSet(β¦), a TypedArray constructor, Object.fromEntries(β¦), Array.from(β¦), or TypedArray.from(β¦) without a mapper function, since these only store the values, and no argument after the first has a side effect. Otherwise, it is offered as a suggestion, because replacing the spread array can change argument evaluation, mutation, callback timing, and error handling.
A spread of a string is ignored. Spreading a string works, but Iterator.concat() throws for a string primitive.
// β
const letters = new Set([...'abc', ...'xyz']);