sindresorhus/eslint-plugin-unicorn

Rule proposal: `no-unnecessary-splice`

Open

#2,359 opened on May 16, 2024

View on GitHub
 (8 comments) (6 reactions) (0 assignees)JavaScript (5,022 stars) (468 forks)user submission
help wantednew rule

Description

Description

Saw splice being used instead of push/pop/shift/unshift in some projects. AFAIK it brings no performance gain and is harder to read/understand than the latter ones.

Basically bans:

  • array.splice(index, 0) (can be removed as no-op)
  • array.splice(0, 1) (as substitutable by array.shift())
  • array.splice(0, 0, element) (as substitutable by array.unshift(element) - for 1 or more elements)
  • array.splice(array.length - 1, 1) (as substitutable by array.pop())
  • array.splice(array.length, 0, element) (as substitutable by array.push(element) - for 1 or more elements)

Should be easy to implement auto-fixer for those replacements.

Fail

array.splice(start, 0)
array.splice(0, 1)
array.splice(array.length, 0, element)

Pass

array.shift()
array.push(element)

Proposed rule name

no-unnecessary-splice

Additional Info

Happy to work on this once accepted (already have a stub).

This would make #2165 not needed any more

Contributor guide