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

Rule discussion: Arrow function should not return assignment #1204

Closed
tom-sherman opened this issue Sep 24, 2018 · 3 comments

Comments

@tom-sherman
Copy link

commented Sep 24, 2018

What version of standard?

v12.0.1

What operating system, Node.js, and npm version?

Node.js

What did you expect to happen?

When trying to sum the sizes of an array of Sets I tried to write

sets.reduce((acc, set) => acc += set.size, 0)

What actually happened?
Standard shows the error

Arrow function should not return assignment. (no-return-assign)

I don't think this rule is helpful in this scenario, I can't use Array.reduce at all in this scenario AFAIK. I understand why you don't want to allow the following code, but this Array.reduce use case seems more legitimate

const foo = x => x = 2
// or
function foo (x) {
  return x = 2
}
// The author in either of these examples probably meant to return a comparison

If the above is is intentional, please can someone explain what code smell I'm creating here?

If not, I'd like to suggest changing the rule to except-parens[1] option provided by Eslint or remove it completely.

The except-parens option would allow for the following style which I like because it clearly signals the author's intent to return an assignment.

sets.reduce((acc, set) => (acc += set.size), 0)
@LinusU

This comment has been minimized.

Copy link
Member

commented Sep 24, 2018

I actually think is a quite good rule to have since it's quite uncommon to want to return an assignment value.

This is currently enforced regardless of wether it is an arrow function or not btw, so return a += 1 is invalid. The proper way to signal that it was the intention, is to wrap it in a parenthesis: return (a += 1), which is valid.

In your code it seems like there is no need to modify acc since that is only a local copy to your function, is there any reason to not write it like this?

sets.reduce((acc, set) => acc + set.size, 0)
@tom-sherman

This comment has been minimized.

Copy link
Author

commented Sep 24, 2018

@LinusU For some reason I had to mutate the accumulator, thanks for the tip 🤦‍♂️

Also, looks like the except-parens is already turned on so this ticket is just completely bogus, please close!

@LinusU

This comment has been minimized.

Copy link
Member

commented Sep 24, 2018

No stress 👌

@LinusU LinusU closed this Sep 24, 2018

@lock lock bot locked as resolved and limited conversation to collaborators Dec 23, 2018

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