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

Address 2637: Relax promise-function-async for short arrow functions #4553

Merged
merged 2 commits into from Mar 2, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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