Skip to content

Commit

Permalink
no-array-callback-reference: Ignore AwaitExpression except `reduc…
Browse files Browse the repository at this point in the history
…e` and `reduceRight` (#814)
  • Loading branch information
fisker committed Jan 11, 2021
1 parent 517a782 commit 4c2b00b
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
2 changes: 2 additions & 0 deletions rules/no-array-callback-reference.js
Expand Up @@ -147,6 +147,7 @@ const create = context => {

for (const [method, options] of iteratorMethods) {
const selector = [
method === 'reduce' || method === 'reduceRight' ? '' : ':not(AwaitExpression) > ',
methodSelector({
name: method,
min: 1,
Expand All @@ -156,6 +157,7 @@ const create = context => {
ignoredCalleeSelector,
ignoredFirstArgumentSelector
].join('');

rules[selector] = node => {
const [iterator] = node.arguments;
check(context, iterator, method, options, sourceCode);
Expand Down
91 changes: 91 additions & 0 deletions test/no-array-callback-reference.js
Expand Up @@ -99,6 +99,17 @@ ruleTester.run('no-array-callback-reference', rule, {
'foo.map(function() {})',
'foo.map(function bar() {})',

// Exclude await expressions
...simpleMethods.map(method => `(async () => await foo.${method}(bar))()`),

// #813
outdent`
async function foo() {
const clientId = 20
const client = await oidc.Client.find(clientId)
}
`,

// #755
outdent`
const results = collection
Expand Down Expand Up @@ -278,6 +289,86 @@ ruleTester.run('no-array-callback-reference', rule, {
]
},

// `await`
invalidTestCase({
code: outdent`
const fn = async () => {
await Promise.all(foo.map(toPromise));
}
`,
method: 'map',
name: 'toPromise',
suggestions: [
outdent`
const fn = async () => {
await Promise.all(foo.map((element) => toPromise(element)));
}
`,
outdent`
const fn = async () => {
await Promise.all(foo.map((element, index) => toPromise(element, index)));
}
`,
outdent`
const fn = async () => {
await Promise.all(foo.map((element, index, array) => toPromise(element, index, array)));
}
`
]
}),
invalidTestCase({
code: outdent`
async function fn() {
for await (const foo of bar.map(toPromise)) {}
}
`,
method: 'map',
name: 'toPromise',
suggestions: [
outdent`
async function fn() {
for await (const foo of bar.map((element) => toPromise(element))) {}
}
`,
outdent`
async function fn() {
for await (const foo of bar.map((element, index) => toPromise(element, index))) {}
}
`,
outdent`
async function fn() {
for await (const foo of bar.map((element, index, array) => toPromise(element, index, array))) {}
}
`
]
}),
invalidTestCase({
code: outdent`
async function fn() {
await foo.reduce(foo, Promise.resolve())
}
`,
method: 'reduce',
name: 'foo',
suggestions: [
outdent`
async function fn() {
await foo.reduce((accumulator, element) => foo(accumulator, element), Promise.resolve())
}
`,
outdent`
async function fn() {
await foo.reduce((accumulator, element, index) => foo(accumulator, element, index), Promise.resolve())
}
`,
outdent`
async function fn() {
await foo.reduce((accumulator, element, index, array) => foo(accumulator, element, index, array), Promise.resolve())
}
`
]
}),

// #418
invalidTestCase({
code: outdent`
Expand Down

0 comments on commit 4c2b00b

Please sign in to comment.