Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rule proposal: no-unnecessary-splice #2359

Open
burtek opened this issue May 16, 2024 · 6 comments · May be fixed by #2361
Open

Rule proposal: no-unnecessary-splice #2359

burtek opened this issue May 16, 2024 · 6 comments · May be fixed by #2361

Comments

@burtek
Copy link

burtek commented May 16, 2024

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

@fisker

This comment was marked as outdated.

@fisker
Copy link
Collaborator

fisker commented May 16, 2024

Fail

array.splice(index, 1, newValue);

Pass

array[index] = newValue;
array = array.with(index, newValue);

@burtek
Copy link
Author

burtek commented May 16, 2024

@fisker good one, but I'd prefer to have that configurable, as it depends on the targeted ECMA version if array.with is supported

EDIT: actually, this doesn't look like that good of a replacement. splice mutates the array and returns old values, same for push/pop/shift/unshift, but neither array[index] = newValue or array.with does it, so it changes the logic, especially since array can't be const for the array.with solution.

Will leave this one for the end

@burtek
Copy link
Author

burtek commented May 17, 2024

One thing to watch out for is whether splice's return value is used or not.

Another thing is for all calls like getArray().splice(4, 0) (no-op case), we cant just remove the whole thing, as we'd be removing call to getArray which might introduce bugs. A lot to consider for auto-fixer. Again, have some stub for this already, working on more

@burtek burtek linked a pull request May 17, 2024 that will close this issue
@sindresorhus
Copy link
Owner

Accepted

@burtek
Copy link
Author

burtek commented May 24, 2024

#2361 is WIP, will try finishing it in the coming week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants