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

[New Rule] add noAsyncWithoutAwait rule #3945

Merged
merged 15 commits into from Jun 16, 2019
1 change: 1 addition & 0 deletions src/configs/all.ts
Expand Up @@ -95,6 +95,7 @@ export const rules = {
// "import-blacklist": no sensible default
"label-position": true,
"no-arg": true,
"no-async-without-await": true,
"no-bitwise": true,
"no-conditional-assignment": true,
"no-console": true,
Expand Down
51 changes: 51 additions & 0 deletions src/rules/code-examples/noAsyncWithoutAwait.examples.ts
@@ -0,0 +1,51 @@
/**
* @license
* Copyright 2018 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 Lint from "../../index";

export const codeExamples = [
{
config: Lint.Utils.dedent`
"rules": { "no-async-without-await": true }
`,
description: "Do not use the async keyword in case it is not actually needed",
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
fail: Lint.Utils.dedent`
async function f() {
fetch();
}

async function f() {
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
async function g() {
await h();
}
}
`,
pass: Lint.Utils.dedent`
async function f() {
await fetch();
}

const f = async () => {
await fetch();
};

const f = async () => {
return 'value';
};
`,
},
];
126 changes: 126 additions & 0 deletions src/rules/noAsyncWithoutAwaitRule.ts
@@ -0,0 +1,126 @@
/**
* @license
* Copyright 2018 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";
import { codeExamples } from "./code-examples/noAsyncWithoutAwait.examples";

export class Rule extends Lint.Rules.AbstractRule {
public static FAILURE_STRING = "Functions marked async must contain an await or return statement.";

public static metadata: Lint.IRuleMetadata = {
codeExamples,
description: Rule.FAILURE_STRING,
hasFix: false,
optionExamples: [true],
options: null,
optionsDescription: "Not configurable.",
rationale: "Cleaner code, can possibly reduce transpiled output",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please explain this in more detail. Someone reading this rule's page on palantir.github.io would leave with more questions than they started with.

ruleName: "no-async-without-await",
type: "functionality",
typescriptOnly: false,
};

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new Walk(sourceFile, this.getOptions()));
}
}

class Walk extends Lint.RuleWalker {
protected visitFunctionDeclaration(node: ts.FunctionDeclaration) {
this.addFailureIfAsyncFunctionHasNoAwait(node);
super.visitFunctionDeclaration(node);
}

protected visitArrowFunction(node: ts.ArrowFunction) {
this.addFailureIfAsyncFunctionHasNoAwait(node);
super.visitArrowFunction(node);
}

protected visitMethodDeclaration(node: ts.MethodDeclaration) {
this.addFailureIfAsyncFunctionHasNoAwait(node);
super.visitMethodDeclaration(node);
}

private isAsyncFunction(node: ts.Node): boolean {
return this.getAsyncModifier(node) !== undefined;
}

private getAsyncModifier(node: ts.Node) {
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
if (node.modifiers !== undefined) {
return node.modifiers.find((modifier) => modifier.kind === ts.SyntaxKind.AsyncKeyword);
}

return undefined;
}

private isAwait(node: ts.Node): boolean {
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
return node.kind === ts.SyntaxKind.AwaitKeyword;
}

private isReturn(node: ts.Node): boolean {
return node.kind === ts.SyntaxKind.ReturnKeyword;
}

private functionBlockHasAwait(node: ts.Node): boolean {
if (this.isAwait(node)) {
return true;
}

// tslint:disable-next-line:no-unsafe-any
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
if (node.kind === ts.SyntaxKind.ArrowFunction
|| node.kind === ts.SyntaxKind.FunctionDeclaration) {
return false;
}

const awaitInChildren = node.getChildren()
.map((functionBlockNode: ts.Node) => this.functionBlockHasAwait(functionBlockNode));
return awaitInChildren.some(Boolean);
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
}

private functionBlockHasReturn(node: ts.Node): boolean {
if (this.isReturn(node)) {
return true;
}

// tslint:disable-next-line:no-unsafe-any
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
if (node.kind === ts.SyntaxKind.ArrowFunction
|| node.kind === ts.SyntaxKind.FunctionDeclaration) {
return false;
}

const returnInChildren = node.getChildren()
.map((functionBlockNode: ts.Node) => this.functionBlockHasReturn(functionBlockNode));
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
return returnInChildren.some(Boolean);
}

private isShortArrowReturn(node: ts.ArrowFunction | ts.FunctionDeclaration | ts.MethodDeclaration) {
return node.kind === ts.SyntaxKind.ArrowFunction && node.body.kind !== ts.SyntaxKind.Block;
}

private addFailureIfAsyncFunctionHasNoAwait(node: ts.ArrowFunction | ts.FunctionDeclaration | ts.MethodDeclaration) {
if (this.isAsyncFunction(node)
&& !this.functionBlockHasAwait(node.body as ts.Node)
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
&& !this.functionBlockHasReturn(node.body as ts.Node)
&& !this.isShortArrowReturn(node)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Performance nit: please move isShortArrowReturn first. Seems like we could be slightly faster by performing that very fast check before doing deeper searches.

const asyncModifier = this.getAsyncModifier(node);
if (asyncModifier !== undefined) {
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
this.addFailureAt(asyncModifier.getStart(), asyncModifier.getEnd() - asyncModifier.getStart(), Rule.FAILURE_STRING);
}
}
}
}
85 changes: 85 additions & 0 deletions test/rules/no-async-without-await/test.ts.lint
@@ -0,0 +1,85 @@
async function a(){
eranshabi marked this conversation as resolved.
Show resolved Hide resolved
~~~~~ [0]
let b = 1;
console.log(b);
}

async function a(){
let b = 1;
await console.log(b);
}

async function a(){
let b = 1;
console.log(await b());
}

async function a(){
~~~~~ [0]
let b = 1;
let c = async () => {
await fetch();
};
}

async function a(){
~~~~~ [Functions marked async must contain an await or return statement.]
let b = 1;
async function f() {
await fetch();
};
}

function a(){
let b = 1;
async function f() {
~~~~~ [Functions marked async must contain an await or return statement.]
fetch();
};
}

const a = async () => {
~~~~~ [Functions marked async must contain an await or return statement.]
let b = 1;
console.log(b);
}

class A {
async b() {
~~~~~ [Functions marked async must contain an await or return statement.]
console.log(1);
}
}

class A {
async b() {
await b();
}
}

async () => {
await a();
class A {
async b() {
~~~~~ [Functions marked async must contain an await or return statement.]
console.log(1);
}
}
};

async function a() {
let b = 1;
return b;
}

let a = async () => 1;

async function a() {
~~~~~ [Functions marked async must contain an await or return statement.]
let b = 1;
let a = () => {
return 1;
}
}

[0]: Functions marked async must contain an await or return statement.
5 changes: 5 additions & 0 deletions test/rules/no-async-without-await/tslint.json
@@ -0,0 +1,5 @@
{
"rules": {
"no-async-without-await": true
}
}