Skip to content

Commit

Permalink
fix(no-await-sync-query): avoid reporting queries if not within callee (
Browse files Browse the repository at this point in the history
#278)

Fixes #276
  • Loading branch information
Belco90 committed Jan 30, 2021
1 parent ca2910b commit ed09979
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/node-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,19 @@ export function findClosestCallNode(
}
}

export function isCallExpressionCallee(
node: TSESTree.CallExpression,
identifier: TSESTree.Identifier
): boolean {
const nodeInnerIdentifier = getIdentifierNode(node);

if (nodeInnerIdentifier) {
return nodeInnerIdentifier.name === identifier.name;
}

return false;
}

export function isObjectExpression(
node: TSESTree.Expression
): node is TSESTree.ObjectExpression {
Expand Down
13 changes: 13 additions & 0 deletions lib/rules/no-await-sync-query.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { TSESTree } from '@typescript-eslint/experimental-utils';
import { createTestingLibraryRule } from '../create-testing-library-rule';
import {
findClosestCallExpressionNode,
isCallExpressionCallee,
} from '../node-utils';

export const RULE_NAME = 'no-await-sync-query';
export type MessageIds = 'noAwaitSyncQuery';
Expand All @@ -26,6 +30,15 @@ export default createTestingLibraryRule<Options, MessageIds>({
create(context, _, helpers) {
return {
'AwaitExpression > CallExpression Identifier'(node: TSESTree.Identifier) {
const closestCallExpression = findClosestCallExpressionNode(node, true);
if (!closestCallExpression) {
return;
}

if (!isCallExpressionCallee(closestCallExpression, node)) {
return;
}

if (helpers.isSyncQuery(node)) {
context.report({
node,
Expand Down
20 changes: 20 additions & 0 deletions tests/lib/rules/no-await-sync-query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,26 @@ ruleTester.run(RULE_NAME, rule, {
}
`,
},

// https://github.com/testing-library/eslint-plugin-testing-library/issues/276
`
// sync query within call expression but not part of the callee
const chooseElementFromSomewhere = async (text, getAllByLabelText) => {
const someElement = getAllByLabelText(text)[0].parentElement;
// ...
await someOtherAsyncFunction();
};
await chooseElementFromSomewhere('someTextToUseInAQuery', getAllByLabelText);
`,

`// edge case for coverage:
// valid use case without call expression
// so there is no innermost function scope found
await test('edge case for no innermost function scope', () => {
const foo = getAllByLabelText
})
`,
],

invalid: [
Expand Down

0 comments on commit ed09979

Please sign in to comment.