-
Notifications
You must be signed in to change notification settings - Fork 20
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
Comments
We can use the already present unicorn plugin for startsWith:
|
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 |
543 hits in CodeSearch for !== -1, and 200 for === 0. Lots of good potential autofixing here. edit: Actually, some of those may be |
Looks like Once #599 is merged, though, we can turn on |
Note that unicorn/prefer-includes autofixes are buggy sometimes, so be careful with it. Example: https://en.wikipedia.org/wiki/MediaWiki_talk:Gadget-popups.js#str.includes_is_not_a_function |
String.startsWith()
andString.includes()
were introduced in ES6 and are great. Very readable.String.indexOf(a) !== -1
, suggestString.includes()
insteadString.indexOf(a) === 0
, suggestString.startsWith()
insteadSource code from another eslint plugin with these rules here and here (MIT license)
edit:
Array.indexOf() !== -1
is also a very common pattern. Might also want to tackle it at the same time.Array.indexOf(a) !== -1
, suggestArray.includes()
insteadCareful of this pattern when autofixing:
(badIndex = params.tags.indexOf('Bad GIF')) !== -1
. That should not be autofixed due to the variable depending on getting the indexOf's value.The text was updated successfully, but these errors were encountered: