-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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: Prefer String#includes
over String#indexOf
#4209
Comments
Thanks for the issue! If you're reporting a bug, please be sure to include:
Requesting a new rule? Please see Proposing a New Rule for instructions. |
Seems reasonable. Do you want to submit a PR? |
Looking at this again, it looks like this would only be possible with string literals since we have no way of knowing if a variable contains a string or not. Given that, I think I was too hasty in marking this accepted, as I don't think there's a good way to do this nor do I think a rule that applies only to string literals is all that useful (since if you have a string literal, you can see if a substring is included). When |
I guess this is yet another case where some kind of type inference would be useful. |
Yup. Or alternately, if every type that has |
TypedArray also has |
We have a stupidly simple version of this that we use at PayPal: module.exports = function(context) {
return {
'MemberExpression': function(node) {
if (node.property.name === 'indexOf') {
context.report(node, 'Expected .includes() instead of .indexOf().');
}
}
};
}; |
Yeah, it's not hard to make, it's just inaccurate, which is why we can't have it as part of core right now. |
@nzakas Noticing this issue is open and has "help wanted" label, but not "accepted" (and comments seem to indicate this won't be accepted). Should the issue be closed and/or should the "help wanted" label be removed? |
Hmm, good point. Should be closed, thanks. |
@nzakas I noticed you added the
That was pretty much the main argument against this rule, so would you be willing to reconsider this? |
Not quite the same, but since it looks like Even so, I'm not sure this is important enough to be a core rule. I'll reopen to see if anyone feels differently. |
Okay, it's been another three months without movement, so closing. |
@alxndr are you planing to submit a PR? |
I wasn't, but I can make one if there's still interest. |
This can now be implemented as an option for "no-restricted-syntax": ["error", "BinaryExpression > CallExpression > MemberExpression > Identifier[name = 'indexOf']"] So I don't think this should be added to the core. |
Agreed with @ilyavolodin, especially since very soon you will also be able to provide custom messages for |
That doesn't address the original comment of only being an error when comparing with -1 and 0 since indexOf is the correct tool otherwise. Does "no-restricted-syntax" handle that? |
I don't think |
With ES2015, it's more succinct to use
'foobar'.includes('foo')
instead of'foobar'.indexOf('foo') !== -1
or'foobar'.indexOf('foo') >= 0
for checking whether a string contains a substring. I would like a rule that enforces this in those two scenarios and the negative cases of that:'foobar'.indexOf('foo') === -1
and'foobar'.indexOf('foo') < 0
.Suggested rule name:
prefer-string-includes
I think this would be a good rule as
.includes
makes the code intent clearer.There's already other rules that enforces new ES2015 features, so it seems natural to have a rule for this too.
The text was updated successfully, but these errors were encountered: