Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Commit

Permalink
Recognize promise then call with rejection handler as valid floating …
Browse files Browse the repository at this point in the history
…promise handling (#3048)
  • Loading branch information
vilicvane authored and ajafff committed Jul 25, 2017
1 parent 9afcf17 commit 3369ff8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/rules/noFloatingPromisesRule.ts
Expand Up @@ -59,7 +59,9 @@ function walk(ctx: Lint.WalkContext<string[]>, tc: ts.TypeChecker) {
return ts.forEachChild(ctx.sourceFile, function cb(node): void {
if (isExpressionStatement(node)) {
const { expression } = node;
if (isCallExpression(expression) && !isPromiseCatchCall(expression)) {
if (isCallExpression(expression) &&
!isPromiseCatchCall(expression) &&
!isPromiseThenCallWithRejectionHandler(expression)) {
const { symbol } = tc.getTypeAtLocation(expression);
if (symbol !== undefined && ctx.options.indexOf(symbol.name) !== -1) {
ctx.addFailureAtNode(expression, Rule.FAILURE_STRING);
Expand All @@ -73,3 +75,9 @@ function walk(ctx: Lint.WalkContext<string[]>, tc: ts.TypeChecker) {
function isPromiseCatchCall(expression: ts.CallExpression): boolean {
return isPropertyAccessExpression(expression.expression) && expression.expression.name.text === "catch";
}

function isPromiseThenCallWithRejectionHandler(expression: ts.CallExpression): boolean {
return isPropertyAccessExpression(expression.expression) &&
expression.expression.name.text === "then" &&
expression.arguments.length >= 2;
}
4 changes: 3 additions & 1 deletion test/rules/no-floating-promises/promises/test.ts.lint
Expand Up @@ -156,4 +156,6 @@ switch(foo) {

returnsPromiseFunction().catch(e => { console.error(e.stack); });

[0]: Promises must be handled appropriately
returnsPromiseFunction().then(() => {}, e => { console.error(e.stack); });

[0]: Promises must be handled appropriately

0 comments on commit 3369ff8

Please sign in to comment.