Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(no-await-sync-query): avoid reporting queries if not within callee #278

Merged
merged 1 commit into from
Jan 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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