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

Commit

Permalink
relax promise-function-async for short arrow functions (#4553)
Browse files Browse the repository at this point in the history
  • Loading branch information
rrogowski authored and Josh Goldberg committed Mar 2, 2019
1 parent 1a0cc06 commit 30bb50a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/rules/promiseFunctionAsyncRule.ts
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

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

import * as Lint from "../index";
Expand Down Expand Up @@ -103,18 +103,20 @@ function walk(ctx: Lint.WalkContext<EnabledSyntaxKinds>, tc: ts.TypeChecker) {
const { sourceFile, options } = ctx;
return ts.forEachChild(sourceFile, function cb(node): void {
if (options.has(node.kind)) {
const declaration = node as ts.FunctionLikeDeclaration;
switch (node.kind) {
case ts.SyntaxKind.MethodDeclaration:
case ts.SyntaxKind.FunctionDeclaration:
if ((node as ts.FunctionLikeDeclaration).body === undefined) {
if (declaration.body === undefined) {
break;
}
// falls through
case ts.SyntaxKind.FunctionExpression:
case ts.SyntaxKind.ArrowFunction:
if (
!hasModifier(node.modifiers, ts.SyntaxKind.AsyncKeyword) &&
returnsPromise(node as ts.FunctionLikeDeclaration, tc)
returnsPromise(declaration, tc) &&
!isCallExpression(declaration.body as ts.Expression)
) {
ctx.addFailure(
node.getStart(sourceFile),
Expand Down
3 changes: 3 additions & 0 deletions test/rules/promise-function-async/test.ts.lint
Expand Up @@ -62,6 +62,9 @@ class Test {
public nonAsyncNonPromiseMethod(n: number) {
return n;
}

// single statement lamda functions that delegate to another promise-returning function are allowed
public delegatingMethod = () => this.asyncPromiseMethodB(1);
}

[0]: functions that return promises must be async

0 comments on commit 30bb50a

Please sign in to comment.