sindresorhus/eslint-plugin-unicorn

Rule proposal: `no-unnecessary-splice`

Open

#2,359 建立於 2024年5月16日

在 GitHub 查看
 (8 留言) (6 反應) (0 負責人)JavaScript (5,022 star) (468 fork)user submission
help wantednew rule

描述

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

貢獻者指南