Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upNew rule: Add linter for single arg fat arrow functions #309
Comments
KevinGrandon
added a commit
to KevinGrandon/eslint-plugin-standard
that referenced
this issue
Oct 26, 2015
This comment has been minimized.
This comment has been minimized.
xjamundx
commented
Oct 27, 2015
|
FYI there's http://eslint.org/docs/rules/arrow-parens |
This comment has been minimized.
This comment has been minimized.
|
Even though I also omit parens on single arguments I don't see a lot of benefit in enforcing it. |
This comment has been minimized.
This comment has been minimized.
|
Can you send a PR with this change? Also, please run the tests and report how many of the repos in the test suite start to fail with this change. That should get the discussion started. |
This comment has been minimized.
This comment has been minimized.
I've seen a lot of bikeshedding around this issue in various projects which is why I think it would be good to enforce this one way or another. |
This comment has been minimized.
This comment has been minimized.
|
Some of this is discussed over here: standard/eslint-config-standard#14 Arrow functions specifically are well enough supported/understood that I think it makes sense to move forward with this specific rule. |
KevinGrandon
added a commit
to KevinGrandon/eslint-config-standard
that referenced
this issue
Oct 27, 2015
This comment has been minimized.
This comment has been minimized.
Submitted a PR in eslint-config-standard.
I'm happy to do this if needed, is there any documentation about how to run the tests? |
This comment has been minimized.
This comment has been minimized.
|
@KevinGrandon Just run |
This comment has been minimized.
This comment has been minimized.
|
Thanks. I assume that this should be run in eslint-config-standard? I ran with both "as-needed" (which I would like to implement), as well as "always". Results are as follows:
|
This comment has been minimized.
This comment has been minimized.
|
@feross - Does that test output look correct? ^ Any thoughts on implementing this rule since it seems most of the test repos follow it? |
This comment has been minimized.
This comment has been minimized.
|
I'm ambivalent about this proposed rule. But it does feel a bit inconsistent... to codify it:
I agree that no parenthesis looks cleaner. If I'm a noob to JavaScript, the rule feels inconsistent. It almost seems as if we should require parenthesis in all cases if we're gonna make a rule. To be clear, I support either. Maybe we defer until later? IDC. |
This comment has been minimized.
This comment has been minimized.
|
I can say this much, I usually write without the parenthesis in the single parameter case. But scanning over code like this: var foo = bar => {
window.alert(bar)
}I have to do a double take. Whereas code like this, feels easier to recognize: var foo = (bar) => {
window.alert(bar)
}All very subjective though. I think @feross will just have to pick something :) |
This comment has been minimized.
This comment has been minimized.
|
My personal preferences are in line with @jprichardson's. |
This comment has been minimized.
This comment has been minimized.
|
Agreed with @jprichardson. My habits are the same. Can we enforce the following: x => x
(x, y) => x + y
(x) => { return x }That way, the braced scope enforces parenthesis, but the 1 liner can be either. |
This comment has been minimized.
This comment has been minimized.
julien-f
commented
Nov 2, 2015
|
I am not sure it should be enforced but if we do I think @dcousens' proposal makes sense. |
This comment has been minimized.
This comment has been minimized.
|
@dcousens
|
This comment has been minimized.
This comment has been minimized.
|
Not sure I follow - you should pick one style with parens, or without. I've seen lots of bikeshedding about this issue in projects, and picking a style would fix that. My preference is to go without parens for single argument functions as I believe that it follows the current style of this guide, and it's the style that the test repos use. |
This comment has been minimized.
This comment has been minimized.
|
@KevinGrandon I think standard is the best readability focused guide. Let me reiterate:
|
This comment has been minimized.
This comment has been minimized.
|
@feross thoughts on: x => x
(x) => { return x }
(x, y) => x + y
(x, y) => { return x + y }The English of it is: no parenthesis for a 1 liner unless there is more than 1 argument. Parenthesis always if a braced expression exists. Meaning, of all the possible combinations (not many), the only one that is rejected is: x => {
return x
} |
dcousens
added
the
enhancement
label
Nov 11, 2015
This comment has been minimized.
This comment has been minimized.
|
Big |
This comment has been minimized.
This comment has been minimized.
|
I think that let y = x => x + 1vs let y = (x) => x + 1may be confusing. But I commonly write someMethod(param1, param2, x => {
// do something
})IDK, @feross, I think it's just one of those cases where you're gonna have to just pick something :) |
This comment has been minimized.
This comment has been minimized.
|
In light of a 'just pick something', I'd prefer @rstacruz's, that is, always |
This comment has been minimized.
This comment has been minimized.
If we go this route (enforcing a rule), this would be my preference as well - for the sake of consistency. |
This comment has been minimized.
This comment has been minimized.
|
It may be best to try to use built-in eslint rules if possible. Does this rule cover @rstacruz's preference? Is there overlap with your rule @KevinGrandon? |
This comment has been minimized.
This comment has been minimized.
|
looks like it does. |
This comment has been minimized.
This comment has been minimized.
joshmanders
commented
Dec 11, 2015
|
I have personally taken to always (x) even with one liners, because of consistency with syntax rules, so I agree with adding it like @dcousens says. |
This comment has been minimized.
This comment has been minimized.
|
Using the new improved test suite that tests the top ~400 packages that use
3%
5% |
This comment has been minimized.
This comment has been minimized.
|
@jprichardson's point about consistency in this comment is really good. The overall sentiment of contributors is leaning towards @KevinGrandon Your original argument about consistency with the dangling comma rule was good, too. I think consistency helps readability and that was also the purpose of the dangling comma rule. I don't think this issue is super important, and consistency is preferable. Let's enable this in v6:
|
This comment has been minimized.
This comment has been minimized.
|
|
feross
added a commit
to standard/standard-packages
that referenced
this issue
Feb 5, 2016
feross
closed this
in
standard/eslint-config-standard@dd85d36
Feb 5, 2016
This comment has been minimized.
This comment has been minimized.
|
Will be released in standard 6 |
This comment has been minimized.
This comment has been minimized.
machadogj
commented
Feb 15, 2016
|
I may be late to the discussion, but while I think that the following is somewhat inconsistent: () => ... //parenths I think this could be avoided with: _ => .... //something common in functional languages In this way, 0 and 1 arg funcs could have no parentheses... forbidding |
This comment has been minimized.
This comment has been minimized.
|
seeing those errors in my editor this morning in regards to a single arg arrow func ... I found my way here. my initial reaction ... well, I didn't like it. but then after some thought, I do agree, it will be more consistent this way. I don't like extra typing when it can be avoided, but I also don't like one-off conventions such as the one @machadogj (not that it is invalid, I actually kinda like the idea of using |
alyssaq
added a commit
to alyssaq/react-redux-table-example
that referenced
this issue
Mar 1, 2016
kvz
added a commit
to freyproject/frey
that referenced
this issue
Mar 18, 2016
This comment has been minimized.
This comment has been minimized.
elyobo
commented
Mar 19, 2016
|
Should this be documented somewhere, e.g. in the rules? |
This comment has been minimized.
This comment has been minimized.
jescalan
commented
Mar 21, 2016
|
My only objection is to not being allowed to use |
kvz
added a commit
to transloadit/uppy
that referenced
this issue
Apr 7, 2016
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
I've wrote some more thoughts regarding this in my PR, suggesting |
KevinGrandon commentedOct 26, 2015
I think we should implement this rule in the following manner:
The reason being is that with only a single argument, the function is much easier to read without parens. Some folks who don't like this might argue about expansion and having a bigger diff when adding arguments down the road. Since we currently fail lint on trailing commas I think we should fail here on un-needed parens, it feels like it follows the same ideology as the trailing comma rule. (If we did enforce trailing commas, then I would probably enforce parens here as well).
I have a simple eslint plugin here that I'd be happy to include/relinquish: https://github.com/KevinGrandon/eslint-plugins/blob/master/lib/rules/arrow-function-parens.js