Skip to content

Commit caa1f8b

Browse files
ipanasenkofisker
andauthored
no-array-reduce: Report optional chaining and calling of .reduce() (#2721)
Co-authored-by: fisker <[email protected]>
1 parent 494dfda commit caa1f8b

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

rules/no-array-reduce.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ const cases = [
1717
minimumArguments: 1,
1818
maximumArguments: 2,
1919
optionalCall: false,
20-
optionalMember: false,
2120
})
2221
&& !isNodeValueNotFunction(callExpression.arguments[0]),
2322
getMethodNode: callExpression => callExpression.callee.property,

test/no-array-reduce.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ test({
1212
'a[b.reduce]()',
1313
'a(b.reduce)',
1414
'a.reduce()',
15+
'a?.reduce()',
16+
'a.reduce?.()',
1517
'a.reduce(1, 2, 3)',
1618
'a.reduce(b, c, d)',
1719
'[][reduce].call()',
@@ -79,6 +81,9 @@ test({
7981
// More or less argument(s)
8082
// We are not checking arguments length
8183

84+
// Ignore optional call to avoid false positive on non-array objects
85+
'array.reduce?.((str, item) => str += item, "")',
86+
8287
// `reduce-like`
8388
'array.reducex(foo)',
8489
'array.xreduce(foo)',
@@ -106,6 +111,7 @@ test({
106111
].flatMap(testCase => [testCase, testCase.replace('reduce', 'reduceRight')]),
107112
invalid: [
108113
'array.reduce((str, item) => str += item, "")',
114+
'array?.reduce((str, item) => str += item, "")',
109115
outdent`
110116
array.reduce((obj, item) => {
111117
obj[item] = null;

0 commit comments

Comments
 (0)