/
no-floating-promises.ts
127 lines (115 loc) · 3.28 KB
/
no-floating-promises.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
import * as tsutils from 'tsutils';
import * as ts from 'typescript';
import * as util from '../util';
export default util.createRule({
name: 'no-floating-promises',
meta: {
docs: {
description:
'Requires promises to be awaited or have a rejection handler.',
category: 'Best Practices',
recommended: false,
tslintName: 'no-floating-promises',
},
messages: {
floating: 'Promises must be handled appropriately',
},
schema: [],
type: 'problem',
},
defaultOptions: [],
create(context) {
const parserServices = util.getParserServices(context);
const checker = parserServices.program.getTypeChecker();
return {
ExpressionStatement(node) {
const { expression } = parserServices.esTreeNodeToTSNodeMap.get(
node,
) as ts.ExpressionStatement;
const type = checker.getTypeAtLocation(expression);
if (
isPromiseLike(checker, expression, type) &&
(!tsutils.isCallExpression(expression) ||
(!isPromiseCatchCallWithHandler(expression) &&
!isPromiseThenCallWithRejectionHandler(expression)))
) {
context.report({
messageId: 'floating',
node,
});
}
},
};
},
});
// Modified from tsutils.isThenable() to only consider thenables which can be
// rejected/caught via a second parameter. Original source (MIT licensed):
//
// https://github.com/ajafff/tsutils/blob/49d0d31050b44b81e918eae4fbaf1dfe7b7286af/util/type.ts#L95-L125
function isPromiseLike(
checker: ts.TypeChecker,
node: ts.Node,
type: ts.Type,
): boolean {
for (const ty of tsutils.unionTypeParts(checker.getApparentType(type))) {
const then = ty.getProperty('then');
if (then === undefined) {
continue;
}
const thenType = checker.getTypeOfSymbolAtLocation(then, node);
if (
hasMatchingSignature(
thenType,
signature =>
signature.parameters.length >= 2 &&
isFunctionParam(checker, signature.parameters[0], node) &&
isFunctionParam(checker, signature.parameters[1], node),
)
) {
return true;
}
}
return false;
}
function hasMatchingSignature(
type: ts.Type,
matcher: (signature: ts.Signature) => boolean,
): boolean {
for (const t of tsutils.unionTypeParts(type)) {
if (t.getCallSignatures().some(matcher)) {
return true;
}
}
return false;
}
function isFunctionParam(
checker: ts.TypeChecker,
param: ts.Symbol,
node: ts.Node,
): boolean {
let type: ts.Type | undefined = checker.getApparentType(
checker.getTypeOfSymbolAtLocation(param, node),
);
for (const t of tsutils.unionTypeParts(type)) {
if (t.getCallSignatures().length !== 0) {
return true;
}
}
return false;
}
function isPromiseCatchCallWithHandler(expression: ts.CallExpression): boolean {
return (
tsutils.isPropertyAccessExpression(expression.expression) &&
expression.expression.name.text === 'catch' &&
expression.arguments.length >= 1
);
}
function isPromiseThenCallWithRejectionHandler(
expression: ts.CallExpression,
): boolean {
return (
tsutils.isPropertyAccessExpression(expression.expression) &&
expression.expression.name.text === 'then' &&
expression.arguments.length >= 2
);
}