-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
await-thenable.ts
67 lines (60 loc) · 1.74 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import type { TSESLint } from '@typescript-eslint/utils';
import * as tsutils from 'ts-api-utils';
import {
createRule,
getParserServices,
isAwaitKeyword,
isTypeAnyType,
isTypeUnknownType,
nullThrows,
NullThrowsReasons,
} from '../util';
export default createRule({
name: 'await-thenable',
meta: {
docs: {
description: 'Disallow awaiting a value that is not a Thenable',
recommended: 'recommended',
requiresTypeChecking: true,
},
hasSuggestions: true,
messages: {
await: 'Unexpected `await` of a non-Promise (non-"Thenable") value.',
removeAwait: 'Remove unnecessary `await`.',
},
schema: [],
type: 'problem',
},
defaultOptions: [],
create(context) {
const services = getParserServices(context);
const checker = services.program.getTypeChecker();
return {
AwaitExpression(node): void {
const type = services.getTypeAtLocation(node.argument);
if (isTypeAnyType(type) || isTypeUnknownType(type)) {
return;
}
const originalNode = services.esTreeNodeToTSNodeMap.get(node);
if (!tsutils.isThenableType(checker, originalNode.expression, type)) {
context.report({
messageId: 'await',
node,
suggest: [
{
messageId: 'removeAwait',
fix(fixer): TSESLint.RuleFix {
const awaitKeyword = nullThrows(
context.sourceCode.getFirstToken(node, isAwaitKeyword),
NullThrowsReasons.MissingToken('await', 'await expression'),
);
return fixer.remove(awaitKeyword);
},
},
],
});
}
},
};
},
});