-
-
Notifications
You must be signed in to change notification settings - Fork 353
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add no-fn-reference-in-iterator
rule - fixes #91
#97
Conversation
I definitely think Otherwise I think the PR looks good 👍 |
Don't think sort should be in the list either. It always has 2 arguments. |
Also, should we handle closures? Might be overdoing it... // invalid
[1, 2, 3].map(fn({unicorn: true}));
// valid
[1, 2, 3].map(x => fn({unicorn: true})(x)); |
Good idea. Think we should yeah! |
no-function-iterator
rule - fixes #91no-function-iterator
rule - fixes #91
@sindresorhus any feedback on |
docs/rules/no-function-iterator.md
Outdated
# Prevents passing a function directly to iterator methods | ||
|
||
Prevents passing a function directly to an iterator method to make it more clear with the function accepts. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The intro should include why it's a useful rule. See: #91 (comment) I don't think most realize the issue with doing this.
rules/no-function-iterator.js
Outdated
@@ -0,0 +1,34 @@ | |||
'use strict'; | |||
const iteratorMethods = [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be a Set
.
docs/rules/no-function-iterator.md
Outdated
@@ -0,0 +1,105 @@ | |||
# Prevents passing a function directly to iterator methods | |||
|
|||
Prevents passing a function directly to an iterator method to make it more clear with the function accepts. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't repeat the title in the text here.
rules/no-function-iterator.js
Outdated
|
||
const create = context => ({ | ||
CallExpression: node => { | ||
const sourcecode = context.getSourceCode(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Getting the source code is expensive. We should only do it when we actually need it.
Wonder if we could use AST selectors for this: http://eslint.org/docs/developer-guide/selectors |
|
I think the rule name could be clearer. Not exactly sure what though. Something like |
I was able to do it like this const iteratorMethods = [
'filter',
'map',
'forEach',
'every',
'filter',
'find',
'findIndex',
'some'
];
const selector = 'CallExpression[callee.property.name=/^' + iteratorMethods.join('|') + '$/]'; That way I can drop the
Wasn't sure about it either. Can't really come up with something that really fits it.
|
docs/rules/no-function-iterator.md
Outdated
@@ -0,0 +1,133 @@ | |||
# Prevents passing a function directly to iterator methods | |||
|
|||
Suppose you have a `unicorn` module: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I rewrote the example a bit. Feel free to provide feedback on wording/style/...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good!
rules/no-function-iterator.js
Outdated
return arg.name; | ||
} | ||
|
||
const sourcecode = context.getSourceCode(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This variable feels moot, just use it directly below.
Nah, I prefer what you already have. |
Maybe we just go verbose |
Actually, let's go with |
no-function-iterator
rule - fixes #91no-fn-reference-in-iterator
rule - fixes #91
I think I processed all the feedback |
Not necessarily true since they have
I think |
I prefer |
Oh, this is embarrassing. I didn't know that. Never used those arguments. I guess they make sense to include after all then.
Me too. |
Just a thought. Should we allow this if the function used is defined in the same scope? Should be make an exception for built-in types? This is a very common pattern |
I think we shouldn't. It's easy to overlook something and someone can also add a second argument to the function without really noticing this caveat.
We could, but I'd suggest using a whitelist. |
Yeah, you're probably right.
Actually, I can only think of |
Good stuff Sam. This one will be very useful :) |
Woop woop, thanks for the feedback :) |
Trying to fix #91 which prevents passing a function directly to an iterator method.
I think that we should also support
reduce
andreduceRight
. Not sure aboutsort
. These are somewhat different though because they accept multiple arguments so should be handled separately.Just let me know if they should be supported as well.