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 allowSimpleOperations option to no-array-reduce rule #1418

Merged

Conversation

manovotny
Copy link
Contributor

This is my first time contributing something to this project besides from minor documentation tweaks, so don't feel bad telling me I did something incorrectly.

I took a stab at the open issue to allow simple sums when using reduce. I am not married to the option name, so please offer better suggestions if you have them.

Fixes #1348

@sindresorhus
Copy link
Owner

I think it should be true by default. This is the one case where "reduce" 100% makes sense.

@manovotny
Copy link
Contributor Author

@sindresorhus I am totally open to that and can start whipping up changes. I figured it was best to start it off as a non-breaking change until a conversation could be had.

@manovotny
Copy link
Contributor Author

@sindresorhus with the flipping of default to true it uncovered a use case that I didn't have right originally and could use your advice of how you want to handle it.

It's this from MDN's reduce documentation on parameters (emphasis added):

A value to use as the first argument to the first call of the callbackFn. If no initialValue is supplied, the first element in the array will be used as the initial accumulator value and skipped as currentValue.

For example:

const nums = [1, 2, 3, 4, 5];
console.log(nums.reduce((total, item) => total + item, 0)); // -> 15
console.log(nums.reduce((total, item) => total + item)); // -> 15

I am unsure of how to check the assumed values that are being iterated on without the ability to check initialValue.

Any great ideas? Or should the rule only work / pass when initialValue is present, thus forcing people to use an initialValue to get started?

@sindresorhus
Copy link
Owner

Or should the rule only work / pass when initialValue is present, thus forcing people to use an initialValue to get started?

I think an initial value helps readability, so I agree we can just enforce that if they want it to pass.

@manovotny
Copy link
Contributor Author

@sindresorhus I've changed the default value to true and updated the documentation. Please let me know if there are any other changes you'd like me to make.

@fisker
Copy link
Collaborator

fisker commented Jul 14, 2021

Do you think the following cases are simple enough to ignore?

const getValueByPath = (object, path) =>
	path.split('.').reduce((value, key) => value[key], object);
const queue = steps =>
	steps.reduce((promise, step) => promise.then(step), Promise.resolve());

@sindresorhus
Copy link
Owner

Do you think the following cases are simple enough to ignore?

IMHO, no.

@fisker
Copy link
Collaborator

fisker commented Jul 14, 2021

I think we should also check the callback function body, only allow simple + or * (maybe ** too?). And let's remove this option, just allow these cases. WDYT?

@sindresorhus
Copy link
Owner

only allow simple + or * (maybe ** too?)

Yeah, and - and /.

@manovotny
Copy link
Contributor Author

@sindresorhus @fisker both your comments make sense and seem doable. I was in all day meetings today and will be again tomorrow. I should be able to circle back to this on Friday or into the weekend. Thanks for the feedback!

@manovotny
Copy link
Contributor Author

I've updated the PR to allow a direct BinaryExpression as the body of the function. That way we shouldn't need to check for specific operators. Let me know if I am not thinking of something by allowing this.

I also renamed the lint rule option to allowSimpleOperations. I am not completely happy with it, but it was the best I could think of. Completely open to suggestions.

And @fisker, I tried changing the lines of code from your comment back to using reduce, but then npm test failed. I think it's because xo needs to update to a newer release of eslint-plugin-unicorn once this rule is merged and updated in xo for it to work. Let me know if I am mistaken in that thinking.

@manovotny manovotny changed the title Add allowNumericInitialValue option to no-array-reduce rule. Add allowSimpleOperations option to no-array-reduce rule. Jul 19, 2021
@manovotny
Copy link
Contributor Author

@sindresorhus @fisker do either of you have any feedback you'd like me to address?

@sindresorhus sindresorhus changed the title Add allowSimpleOperations option to no-array-reduce rule. Add allowSimpleOperations option to no-array-reduce rule Jul 29, 2021
@manovotny manovotny force-pushed the no-array-reduce-allow-numeric-initial-value branch from c873345 to 5aadd69 Compare July 31, 2021 04:00
@sindresorhus
Copy link
Owner

Can you fix the lint violation?

manovotny and others added 6 commits July 31, 2021 13:26
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
@manovotny
Copy link
Contributor Author

Can you fix the lint violation?

Unless I am misunderstanding the failure, I believe it's coming from prevent-abbreviations.js. Perhaps an oversight of #1445?

I can address it here if you'd like or in a separate PR. Your call. Either way. It take a little more work (not a ton) because it's a dynamically generated message.

@manovotny
Copy link
Contributor Author

manovotny commented Jul 31, 2021

@sindresorhus aside from the lint issue and my comment above regarding the issue, I believe everything else on the PR has been addressed.

@sindresorhus
Copy link
Owner

#1418 (comment)

@manovotny
Copy link
Contributor Author

@sindresorhus all arr abbreviations removed in the rest file. Please let me know if / how you'd like me to resolve the lint issue.

@fisker fisker merged commit 153eb2c into sindresorhus:main Aug 2, 2021
@manovotny manovotny deleted the no-array-reduce-allow-numeric-initial-value branch August 2, 2021 03:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ignore simple sum in no-array-reduce
3 participants