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

no-confusing-arrow flags conditionals inside parentheses. #5332

Closed
dtinth opened this issue Feb 19, 2016 · 10 comments
Closed

no-confusing-arrow flags conditionals inside parentheses. #5332

dtinth opened this issue Feb 19, 2016 · 10 comments
Assignees
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly rule Relates to ESLint's core rules

Comments

@dtinth
Copy link
Contributor

dtinth commented Feb 19, 2016

ESLint Version: 2.1.0

This is a pattern I use frequently in JavaScript code:

const renderItem = item => (isSeparator(item)
  ? React.createElement(Separator)
  : React.createElement(MenuItem, item)
)

Note that the conditionals are inside parentheses. In this case, the meaning is not ambiguous. You can’t interpret a => (1 ? 2 : 3) as (a >= 1) ? 2 : 3.

Test Case

/* eslint no-confusing-arrow: 2 */
var x = a => (1 ? 2 : 3)

Expected Result

No lint errors.

Actual Result

Arrow function used ambiguously with a conditional expression. (no-confusing-arrow)

@nzakas nzakas added bug ESLint is working incorrectly rule Relates to ESLint's core rules accepted There is consensus among the team that this change meets the criteria for inclusion labels Feb 19, 2016
@nzakas
Copy link
Member

nzakas commented Feb 19, 2016

Confirmed. Do you want to submit a pull request for this?

@alberto
Copy link
Member

alberto commented Feb 20, 2016

What about this?

var a = 2;
var x = a >= (true ? 2 : 3);

@alecmev
Copy link

alecmev commented Feb 21, 2016

This bug is even worse with arrow-body-style set to as-needed. It essentially makes it impossible to express a => 1 ? 2 : 3 without ESLint showing an error.

@gilbarbara
Copy link

Hey! I just ran into this problem..
With no-confusing-arrow and arrow-body-style rules on, I can't seem to pass the validations.

input.replace(tags, ($0, $1) => (allowed.indexOf(`<${$1.toLowerCase()}>`) > -1 ? $0 : ''));

triggers no-confusing-arrow

input.replace(tags, ($0, $1) => { return allowed.indexOf(`<${$1.toLowerCase()}>`) > -1 ? $0 : ''; });

triggers arrow-body-style

I think that allowng parentheses around the expression will be great.

@dtinth
Copy link
Contributor Author

dtinth commented Feb 22, 2016

Would love to work on it, but have been quite busy, so I am not sure whether I’ll have time to do it.

@nzakas @jeremejevs @gilbarbara What do you think about @alberto’s case?

Does var x = a => (true ? 2 : 3) look confusing?

For me, I think it’s pretty clear that it’s a function that returns some value, because I get used to seeing this: => (.

@platinumazure
Copy link
Member

I think an arrow shouldn't be considered "confusing" (as in, could be confused with >=) if there is a parenthesis token in between. Or at least, there should be an option for it. (I was trying to think if >= ( was always non-parseable, but unfortunately it does parse in many cases, so it should probably be an option for the user.)

@alecmev
Copy link

alecmev commented Feb 22, 2016

@alberto Your example isn't limited to ternary, though. Any (every?) "concise body" arrow function can be confusing in one way or another, no?

var a = 2;
var x = a >= (true ? 2 : 3);
var y = a >= (b || a);
var z = a >= Math.sin(a);

Isn't this static typing jurisdiction?

@alberto
Copy link
Member

alberto commented Feb 22, 2016

@jeremejevs Every arrow function with a concise body and taking exactly one argument, yes. I think that's (or should be) the purpose of the rule.

ljharb added a commit to airbnb/javascript that referenced this issue Feb 22, 2016
SpaceK33z added a commit to CodeYellowBV/eslint-config-codeyellow that referenced this issue Feb 23, 2016
This update _temporarily_ removes `no-confusing-arrow`, so our override can be removed.

More info about this eslint/eslint#5332
@BYK BYK self-assigned this Mar 7, 2016
@BYK
Copy link
Member

BYK commented Mar 7, 2016

Working on this, pretty close. Added a allowParens option defaulting to false to keep the old behavior as default.

@shinzui
Copy link

shinzui commented May 8, 2016

Is allowParens set to true supposed to work with no-extra-parens. It's now triggering "Gratuitous parentheses around expression. (no-extra-parens)".

jaylaw81 pushed a commit to appirio-digital/ads-best-practices that referenced this issue Sep 19, 2017
sensiblegame added a commit to sensiblegame/React-BNB that referenced this issue Oct 23, 2017
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Feb 6, 2018
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Feb 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly rule Relates to ESLint's core rules
Projects
None yet
Development

No branches or pull requests

8 participants