/
await-thenable.ts
45 lines (40 loc) · 1.19 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
44
45
import * as tsutils from 'tsutils';
import * as ts from 'typescript';
import * as util from '../util';
export default util.createRule({
name: 'await-thenable',
meta: {
docs: {
description: 'Disallows awaiting a value that is not a Thenable',
category: 'Best Practices',
recommended: 'error',
requiresTypeChecking: true,
},
messages: {
await: 'Unexpected `await` of a non-Promise (non-"Thenable") value.',
},
schema: [],
type: 'problem',
},
defaultOptions: [],
create(context) {
const parserServices = util.getParserServices(context);
const checker = parserServices.program.getTypeChecker();
return {
AwaitExpression(node): void {
const originalNode = parserServices.esTreeNodeToTSNodeMap.get(node);
const type = checker.getTypeAtLocation(originalNode.expression);
if (
!tsutils.isTypeFlagSet(type, ts.TypeFlags.Any) &&
!tsutils.isTypeFlagSet(type, ts.TypeFlags.Unknown) &&
!tsutils.isThenableType(checker, originalNode.expression, type)
) {
context.report({
messageId: 'await',
node,
});
}
},
};
},
});