From 6f932ddd093eba38d25c8b99c714462c713ca10f Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Tue, 18 Oct 2016 19:18:15 -0700 Subject: [PATCH] Added options for classes to no-floating-promises Users can now specify rules that are considered to be promises, such as `"JQueryPromise"`. --- src/rules/noFloatingPromisesRule.ts | 30 +++++++++++++++---- .../jquerypromise/test.ts.lint | 17 +++++++++++ .../jquerypromise/tslint.json | 8 +++++ .../{ => promises}/test.ts.lint | 0 .../{ => promises}/tslint.json | 0 5 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 test/rules/no-floating-promises/jquerypromise/test.ts.lint create mode 100644 test/rules/no-floating-promises/jquerypromise/tslint.json rename test/rules/no-floating-promises/{ => promises}/test.ts.lint (100%) rename test/rules/no-floating-promises/{ => promises}/tslint.json (100%) diff --git a/src/rules/noFloatingPromisesRule.ts b/src/rules/noFloatingPromisesRule.ts index eb4cd2c5392..db00b30fa45 100644 --- a/src/rules/noFloatingPromisesRule.ts +++ b/src/rules/noFloatingPromisesRule.ts @@ -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, }; @@ -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); } } @@ -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); @@ -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) { diff --git a/test/rules/no-floating-promises/jquerypromise/test.ts.lint b/test/rules/no-floating-promises/jquerypromise/test.ts.lint new file mode 100644 index 00000000000..4d6096e18c1 --- /dev/null +++ b/test/rules/no-floating-promises/jquerypromise/test.ts.lint @@ -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 diff --git a/test/rules/no-floating-promises/jquerypromise/tslint.json b/test/rules/no-floating-promises/jquerypromise/tslint.json new file mode 100644 index 00000000000..8bf22730c37 --- /dev/null +++ b/test/rules/no-floating-promises/jquerypromise/tslint.json @@ -0,0 +1,8 @@ +{ + "linterOptions": { + "typeCheck": true + }, + "rules": { + "no-floating-promises": [true, "JQueryPromise"] + } +} \ No newline at end of file diff --git a/test/rules/no-floating-promises/test.ts.lint b/test/rules/no-floating-promises/promises/test.ts.lint similarity index 100% rename from test/rules/no-floating-promises/test.ts.lint rename to test/rules/no-floating-promises/promises/test.ts.lint diff --git a/test/rules/no-floating-promises/tslint.json b/test/rules/no-floating-promises/promises/tslint.json similarity index 100% rename from test/rules/no-floating-promises/tslint.json rename to test/rules/no-floating-promises/promises/tslint.json