Skip to content
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

fix(eslint-plugin): check optional chaining for floating promises #4096

Merged
merged 1 commit into from Nov 11, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion packages/eslint-plugin/src/rules/no-floating-promises.ts
Expand Up @@ -65,7 +65,13 @@ export default util.createRule<Options, MessageId>({
return;
}

if (isUnhandledPromise(checker, node.expression)) {
let expression = node.expression;

if (expression.type === AST_NODE_TYPES.ChainExpression) {
expression = expression.expression;
}
Comment on lines +68 to +72
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: this is similar to skipChainExpression from padding-line-between-statements. You might consider making a util for it? I imagine this situation is one that will keep coming up.

Not a blocker, just a thought!


if (isUnhandledPromise(checker, expression)) {
if (options.ignoreVoid) {
context.report({
node,
Expand Down
80 changes: 80 additions & 0 deletions packages/eslint-plugin/tests/rules/no-floating-promises.test.ts
Expand Up @@ -334,6 +334,28 @@ async function test() {
returnsPromise()?.finally(() => {});
return returnsPromise();
}
`,
`
const doSomething = async (
obj1: { a?: { b?: { c?: () => Promise<void> } } },
obj2: { a?: { b?: { c: () => Promise<void> } } },
obj3: { a?: { b: { c?: () => Promise<void> } } },
obj4: { a: { b: { c?: () => Promise<void> } } },
obj5: { a?: () => { b?: { c?: () => Promise<void> } } },
obj6?: { a: { b: { c?: () => Promise<void> } } },
callback?: () => Promise<void>,
): Promise<void> => {
await obj1.a?.b?.c?.();
await obj2.a?.b?.c();
await obj3.a?.b.c?.();
await obj4.a.b.c?.();
await obj5.a?.().b?.c?.();
await obj6?.a.b.c?.();

return callback?.();
};

void doSomething();
`,
// ignoreIIFE
{
Expand Down Expand Up @@ -414,6 +436,64 @@ async function test() {
},
],
},
{
code: `
const doSomething = async (
obj1: { a?: { b?: { c?: () => Promise<void> } } },
obj2: { a?: { b?: { c: () => Promise<void> } } },
obj3: { a?: { b: { c?: () => Promise<void> } } },
obj4: { a: { b: { c?: () => Promise<void> } } },
obj5: { a?: () => { b?: { c?: () => Promise<void> } } },
obj6?: { a: { b: { c?: () => Promise<void> } } },
callback?: () => Promise<void>,
): Promise<void> => {
obj1.a?.b?.c?.();
obj2.a?.b?.c();
obj3.a?.b.c?.();
obj4.a.b.c?.();
obj5.a?.().b?.c?.();
obj6?.a.b.c?.();

callback?.();
};

doSomething();
`,
errors: [
{
line: 11,
messageId: 'floatingVoid',
},
{
line: 12,
messageId: 'floatingVoid',
},
{
line: 13,
messageId: 'floatingVoid',
},
{
line: 14,
messageId: 'floatingVoid',
},
{
line: 15,
messageId: 'floatingVoid',
},
{
line: 16,
messageId: 'floatingVoid',
},
{
line: 18,
messageId: 'floatingVoid',
},
{
line: 21,
messageId: 'floatingVoid',
},
],
},
{
options: [{ ignoreVoid: true }],
code: `
Expand Down