Skip to content

Commit ed09979

Browse files
authored
fix(no-await-sync-query): avoid reporting queries if not within callee (#278)
Fixes #276
1 parent ca2910b commit ed09979

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

lib/node-utils.ts

+13
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,19 @@ export function findClosestCallNode(
147147
}
148148
}
149149

150+
export function isCallExpressionCallee(
151+
node: TSESTree.CallExpression,
152+
identifier: TSESTree.Identifier
153+
): boolean {
154+
const nodeInnerIdentifier = getIdentifierNode(node);
155+
156+
if (nodeInnerIdentifier) {
157+
return nodeInnerIdentifier.name === identifier.name;
158+
}
159+
160+
return false;
161+
}
162+
150163
export function isObjectExpression(
151164
node: TSESTree.Expression
152165
): node is TSESTree.ObjectExpression {

lib/rules/no-await-sync-query.ts

+13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { TSESTree } from '@typescript-eslint/experimental-utils';
22
import { createTestingLibraryRule } from '../create-testing-library-rule';
3+
import {
4+
findClosestCallExpressionNode,
5+
isCallExpressionCallee,
6+
} from '../node-utils';
37

48
export const RULE_NAME = 'no-await-sync-query';
59
export type MessageIds = 'noAwaitSyncQuery';
@@ -26,6 +30,15 @@ export default createTestingLibraryRule<Options, MessageIds>({
2630
create(context, _, helpers) {
2731
return {
2832
'AwaitExpression > CallExpression Identifier'(node: TSESTree.Identifier) {
33+
const closestCallExpression = findClosestCallExpressionNode(node, true);
34+
if (!closestCallExpression) {
35+
return;
36+
}
37+
38+
if (!isCallExpressionCallee(closestCallExpression, node)) {
39+
return;
40+
}
41+
2942
if (helpers.isSyncQuery(node)) {
3043
context.report({
3144
node,

tests/lib/rules/no-await-sync-query.test.ts

+20
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,26 @@ ruleTester.run(RULE_NAME, rule, {
7676
}
7777
`,
7878
},
79+
80+
// https://github.com/testing-library/eslint-plugin-testing-library/issues/276
81+
`
82+
// sync query within call expression but not part of the callee
83+
const chooseElementFromSomewhere = async (text, getAllByLabelText) => {
84+
const someElement = getAllByLabelText(text)[0].parentElement;
85+
// ...
86+
await someOtherAsyncFunction();
87+
};
88+
89+
await chooseElementFromSomewhere('someTextToUseInAQuery', getAllByLabelText);
90+
`,
91+
92+
`// edge case for coverage:
93+
// valid use case without call expression
94+
// so there is no innermost function scope found
95+
await test('edge case for no innermost function scope', () => {
96+
const foo = getAllByLabelText
97+
})
98+
`,
7999
],
80100

81101
invalid: [

0 commit comments

Comments
 (0)