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

es6: prefer String.startsWith() and String.includes() #565

Open
2 tasks
NovemLinguae opened this issue Apr 18, 2024 · 2 comments
Open
2 tasks

es6: prefer String.startsWith() and String.includes() #565

NovemLinguae opened this issue Apr 18, 2024 · 2 comments

Comments

@NovemLinguae
Copy link

NovemLinguae commented Apr 18, 2024

String.startsWith() and String.includes() were introduced in ES6 and are great. Very readable.

  • I'd like to suggest a rule that looks for String.indexOf(a) !== -1 and String.indexOf(a) === 0 and warns or errors for these, telling the user to change them to String.startsWith() and String.includes()
  • Could also auto fix these

Source code from another eslint plugin with these rules here and here (MIT license)

@edg2s
Copy link
Member

edg2s commented Apr 18, 2024

We can use the already present unicorn plugin for startsWith:

@edg2s
Copy link
Member

edg2s commented Apr 18, 2024

Alternatively the rules could be applied to ES2016 rules only, where Array.indexOf can also be replaced by Array.includes, and so the type wouldn't matter

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

No branches or pull requests

2 participants