Skip to content

Latest commit

Β 

History

History
39 lines (27 loc) Β· 1.23 KB

File metadata and controls

39 lines (27 loc) Β· 1.23 KB

prefer-array-from-range

πŸ“ Prefer Array.from({length}, …) when creating range arrays.

πŸ’Ό This rule is enabled in the following configs: βœ… recommended, β˜‘οΈ unopinionated.

πŸ”§ This rule is automatically fixable by the --fix CLI option.

Prefer Array.from({length}, (_, index) => index) when directly materializing a range from Array(length).keys() or new Array(length).keys().

This rule only reports direct array materialization from Array constructor ranges. Lazy iterator use, values(), entries(), computed members, optional calls, and shadowed Array bindings are ignored.

Examples

// ❌
const indexes = [...Array(length).keys()];

// βœ…
const indexes = Array.from({length}, (_, index) => index);
// ❌
const indexes = Array.from(Array(count + 1).keys());

// βœ…
const indexes = Array.from({length: count + 1}, (_, index) => index);
// βœ…
for (const index of Array(length).keys()) {
	console.log(index);
}