-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathawait-async-utils.ts
125 lines (110 loc) · 3.56 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
import { ESLintUtils, TSESTree } from '@typescript-eslint/experimental-utils';
import { getDocsUrl, ASYNC_UTILS, LIBRARY_MODULES } from '../utils';
import {
isAwaited,
isPromiseResolved,
getVariableReferences,
} 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('|')})$`);
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 (node.type === 'ImportSpecifier') {
importedAsyncUtils.push(node.imported.name);
}
if (node.type === 'ImportNamespaceSpecifier') {
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 (usage.node.type === 'MemberExpression') {
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)
) {
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;
}
}
}
});
},
};
},
});