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
2 changes: 1 addition & 1 deletion src/rules/code-examples/noAsyncWithoutAwait.examples.ts
Expand Up @@ -22,7 +22,7 @@ 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",
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();
Expand Down
12 changes: 6 additions & 6 deletions src/rules/noAsyncWithoutAwaitRule.ts
Expand Up @@ -20,9 +20,11 @@ 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 statement.";

public static metadata: Lint.IRuleMetadata = {
codeExamples,
description: "Do not write async functions that do not have an await statement in them",
description: Rule.FAILURE_STRING,
hasFix: false,
optionExamples: [true],
options: null,
Expand All @@ -33,8 +35,6 @@ export class Rule extends Lint.Rules.AbstractRule {
typescriptOnly: false,
};

public static FAILURE_STRING = "Async functions with no await are not allowed.";

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new Walk(sourceFile, this.getOptions()));
}
Expand All @@ -57,7 +57,7 @@ class Walk extends Lint.RuleWalker {
}

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

private getAsyncModifier(node: ts.Node) {
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -72,7 +72,7 @@ class Walk extends Lint.RuleWalker {
return node.kind === ts.SyntaxKind.AwaitKeyword;
}

private functionBlockHasAwait(node: ts.Node) {
private functionBlockHasAwait(node: ts.Node): boolean {
if (this.isAwait(node)) {
return true;
}
Expand All @@ -83,7 +83,7 @@ class Walk extends Lint.RuleWalker {
return false;
}

const awaitInChildren: boolean[] = node.getChildren()
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
}
Expand Down
4 changes: 2 additions & 2 deletions src/runner.ts
Expand Up @@ -156,8 +156,8 @@ 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
async function runLinter(options: Options, logger: Logger): Promise<LintResult> {
// tslint:disable-next-line:promise-function-async
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
function runLinter(options: Options, logger: Logger): Promise<LintResult> {
const { files, program } = resolveFilesAndProgram(options, logger);
// if type checking, run the type checker
if (program && options.typeCheck) {
Expand Down
14 changes: 7 additions & 7 deletions test/rules/no-async-without-await/test.ts.lint
Expand Up @@ -23,7 +23,7 @@ async function a(){
}

async function a(){
~~~~~ [Async functions with no await are not allowed.]
~~~~~ [Functions marked async must contain an await statement.]
let b = 1;
async function f() {
await fetch();
Expand All @@ -33,24 +33,24 @@ async function a(){
function a(){
let b = 1;
async function f() {
~~~~~ [Async functions with no await are not allowed.]
~~~~~ [Functions marked async must contain an await statement.]
fetch();
};
}

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


const a = async () => 1;
~~~~~ [Async functions with no await are not allowed.]
~~~~~ [Functions marked async must contain an await statement.]

class A {
async b() {
~~~~~ [Async functions with no await are not allowed.]
~~~~~ [Functions marked async must contain an await statement.]
console.log(1);
}
}
Expand All @@ -65,10 +65,10 @@ async () => {
await a();
class A {
async b() {
~~~~~ [Async functions with no await are not allowed.]
~~~~~ [Functions marked async must contain an await statement.]
console.log(1);
}
}
};

[0]: Async functions with no await are not allowed.
[0]: Functions marked async must contain an await statement.