Skip to content

feat(eslint-plugin): [no-misused-promises] check subtype methods against heritage type methods #8765

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

Conversation

alythobani
Copy link
Contributor

@alythobani alythobani commented Mar 25, 2024

PR Checklist

Overview

This PR adds a new heritageTypes option (under checksVoidReturn) to the no-misused-promises rule, defaulted to true. If enabled, it checks ClassDeclaration / ClassExpression / InterfaceDeclaration nodes for any methods that return Promises, and checks those methods against any matching methods in their declared extends ... / implements ... heritage types. Error outputted when a heritage type has the given method declared/implemented as returning void instead of a Promise.

(The issue #8739 I filed only mentioned abstract classes/methods, but I realized the topic is more generalizable to heritage types in general.)

Note that this is my first PR here and first time diving into this repo / the Typescript source code. So hopefully I didn't miss any cases to test / handle, but it's possible I did! There did end up being small edge cases I realized weren't handled as I iterated and had to tweak things. E.g., calling checker.getTypeAtLocation(heritageTypeExpression) ended up being necessary in order to properly check against a ClassExpression heritage type's members; checker.getTypeFromTypeNode(heritageTypeExpression) worked for all other heritage types, just not class expressions.

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @alythobani!

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 Mar 25, 2024

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit 8304cef
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/66ba352e6d1ab700084b289a
😎 Deploy Preview https://deploy-preview-8765--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 4 from production)
Accessibility: 100 (no change from production)
Best Practices: 92 (no change from production)
SEO: 90 (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.

@alythobani alythobani changed the title feat(eslint-plugin): checksVoidReturn.subtypes option added to no-misused-promises rule (checks methods against heritage type methods) feat(eslint-plugin): Check against heritage type methods (no-misused-promises / checksVoidReturn.subtypes) Mar 25, 2024
@alythobani alythobani changed the title feat(eslint-plugin): Check against heritage type methods (no-misused-promises / checksVoidReturn.subtypes) feat(eslint-plugin): check subtype methods against heritage type methods (no-misused-promises) Mar 25, 2024
@bradzacher bradzacher changed the title feat(eslint-plugin): check subtype methods against heritage type methods (no-misused-promises) feat(eslint-plugin): [no-misused-promises] check subtype methods against heritage type methods Mar 25, 2024
Copy link

codecov bot commented Mar 25, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 87.91%. Comparing base (3d9ae44) to head (8304cef).
Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #8765   +/-   ##
=======================================
  Coverage   87.91%   87.91%           
=======================================
  Files         402      402           
  Lines       13725    13775   +50     
  Branches     3998     4019   +21     
=======================================
+ Hits        12066    12110   +44     
- Misses       1352     1358    +6     
  Partials      307      307           
Flag Coverage Δ
unittest 87.91% <100.00%> (+<0.01%) ⬆️

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

Files Coverage Δ
...ges/eslint-plugin/src/rules/no-misused-promises.ts 97.51% <100.00%> (+0.32%) ⬆️

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.

Looks like a great start, nice! I haven't reviewed to deeply yet, just waiting on the missing test coverage in the file in case it needs some big changes (you never know). LMK if I'm off base though! ✨

@JoshuaKGoldberg JoshuaKGoldberg added the awaiting response Issues waiting for a reply from the OP or another party label Mar 25, 2024
@JoshuaKGoldberg
Copy link
Member

I realized the topic is more generalizable to heritage types in general

+1, this is a nice spot!

- checksVoidReturn sub-options each get their own subsection
- option descriptions go under Options subsection instead of Examples
@github-actions github-actions bot removed the awaiting response Issues waiting for a reply from the OP or another party label Jun 7, 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.

Sorry for the delay! I commented on the last remaining thread.

@github-actions github-actions bot removed the awaiting response Issues waiting for a reply from the OP or another party label Jul 17, 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.

I have been mollified. 😄

This really is a lovely PR with a super interesting bug case. Thanks for all your work on it @alythobani! 🙌

@JoshuaKGoldberg JoshuaKGoldberg added the 1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge label Jul 20, 2024
@alythobani
Copy link
Contributor Author

😁 Sweet, and thanks for such a detailed review @JoshuaKGoldberg! Really appreciate all the catches and suggestions and all the time you put into it! And for putting up with my overly long explanations 😇

condensing undefined and 0 check into one
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.

Looks great to me, thanks! Since @kirkwaiblinger was also reviewing, will wait a bit for Kirk to review.

kirkwaiblinger
kirkwaiblinger previously approved these changes Aug 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.

🚀 😁

if (memberName === undefined) {
// Call/construct/index signatures don't have names. TS allows call signatures to mismatch,
// and construct signatures can't be async.
// TODO - Once we're able to use `checker.isTypeAssignableTo` (v8), we can check an index
Copy link
Member

Choose a reason for hiding this comment

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

v8 is upon us! Though of course this PR has been in the works since long before that.

@alythobani, let's merge as-is, but would you mind filing a followup (that we now know will not be blocked 🙂)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sweet, yeah definitely! Do you mean filing an issue for this? And I can start looking into a proper implementation too if you'd like

@JoshuaKGoldberg JoshuaKGoldberg dismissed stale reviews from kirkwaiblinger and themself via 8304cef August 12, 2024 16:15
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.

All right!~ This is looking great! Thanks so much for working with us a bunch on it @alythobani. Your explanations were really helpful and I'm super happy where this went. Awesome job.

Merging now to be released later today in our weekly release! 🙌

Homer Simpson raising his fists up and yelling "Whoo-hoo!"

@JoshuaKGoldberg JoshuaKGoldberg merged commit 6a1c177 into typescript-eslint:main Aug 12, 2024
59 checks passed
@alythobani
Copy link
Contributor Author

Sorry again for the late replies here, life is a little hectic these days! But amaaazing, so good to see this merged! Thanks again so much for all the collaboration here @JoshuaKGoldberg and @kirkwaiblinger !!

Obviously please keep me updated on any issues that arise, but hopefully there won't be any :)

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge enhancement New feature or request
Projects
None yet
5 participants