eslint-plugin-unicorn icon indicating copy to clipboard operation
eslint-plugin-unicorn copied to clipboard

`prefer-array-some` false positive for `notArray.find()`

Open Hoishin opened this issue 4 years ago • 3 comments

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

Hoishin avatar Jan 13 '21 11:01 Hoishin

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?

sindresorhus avatar Jan 13 '21 11:01 sindresorhus

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.

Hoishin avatar Jan 14 '21 08:01 Hoishin

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'

TkDodo avatar Feb 15 '24 11:02 TkDodo