Skip to content

Latest commit

Β 

History

History
32 lines (21 loc) Β· 1.05 KB

File metadata and controls

32 lines (21 loc) Β· 1.05 KB

no-confusing-array-with

πŸ“ Disallow confusing uses of Array#with().

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

Array#with() throws a RangeError when the index is outside the array bounds.

Avoid using a negative index when the array may be empty, and avoid passing .length as the index since it is always outside the valid range.

This rule intentionally does not try to detect length guards. Use Array#with() with a clearly valid index.

Examples

// ❌
const result = array.with(-1, value);

// βœ…
const result = array.length === 0 ? array : array.with(array.length - 1, value);
// ❌
const result = array.with(array.length, value);

// βœ…
const result = [...array, value];