Skip to content

Latest commit

Β 

History

History
54 lines (37 loc) Β· 1.74 KB

File metadata and controls

54 lines (37 loc) Β· 1.74 KB

no-confusing-array-splice

πŸ“ Disallow confusing uses of Array#{splice,toSpliced}().

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

πŸ’‘ This rule is manually fixable by editor suggestions.

Some Array#splice() and Array#toSpliced() calls are harder to read than the operation they express.

Use assignment when replacing one element in place, and use Array#with() when creating a copy with one element replaced. Resolve negative indexes before assigning with bracket notation.

For insertion at -1, prefer spelling out the intended boundary behavior instead of relying on splice() index normalization.

This rule does not automatically fix code because splice(), toSpliced(), assignment, and with() behave differently for return values, negative indexes, and out-of-range indexes.

Examples

// ❌
array.splice(index, 1, element);

// βœ…
array[index] = element;
// ❌
const result = array.toSpliced(index, 1, element);

// βœ…
const result = array.with(index, element);
// ❌
array.splice(-1, 0, element);

// βœ…
const insertionIndex = Math.max(array.length - 1, 0);
array.splice(insertionIndex, 0, element);
// ❌
const result = array.toSpliced(-1, 0, element);

// βœ…
const insertionIndex = Math.max(array.length - 1, 0);
const result = array.toSpliced(insertionIndex, 0, element);