🌐 AI搜索 & 代理 主页
Skip to content
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
22 changes: 22 additions & 0 deletions packages/eslint-plugin/src/rules/no-misused-promises.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ import * as ts from 'typescript';

import {
createRule,
getConstrainedTypeAtLocation,
getFunctionHeadLoc,
getParserServices,
isArrayMethodCallWithPredicate,
isFunction,
isPromiseLike,
isRestParameterDeclaration,
nullThrows,
NullThrowsReasons,
} from '../util';
import { parseFinallyCall } from '../util/promiseUtils';

export type Options = [
{
Expand Down Expand Up @@ -360,6 +363,13 @@ export default createRule<Options, MessageId>({
function checkArguments(
node: TSESTree.CallExpression | TSESTree.NewExpression,
): void {
if (
node.type === AST_NODE_TYPES.CallExpression &&
isPromiseFinallyMethod(node)
) {
return;
}

const tsNode = services.esTreeNodeToTSNodeMap.get(node);
const voidArgs = voidFunctionArguments(checker, tsNode);
if (voidArgs.size === 0) {
Expand Down Expand Up @@ -563,6 +573,18 @@ export default createRule<Options, MessageId>({
}
}

function isPromiseFinallyMethod(node: TSESTree.CallExpression): boolean {
const promiseFinallyCall = parseFinallyCall(node, context);

return (
promiseFinallyCall != null &&
isPromiseLike(
services.program,
getConstrainedTypeAtLocation(services, promiseFinallyCall.object),
)
);
}

function checkClassLikeOrInterfaceNode(
node:
| TSESTree.ClassDeclaration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,13 @@ declare const useCallback: <T extends (...args: unknown[]) => unknown>(
) => T;
useCallback<ReturnsVoid | ReturnsPromiseVoid>(async () => {});
`,
`
Promise.reject(3).finally(async () => {});
`,
`
const f = 'finally';
Promise.reject(3)[f](async () => {});
`,
],

invalid: [
Expand Down
Loading