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

feat(eslint-plugin): [no-floating-promises] add an 'allowForKnownSafePromises' option #8502

Closed
wants to merge 38 commits into from

Conversation

arka1002
Copy link
Contributor

@arka1002 arka1002 commented Feb 18, 2024

PR Checklist

Overview

Hi, I'm really curious, is there any way to test code which have import in them ? #4436 also didn't have them.
Nvm, found it.

Let's discuss.

'allowForKnownSafePromises' option

fixes: typescript-eslint#7008
@typescript-eslint
Copy link
Contributor

Thanks for the PR, @arka1002!

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.

Copy link

netlify bot commented Feb 18, 2024

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit cbbda8a
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/665304aa7961aa000851b468
😎 Deploy Preview https://deploy-preview-8502--typescript-eslint.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 99 (🟢 up 7 from production)
Accessibility: 100 (no change from production)
Best Practices: 92 (no change from production)
SEO: 98 (no change from production)
PWA: 80 (no change from production)
View the detailed breakdown and full score reports

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

@arka1002 arka1002 marked this pull request as draft February 18, 2024 16:49
@arka1002 arka1002 marked this pull request as ready for review February 18, 2024 17:23
@arka1002 arka1002 changed the title feat: [no-floating-promises] add an 'allowForKnownSafePromises' option feat(eslint-plugin): [no-floating-promises] add an 'allowForKnownSafePromises' option Feb 19, 2024
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.

Haven't been able to look deeply yet, but - looks like there's a mismatch in naming?

@arka1002
Copy link
Contributor Author

Ahh... I think I made a terrible mistake.

Thanks for the reviews though.
I'm marking this as draft. I need some more time to thoroughly read the issues once again.

Sorry for the inconvenience.

@JoshuaKGoldberg
Copy link
Member

👋 @arka1002 adding the new allow* options to no-floating-promises is one of our priorities for the next few months. I'd like to make sure this is actively moving towards completion. Do you think you'll be able to make a lot of progress over the next week?

If not, no worries - I'd happily take this on myself. It's a nuanced rule with a lot of context around it. ❤️

@arka1002
Copy link
Contributor Author

Yes, I've almost done the tests, give me just 3 more days. I'll open it for review.

@arka1002 arka1002 marked this pull request as ready for review March 19, 2024 14:25
@github-actions github-actions bot removed the awaiting response Issues waiting for a reply from the OP or another party label Apr 27, 2024
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 for trimming down the test cases! I think this is looking pretty great. Just one last bug from my end and I think I'll be done requesting changes (hopefully, probably).

I'll also leave this open to get at least one other review from @typescript-eslint/triage-team. I've looked at it enough that I don't trust myself to not be missing something. 😄

arka1002 and others added 2 commits April 29, 2024 13:09
Co-authored-by: Josh Goldberg ✨ <git@joshuakgoldberg.com>
@JoshuaKGoldberg JoshuaKGoldberg requested review from a team and removed request for StyleShit April 29, 2024 12:25
@kirkwaiblinger kirkwaiblinger added awaiting response Issues waiting for a reply from the OP or another party and removed awaiting response Issues waiting for a reply from the OP or another party labels May 1, 2024
Copy link
Member

@kirkwaiblinger kirkwaiblinger left a comment

Choose a reason for hiding this comment

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

Requesting some changes in #8502 (comment), but otherwise I think this is getting close! 🙂

@arka1002
Copy link
Contributor Author

Thanks for the reviews. I'm just writing down some notes for the future people who are going to stumble upon this pr.

  1. then and catch functions with 1 and 0 arguments should be reported based on the above review comment. Here are the tests.
  2. About mixed arrays, its very interesting though, Typescript actually reduces the type of the array if it contains any branded type or anything else. Here is the minimum repro(check the 2 slash comments). I have added 1 test. I think if in any future version typescript changes the type to ( Promise<number> | A )[] ( which might be the correct one ), we have to change the logic in this rule.

@github-actions github-actions bot removed the awaiting response Issues waiting for a reply from the OP or another party label May 11, 2024
@@ -2079,19 +2079,19 @@ promise;
},
{
code: `
interface UnsafeThenable<T> {
interface SafeThenable<T> {
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 one, should be valid, because the overall expression results in a SafeThenable rather than ordinary promise (due to the way the SafeThenable#then is defined)

Copy link
Contributor Author

@arka1002 arka1002 May 26, 2024

Choose a reason for hiding this comment

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

Yes, there's an issue which exactly describes Thenable in this rule - #8433. (It also says thenables should be valid)
It'll be solved there.

Copy link
Member

Choose a reason for hiding this comment

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

That is a separate thing entirely (and if it were the case, it would make this entire PR moot, not just this test case). This option needs to be able to support expressions that result in one of the allowed types.

Please move this case to the valid array.

@kirkwaiblinger kirkwaiblinger added the awaiting response Issues waiting for a reply from the OP or another party label May 18, 2024
@github-actions github-actions bot removed the awaiting response Issues waiting for a reply from the OP or another party label May 26, 2024
@bradzacher bradzacher added the enhancement: plugin rule option New rule option for an existing eslint-plugin rule label May 28, 2024
Copy link
Member

@kirkwaiblinger kirkwaiblinger left a comment

Choose a reason for hiding this comment

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

Hey @arka1002! 👋

Heads up that that this PR is blocking some high-priority no-floating-promises issues. We appreciate how much work you've put into it 🙏 , but we really need to wrap it up soon.

We're going to "time-box" this PR; if it isn't merged before Monday the 3rd, we'll plan to take it over ourselves in order to have it ready for the release ~1pm EST that day.

If you have time to iterate with us this week on the requested changes, that's awesome! If not, we also understand, and we have bandwidth to handle the final rounds of change requests on our side.

Thanks again for your work on this! ❤️

@@ -2079,19 +2079,19 @@ promise;
},
{
code: `
interface UnsafeThenable<T> {
interface SafeThenable<T> {
Copy link
Member

Choose a reason for hiding this comment

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

That is a separate thing entirely (and if it were the case, it would make this entire PR moot, not just this test case). This option needs to be able to support expressions that result in one of the allowed types.

Please move this case to the valid array.

},
],

create(context, [options]) {
const services = getParserServices(context);
const checker = services.program.getTypeChecker();
// TODO: #5439
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const allowForKnownSafePromises = options.allowForKnownSafePromises!;

return {
ExpressionStatement(node): void {
Copy link
Member

Choose a reason for hiding this comment

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

Spinning off from https://github.com/typescript-eslint/typescript-eslint/pull/8502/files/7422cf504ba51cc2e1855f0352e151877720755e#r1585695716, please move the duplicative checks for doesTypeMatchSpecifier throughout the ExpressionStatement handler into the start of isPromiseLike function down below. (this will also resolve the test case in https://github.com/typescript-eslint/typescript-eslint/pull/8502/files/7422cf504ba51cc2e1855f0352e151877720755e#r1605894419).

(You might need to pass in context or just move isPromiseLike into the create function so it has access to the options and such, similarly to how you already handled isPromiseArray 🙂).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement: plugin rule option New rule option for an existing eslint-plugin rule
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enhancement: [no-floating-promises] add an 'allowForKnownSafePromises' option
5 participants