sindresorhus/eslint-plugin-unicorn

Rule proposal: `no-unnecessary-splice`

Chiusa

#2359 aperta il 16 mag 2024

 (8 commenti) (6 reazioni) (0 assegnatari)JavaScript (468 fork)user submission
help wantednew rule

Metriche repository

Star
 (5022 stelle)
Metriche merge PR
 (Merge medio 4h 30m) (26 PR mergiate in 30 g)

Descrizione

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

Guida contributor