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): [no-floating-promises] false negative calling .then with second argument undefined #6881

Merged
merged 5 commits into from Jul 15, 2023
Merged

fix(eslint-plugin): [no-floating-promises] false negative calling .then with second argument undefined #6881

merged 5 commits into from Jul 15, 2023

Conversation

kirkwaiblinger
Copy link
Member

@kirkwaiblinger kirkwaiblinger commented Apr 11, 2023

PR Checklist

Overview

In no-floating-promises, the presence of an onRejected handler for .then or .catch is one of the valid ways of terminating an expression involving a promise. However, not all values supplied for onRejected are actually valid. Namely, anything that is not a function (looking at you, null and undefined) is simply ignored in EcmaScript. This PR adds validation to the rejection handler to verify that it is actually a function.

In short,

Promise.reject().then(() => {}) // previously was invalid, still is invalid
Promise.reject().then(() => {}, undefined) // previously was valid, now is invalid

In addition to that narrow issue, I have added the same validation for the .catch handler, since the same principles applies.

Promise.reject().catch() // previously was invalid, still is invalid
Promise.reject().catch(undefined) // previously was valid, now is invalid

In general, any onRejected argument to .then or .catch is now required to be callable.

There is a special case error message per discussion in the issue.

I do have several things to bring to the attention of reviewers

  1. I notice that this rule considers any "Promise-like" thenables, not just EcmaScript Promises. The claim that all rejection handlers are ignored if they are not callable is explicitly based on EcmaScript Promises, and may not be true for other Promise implementations. Is this a concern? Or should we go ahead with flagging errors assuming the ES implementation, despite the fact that, in principle, they could be false positives for an arbitrary thenable?

  2. The rule at present never flags on a .finally that has a nonzero number of arguments. I don't actually understand why (is this intentional behavior or a bug??), since finally can make a resolved promise reject, but it can never make a rejected promise resolve. Because I don't understand the intention, and because the finally handler is not a rejection handler, I made no attempt to add validation to the handler in .finally.

  3. I'm not totally happy with passing the result object up the recursion in isHandledPromise, but I'm not sure I know of a simpler way of passing the additional info required in order to special case the error message. Open to suggestions...

PS this is my first open source PR. Apologies in advance if I commit any major faux pas

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @kirkwaiblinger!

typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community.

The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately.

Thanks again!


🙏 Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently on https://opencollective.com/typescript-eslint.

@netlify
Copy link

netlify bot commented Apr 11, 2023

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit 822ddc6
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/64b1bda6c2523e0008d7289c
😎 Deploy Preview https://deploy-preview-6881--typescript-eslint.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@nx-cloud
Copy link

nx-cloud bot commented Apr 11, 2023

☁️ Nx Cloud Report

CI is running/has finished running commands for commit 822ddc6. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this branch


✅ Successfully ran 2 targets

Sent with 💌 from NxCloud.

@kirkwaiblinger kirkwaiblinger changed the title fix(eslint-plugin): [no-floating-promises] False negative calling .then with second argument undefined (Issue #6850) fix(eslint-plugin): [no-floating-promises] False negative calling .then with second argument undefined Apr 11, 2023
@kirkwaiblinger kirkwaiblinger changed the title fix(eslint-plugin): [no-floating-promises] False negative calling .then with second argument undefined fix(eslint-plugin): [no-floating-promises] false negative calling .then with second argument undefined Apr 11, 2023
@codecov
Copy link

codecov bot commented Apr 11, 2023

Codecov Report

Merging #6881 (822ddc6) into main (e08a722) will increase coverage by 0.02%.
The diff coverage is 100.00%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6881      +/-   ##
==========================================
+ Coverage   87.54%   87.57%   +0.02%     
==========================================
  Files         377      377              
  Lines       13172    13201      +29     
  Branches     3892     3901       +9     
==========================================
+ Hits        11532    11561      +29     
  Misses       1261     1261              
  Partials      379      379              
Flag Coverage Δ
unittest 87.57% <100.00%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...es/eslint-plugin/src/rules/no-floating-promises.ts 100.00% <100.00%> (ø)

Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

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

Congrats on an excellent first open source PR! 🚀

This generally looks good to me, except for a few small touchups - and the big TODO.

No major (or minor) faux pas 😄 - I appreciate you asking for clarification and adhering to existing styles really well!

@JoshuaKGoldberg JoshuaKGoldberg added the awaiting response Issues waiting for a reply from the OP or another party label Apr 11, 2023
@kirkwaiblinger
Copy link
Member Author

kirkwaiblinger commented Jun 26, 2023

Hi @JoshuaKGoldberg, it's been about 3 months since the last activity on this PR, which is the amount of time that the contributing guide suggests to wait before pinging pending reviewers, so I am gently pinging you. Would you be able to take a final pass on this? I believe I have made the minor changes requested.

@JoshuaKGoldberg
Copy link
Member

Yes! @kirkwaiblinger so sorry about the delay - you've done everything right (and are even being very kind about my blatantly ignoring you 😅). Very sorry to do that. Now that we're done with v6, I'm going through the PR queue.

But I shouldn't have let this go without at least an ACK - sorry about that. I'll try to make sure to not leave things open for this long again.

@JoshuaKGoldberg JoshuaKGoldberg removed the awaiting response Issues waiting for a reply from the OP or another party label Jul 13, 2023
Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

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

OK! Again so very sorry about taking so many months to get to this (and on your first PR even! 😭).

The only blocking comment is adding a test case for the ignoreVoid option, to make sure that error ID logic is tested. Everything else is nitpicking and non-blocking - no worries if you ignore those comments 😄.

If you don't have time to address the changes, no worries - I'll set a reminder for myself to tackle them on Sunday or Monday morning before our next scheduled release.

Thanks again - really great set of PRs here! ❤️

Tony the Tiger saying ggggrreeeaatt

@JoshuaKGoldberg JoshuaKGoldberg added the awaiting response Issues waiting for a reply from the OP or another party label Jul 13, 2023
@kirkwaiblinger
Copy link
Member Author

Yes! @kirkwaiblinger so sorry about the delay - you've done everything right (and are even being very kind about my blatantly ignoring you 😅). Very sorry to do that. Now that we're done with v6, I'm going through the PR queue.

But I shouldn't have let this go without at least an ACK - sorry about that. I'll try to make sure to not leave things open for this long again.

No worries!

@kirkwaiblinger
Copy link
Member Author

I'm not really sure what caused the CI failures, but neither appears to be directly related to my changes as far as I can tell. Looking for advice on this

@JoshuaKGoldberg
Copy link
Member

I'm not really sure what caused the CI failures, but neither appears to be directly related to my changes as far as I can tell. Looking for advice on this

<--- Last few GCs --->
[2598:0x4d801c0]   237024 ms: Mark-sweep 2024.6 (2087.5) -> 2013.3 (2092.7) MB, 1363.3 / 0.0 ms  (average mu = 0.274, current mu = 0.103) allocation failure scavenge might not succeed
[2598:0x4d801c0]   238998 ms: Mark-sweep 2029.9 (2093.3) -> 2020.3 (2099.0) MB, 1822.2 / 0.0 ms  (average mu = 0.169, current mu = 0.077) allocation failure scavenge might not succeed
<--- JS stacktrace --->
FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory
 1: 0xb090e0 node::Abort() [/opt/hostedtoolcache/node/16.20.1/x64/bin/node]
 2: 0xa1b70e  [/opt/hostedtoolcache/node/16.20.1/x64/bin/node]
 3: 0xce1a20 v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [/opt/hostedtoolcache/node/16.20.1/x64/bin/node]

🙃 love it. We might have too many unit tests for the Jest+Node setup we're using.

Passed on re-run: https://github.com/typescript-eslint/typescript-eslint/actions/runs/5558453479/jobs/10164457558 & https://github.com/typescript-eslint/typescript-eslint/actions/runs/5558453479/jobs/10164509125. 🤷. If it keeps happening we can file an issue and take a look at reducing the memory footprint.

Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

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

💯 thanks - excited to land this one!

@JoshuaKGoldberg JoshuaKGoldberg merged commit 606a52c into typescript-eslint:main Jul 15, 2023
45 of 46 checks passed
@JoshuaKGoldberg JoshuaKGoldberg removed the awaiting response Issues waiting for a reply from the OP or another party label Jul 15, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: [no-floating-promises] False negative calling .then with second argument undefined
3 participants