8000 fix(eslint-plugin): [await-thenable, return-await] don't flag awaiting unconstrained type parameter as unnecessary by ronami · Pull Request #10314 · typescript-eslint/typescript-eslint · GitHub
[go: up one dir, main page]

Skip to content

fix(eslint-plugin): [await-thenable, return-await] don't flag awaiting unconstrained type parameter as unnecessary #10314

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

Merged
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
9 changes: 4 additions & 5 deletions packages/eslint-plugin/src/rules/await-thenable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import type { TSESLint, TSESTree } from '@typescript-eslint/utils';
import * as tsutils from 'ts-api-utils';

import {
Awaitable,
createRule,
getFixOrSuggest,
getParserServices,
isAwaitKeyword,
isTypeAnyType,
isTypeUnknownType,
needsToBeAwaited,
nullThrows,
NullThrowsReasons,
} from '../util';
Expand Down Expand Up @@ -51,13 +52,11 @@ export default createRule<[], MessageId>({
return {
AwaitExpression(node): void {
const type = services.getTypeAtLocation(node.argument);
if (isTypeAnyType(type) || isTypeUnknownType(type)) {
return;
}

const originalNode = services.esTreeNodeToTSNodeMap.get(node);
const certainty = needsToBeAwaited(checker, originalNode, type);

if (!tsutils.isThenableType(checker, originalNode.expression, type)) {
if (certainty === Awaitable.Never) {
context.report({
node,
messageId: 'await',
Expand Down
12 changes: 5 additions & 7 deletions packages/eslint-plugin/src/rules/return-await.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import type { TSESLint, TSESTree } from '@typescript-eslint/utils';

import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import * as tsutils from 'ts-api-utils';
import * as ts from 'typescript';

import {
Awaitable,
createRule,
getFixOrSuggest,
getParserServices,
isAwaitExpression,
isAwaitKeyword,
isTypeAnyType,
isTypeUnknownType,
needsToBeAwaited,
nullThrows,
} from '../util';
import { getOperatorPrecedence } from '../util/getOperatorPrecedence';
Expand Down Expand Up @@ -304,14 +303,13 @@ export default createRule({
}

const type = checker.getTypeAtLocation(child);
const isThenable = tsutils.isThenableType(checker, expression, type);
const certainty = needsToBeAwaited(checker, expression, type);

// handle awaited _non_thenables

if (!isThenable) {
if (certainty !== Awaitable.Always) {
if (isAwait) {
// any/unknown could be thenable; do not enforce whether they are `await`ed.
if (isTypeAnyType(type) || isTypeUnknownType(type)) {
if (certainty === Awaitable.May) {
return;
}
context.report({
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-plugin/src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export * from './isUndefinedIdentifier';
export * from './misc';
export * from './needsPrecedingSemiColon';
export * from './objectIterators';
export * from './needsToBeAwaited';
export * from './scopeUtils';
export * from './types';

Expand Down
43 changes: 43 additions & 0 deletions packages/eslint-plugin/src/util/needsToBeAwaited.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import type * as ts from 'typescript';

import {
isTypeAnyType,
isTypeUnknownType,
} from '@typescript-eslint/type-utils';
import * as tsutils from 'ts-api-utils';

export enum Awaitable {
Always,
Never,
May,
}

export function needsToBeAwaited(
checker: ts.TypeChecker,
node: ts.Node,
type: ts.Type,
): Awaitable {
// can't use `getConstrainedTypeAtLocation` directly since it's bugged for
// unconstrained generics.
const constrainedType = !tsutils.isTypeParameter(type)
? type
: checker.getBaseConstraintOfType(type);

// unconstrained generic types should be treated as unknown
if (constrainedType == null) {
return Awaitable.May;
}

// `any` and `unknown` types may need to be awaited
if (isTypeAnyType(constrainedType) || isTypeUnknownType(constrainedType)) {
return Awaitable.May;
}

// 'thenable' values should always be be awaited
if (tsutils.isThenableType(checker, node, constrainedType)) {
return Awaitable.Always;
}

// anything else should not be awaited
return Awaitable.Never;
}
148 changes: 148 additions & 0 deletions packages/eslint-plugin/tests/rules/await-thenable.test.ts
A92E
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,68 @@ async function foo() {
async function iterateUsing(arr: Array<AsyncDisposable>) {
for (await using foo of arr) {
}
}
`,
},
{
code: `
async function wrapper<T>(value: T) {
return await value;
}
`,
},
{
code: `
async function wrapper<T extends unknown>(value: T) {
return await value;
}
`,
},
{
code: `
async function wrapper<T extends any>(value: T) {
return await value;
}
`,
},
{
code: `
async function wrapper<T extends Promise<unknown>>(value: T) {
return await value;
}
`,
},
{
code: `
async function wrapper<T extends number | Promise<unknown>>(value: T) {
return await value;
}
`,
},
{
code: `
class C<T> {
async wrapper<T>(value: T) {
return await value;
}
}
`,
},
{
code: `
class C<R> {
async wrapper<T extends R>(value: T) {
return await value;
}
}
`,
},
{
code: `
class C<R extends unknown> {
async wrapper<T extends R>(value: T) {
return await value;
}
}
`,
},
Expand Down Expand Up @@ -639,5 +701,91 @@ async function foo() {
},
],
},
{
code: `
async function wrapper<T extends number>(value: T) {
return await value;
}
`,
errors: [
{
column: 10,
endColumn: 21,
endLine: 3,
line: 3,
messageId: 'await',
suggestions: [
{
messageId: 'removeAwait',
output: `
async function wrapper<T extends number>(value: T) {
return value;
}
`,
},
],
},
],
},
{
code: `
class C<T> {
async wrapper<T extends string>(value: T) {
return await value;
}
}
`,
errors: [
{
column: 12,
endColumn: 23,
endLine: 4,
line: 4,
messageId: 'await',
suggestions: [
{
messageId: 'removeAwait',
output: `
class C<T> {
async wrapper<T extends string>(value: T) {
return value;
}
}
`,
},
],
},
],
},
{
code: `
class C<R extends number> {
async wrapper<T extends R>(value: T) {
return await value;
}
}
`,
errors: [
{
column: 12,
endColumn: 23,
endLine: 4,
line: 4,
messageId: 'await',
suggestions: [
{
messageId: 'removeAwait',
output: `
class C<R extends number> {
async wrapper<T extends R>(value: T) {
return value;
}
}
`,
},
],
},
],
},
],
});
Loading
Loading
0