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

Commit

Permalink
Added options for classes to no-floating-promises
Browse files Browse the repository at this point in the history
Users can now specify rules that are considered to be promises, such as
`"JQueryPromise"`.
  • Loading branch information
Josh Goldberg committed Oct 19, 2016
1 parent e509f3f commit 6f932dd
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 5 deletions.
30 changes: 25 additions & 5 deletions src/rules/noFloatingPromisesRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,17 @@ 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.",
optionsDescription: "Not configurable.",
options: null,
optionExamples: ["true"],
optionsDescription: Lint.Utils.dedent`
A list of \'string\' names of any additional classes that should also be handled as Promises.
`,
options: {
type: "list",
listType: {
type: "array",
items: {type: "string"},
},
},
optionExamples: ["true", `[true, "JQueryPromise"]`],
type: "functionality",
requiresTypeInfo: true,
};
Expand All @@ -35,7 +43,13 @@ 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[] {
return this.applyWithWalker(new NoFloatingPromisesWalker(sourceFile, this.getOptions(), program));
const walker = new NoFloatingPromisesWalker(sourceFile, this.getOptions(), program);

for (const className of this.getOptions().ruleArguments) {
walker.addPromiseClass(className);
}

return this.applyWithWalker(walker);
}
}

Expand All @@ -45,6 +59,12 @@ class NoFloatingPromisesWalker extends Lint.ProgramAwareRuleWalker {
[ts.SyntaxKind.VariableDeclaration]: true,
};

private promiseClasses = ["Promise"];

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

public visitCallExpression(node: ts.CallExpression): void {
this.checkCallExpression(node);
super.visitCallExpression(node);
Expand All @@ -68,7 +88,7 @@ class NoFloatingPromisesWalker extends Lint.ProgramAwareRuleWalker {
return false;
}

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

private kindCanContainPromise(kind: ts.SyntaxKind) {
Expand Down
17 changes: 17 additions & 0 deletions test/rules/no-floating-promises/jquerypromise/test.ts.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Promise { }
class JQueryPromise { }
class NotAPromise { }

const returnsPromise = () => new Promise();
const returnsJQueryPromise = () => new JQueryPromise();
const returnsNotAPromise = () => new NotAPromise();

returnsPromise();
~~~~~~~~~~~~~~~~ [0]

returnsJQueryPromise();
~~~~~~~~~~~~~~~~~~~~~~ [0]

returnsNotAPromise();

[0]: Promises must be handled appropriately
8 changes: 8 additions & 0 deletions test/rules/no-floating-promises/jquerypromise/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"linterOptions": {
"typeCheck": true
},
"rules": {
"no-floating-promises": [true, "JQueryPromise"]
}
}

0 comments on commit 6f932dd

Please sign in to comment.