Skip to content

Commit 4c2b00b

Browse files
authored
no-array-callback-reference: Ignore AwaitExpression except reduce and reduceRight (#814)
1 parent 517a782 commit 4c2b00b

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

rules/no-array-callback-reference.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ const create = context => {
147147

148148
for (const [method, options] of iteratorMethods) {
149149
const selector = [
150+
method === 'reduce' || method === 'reduceRight' ? '' : ':not(AwaitExpression) > ',
150151
methodSelector({
151152
name: method,
152153
min: 1,
@@ -156,6 +157,7 @@ const create = context => {
156157
ignoredCalleeSelector,
157158
ignoredFirstArgumentSelector
158159
].join('');
160+
159161
rules[selector] = node => {
160162
const [iterator] = node.arguments;
161163
check(context, iterator, method, options, sourceCode);

test/no-array-callback-reference.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,17 @@ ruleTester.run('no-array-callback-reference', rule, {
9999
'foo.map(function() {})',
100100
'foo.map(function bar() {})',
101101

102+
// Exclude await expressions
103+
...simpleMethods.map(method => `(async () => await foo.${method}(bar))()`),
104+
105+
// #813
106+
outdent`
107+
async function foo() {
108+
const clientId = 20
109+
const client = await oidc.Client.find(clientId)
110+
}
111+
`,
112+
102113
// #755
103114
outdent`
104115
const results = collection
@@ -278,6 +289,86 @@ ruleTester.run('no-array-callback-reference', rule, {
278289
]
279290
},
280291

292+
// `await`
293+
invalidTestCase({
294+
code: outdent`
295+
const fn = async () => {
296+
await Promise.all(foo.map(toPromise));
297+
}
298+
`,
299+
method: 'map',
300+
name: 'toPromise',
301+
suggestions: [
302+
outdent`
303+
const fn = async () => {
304+
await Promise.all(foo.map((element) => toPromise(element)));
305+
}
306+
`,
307+
outdent`
308+
const fn = async () => {
309+
await Promise.all(foo.map((element, index) => toPromise(element, index)));
310+
}
311+
`,
312+
outdent`
313+
const fn = async () => {
314+
await Promise.all(foo.map((element, index, array) => toPromise(element, index, array)));
315+
}
316+
`
317+
]
318+
}),
319+
invalidTestCase({
320+
code: outdent`
321+
async function fn() {
322+
for await (const foo of bar.map(toPromise)) {}
323+
}
324+
`,
325+
method: 'map',
326+
name: 'toPromise',
327+
suggestions: [
328+
outdent`
329+
async function fn() {
330+
for await (const foo of bar.map((element) => toPromise(element))) {}
331+
}
332+
`,
333+
outdent`
334+
async function fn() {
335+
for await (const foo of bar.map((element, index) => toPromise(element, index))) {}
336+
}
337+
`,
338+
outdent`
339+
async function fn() {
340+
for await (const foo of bar.map((element, index, array) => toPromise(element, index, array))) {}
341+
}
342+
`
343+
]
344+
}),
345+
invalidTestCase({
346+
code: outdent`
347+
async function fn() {
348+
await foo.reduce(foo, Promise.resolve())
349+
}
350+
`,
351+
method: 'reduce',
352+
name: 'foo',
353+
suggestions: [
354+
outdent`
355+
async function fn() {
356+
await foo.reduce((accumulator, element) => foo(accumulator, element), Promise.resolve())
357+
}
358+
`,
359+
outdent`
360+
async function fn() {
361+
await foo.reduce((accumulator, element, index) => foo(accumulator, element, index), Promise.resolve())
362+
}
363+
`,
364+
outdent`
365+
async function fn() {
366+
await foo.reduce((accumulator, element, index, array) => foo(accumulator, element, index, array), Promise.resolve())
367+
}
368+
`
369+
]
370+
}),
371+
281372
// #418
282373
invalidTestCase({
283374
code: outdent`

0 commit comments

Comments
 (0)