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

no-floating-promises: rewrite as walker function #2631

Merged
merged 1 commit into from
Apr 29, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 16 additions & 53 deletions src/rules/noFloatingPromisesRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* limitations under the License.
*/

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

import * as Lint from "../index";
Expand All @@ -24,6 +25,7 @@ export class Rule extends Lint.Rules.TypedRule {
public static metadata: Lint.IRuleMetadata = {
ruleName: "no-floating-promises",
description: "Promises returned by functions must be handled appropriately.",
descriptionDetails: "Use `no-unused-expressions` in addition to this rule to reveal even more floating promises.",
optionsDescription: Lint.Utils.dedent`
A list of \'string\' names of any additional classes that should also be handled as Promises.
`,
Expand All @@ -45,61 +47,22 @@ export class Rule extends Lint.Rules.TypedRule {
public static FAILURE_STRING = "Promises must be handled appropriately";

public applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): Lint.RuleFailure[] {
const walker = new NoFloatingPromisesWalker(sourceFile, this.getOptions(), program);

for (const className of this.ruleArguments as string[]) {
walker.addPromiseClass(className);
}

return this.applyWithWalker(walker);
return this.applyWithFunction(
sourceFile,
(ctx: Lint.WalkContext<string[]>) => walk(ctx, program.getTypeChecker()),
["Promise", ...this.ruleArguments as string[]],
);
}
}

class NoFloatingPromisesWalker extends Lint.ProgramAwareRuleWalker {
private static barredParentKinds: { [x: number]: boolean } = {
[ts.SyntaxKind.Block]: true,
[ts.SyntaxKind.ExpressionStatement]: true,
[ts.SyntaxKind.SourceFile]: true,
};

private promiseClasses = ["Promise"];

public addPromiseClass(className: string) {
this.promiseClasses.push(className);
}

public visitCallExpression(node: ts.CallExpression): void {
this.checkNode(node);
super.visitCallExpression(node);
}

public visitExpressionStatement(node: ts.ExpressionStatement): void {
this.checkNode(node);
super.visitExpressionStatement(node);
}

private checkNode(node: ts.CallExpression | ts.ExpressionStatement) {
if (node.parent && this.kindCanContainPromise(node.parent.kind)) {
return;
}

const typeChecker = this.getTypeChecker();
const type = typeChecker.getTypeAtLocation(node);

if (this.symbolIsPromise(type.symbol)) {
this.addFailureAtNode(node, Rule.FAILURE_STRING);
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);
}
}
}

private symbolIsPromise(symbol?: ts.Symbol) {
if (!symbol) {
return false;
}

return this.promiseClasses.indexOf(symbol.name) !== -1;
}

private kindCanContainPromise(kind: ts.SyntaxKind) {
return !NoFloatingPromisesWalker.barredParentKinds[kind];
}
return ts.forEachChild(node, cb);
});
}
16 changes: 16 additions & 0 deletions test/rules/no-floating-promises/promises/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,20 @@ let promise = Math.random() > .5
? returnsPromiseFunction()
: returnsPromiseFunction().then();

declare var foo: boolean;
if (foo)
returnsPromiseFunction();
~~~~~~~~~~~~~~~~~~~~~~~~ [0]
switch(foo) {
case true:
returnsPromiseFunction();
~~~~~~~~~~~~~~~~~~~~~~~~ [0]
break;
case false: {
returnsPromiseFunction();
~~~~~~~~~~~~~~~~~~~~~~~~ [0]
break;
}
}

[0]: Promises must be handled appropriately