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

no-floating-promises: Allow 'promise.catch()' #2774

Merged
merged 2 commits into from
May 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/rules/noFloatingPromisesRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

import { isExpressionStatement } from "tsutils";
import { isCallExpression, isExpressionStatement, isPropertyAccessExpression } from "tsutils";
import * as ts from "typescript";

import * as Lint from "../index";
Expand Down Expand Up @@ -57,12 +57,19 @@ export class Rule extends Lint.Rules.TypedRule {

function walk(ctx: Lint.WalkContext<string[]>, tc: ts.TypeChecker) {
return ts.forEachChild(ctx.sourceFile, function cb(node): void {
if (isExpressionStatement(node) && node.expression.kind === ts.SyntaxKind.CallExpression) {
const {symbol} = tc.getTypeAtLocation(node.expression);
if (symbol !== undefined && ctx.options.indexOf(symbol.name) !== -1) {
ctx.addFailureAtNode(node.expression, Rule.FAILURE_STRING);
if (isExpressionStatement(node)) {
const { expression } = node;
if (isCallExpression(expression) && !isPromiseCatchCall(expression)) {
const { symbol } = tc.getTypeAtLocation(expression);
if (symbol !== undefined && ctx.options.indexOf(symbol.name) !== -1) {
ctx.addFailureAtNode(expression, Rule.FAILURE_STRING);
}
}
}
return ts.forEachChild(node, cb);
});
}

function isPromiseCatchCall(expression: ts.CallExpression): boolean {
return isPropertyAccessExpression(expression.expression) && expression.expression.name.text === "catch";
}
2 changes: 2 additions & 0 deletions test/rules/no-floating-promises/promises/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,6 @@ switch(foo) {
}
}

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

[0]: Promises must be handled appropriately