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 rule to forbid "chained arrows" #4

Closed
getify opened this Issue Mar 11, 2019 · 6 comments

Comments

Projects
None yet
3 participants
@getify
Copy link
Owner

getify commented Mar 11, 2019

Because some of us feel that chained arrow returns (such as are common in curried function definitions) is much harder to read, given that you basically have to read them right-to-left to figure out where the function boundaries are, add rule that forbids chained arrow returns:

var f => x + 1;   // not chained, OK

var g => x => y => x + y;   // Error

Also, we need a flag to optionally allow/ignore these:

var g = x => (y => x + y);
var g = x => { return y => x + y; }

@getify getify changed the title Add rule to disable "chained arrows" Add rule to forbid "chained arrows" Mar 11, 2019

@the-simian

This comment has been minimized.

Copy link

the-simian commented Mar 11, 2019

I feel like an element of this proposal with utility would be to enforce a certain depth (rather than wholistically allow or disallow entirely). You could, of course, set a depth of 0 (allow nothing).

Reasoning: This patterns nice to add args into scope for things like event handlers/ map, where a developer may need additional args in the closure, and the 'last' function has predetermined args. Here an enforced chaining depth is useful. It also allows devs to use curried functions as a practice but prevents 'over currying' where one might want to investigate more practical abstractions.

With depth, regardless of a developers comfort zone, they can tweak this rule to their taste.

@getify

This comment has been minimized.

Copy link
Owner Author

getify commented Mar 11, 2019

From: https://twitter.com/dev01ution/status/1105209319174361090

Perhaps another option could be to allow a chained arrow return if it's surrounded by parentheses?

fn = x => y => x + y;   // error
fn = x => ( y => x + y );  // OK
@getify

This comment has been minimized.

Copy link
Owner Author

getify commented Mar 14, 2019

BTW, for consistency, I think this rule will actually be called "return", and will merge in the existing "object-return" behavior, so that one rule controls the various options on "return" values, and one controls the various options on "params".

@getify

This comment has been minimized.

Copy link
Owner Author

getify commented Mar 15, 2019

@the-simian Just curious, can you show me an example where you like using the chained arrow?

Does it look something like?

btn.addEventListener(
  "click",
  (data => evt => doSomething(data,evt))( mydata )
);

I'm not really clear on the use case.

@getify getify closed this in 47fc6c9 Mar 15, 2019

getify added a commit that referenced this issue Mar 16, 2019

@davelogear

This comment has been minimized.

Copy link

davelogear commented Mar 17, 2019

I'm curious why this was closed, even though I'm uncomfortable with the idea of JavaScript throwing errors or failing a script because it's not as "readable" as another JavaScript developer thinks it should be.

@getify

This comment has been minimized.

Copy link
Owner Author

getify commented Mar 17, 2019

It was closed because the feature being discussed -- a configurable rule about chained arrow function returns -- in this thread was added.

By definition, what linters do is apply opinions to your code. You may agree with the opinions or disagree with them, but that's what linters are for. The best thing is when linter tools are configurable, so you (and your team) can make the decision about which opinions you agree with and find helpful, and which ones you don't.

So, you can use this linter plugin or not use it. And if you use it, you can configure whichever included rules you like or not, so you can benefit from parts of it even you disagree with others.

Or don't. Makes no difference to me. That's the beauty of configurable rules!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.