Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Show file tree
Hide file tree
Changes fromall 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 deletionspackages/eslint-plugin/src/rules/await-thenable.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 deletionspackages/eslint-plugin/src/rules/return-await.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand Up@@ -304,14 +303,13 @@ export default createRule({
}

const type = checker.getTypeAtLocation(child);
constisThenable =tsutils.isThenableType(checker, expression, type);
constcertainty =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 deletionspackages/eslint-plugin/src/util/index.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionspackages/eslint-plugin/src/util/needsToBeAwaited.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionspackages/eslint-plugin/tests/rules/await-thenable.test.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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

[8]ページ先頭

©2009-2025 Movatter.jp