Skip to content

Conversation

@azat-io
Copy link
Contributor

@azat-io azat-io commented Nov 15, 2024

The no-array-for-each rule is good for making code easier to understand and shorter. However, in some cases I find it easier to use forEach when I have a function that is applied via forEach in multiple places.

This PR adds the ability to ignore the rule if only the function name is passed in the arguments to forEach.

Example:
https://github.com/azat-io/eslint-plugin-perfectionist/blob/main/rules/sort-classes.ts#L341-L374

if ('elements' in nodeValue) {
  nodeValue.elements
    .filter(currentNode => currentNode !== null)
    .forEach(traverseNode)
}

if ('argument' in nodeValue && nodeValue.argument) {
  traverseNode(nodeValue.argument)
}

if ('arguments' in nodeValue) {
  nodeValue.arguments.forEach(traverseNode)
}

if ('declarations' in nodeValue) {
  nodeValue.declarations.forEach(traverseNode)
}

if ('properties' in nodeValue) {
  nodeValue.properties.forEach(traverseNode)
}

if ('expressions' in nodeValue) {
  nodeValue.expressions.forEach(traverseNode)
}

@sindresorhus
Copy link
Owner

Thanks for the PR, but I don't think this is something we want. Passing a function directly into a callback is generally an anti-pattern. We even have a rule against it: https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/no-array-callback-reference.md

@azat-io
Copy link
Contributor Author

azat-io commented Nov 15, 2024

Sad, thank you, I hear you!

@fisker fisker closed this Nov 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants