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
47 changes: 47 additions & 0 deletions src/rules/code-examples/noAsyncWithoutAwait.examples.ts
@@ -0,0 +1,47 @@
/**
* @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 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();
};
`,
},
];
99 changes: 99 additions & 0 deletions src/rules/noAsyncWithoutAwaitRule.ts
@@ -0,0 +1,99 @@
/**
* @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 metadata: Lint.IRuleMetadata = {
codeExamples,
description: "Do not write async functions that do not have an await statement in them",
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
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 static FAILURE_STRING = "Async functions with no await are not allowed.";
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved

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 Boolean(this.getAsyncModifier(node));
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
}

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 functionBlockHasAwait(node: ts.Node) {
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: boolean[] = node.getChildren()
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
.map((functionBlockNode: ts.Node) => this.functionBlockHasAwait(functionBlockNode));
return awaitInChildren.some(Boolean);
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
}

private addFailureIfAsyncFunctionHasNoAwait(node: ts.ArrowFunction | ts.FunctionDeclaration | ts.MethodDeclaration) {
if (this.isAsyncFunction(node) && !this.functionBlockHasAwait(node.body as ts.Node)) {
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);
}
}
}
}
2 changes: 2 additions & 0 deletions src/runner.ts
Expand Up @@ -156,6 +156,7 @@ async function runWorker(options: Options, logger: Logger): Promise<Status> {
return options.force || errorCount === 0 ? Status.Ok : Status.LintError;
}

// tslint:disable-next-line:no-async-without-await
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
async function runLinter(options: Options, logger: Logger): Promise<LintResult> {
const { files, program } = resolveFilesAndProgram(options, logger);
// if type checking, run the type checker
Expand Down Expand Up @@ -287,6 +288,7 @@ async function doLinting(options: Options, files: string[], program: ts.Program
}

/** Read a file, but return undefined if it is an MPEG '.ts' file. */
// tslint:disable-next-line:no-async-without-await
async function tryReadFile(filename: string, logger: Logger): Promise<string | undefined> {
if (!fs.existsSync(filename)) {
throw new FatalError(`Unable to open file: ${filename}`);
Expand Down
74 changes: 74 additions & 0 deletions test/rules/no-async-without-await/test.ts.lint
@@ -0,0 +1,74 @@
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(){
~~~~~ [Async functions with no await are not allowed.]
let b = 1;
async function f() {
await fetch();
};
}

function a(){
let b = 1;
async function f() {
~~~~~ [Async functions with no await are not allowed.]
fetch();
};
}

const a = async () => {
~~~~~ [Async functions with no await are not allowed.]
let b = 1;
console.log(b);
}


const a = async () => 1;
~~~~~ [Async functions with no await are not allowed.]

class A {
async b() {
~~~~~ [Async functions with no await are not allowed.]
console.log(1);
}
}

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

async () => {
await a();
class A {
async b() {
~~~~~ [Async functions with no await are not allowed.]
console.log(1);
}
}
};

[0]: Async functions with no await are not allowed.
5 changes: 5 additions & 0 deletions test/rules/no-async-without-await/tslint.json
@@ -0,0 +1,5 @@
{
"rules": {
"no-async-without-await": true
}
}