wikimedia / eslint-config-wikimedia

JavaScript style guide for Wikimedia.
https://www.mediawiki.org/wiki/Manual:Coding_conventions/JavaScript
MIT License
29 stars 20 forks source link

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

Open NovemLinguae opened 7 months ago

NovemLinguae commented 7 months ago

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

Source 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.

Careful 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.

edg2s commented 7 months ago

We can use the already present unicorn plugin for startsWith:

edg2s commented 7 months ago

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

NovemLinguae commented 1 week ago

543 hits in CodeSearch for !== -1, and 200 for === 0. Lots of good potential autofixing here.

edit: Actually, some of those may be Array.indexOf().