Skip to content
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

prefer-reject does not warn on simple variable comparisons #104

Closed
Wilfred opened this issue Oct 5, 2016 · 4 comments
Closed

prefer-reject does not warn on simple variable comparisons #104

Wilfred opened this issue Oct 5, 2016 · 4 comments
Labels

Comments

@Wilfred
Copy link

Wilfred commented Oct 5, 2016

This produces a warning:

_.filter(users, function(user) {
  return user.name !== 'Bob';
});

This, however, does not:

_.filter(users, function(user) {
  return user !== 'Bob';
});

I've also experimented with different values of maximum path length in my .estlintrc.json test:

        "lodash/prefer-reject": ["error", 0],
        "lodash/prefer-reject": ["error", 1],
        "lodash/prefer-reject": ["error", 10],

None seem to change the behaviour (though I don't understand quite what this setting is for).

@mik01aj
Copy link

mik01aj commented Oct 6, 2016

I'm not sure, but maybe when you're comparing the iteratee with something using ===, you could as well just count the occurences, so maybe in this case some other rule should warn.

Good point about the docs for maximum path length in prefer-reject. They are pretty vague.

@ganimomer
Copy link
Contributor

Hi,
Currently the rule only warns against a function that could be shortened to any form of shorthand if switched to prefer-reject.
For instance,

_.filter(users, function(user) {
  return user.name !== 'Bob';
});

Can be shortened to

_.reject(users, {name: 'Bob'});

But, unless you have a curried _.eq function (which is included in lodash/fp, but this plugin doesn't support the fp version), You can't shorten the second example you gave.

Do you see any reason for this rule to warn in that case?

Also, I agree about the docs for this rule being vague.
I'll open a separate issue to clarify them.

@Wilfred
Copy link
Author

Wilfred commented Oct 6, 2016

The docs say:

When using _.filter with a negative condition, it could improve readability by switching to _.reject

So I was expecting this to warn:

_.filter(users, function(user) {
  return user !== 'Bob';
});

FWIW I do think it's clearer to write this in terms of _.reject, and I was hoping the rule would help me with this.

@ganimomer
Copy link
Contributor

Actually, if users is an array, I think the best way to write this is _.without. (For some reason it's only an array function and doesn't work on any other collections):

_.without(users, 'Bob')

I'd suggest a prefer-without rule, but since it only works on arrays, it would have false-positives.
Still, I think in this case the rule should not warn (since I don't think it makes it that much clearer).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants