Skip to content

fix(eslint-plugin): [no-floating-promises] allowForKnownSafeCalls now supports function names (fixes #11423) #11430

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion packages/eslint-plugin/docs/rules/no-floating-promises.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ unsafe('...', () => {});
</TabItem>
<TabItem value="✅ Correct">

```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<void>;

safe('...', () => {});
Expand Down
12 changes: 12 additions & 0 deletions packages/eslint-plugin/src/rules/no-floating-promises.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
readonlynessOptionsSchema,
skipChainExpression,
typeMatchesSomeSpecifier,
valueMatchesSomeSpecifier,
} from '../util';
import {
parseCatchCall,
Expand Down Expand Up @@ -242,6 +243,17 @@ export default createRule<Options, MessageId>({

const type = services.getTypeAtLocation(node.callee);

if (
valueMatchesSomeSpecifier(
node.callee,
allowForKnownSafeCalls,
services.program,
type,
)
) {
return true;
}

return typeMatchesSomeSpecifier(
type,
allowForKnownSafeCalls,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions packages/eslint-plugin/tests/rules/no-floating-promises.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down
Loading