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

New rule: no partial array reduce #527

Closed
danielnixon opened this issue Feb 5, 2023 · 4 comments
Closed

New rule: no partial array reduce #527

danielnixon opened this issue Feb 5, 2023 · 4 comments
Labels
Status: Awaiting Feedback Issue or PR awaits feedback from the community. Type: Idea Marks an idea, which might be accepted and implemented.

Comments

@danielnixon
Copy link

danielnixon commented Feb 5, 2023

Suggestion

A new rule that forbids the partial versions of reduce and reduceRight.

Given:

  const arr: readonly string[] = ...; // may or may not be empty

Would be invalid according to suggested rule:

  // compiles, throws TypeError: Reduce of empty array with no initial value
  const foo = arr.reduce((a, b) => a);

  // compiles, throws TypeError: Reduce of empty array with no initial value
  const bar = arr.reduceRight((a, b) => a);

Would be valid according to suggested rule:

  // compiles, doesn't throw
  const foo = arr.reduce((a, b) => a + b, "");

  // compiles, doesn't throw
  const bar = arr.reduceRight((a, b) => a + b, "");


  // if you can prove to tsc that you have at least one element, the form without `initialValue` is safe:
  const arr: readonly [string, ...(readonly string[])] = [""];
  const foo = arr.reduce((a, b) => a);

This is equivalent to the situation with reduce versus fold in Scala: https://www.wartremover.org/doc/warts.html#iterableops

@danielnixon danielnixon added Status: Triage This issue needs to be triaged. Type: Idea Marks an idea, which might be accepted and implemented. labels Feb 5, 2023
@RebeccaStevens RebeccaStevens removed Type: Idea Marks an idea, which might be accepted and implemented. Status: Triage This issue needs to be triaged. labels Mar 18, 2023
@RebeccaStevens
Copy link
Collaborator

I'm really unsure about whether to add this or not.
eslint-plugin-total-functions might just be a better home for such a rule.

@jonaskello Do you have any opinion on this?

@danielnixon
Copy link
Author

That's fair enough, you won't offend me if that's the outcome.

I had a inkling it might be appropriate over here thanks to the combination of already being anti-exceptions (plugin:functional/no-exceptions) and reduce being an especially common operation in FP-oriented codebases.

@RebeccaStevens RebeccaStevens added Type: Idea Marks an idea, which might be accepted and implemented. Status: Awaiting Feedback Issue or PR awaits feedback from the community. labels Jul 21, 2023
@RebeccaStevens
Copy link
Collaborator

I'm going to close this as there hasn't been any feedback on it.

@RebeccaStevens RebeccaStevens closed this as not planned Won't fix, can't repro, duplicate, stale Mar 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Awaiting Feedback Issue or PR awaits feedback from the community. Type: Idea Marks an idea, which might be accepted and implemented.
Projects
None yet
Development

No branches or pull requests

2 participants