Skip to content

Commit

Permalink
fix(no-expression-statements): ignore Promise<void> when `ignoreVoi…
Browse files Browse the repository at this point in the history
…d` is set (#866)
  • Loading branch information
RebeccaStevens committed Aug 26, 2024
1 parent 72969f4 commit 2e1a992
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
3 changes: 2 additions & 1 deletion docs/rules/no-expression-statements.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ const defaults = {

### `ignoreVoid`

When enabled, expression of type void are not flagged as violations. This options requires TypeScript in order to work.
When enabled, expression of type `void` and `Promise<void>` are not flagged as violations.
This options requires TypeScript in order to work.

### `ignoreSelfReturning`

Expand Down
18 changes: 16 additions & 2 deletions src/rules/no-expression-statements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
} from "@typescript-eslint/utils/json-schema";
import type { RuleContext } from "@typescript-eslint/utils/ts-eslint";
import { deepmerge } from "deepmerge-ts";
import type ts from "typescript";

import tsApiUtils from "#/conditional-imports/ts-api-utils";
import typescript from "#/conditional-imports/typescript";
Expand All @@ -21,7 +22,11 @@ import {
createRule,
getTypeOfNode,
} from "#/utils/rule";
import { isCallExpression, isYieldExpression } from "#/utils/type-guards";
import {
isCallExpression,
isPromiseType,
isYieldExpression,
} from "#/utils/type-guards";

/**
* The name of this rule.
Expand Down Expand Up @@ -136,7 +141,16 @@ function checkExpressionStatement(
};
}

if (ignoreVoid && tsApiUtils?.isIntrinsicVoidType(returnType) === true) {
if (
ignoreVoid &&
(tsApiUtils?.isIntrinsicVoidType(returnType) === true ||
("typeArguments" in returnType &&
isPromiseType(context, returnType) &&
(returnType.typeArguments as ts.Type[]).length > 0 &&
tsApiUtils?.isIntrinsicVoidType(
(returnType.typeArguments as ts.Type[])[0]!,
) === true))
) {
return {
context,
descriptors: [],
Expand Down
8 changes: 8 additions & 0 deletions tests/rules/no-expression-statements.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ describe(name, () => {
`,
options: [{ ignoreVoid: true }],
});

valid({
code: dedent`
function foo() { return Promise.resolve(); }
foo();
`,
options: [{ ignoreVoid: true }],
});
});

it("ignoreSelfReturning", () => {
Expand Down

0 comments on commit 2e1a992

Please sign in to comment.