-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
promise-function-async.ts
233 lines (217 loc) · 6.42 KB
/
promise-function-async.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import type { TSESTree } from '@typescript-eslint/utils';
import { AST_NODE_TYPES, AST_TOKEN_TYPES } from '@typescript-eslint/utils';
import * as ts from 'typescript';
import * as util from '../util';
type Options = [
{
allowAny?: boolean;
allowedPromiseNames?: string[];
checkArrowFunctions?: boolean;
checkFunctionDeclarations?: boolean;
checkFunctionExpressions?: boolean;
checkMethodDeclarations?: boolean;
},
];
type MessageIds = 'missingAsync';
export default util.createRule<Options, MessageIds>({
name: 'promise-function-async',
meta: {
type: 'suggestion',
fixable: 'code',
docs: {
description:
'Require any function or method that returns a Promise to be marked async',
recommended: false,
requiresTypeChecking: true,
},
messages: {
missingAsync: 'Functions that return promises must be async.',
},
schema: [
{
type: 'object',
properties: {
allowAny: {
description:
'Whether to consider `any` and `unknown` to be Promises.',
type: 'boolean',
},
allowedPromiseNames: {
description:
'Any extra names of classes or interfaces to be considered Promises.',
type: 'array',
items: {
type: 'string',
},
},
checkArrowFunctions: {
type: 'boolean',
},
checkFunctionDeclarations: {
type: 'boolean',
},
checkFunctionExpressions: {
type: 'boolean',
},
checkMethodDeclarations: {
type: 'boolean',
},
},
additionalProperties: false,
},
],
},
defaultOptions: [
{
allowAny: true,
allowedPromiseNames: [],
checkArrowFunctions: true,
checkFunctionDeclarations: true,
checkFunctionExpressions: true,
checkMethodDeclarations: true,
},
],
create(
context,
[
{
allowAny,
allowedPromiseNames,
checkArrowFunctions,
checkFunctionDeclarations,
checkFunctionExpressions,
checkMethodDeclarations,
},
],
) {
const allAllowedPromiseNames = new Set([
'Promise',
...allowedPromiseNames!,
]);
const parserServices = util.getParserServices(context);
const checker = parserServices.program.getTypeChecker();
const sourceCode = context.getSourceCode();
function validateNode(
node:
| TSESTree.ArrowFunctionExpression
| TSESTree.FunctionDeclaration
| TSESTree.FunctionExpression,
): void {
const originalNode = parserServices.esTreeNodeToTSNodeMap.get(node);
const signatures = checker
.getTypeAtLocation(originalNode)
.getCallSignatures();
if (!signatures.length) {
return;
}
const returnType = checker.getReturnTypeOfSignature(signatures[0]);
if (
!util.containsAllTypesByName(
returnType,
allowAny!,
allAllowedPromiseNames,
)
) {
// Return type is not a promise
return;
}
if (node.parent.type === AST_NODE_TYPES.TSAbstractMethodDefinition) {
// Abstract method can't be async
return;
}
if (
(node.parent.type === AST_NODE_TYPES.Property ||
node.parent.type === AST_NODE_TYPES.MethodDefinition) &&
(node.parent.kind === 'get' || node.parent.kind === 'set')
) {
// Getters and setters can't be async
return;
}
if (
util.isTypeFlagSet(returnType, ts.TypeFlags.Any | ts.TypeFlags.Unknown)
) {
// Report without auto fixer because the return type is unknown
return context.report({
messageId: 'missingAsync',
node,
loc: util.getFunctionHeadLoc(node, sourceCode),
});
}
context.report({
messageId: 'missingAsync',
node,
loc: util.getFunctionHeadLoc(node, sourceCode),
fix: fixer => {
if (
node.parent.type === AST_NODE_TYPES.MethodDefinition ||
(node.parent.type === AST_NODE_TYPES.Property && node.parent.method)
) {
// this function is a class method or object function property shorthand
const method = node.parent;
// the token to put `async` before
let keyToken = sourceCode.getFirstToken(method)!;
// if there are decorators then skip past them
if (
method.type === AST_NODE_TYPES.MethodDefinition &&
method.decorators
) {
const lastDecorator =
method.decorators[method.decorators.length - 1];
keyToken = sourceCode.getTokenAfter(lastDecorator)!;
}
// if current token is a keyword like `static` or `public` then skip it
while (
keyToken.type === AST_TOKEN_TYPES.Keyword &&
keyToken.range[0] < method.key.range[0]
) {
keyToken = sourceCode.getTokenAfter(keyToken)!;
}
// check if there is a space between key and previous token
const insertSpace = !sourceCode.isSpaceBetween!(
sourceCode.getTokenBefore(keyToken)!,
keyToken,
);
let code = 'async ';
if (insertSpace) {
code = ` ${code}`;
}
return fixer.insertTextBefore(keyToken, code);
}
return fixer.insertTextBefore(node, 'async ');
},
});
}
return {
...(checkArrowFunctions && {
'ArrowFunctionExpression[async = false]'(
node: TSESTree.ArrowFunctionExpression,
): void {
validateNode(node);
},
}),
...(checkFunctionDeclarations && {
'FunctionDeclaration[async = false]'(
node: TSESTree.FunctionDeclaration,
): void {
validateNode(node);
},
}),
'FunctionExpression[async = false]'(
node: TSESTree.FunctionExpression,
): void {
if (
node.parent.type === AST_NODE_TYPES.MethodDefinition &&
node.parent.kind === 'method'
) {
if (checkMethodDeclarations) {
validateNode(node);
}
return;
}
if (checkFunctionExpressions) {
validateNode(node);
}
},
};
},
});