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

feature suggestion: must use value #54

Closed
lukeapage opened this issue Apr 2, 2016 · 7 comments
Closed

feature suggestion: must use value #54

lukeapage opened this issue Apr 2, 2016 · 7 comments

Comments

@lukeapage
Copy link
Contributor

so we require explicit chains, but I don't like the look of..

_.chain(a)
    .map('a')
    .forEach((a) => {})
    .value();

I think the code would be clearer and more likely correct if any chains ending in value() are assigned to something, so the above code would have to be

var description = _.chain(a)
    .map('a')
    .value();
_.forEach(description, (a) => {})

and it would prevent chains not being assigned (e.g. useless code)

wdyt?

btw sorry I know I haven't submitted a pr for my last feature but still slowly switching on your rules on our large codebase...

@ganimomer
Copy link
Contributor

Well, since Lodash v4, forEach and its kind all end implicit chaining, so you could technically write

_(a)
    .map('a')
    .forEach((a) => {})   

Which would make this redundant. If you use the chain-style with as-needed, it would warn against this, and then no-double-unwrap would warn against the extra .value().

Also, this would assume that everyone uses each function for its intended use.
I've seen people use map, filter and especially some for side effects, and in some cases it's too hard for the plugin or Eslint itself to check for that.

@lukeapage
Copy link
Contributor Author

Yes, im aware we could use implicit chaining but i was outvoted, other developers preferred consistency/simplicity over elegance.
A better description might be "avoidCallsOnlyForSideEffects" with an ignore option or somesuch defaulting to forEach.. i guess that doesnt really address my forEach chain issue unless i make the ignores more specific but it does address my side-effects concern.

@lukeapage
Copy link
Contributor Author

And it would be easy to tell if lodash was used only for side effects (basically determine if the parent is a statement), just difficult to tell if it was used for value and side effects (but again possibleb

@captbaritone
Copy link
Contributor

Possibly related, I've been thinking that there should be a rule about requiring that the output of _.each does not get used, and another that the output of other collection methods DO get used.

This would help prevent people using _.map for side effects only.

@lukeapage
Copy link
Contributor Author

Yes, that would be good.

@ganimomer
Copy link
Contributor

You make a good point - the whole point of giving options to rules like chain-style is not to make assumptions like I just made.
I'd say something like collection-method-value would work.

@lukeapage
Copy link
Contributor Author

Good rule, it found 4 cases of confusing code. I am wondering if for my other concern, which is purely stylistic, another rule "disallow-chain-methods"(with option for methods to not allow chaining with) might make sense so I could disallow forEach within a chain ?

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

No branches or pull requests

3 participants