Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(no-expression-statements): ignore Promise<void> when ignoreVoid is set #866

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading