Skip to content

Latest commit

Β 

History

History
58 lines (39 loc) Β· 1.82 KB

File metadata and controls

58 lines (39 loc) Β· 1.82 KB

prefer-iterator-to-array

πŸ“ Prefer Iterator#toArray() over temporary arrays from iterator spreads.

πŸ’ΌπŸš« This rule is enabled in the βœ… recommended config. This rule is disabled in the β˜‘οΈ unopinionated config.

πŸ”§πŸ’‘ This rule is automatically fixable by the --fix CLI option and manually fixable by editor suggestions.

Prefer Iterator#toArray() over temporary arrays created with a single iterator spread.

Iterator#toArray() makes iterator-to-array conversion explicit and keeps iterator helper chains readable.

Examples

// ❌
const values = [...map.values()];

// βœ…
const values = map.values().toArray();
// ❌
const values = [...map.values().map(value => value * 2)];

// βœ…
const values = map.values().map(value => value * 2).toArray();

This rule is intentionally narrow. It does not report arbitrary iterables because not every iterable has Iterator#toArray().

// βœ…
const values = [...set];

Calls like .values(), .keys(), .entries(), and .matchAll() are reported as suggestions because custom methods with those names may return non-iterator iterables.

Iterable-accepting contexts are intentionally ignored because they do not need an array.

// βœ…
new Set([...map.values()]);

Mixed arrays and multi-spread arrays are intentionally ignored.

// βœ…
const values = [first, ...map.values()];

// βœ…
const values = [...foo, ...bar];