sindresorhus / eslint-plugin-unicorn

More than 100 powerful ESLint rules
MIT License
4.21k stars 364 forks source link

`no-array-callback-reference` false positives on non-array methods #1193

Open carakessler opened 3 years ago

carakessler commented 3 years ago

I have found a problem with no-array-callback-reference:

The rule is erroring on a custom implementation of a method called find We have a class that contains a function find, and I am getting an error on passing in a parameter to that function, which is NOT the native array find

Screen Shot 2021-04-19 at 4 33 33 PM

Additionally, the parameter taskName (as shown in the screenshot below) is not a function, and is in fact a string (although again, it shouldn't matter because this isn't a native array method anyway)

Screen Shot 2021-04-19 at 4 35 41 PM
sindresorhus commented 3 years ago

which is NOT the native array find

There's no way to statically infer that unless the method definition is in the same scope.

carakessler commented 3 years ago

Ah that makes sense, I wonder if it makes sense to expose a more ergonomic way to override the rule.. I'll think on that. Thanks for the prompt response!

matthiasg commented 2 years ago

We also get this false positive when it should be statically clear that the data passed in is not a callback:

const query = {}
const docs = m.find(query) // FAILS here (m is NOT an array etc)
jase88 commented 1 year ago

I also ran into this false positive with Angular using ng-mocks library

import { ngMocks } from 'ng-mocks';
import { MyComponent } from './my-component';

ngMocks.find(MyComponent)
Samuel-Therrien-Beslogic commented 3 months ago

https://github.com/sindresorhus/eslint-plugin-unicorn/issues/1480#issue-967623165

mongoose Models have a find function to query a MongoDB Database. I think this could be added to ignoredCallee

import { model, Schema, Types } from 'mongoose'

type Account = { _id: string, id: string }
const schema = new Schema<Account, Model<Account>, Account>()
const AccountModel = model('Account', schema)

AccountModel.find({_id: '000000000000000000000000'})

image