eslint-plugin-unicorn: `no-fn-reference-in-iterator` false positives on non-array methods

const obj = {filter: () => {}};
obj.filter([].join()); // error: Do not pass a function reference directly to an iterator method

.filter is not an iterator here. I think the rule confuses it with Array.prototype.filter and the false-positive seems specifically triggered by a CallExpression argument.

The autofix produces broken code for this case.

About this issue

  • Original URL
  • State: open
  • Created 7 years ago
  • Reactions: 1
  • Comments: 24 (1 by maintainers)

Most upvoted comments

This rule also triggers for most Bluebird.js use cases. (Bluebird.map, Bluebird.filter, etc).

Obviously I’ll just disable to the rule globally in my case, but it seems like this rule has really a lot of false positives and may not be great to have enabled (neither warn nor error) by default.

Just my 2 cents.

@fisker I agree that a ‘more urgent’ action is to disable the auto-fix, but we don’t want the rule mistakenly reporting errors anyway.

Anyone using MongoDB and Mongoose?

  const filterModel = {};
  const windows = await Tour.find(filterModel);

because it thought Mongo db.collection.find() Mongoose Model.find() as normal JS find() and raise the false positive!

Do not pass function filterModel directly to .find(…).eslintunicorn/no-fn-reference-in-iterator

My object fileterModel does not look like a function at all,

Make me so confuse @@

What if the linting error itself provides a hint such as:

If you think this is a false positive, disable this check with // eslint-disable-next-line unicorn/no-fn-reference-in-iterator

Good idea, PR welcome.

What if the linting error itself provides a hint such as:

If you think this is a false positive, disable this check with // eslint-disable-next-line unicorn/no-fn-reference-in-iterator