question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

no-fn-reference-in-iterator: Ignore locally declared functions?

See original GitHub issue

I would like the following to be ignored by no-fn-reference-in-iterator:

const fn = item => {
  // do stuff with iterator items
};

iter1.forEach(fn);
iter2.forEach(fn);
iter3.forEach(fn);

Since fn is locally declared and defined the purpose of no-fn-reference-in-iterator does not apply. I see value in the following situations being reported:

const mapper = require('unicorn-mapper');

function doStuff(iter, fn) {
  iter.forEach(fn); // error on `fn` provided by argument
}

[2, 3, 4].forEach(mapper); // error as `mapper` is from a `require`

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
fregantecommented, May 11, 2020

I’ve been disabling this rule as I go.

I get that it tries to prevent future breakage, but this exclusively depends on the function signature:

  • does fn accept multiple arguments? Report it.
  • the function signature cannot be verified? Report it.

That’s it.

Only node_modules introduce uncertainty because a future npm install might change the signature of fn after the lint, unless…

  • there’s a lockfile which guarantees that fn won’t just change under your feet.

This means that single-parameter local functions (same file or other file) should not be reported by this check. If a local fn is changed to a multi-parameter, the lint will likely be run after this change and report it.

If lockfile detection can be part of a rule, node_modules can also follow the same logic.

0reactions
fregantecommented, Jun 18, 2021

Not always true that multiple arguments is a problem:

You’re right.

Changed my mind on this specific point:

- const showFruits = (name) => console.log(`I have many ${name}`)
+ const showFruits = (name, count = 1) => console.log(count `I have exactly ${count} ${name}`)

list.forEach(showFruits);

The problem is that (regardless of types) the linter does not know what the expected input of the function is. The second parameter could match the exact type and pass everywhere, yet still fall into the trap that this rule is meant to prevent.

If you follow this rule, you’d have written the loop this way and that diff wouldn’t have caused the issue:

- const showFruits = (name) => console.log(`I have many ${name}`)
+ const showFruits = (name, count = 1) => console.log(`I have exactly ${count} ${name}`)

list.forEach(fruit => showFruits(fruit));
Read more comments on GitHub >

github_iconTop Results From Across the Web

git - ignore some files in locally without change gitignore project
There is no way in Git to ignore changes to tracked files. From the git update-index documentation:.
Read more >
Ignoring files - GitHub Docs
gitignore file to share with others, you can create rules that are not committed with the repository. You can use this technique for...
Read more >
Git Ignore Locally: Ignore Files in Local Copy
It gives you three Git shortcuts ignore (to locally ignore some files), unignore (to stop the local ignore on specific files), and ignored...
Read more >
gitignore Documentation - Git
DESCRIPTION. A gitignore file specifies intentionally untracked files that Git should ignore. Files already tracked by Git are not affected; see the NOTES ......
Read more >
.gitignore file - ignoring files in Git | Atlassian Git Tutorial
Git ignore patterns are used to exclude certain files in your working directory from your Git history. They can be local, global, or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found