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

Add arrow function rules to eslint #4813

Merged
merged 2 commits into from
Jan 28, 2016
Merged

Add arrow function rules to eslint #4813

merged 2 commits into from
Jan 28, 2016

Conversation

cjihrig
Copy link
Contributor

@cjihrig cjihrig commented Jan 22, 2016

This PR enforces the following linting rules:

  • arrow-parens - requires parens around arrow function arguments
  • arrow-spacing - ensures a space on each side of the =>
  • no-arrow-condition - prevents accidental use of => in cases
    where the user really intends to use <=

The only code that violated these rules existed in the test directory.
R= @rvagg

@rvagg
Copy link
Member

rvagg commented Jan 22, 2016

lgtm

@Qard
Copy link
Member

Qard commented Jan 22, 2016

LGTM

2 similar comments
@jasnell
Copy link
Member

jasnell commented Jan 22, 2016

LGTM

@thefourtheye
Copy link
Contributor

LGTM

@timoxley
Copy link
Contributor

@rvagg

I find the terseness without them make them harder to visually parse when reading code.

This is truly only temporary from being used to ES5, it does go away but…

I've been thinking about the issue of arrow function parens a bit recently and I think there's a good case for a middle ground.

Perhaps consider this instead:

  • No parens for single line arrows
  • Always parens for multi-line, even if single arg
  • Always multi-line for multiple args

Having more than one argument messes up terseness/simplicity of single line arrows anyway.
This keeps single line arrows nice and clean, but also restricts the use of single line arrow to simpler, one arg types of functions, which I think is where they excel. Value proposition of single line drops dramatically as complexity of the function goes up.

Also found you're far more likely to need to add multiple args for something that's already a multi-line function, so this pre-empts that and removes annoying need to add parens around just because you're adding one argument.

So trivial things like this remain nice and terse:

items.map(item => item.price)

but adding a second arg messes the terseness of a single line up even before using the arg in a function body:

items.map((item, index) => item.price)

Instead, the proposed option would force a multi-line structure for multi-args, like so:

items.map((item, index) => {
  return item.price
})

Not sure if there's a rule for this in eslint.

@mscdex mscdex added the tools Issues and PRs related to the tools directory. label Jan 22, 2016
@saghul
Copy link
Member

saghul commented Jan 22, 2016

LGTM.

@targos
Copy link
Member

targos commented Jan 22, 2016

lgtm

@mcollina
Copy link
Member

LGTM, but I'm also ok with @timoxley proposal.

## Enforce parens around arrow function arguments
arrow-parens: [2, "always"]
## Require a space on each side of arrow operator
arrow-spacing: [2, { "before": true, "after": true }]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: no spaces inside braces.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and technically the quotes are optional but we don't have consistent use of them in this file...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, I wasn't aware that quotes are optional in most cases YAML. I'll investigate if we can remove all quotes.

@evanlucas
Copy link
Contributor

LGTM. Definitely agree on this

@bnoordhuis
Copy link
Member

So, am I the only one who finds parens around a single argument arrow function ugly?

@zeusdeux
Copy link
Contributor

Nope. I am with you on that @bnoordhuis.

@Fishrock123
Copy link
Contributor

Better to have less options here for the sake of new people who will inevitably become confused by the like 5 different cases of style for arrow functions. LGTM

@Qard
Copy link
Member

Qard commented Jan 22, 2016

Personally, I prefer to only use arrow functions in the single line, paren free style, and with short argument names.

The needing to explicitly return in multi line arrow functions but not single line ones is super confusing to new people, so I'd avoid that style entirely.

@Fishrock123
Copy link
Contributor

Personally, I prefer to only use arrow functions in the single line, paren free style, and with short argument names.

Sure, but chances are we'll care more about the speed of the context capturing vs bind() over style.

@cjihrig
Copy link
Contributor Author

cjihrig commented Jan 22, 2016

@jasnell
Copy link
Member

jasnell commented Jan 23, 2016

CI failures look unrelated. There are more than enough signoffs to land but given the discussion it may be worth while holding this one open for another day.

@jasnell
Copy link
Member

jasnell commented Jan 23, 2016

@bnoordhuis ... I personally find arrow functions ugly in general along with the parens, but I'd rather we had consistency

@jasnell
Copy link
Member

jasnell commented Jan 23, 2016

going to mark this one as do not land in v4 tho... can revisit that if folks feel strongly about it tho

@thefourtheye
Copy link
Contributor

@jasnell If we do not backport linter rules, landing code which follow this would be difficult in the future, no?

@jasnell
Copy link
Member

jasnell commented Jan 23, 2016

yes, if those rules are less restrictive or change an existing rule. In this case, not adding this rule shouldn't impact anything (tho I could be wrong)

@thefourtheye
Copy link
Contributor

not adding this rule shouldn't impact anything

Yes, but I am thinking of a very rare case. Let's say we land a fix which is specific to 4.x and if that doesn't follow this rule, then we will have two slightly different style systems and it wouldn't be consistent, right?

@jasnell
Copy link
Member

jasnell commented Jan 23, 2016

Yeah, you're right.
On Jan 22, 2016 10:04 PM, "thefourtheye" notifications@github.com wrote:

not adding this rule shouldn't impact anything

Yes, but I am thinking of a very rare case. Let's say we land a fix which
is specific to 4.x and if that doesn't follow this rule, then we will have
two slightly different style systems and it wouldn't be consistent, right?


Reply to this email directly or view it on GitHub
#4813 (comment).

arrow-spacing: [2, {"before": true, "after": true}]
## Prevent using => in a condition where <= is intended
no-arrow-condition: 2

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no Arrow Functions category in http://eslint.org/docs/rules/. Can you structure the rules like in that list?

@benjamingr
Copy link
Member

I'm with @bnoordhuis writing e => e.text is nicer than (e) => e.text. The language lets us omit these because it is a simple one parameter case.

As a data point - linters for C# (like ReSharper's rules) raise a warning (by default) if you include the ()s in a single parameter. If I remember correctly (please correct me) so do Java IDEs like IntelliJ (for ->) and Scala.

@cjihrig
Copy link
Contributor Author

cjihrig commented Jan 25, 2016

@jasnell thoughts on landing this now? The nice thing about disabling or loosening a lint rule in the future is that it doesn't require going back and fixing the code again.

MylesBorins pushed a commit that referenced this pull request Mar 2, 2016
This commit applies new arrow function linting rules across the
codebase. As it turns out, the only offenders were in the test
directory.

PR-URL: #4813
Reviewed-By: Rod Vagg <rod@vagg.org>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Saúl Ibarra Corretgé <saghul@gmail.com>
Reviewed-By: Michaël Zasso <mic.besace@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
scovetta pushed a commit to scovetta/node that referenced this pull request Apr 2, 2016
This commit enables the following rules:
* arrow-parens - requires parens around arrow function arguments
* arrow-spacing - ensures a space on each side of the =>
* no-arrow-condition - prevents accidental use of => in cases
where the user really intends to use <=

PR-URL: nodejs#4813
Reviewed-By: Rod Vagg <rod@vagg.org>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Saúl Ibarra Corretgé <saghul@gmail.com>
Reviewed-By: Michaël Zasso <mic.besace@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
scovetta pushed a commit to scovetta/node that referenced this pull request Apr 2, 2016
This commit applies new arrow function linting rules across the
codebase. As it turns out, the only offenders were in the test
directory.

PR-URL: nodejs#4813
Reviewed-By: Rod Vagg <rod@vagg.org>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Saúl Ibarra Corretgé <saghul@gmail.com>
Reviewed-By: Michaël Zasso <mic.besace@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tools Issues and PRs related to the tools directory.
Projects
None yet
Development

Successfully merging this pull request may close these issues.