sindresorhus/eslint-plugin-unicorn

Rule proposal: Forbid `.splice(index, 1, element)` and `.splice(-1, 0, element)`

Open

#2.765 geöffnet am 25. Sept. 2025

Auf GitHub ansehen
 (2 Kommentare) (5 Reaktionen) (0 zugewiesene Personen)JavaScript (5.022 Stars) (468 Forks)user submission
help wantednew rule

Beschreibung

Description

They are hard to read and confusing.

Examples

// ❌
array.splice(-1, 1, element);
array.splice(array.length - 1, 1, element); // This can be ignored since `unicorn/prefer-negative-index` already fix it to the previous one.

// ✅
array[array.length - 1] = element;
// ❌
array.splice(10, 1, element);

// ✅
array[10] = element;
// ❌
array.splice(-1, 0, element);

// ✅
array.push(element);

Proposed rule name

no-confusing-array-splice

Additional Info

I'm not sure about the rule name.

Note: the replacements in examples are not safe to fix, since they behavior differently when the index is out of boundary, even the -1 case, since the array can be empty.

Contributor Guide