-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathawait-async-utils.ts
157 lines (140 loc) · 4.35 KB
/
await-async-utils.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import {
ESLintUtils,
TSESTree,
ASTUtils,
} from '@typescript-eslint/experimental-utils';
import { getDocsUrl, ASYNC_UTILS, LIBRARY_MODULES } from '../utils';
import {
isAwaited,
isPromiseResolved,
getVariableReferences,
isMemberExpression,
isImportSpecifier,
isImportNamespaceSpecifier,
isCallExpression,
isArrayExpression,
} from '../node-utils';
export const RULE_NAME = 'await-async-utils';
export type MessageIds = 'awaitAsyncUtil';
type Options = [];
const ASYNC_UTILS_REGEXP = new RegExp(`^(${ASYNC_UTILS.join('|')})$`);
// verifies the CallExpression is Promise.all()
function isPromiseAll(node: TSESTree.CallExpression) {
return (
isMemberExpression(node.callee) &&
ASTUtils.isIdentifier(node.callee.object) &&
node.callee.object.name === 'Promise' &&
ASTUtils.isIdentifier(node.callee.property) &&
node.callee.property.name === 'all'
);
}
// verifies the node is part of an array used in a CallExpression
function isInPromiseAll(node: TSESTree.Node) {
const parent = node.parent;
return (
isCallExpression(parent) &&
isArrayExpression(parent.parent) &&
isCallExpression(parent.parent.parent) &&
isPromiseAll(parent.parent.parent)
);
}
export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
name: RULE_NAME,
meta: {
type: 'problem',
docs: {
description: 'Enforce async utils to be awaited properly',
category: 'Best Practices',
recommended: 'warn',
},
messages: {
awaitAsyncUtil: 'Promise returned from `{{ name }}` must be handled',
},
fixable: null,
schema: [],
},
defaultOptions: [],
create(context) {
const asyncUtilsUsage: Array<{
node: TSESTree.Identifier | TSESTree.MemberExpression;
name: string;
}> = [];
const importedAsyncUtils: string[] = [];
return {
'ImportDeclaration > ImportSpecifier,ImportNamespaceSpecifier'(
node: TSESTree.Node
) {
const parent = node.parent as TSESTree.ImportDeclaration;
if (!LIBRARY_MODULES.includes(parent.source.value.toString())) {
return;
}
if (isImportSpecifier(node)) {
importedAsyncUtils.push(node.imported.name);
}
if (isImportNamespaceSpecifier(node)) {
importedAsyncUtils.push(node.local.name);
}
},
[`CallExpression > Identifier[name=${ASYNC_UTILS_REGEXP}]`](
node: TSESTree.Identifier
) {
asyncUtilsUsage.push({ node, name: node.name });
},
[`CallExpression > MemberExpression > Identifier[name=${ASYNC_UTILS_REGEXP}]`](
node: TSESTree.Identifier
) {
const memberExpression = node.parent as TSESTree.MemberExpression;
const identifier = memberExpression.object as TSESTree.Identifier;
const memberExpressionName = identifier.name;
asyncUtilsUsage.push({
node: memberExpression,
name: memberExpressionName,
});
},
'Program:exit'() {
const testingLibraryUtilUsage = asyncUtilsUsage.filter((usage) => {
if (isMemberExpression(usage.node)) {
const object = usage.node.object as TSESTree.Identifier;
return importedAsyncUtils.includes(object.name);
}
return importedAsyncUtils.includes(usage.name);
});
testingLibraryUtilUsage.forEach(({ node, name }) => {
const references = getVariableReferences(context, node.parent.parent);
if (
references &&
references.length === 0 &&
!isAwaited(node.parent.parent) &&
!isPromiseResolved(node) &&
!isInPromiseAll(node)
) {
context.report({
node,
messageId: 'awaitAsyncUtil',
data: {
name,
},
});
} else {
for (const reference of references) {
const referenceNode = reference.identifier;
if (
!isAwaited(referenceNode.parent) &&
!isPromiseResolved(referenceNode)
) {
context.report({
node,
messageId: 'awaitAsyncUtil',
data: {
name,
},
});
break;
}
}
}
});
},
};
},
});