Thanks to visit codestin.com
Credit goes to github.com

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
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
16 changes: 14 additions & 2 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 @@ -142,7 +143,7 @@ export default createRule<Options, MessageId>({

const expression = skipChainExpression(node.expression);

if (isKnownSafePromiseReturn(expression)) {
if (isKnownSafePromiseCall(expression)) {
return;
}

Expand Down Expand Up @@ -235,13 +236,24 @@ export default createRule<Options, MessageId>({
];
}

function isKnownSafePromiseReturn(node: TSESTree.Node): boolean {
function isKnownSafePromiseCall(node: TSESTree.Node): boolean {
if (node.type !== AST_NODE_TYPES.CallExpression) {
return false;
}

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