Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 54ee4ae

Browse files
authored
fix(await-async-event): prevent adding async/await in forEach loops (#1075)
Fixes #843
1 parent 1477f21 commit 54ee4ae

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

‎lib/rules/await-async-events.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { createTestingLibraryRule } from '../create-testing-library-rule';
44
import {
55
findClosestCallExpressionNode,
66
findClosestFunctionExpressionNode,
7+
getDeepestIdentifierNode,
78
getFunctionName,
89
getInnermostReturningFunction,
910
getVariableReferences,
@@ -250,6 +251,13 @@ export default createTestingLibraryRule<Options, MessageIds>({
250251
findClosestFunctionExpressionNode(node);
251252

252253
if (functionExpression) {
254+
const deepestCalleeIdentifier = getDeepestIdentifierNode(
255+
functionExpression.parent
256+
);
257+
if (deepestCalleeIdentifier?.name === 'forEach') {
258+
return null;
259+
}
260+
253261
const memberExpressionFixer = fixer.insertTextBefore(
254262
node.parent,
255263
'await '

‎tests/lib/rules/await-async-events.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,5 +1460,24 @@ ruleTester.run(RULE_NAME, rule, {
14601460
})
14611461
`,
14621462
},
1463+
{
1464+
code: `
1465+
import userEvent from '${USER_EVENT_ASYNC_FRAMEWORKS[0]}'
1466+
test('setup method called is valid', () => {
1467+
const foo = [];
1468+
foo.forEach(() => {
1469+
userEvent.click();
1470+
});
1471+
})
1472+
`,
1473+
errors: [
1474+
{
1475+
line: 6,
1476+
column: 6,
1477+
messageId: 'awaitAsyncEvent',
1478+
data: { name: 'click' },
1479+
},
1480+
],
1481+
},
14631482
],
14641483
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /