-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathawait-async-query.ts
143 lines (130 loc) · 4.06 KB
/
await-async-query.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import {
ESLintUtils,
TSESTree,
ASTUtils,
} from '@typescript-eslint/experimental-utils';
import { getDocsUrl, LIBRARY_MODULES } from '../utils';
import {
isCallExpression,
isMemberExpression,
isAwaited,
isPromiseResolved,
getVariableReferences,
} from '../node-utils';
import { ReportDescriptor } from '@typescript-eslint/experimental-utils/dist/ts-eslint';
export const RULE_NAME = 'await-async-query';
export type MessageIds = 'awaitAsyncQuery';
type Options = [];
const ASYNC_QUERIES_REGEXP = /^find(All)?By(LabelText|PlaceholderText|Text|AltText|Title|DisplayValue|Role|TestId)$/;
function hasClosestExpectResolvesRejects(node: TSESTree.Node): boolean {
if (!node.parent) {
return false;
}
if (
isCallExpression(node) &&
ASTUtils.isIdentifier(node.callee) &&
isMemberExpression(node.parent) &&
node.callee.name === 'expect'
) {
const expectMatcher = node.parent.property;
return (
ASTUtils.isIdentifier(expectMatcher) &&
(expectMatcher.name === 'resolves' || expectMatcher.name === 'rejects')
);
} else {
return hasClosestExpectResolvesRejects(node.parent);
}
}
export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
name: RULE_NAME,
meta: {
type: 'problem',
docs: {
description: 'Enforce async queries to have proper `await`',
category: 'Best Practices',
recommended: 'warn',
},
messages: {
awaitAsyncQuery: '`{{ name }}` must have `await` operator',
},
fixable: null,
schema: [],
},
defaultOptions: [],
create(context) {
const testingLibraryQueryUsage: {
node: TSESTree.Identifier | TSESTree.MemberExpression;
queryName: string;
}[] = [];
const isQueryUsage = (
node: TSESTree.Identifier | TSESTree.MemberExpression
) =>
!isAwaited(node.parent.parent) &&
!isPromiseResolved(node) &&
!hasClosestExpectResolvesRejects(node);
let hasImportedFromTestingLibraryModule = false;
function report(params: ReportDescriptor<'awaitAsyncQuery'>) {
if (hasImportedFromTestingLibraryModule) {
context.report(params);
}
}
return {
'ImportDeclaration > ImportSpecifier,ImportNamespaceSpecifier'(
node: TSESTree.Node
) {
const importDeclaration = node.parent as TSESTree.ImportDeclaration;
const module = importDeclaration.source.value.toString();
if (LIBRARY_MODULES.includes(module)) {
hasImportedFromTestingLibraryModule = true;
}
},
[`CallExpression > Identifier[name=${ASYNC_QUERIES_REGEXP}]`](
node: TSESTree.Identifier
) {
if (isQueryUsage(node)) {
testingLibraryQueryUsage.push({ node, queryName: node.name });
}
},
[`MemberExpression > Identifier[name=${ASYNC_QUERIES_REGEXP}]`](
node: TSESTree.Identifier
) {
// Perform checks in parent MemberExpression instead of current identifier
const parent = node.parent as TSESTree.MemberExpression;
if (isQueryUsage(parent)) {
testingLibraryQueryUsage.push({ node: parent, queryName: node.name });
}
},
'Program:exit'() {
testingLibraryQueryUsage.forEach(({ node, queryName }) => {
const references = getVariableReferences(context, node.parent.parent);
if (references && references.length === 0) {
report({
node,
messageId: 'awaitAsyncQuery',
data: {
name: queryName,
},
});
} else {
for (const reference of references) {
const referenceNode = reference.identifier;
if (
!isAwaited(referenceNode.parent) &&
!isPromiseResolved(referenceNode)
) {
report({
node,
messageId: 'awaitAsyncQuery',
data: {
name: queryName,
},
});
break;
}
}
}
});
},
};
},
});