This repository has been archived by the owner on Mar 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 887
New rule: no-floating-promises #1632
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/** | ||
* @license | ||
* Copyright 2016 Palantir Technologies, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import * as ts from "typescript"; | ||
|
||
import * as Lint from "../index"; | ||
|
||
export class Rule extends Lint.Rules.TypedRule { | ||
/* tslint:disable:object-literal-sort-keys */ | ||
public static metadata: Lint.IRuleMetadata = { | ||
ruleName: "no-floating-promises", | ||
description: "Promises returned by functions must be handled appropriately.", | ||
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"]`], | ||
rationale: "Unhandled Promises can cause unexpected behavior, such as resolving at unexpected times.", | ||
type: "functionality", | ||
typescriptOnly: false, | ||
}; | ||
/* tslint:enable:object-literal-sort-keys */ | ||
|
||
public static FAILURE_STRING = "Promises must be handled appropriately"; | ||
|
||
public applyWithProgram(sourceFile: ts.SourceFile, langSvc: ts.LanguageService): Lint.RuleFailure[] { | ||
const walker = new NoFloatingPromisesWalker(sourceFile, this.getOptions(), langSvc.getProgram()); | ||
|
||
for (const className of this.getOptions().ruleArguments) { | ||
walker.addPromiseClass(className); | ||
} | ||
|
||
return this.applyWithWalker(walker); | ||
} | ||
} | ||
|
||
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.addFailure(this.createFailure(node.getStart(), node.getWidth(), 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]; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
test/rules/no-floating-promises/jquerypromise/test.ts.lint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"linterOptions": { | ||
"typeCheck": true | ||
}, | ||
"rules": { | ||
"no-floating-promises": [true, "JQueryPromise"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
class Promise { | ||
then(): Promise; | ||
} | ||
|
||
function returnsPromiseFunction() { | ||
return new Promise(); | ||
} | ||
|
||
const returnsPromiseVariable = () => new Promise(); | ||
|
||
class ReturnsPromiseClass { | ||
returnsPromiseMemberFunction() { | ||
return new Promise(); | ||
} | ||
|
||
returnsPromiseMemberVariable = () => new Promise(); | ||
|
||
static returnsPromiseStaticFunction = () => new Promise(); | ||
} | ||
|
||
let a = returnsPromiseFunction(); | ||
let b = returnsPromiseVariable(); | ||
let c = new ReturnsPromiseClass().returnsPromiseMemberFunction(); | ||
let d = new ReturnsPromiseClass().returnsPromiseMemberVariable(); | ||
let e = ReturnsPromiseClass.returnsPromiseStaticFunction(); | ||
|
||
returnsPromiseFunction(); | ||
~~~~~~~~~~~~~~~~~~~~~~~~ [0] | ||
|
||
returnsPromiseFunction().then(); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0] | ||
|
||
returnsPromiseVariable(); | ||
~~~~~~~~~~~~~~~~~~~~~~~~ [0] | ||
|
||
returnsPromiseVariable().then(); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0] | ||
|
||
new ReturnsPromiseClass().returnsPromiseMemberFunction(); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0] | ||
|
||
new ReturnsPromiseClass().returnsPromiseMemberFunction().then(); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0] | ||
|
||
new ReturnsPromiseClass().returnsPromiseMemberVariable(); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0] | ||
|
||
new ReturnsPromiseClass().returnsPromiseMemberVariable().then(); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0] | ||
|
||
ReturnsPromiseClass.returnsPromiseStaticFunction(); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0] | ||
|
||
ReturnsPromiseClass.returnsPromiseStaticFunction().then(); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0] | ||
|
||
(function () { | ||
let a = returnsPromiseFunction(); | ||
|
||
returnsPromiseFunction(); | ||
~~~~~~~~~~~~~~~~~~~~~~~~ [0] | ||
|
||
returnsPromiseFunction().then(); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0] | ||
|
||
new ReturnsPromiseClass().returnsPromiseMemberFunction(); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0] | ||
|
||
new ReturnsPromiseClass().returnsPromiseMemberFunction().then(); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0] | ||
|
||
new ReturnsPromiseClass().returnsPromiseMemberVariable(); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0] | ||
|
||
new ReturnsPromiseClass().returnsPromiseMemberVariable().then(); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0] | ||
|
||
ReturnsPromiseClass.returnsPromiseStaticFunction(); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0] | ||
|
||
ReturnsPromiseClass.returnsPromiseStaticFunction().then(); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0] | ||
})(); | ||
|
||
(() => { | ||
let a = returnsPromiseFunction(); | ||
|
||
returnsPromiseFunction(); | ||
~~~~~~~~~~~~~~~~~~~~~~~~ [0] | ||
|
||
returnsPromiseFunction().then(); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0] | ||
|
||
new ReturnsPromiseClass().returnsPromiseMemberFunction(); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0] | ||
|
||
new ReturnsPromiseClass().returnsPromiseMemberFunction().then(); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0] | ||
|
||
new ReturnsPromiseClass().returnsPromiseMemberVariable(); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0] | ||
|
||
new ReturnsPromiseClass().returnsPromiseMemberVariable().then(); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0] | ||
|
||
ReturnsPromiseClass.returnsPromiseStaticFunction(); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0] | ||
|
||
ReturnsPromiseClass.returnsPromiseStaticFunction().then(); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0] | ||
})(); | ||
|
||
[].push(returnsPromiseFunction()); | ||
|
||
[].push(returnsPromiseFunction().then()); | ||
|
||
[].push(ReturnsPromiseClass.returnsPromiseStaticFunction()); | ||
|
||
[].push(ReturnsPromiseClass.returnsPromiseStaticFunction().then()); | ||
|
||
while (returnsPromiseFunction()); | ||
|
||
while (returnsPromiseFunction().then()); | ||
|
||
for (let i = 0; i < returnsPromiseFunction(); i += 1); | ||
|
||
for (let i = 0; i < returnsPromiseFunction().then(); i += 1); | ||
|
||
for (let i in returnsPromiseFunction()); | ||
|
||
for (let i in returnsPromiseFunction().then()); | ||
|
||
for (const promise of returnsPromiseFunction()); | ||
|
||
for (const promise of returnsPromiseFunction().then()); | ||
|
||
let promise = Math.random() > .5 | ||
? returnsPromiseFunction() | ||
: returnsPromiseFunction().then(); | ||
|
||
[0]: Promises must be handled appropriately |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"linterOptions": { | ||
"typeCheck": true | ||
}, | ||
"rules": { | ||
"no-floating-promises": true | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think populating the
rationale
property here would be pretty useful. What does it mean to be handled "appropriately"? What happens when it's not handled?