Skip to content

Conversation

ipanasenko
Copy link
Contributor

@ipanasenko ipanasenko commented Aug 29, 2025

Fixes #2720

This PR enables checking for optionally chainged .reduce() calls for no-array-reduce rule.

@ipanasenko ipanasenko changed the title no-array-reduce - add test cases for optional reduce no-array-reduce - enable reporting of optional chaining and calling of .reduce() Sep 1, 2025
@ipanasenko ipanasenko marked this pull request as ready for review September 1, 2025 14:21
@ipanasenko ipanasenko requested a review from fregante September 1, 2025 14:22
@@ -12,6 +12,8 @@ test({
'a[b.reduce]()',
'a(b.reduce)',
'a.reduce()',
'a?.reduce()',
'a.reduce?.()',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't make sense to allow optional call.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are valid cases in these two rules, and they both use optionalCall: false,

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean a?.reduce() should be invalid, but a.reduce?.() should be valid.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I comment this in a misleading place, I mean L112

Copy link
Contributor Author

@ipanasenko ipanasenko Sep 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand, sorry :(
Why one should be valid and another invalid? I thought this rule finds usages of reduce, and reports it (except trivial callback cases, if corresponding option is provided). Why reporting of a?.reduce() should be different to a.reduce?.() and a.reduce()?
Or array.reduce((str, item) => str += item, "") different to array?.reduce((str, item) => str += item, "") and array.reduce?.((str, item) => str += item, "") if we talk about L112?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it's doesn't make sense to check Array#reduce, if user using foo.reduce?.() then foo can be a non-array object.

Explained in this commit 8240f4f (#2721)

@fisker
Copy link
Collaborator

fisker commented Sep 1, 2025

@sindresorhus I plan to go through all rules to check if optionalMemeber check is needed this week, do not release until I finish.

@sindresorhus sindresorhus merged commit caa1f8b into sindresorhus:main Sep 1, 2025
18 checks passed
@ipanasenko ipanasenko deleted the reduce-optional branch September 2, 2025 09:27
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.

no-array-reduce - doesn't report optional chaining
4 participants