Skip to content

Commit 2e1a992

Browse files
fix(no-expression-statements): ignore Promise<void> when ignoreVoid is set (#866)
1 parent 72969f4 commit 2e1a992

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

docs/rules/no-expression-statements.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ const defaults = {
8181

8282
### `ignoreVoid`
8383

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

8687
### `ignoreSelfReturning`
8788

src/rules/no-expression-statements.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type {
55
} from "@typescript-eslint/utils/json-schema";
66
import type { RuleContext } from "@typescript-eslint/utils/ts-eslint";
77
import { deepmerge } from "deepmerge-ts";
8+
import type ts from "typescript";
89

910
import tsApiUtils from "#/conditional-imports/ts-api-utils";
1011
import typescript from "#/conditional-imports/typescript";
@@ -21,7 +22,11 @@ import {
2122
createRule,
2223
getTypeOfNode,
2324
} from "#/utils/rule";
24-
import { isCallExpression, isYieldExpression } from "#/utils/type-guards";
25+
import {
26+
isCallExpression,
27+
isPromiseType,
28+
isYieldExpression,
29+
} from "#/utils/type-guards";
2530

2631
/**
2732
* The name of this rule.
@@ -136,7 +141,16 @@ function checkExpressionStatement(
136141
};
137142
}
138143

139-
if (ignoreVoid && tsApiUtils?.isIntrinsicVoidType(returnType) === true) {
144+
if (
145+
ignoreVoid &&
146+
(tsApiUtils?.isIntrinsicVoidType(returnType) === true ||
147+
("typeArguments" in returnType &&
148+
isPromiseType(context, returnType) &&
149+
(returnType.typeArguments as ts.Type[]).length > 0 &&
150+
tsApiUtils?.isIntrinsicVoidType(
151+
(returnType.typeArguments as ts.Type[])[0]!,
152+
) === true))
153+
) {
140154
return {
141155
context,
142156
descriptors: [],

tests/rules/no-expression-statements.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ describe(name, () => {
9393
`,
9494
options: [{ ignoreVoid: true }],
9595
});
96+
97+
valid({
98+
code: dedent`
99+
function foo() { return Promise.resolve(); }
100+
foo();
101+
`,
102+
options: [{ ignoreVoid: true }],
103+
});
96104
});
97105

98106
it("ignoreSelfReturning", () => {

0 commit comments

Comments
 (0)