-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
await-thenable.ts
43 lines (37 loc) · 1.05 KB
/
await-thenable.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
import * as tools from 'ts-api-utils';
import * as util from '../util';
export default util.createRule({
name: 'await-thenable',
meta: {
docs: {
description: 'Disallow awaiting a value that is not a Thenable',
recommended: 'error',
requiresTypeChecking: true,
},
messages: {
await: 'Unexpected `await` of a non-Promise (non-"Thenable") value.',
},
schema: [],
type: 'problem',
},
defaultOptions: [],
create(context) {
const services = util.getParserServices(context);
const checker = services.program.getTypeChecker();
return {
AwaitExpression(node): void {
const type = services.getTypeAtLocation(node.argument);
if (util.isTypeAnyType(type) || util.isTypeUnknownType(type)) {
return;
}
const originalNode = services.esTreeNodeToTSNodeMap.get(node);
if (!tools.isThenableType(checker, originalNode.expression, type)) {
context.report({
messageId: 'await',
node,
});
}
},
};
},
});