Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions rules/no-array-reduce.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ const cases = [
methods: ['reduce', 'reduceRight'],
minimumArguments: 1,
maximumArguments: 2,
optionalCall: false,
optionalMember: false,
})
&& !isNodeValueNotFunction(callExpression.arguments[0]),
getMethodNode: callExpression => callExpression.callee.property,
Expand Down Expand Up @@ -47,8 +45,6 @@ const cases = [
test: callExpression =>
isMethodCall(callExpression, {
method: 'call',
optionalCall: false,
optionalMember: false,
})
&& isArrayPrototypeProperty(callExpression.callee.object, {
properties: ['reduce', 'reduceRight'],
Expand All @@ -64,8 +60,6 @@ const cases = [
test: callExpression =>
isMethodCall(callExpression, {
method: 'apply',
optionalCall: false,
optionalMember: false,
})
&& isArrayPrototypeProperty(callExpression.callee.object, {
properties: ['reduce', 'reduceRight'],
Expand Down
4 changes: 4 additions & 0 deletions test/no-array-reduce.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

'a.reduce(1, 2, 3)',
'a.reduce(b, c, d)',
'[][reduce].call()',
Expand Down Expand Up @@ -106,6 +108,8 @@ test({
].flatMap(testCase => [testCase, testCase.replace('reduce', 'reduceRight')]),
invalid: [
'array.reduce((str, item) => str += item, "")',
'array?.reduce((str, item) => str += item, "")',
'array.reduce?.((str, item) => str += item, "")',
outdent`
array.reduce((obj, item) => {
obj[item] = null;
Expand Down
Loading