diff --git a/packages/eslint-plugin/docs/rules/no-floating-promises.mdx b/packages/eslint-plugin/docs/rules/no-floating-promises.mdx index 12bf7465b537..977b10b2a078 100644 --- a/packages/eslint-plugin/docs/rules/no-floating-promises.mdx +++ b/packages/eslint-plugin/docs/rules/no-floating-promises.mdx @@ -258,7 +258,7 @@ unsafe('...', () => {}); -```ts option='{"allowForKnownSafeCalls":[{"from":"file","name":"safe","path":"input.ts"}]}' skipValidation +```ts option='{"allowForKnownSafeCalls":[{"from":"file","name":"safe","path":"input.ts"}]}' declare function safe(...args: unknown[]): Promise; safe('...', () => {}); diff --git a/packages/eslint-plugin/src/rules/no-floating-promises.ts b/packages/eslint-plugin/src/rules/no-floating-promises.ts index 50965093ee98..8a46b328c464 100644 --- a/packages/eslint-plugin/src/rules/no-floating-promises.ts +++ b/packages/eslint-plugin/src/rules/no-floating-promises.ts @@ -16,6 +16,7 @@ import { readonlynessOptionsSchema, skipChainExpression, typeMatchesSomeSpecifier, + valueMatchesSomeSpecifier, } from '../util'; import { parseCatchCall, @@ -242,6 +243,17 @@ export default createRule({ const type = services.getTypeAtLocation(node.callee); + if ( + valueMatchesSomeSpecifier( + node.callee, + allowForKnownSafeCalls, + services.program, + type, + ) + ) { + return true; + } + return typeMatchesSomeSpecifier( type, allowForKnownSafeCalls, diff --git a/packages/eslint-plugin/tests/docs-eslint-output-snapshots/no-floating-promises.shot b/packages/eslint-plugin/tests/docs-eslint-output-snapshots/no-floating-promises.shot index 8cdd60d375d6..9a5130ce0c07 100644 --- a/packages/eslint-plugin/tests/docs-eslint-output-snapshots/no-floating-promises.shot +++ b/packages/eslint-plugin/tests/docs-eslint-output-snapshots/no-floating-promises.shot @@ -135,4 +135,3 @@ Options: {"allowForKnownSafeCalls":[{"from":"file","name":"safe","path":"input.t declare function safe(...args: unknown[]): Promise; safe('...', () => {}); -~~~~~~~~~~~~~~~~~~~~~~ Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator. diff --git a/packages/eslint-plugin/tests/rules/no-floating-promises.test.ts b/packages/eslint-plugin/tests/rules/no-floating-promises.test.ts index 4835087e7cb6..dc4ee2e3cab8 100644 --- a/packages/eslint-plugin/tests/rules/no-floating-promises.test.ts +++ b/packages/eslint-plugin/tests/rules/no-floating-promises.test.ts @@ -866,6 +866,34 @@ declare function createMyThenable(): MyThenable; createMyThenable(); `, + { + code: ` +const randomAsyncFunction = async () => { + return Promise.resolve(true); +}; + +randomAsyncFunction(); + `, + options: [ + { + allowForKnownSafeCalls: ['randomAsyncFunction'], + }, + ], + }, + { + code: ` +async function myAsyncFunction() { + return Promise.resolve('test'); +} + +myAsyncFunction(); + `, + options: [ + { + allowForKnownSafeCalls: ['myAsyncFunction'], + }, + ], + }, ], invalid: [