Skip to content

noUselessSplice (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"noUselessSplice": "error"
}
}
}
}

Disallow splice() calls that have a simpler alternative.

splice() can remove and add elements anywhere in an array. To understand a splice() call, the reader has to work out what each argument means. When a call only changes the start or the end of an array, a dedicated method says the same thing with its name.

This rule reports splice() calls that:

  • don’t change the array, because they remove no elements and add none.
  • remove the first element. Use shift() instead.
  • add elements to the start. Use unshift() instead.
  • remove the last element. Use pop() instead.
  • add elements to the end. Use push() instead.
  • remove every element. Set length to 0 instead.

splice() returns an array that contains the removed elements. shift() and pop() return the removed element, and unshift() and push() return the new length of the array. For that reason, the rule only offers a fix when the code doesn’t use the value that the call returns. A call that removes every element is only reported when its returned value isn’t used.

The rule ignores a call when the array is read with optional chaining (?.), such as object?.items.splice(0, 1).

array.splice(index, 0);
code-block.js:1:1 lint/nursery/noUselessSplice  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ This splice() call does not change the array.

> 1 │ array.splice(index, 0);
│ ^^^^^^^^^^^^^^^^^^^^^^
2 │

ℹ The call removes no elements and adds none. Remove it, or change its arguments if it is meant to modify the array.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Remove the splice() call.

1 │ array.splice(index,·0);
│ -----------------------
array.splice(0, 1);
code-block.js:1:1 lint/nursery/noUselessSplice  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ This splice() call does the same thing as shift().

> 1 │ array.splice(0, 1);
│ ^^^^^^^^^^^^^^^^^^
2 │

ℹ shift() is easier to read, because its name says what the call does.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Use shift() instead.

1 │ - array.splice(0,·1);
1 │ + array.shift();
2 2 │

array.splice(0, 0, item);
code-block.js:1:1 lint/nursery/noUselessSplice  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ This splice() call does the same thing as unshift().

> 1 │ array.splice(0, 0, item);
│ ^^^^^^^^^^^^^^^^^^^^^^^^
2 │

ℹ unshift() is easier to read, because its name says what the call does.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Use unshift() instead.

1 │ - array.splice(0,·0,·item);
1 │ + array.unshift(item);
2 2 │

array.splice(array.length - 1, 1);
code-block.js:1:1 lint/nursery/noUselessSplice  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ This splice() call does the same thing as pop().

> 1 │ array.splice(array.length - 1, 1);
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │

ℹ pop() is easier to read, because its name says what the call does.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Use pop() instead.

1 │ - array.splice(array.length·-·1,·1);
1 │ + array.pop();
2 2 │

array.splice(array.length, 0, item);
code-block.js:1:1 lint/nursery/noUselessSplice  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ This splice() call does the same thing as push().

> 1 │ array.splice(array.length, 0, item);
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │

ℹ push() is easier to read, because its name says what the call does.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Use push() instead.

1 │ - array.splice(array.length,·0,·item);
1 │ + array.push(item);
2 2 │

array.splice(0);
code-block.js:1:1 lint/nursery/noUselessSplice  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ This splice() call removes every element of the array.

> 1 │ array.splice(0);
│ ^^^^^^^^^^^^^^^
2 │

ℹ Setting length to 0 does the same thing and is easier to read.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Set length to 0 instead.

1 │ - array.splice(0);
1 │ + array.length·=·0;
2 2 │

array.shift();
array.unshift(item);
array.pop();
array.push(item);
array.length = 0;
array.splice(index, 1);
array.splice(1, 0, item);
const removed = array.splice(0);
array?.splice(0, 1);