eslint-plugin-unicorn
eslint-plugin-unicorn copied to clipboard
`prefer-array-some` false positive for `notArray.find()`
prefer-array-some
reports error for any .find()
calls even if it's not array's method
class Foo {
find() {
return "test"
}
}
if (new Foo().find(t => t === "t")) {
console.log("this find is reported")
}
Apologies if it was the case where it's just impossible without TypeScript type info
Without TS type info, it's impossible to avoid false-positives. In this specific case, we could not report if the caller is a constructor call. Is the above example representable of the actual code?
That makes sense.
The code example I made it as simple as possible but this is closer to my actual code:
class A {
// ...
find (key: string) {
const data = this.getItem(key)
return typeof data !== "undefined" && data !== null
}
// ...
}
// ...
class B {
store = new A()
foo() {
if (this.store.find(SOME_KEY)) {
// ...
}
}
}
Now that I looked back, this particular code can be improved by renaming the defined .find
to .some
or something following ES naming concept.
we stumbled upon the same issue today, using a QueryCache
from react-query
, which has a find
method, but no some
method:
queryClient.getQueryCache().find(['foo']) ? 'foo' : 'bar'