Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 56af989

Browse files
authored
refactor(eslint-plugin): [no-floating-promises] update code to use AST instead of ts nodes (typescript-eslint#3195)
1 parent 6703df1 commit 56af989

File tree

1 file changed

+34
-31
lines changed

1 file changed

+34
-31
lines changed

packages/eslint-plugin/src/rules/no-floating-promises.ts

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,11 @@ export default util.createRule<Options, MessageId>({
6060

6161
return {
6262
ExpressionStatement(node): void {
63-
const { expression } = parserServices.esTreeNodeToTSNodeMap.get(node);
64-
6563
if (options.ignoreIIFE && isAsyncIife(node)) {
6664
return;
6765
}
6866

69-
if (isUnhandledPromise(checker, expression)) {
67+
if (isUnhandledPromise(checker, node.expression)) {
7068
if (options.ignoreVoid) {
7169
context.report({
7270
node,
@@ -107,52 +105,52 @@ export default util.createRule<Options, MessageId>({
107105

108106
function isUnhandledPromise(
109107
checker: ts.TypeChecker,
110-
node: ts.Node,
108+
node: TSESTree.Node,
111109
): boolean {
112110
// First, check expressions whose resulting types may not be promise-like
113-
if (
114-
ts.isBinaryExpression(node) &&
115-
node.operatorToken.kind === ts.SyntaxKind.CommaToken
116-
) {
111+
if (node.type === AST_NODE_TYPES.SequenceExpression) {
117112
// Any child in a comma expression could return a potentially unhandled
118113
// promise, so we check them all regardless of whether the final returned
119114
// value is promise-like.
120-
return (
121-
isUnhandledPromise(checker, node.left) ||
122-
isUnhandledPromise(checker, node.right)
123-
);
115+
return node.expressions.some(item => isUnhandledPromise(checker, item));
124116
}
125117

126-
if (ts.isVoidExpression(node) && !options.ignoreVoid) {
118+
if (
119+
!options.ignoreVoid &&
120+
node.type === AST_NODE_TYPES.UnaryExpression &&
121+
node.operator === 'void'
122+
) {
127123
// Similarly, a `void` expression always returns undefined, so we need to
128124
// see what's inside it without checking the type of the overall expression.
129-
return isUnhandledPromise(checker, node.expression);
125+
return isUnhandledPromise(checker, node.argument);
130126
}
131127

132128
// Check the type. At this point it can't be unhandled if it isn't a promise
133-
if (!isPromiseLike(checker, node)) {
129+
if (
130+
!isPromiseLike(checker, parserServices.esTreeNodeToTSNodeMap.get(node))
131+
) {
134132
return false;
135133
}
136134

137-
if (ts.isCallExpression(node)) {
135+
if (node.type === AST_NODE_TYPES.CallExpression) {
138136
// If the outer expression is a call, it must be either a `.then()` or
139137
// `.catch()` that handles the promise.
140138
return (
141139
!isPromiseCatchCallWithHandler(node) &&
142140
!isPromiseThenCallWithRejectionHandler(node) &&
143141
!isPromiseFinallyCallWithHandler(node)
144142
);
145-
} else if (ts.isConditionalExpression(node)) {
143+
} else if (node.type === AST_NODE_TYPES.ConditionalExpression) {
146144
// We must be getting the promise-like value from one of the branches of the
147145
// ternary. Check them directly.
148146
return (
149-
isUnhandledPromise(checker, node.whenFalse) ||
150-
isUnhandledPromise(checker, node.whenTrue)
147+
isUnhandledPromise(checker, node.alternate) ||
148+
isUnhandledPromise(checker, node.consequent)
151149
);
152150
} else if (
153-
ts.isPropertyAccessExpression(node) ||
154-
ts.isIdentifier(node) ||
155-
ts.isNewExpression(node)
151+
node.type === AST_NODE_TYPES.MemberExpression ||
152+
node.type === AST_NODE_TYPES.Identifier ||
153+
node.type === AST_NODE_TYPES.NewExpression
156154
) {
157155
// If it is just a property access chain or a `new` call (e.g. `foo.bar` or
158156
// `new Promise()`), the promise is not handled because it doesn't have the
@@ -225,30 +223,35 @@ function isFunctionParam(
225223
return false;
226224
}
227225

228-
function isPromiseCatchCallWithHandler(expression: ts.CallExpression): boolean {
226+
function isPromiseCatchCallWithHandler(
227+
expression: TSESTree.CallExpression,
228+
): boolean {
229229
return (
230-
ts.isPropertyAccessExpression(expression.expression) &&
231-
expression.expression.name.text === 'catch' &&
230+
expression.callee.type === AST_NODE_TYPES.MemberExpression &&
231+
expression.callee.property.type === AST_NODE_TYPES.Identifier &&
232+
expression.callee.property.name === 'catch' &&
232233
expression.arguments.length >= 1
233234
);
234235
}
235236

236237
function isPromiseThenCallWithRejectionHandler(
237-
expression: ts.CallExpression,
238+
expression: TSESTree.CallExpression,
238239
): boolean {
239240
return (
240-
ts.isPropertyAccessExpression(expression.expression) &&
241-
expression.expression.name.text === 'then' &&
241+
expression.callee.type === AST_NODE_TYPES.MemberExpression &&
242+
expression.callee.property.type === AST_NODE_TYPES.Identifier &&
243+
expression.callee.property.name === 'then' &&
242244
expression.arguments.length >= 2
243245
);
244246
}
245247

246248
function isPromiseFinallyCallWithHandler(
247-
expression: ts.CallExpression,
249+
expression: TSESTree.CallExpression,
248250
): boolean {
249251
return (
250-
ts.isPropertyAccessExpression(expression.expression) &&
251-
expression.expression.name.text === 'finally' &&
252+
expression.callee.type === AST_NODE_TYPES.MemberExpression &&
253+
expression.callee.property.type === AST_NODE_TYPES.Identifier &&
254+
expression.callee.property.name === 'finally' &&
252255
expression.arguments.length >= 1
253256
);
254257
}

0 commit comments

Comments
 (0)