-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Rule proposal: Preferring .find(...) over .filter(...)[0] #6886
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
Comments
https://github.com/WebbyLab/eslint-plugin-more/blob/master/docs/no-filter-instead-of-find.md |
Why this needs the type checker: interface JerkCode<T> {
filter(predicate: (item: T) => boolean): JerkCode<T>;
}
declare const jerkCode: JerkCode<string>;
jerkCode.filter(item => item === "aha"); (making this explicit in case future folks ask) |
I guess there should ideally also be a check to see if the filter function does a side-effect, in which case the change will affect the current behavior. |
We can do that once we solve the halting problem 😄. |
Good luck, I'm looking forward to the results 😄
I agree it is bad because it is unexpected. But I don't know about actively wanting to break it 😅 Apart from that, I agree with the proposed rule (it's something I'd like for Elm as well but |
The current title I think has them the wrong way around:
Consider updating to:
|
Is this already covered by https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/prefer-array-find.md? |
See earlier comment - #6886 (comment) |
Oh and fun fact, https://sourcegraph.com/search?q=context:global+/filter.*%5C%29%5C%5B0%5C%5D/&patternType=standard&sm=1&groupBy=repo shows quite a few examples of people doing the |
I have a related request: I think you should prefer |
Whoo that's a complex one 😄. I don't know if it'd be easily feasible to get into this rule... maybe a good followup? Not sure 🤔 |
I wonder whether this ought to just be a whole separate rule. I could certainly try to include it as an option for the fix for .filter(...)[0], but then existing code like
would not get flagged. So maybe there should just be another rule entirely to prefer .some over .find in boolean contexts. I guess the drawback would be that if this rule just rewrites .filter(...)[0] -> .find(...) no matter what, then that will mean a second pass will be necessary to additionally fix the new rule. Overall it seems like that rule would have quite a bit more complexity than makes sense just to add as a suggestion for this rule, since we'd have to start asking what we want done with
Thoughts? |
Before You File a Proposal Please Confirm You Have Done The Following...
My proposal is suitable for this project
Description
I've seen occasional code authors write code with a
.filter
returning element[0]
, perhaps not knowing that.find
does the same thing. Perhaps we could write a rule that flags for this, and uses the type system to know whether the collection being.filter
ed is an array/array-like?Fail Cases
Pass Cases
Additional Info
No response
The text was updated successfully, but these errors were encountered: