8000 feat(eslint-plugin): [no-unsafe-return] check promise any by yeonjuan · Pull Request #8693 · typescript-eslint/typescript-eslint · GitHub
[go: up one dir, main page]

Skip to content

feat(eslint-plugin): [no-unsafe-return] check promise any #8693

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

Merged
merged 22 commits into from
Aug 12, 2024

Conversation

yeonjuan
Copy link
Contributor

PR Checklist

Overview

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @yeonjuan!

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.

@yeonjuan yeonjuan marked this pull request as draft March 17, 2024 07:52
Copy link
netlify bot commented Mar 17, 2024

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit 006d16c
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/669e79e1b9ebfd00092dabdb
😎 Deploy Preview https://deploy-preview-8693--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: 100 (🟢 up 6 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.

@yeonjuan yeonjuan marked this pull request as ready for review April 10, 2024 08:30
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.

A good start! Curious to see what ends up changing in order to support the added test cases.

And a thought - these are realllly tricky. I spent a lot of time myself getting confused over whether the code I was trying out actually should pass or fail 😆.

So, I wonder if we can help the user out by supplying a custom error message for the async cases that don't directly return any, but whose type evaluates to any after being Awaited? Feel free to ignore this if it's a hassle to implement. But, my thought would be something like "Unsafe return of type Promise<any> that is equivalent to simply returning any within an async function." I'm curious for your thoughts. And, as said, it's completely ok if you would prefer to ignore this idea!

@kirkwaiblinger kirkwaiblinger added the awaiting response Issues waiting for a reply from the OP or another party label Apr 21, 2024
messageId: 'unsafeReturn',
line: 4,
column: 3,
endColumn: 16,
Copy link
Member

Choose a reason for hiding this comment

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

(nit, optional) If you're gonna include the endColumn, might as well include the endLine too for consistency. Here and elsewhere

@yeonjuan
Copy link
Contributor Author

So, I wonder if we can help the user out by supplying a custom error message for the async cases that don't directly return any, but whose type evaluates to any after being Awaited? Feel free to ignore this if it's a hassle to implement. But, my thought would be something like "Unsafe return of type Promise that is equivalent to simply returning any within an async function." I'm curious for your thoughts. And, as said, it's completely ok if you would prefer to ignore this idea!

@kirkwaiblinger I agree, this is great, but how about making the error message a little more concise? "Unsafe return of type Promise<any> typed value"

@kirkwaiblinger
Copy link
Member

So, I wonder if we can help the user out by supplying a custom error message for the async cases that don't directly return any, but whose type evaluates to any after being Awaited? Feel free to ignore this if it's a hassle to implement. But, my thought would be something like "Unsafe return of type Promise that is equivalent to simply returning any within an async function." I'm curious for your thoughts. And, as said, it's completely ok if you would prefer to ignore this idea!

@kirkwaiblinger I agree, this is great, but how about making the error message a little more concise? "Unsafe return of type Promise<any> typed value"

👍 👍

# Conflicts:
#	packages/eslint-plugin/tests/rules/no-unsafe-return.test.ts
Copy link
codecov bot commented Apr 30, 2024

Codecov Report

Attention: Patch coverage is 71.42857% with 8 lines in your changes missing coverage. Please review.

Project coverage is 88.42%. Comparing base (d32d8f4) to head (006d16c).
Report is 158 commits behind head on main.

Files Patch % Lines
packages/type-utils/src/predicates.ts 0.00% 8 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8693      +/-   ##
==========================================
- Coverage   88.45%   88.42%   -0.03%     
==========================================
  Files         422      422              
  Lines       14695    14717      +22     
  Branches     4298     4308      +10     
==========================================
+ Hits        12998    13014      +16     
- Misses       1372     1378       +6     
  Partials      325      325              
Flag Coverage Δ
unittest 88.42% <71.42%> (-0.03%) ⬇️

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

Files Coverage Δ
...ckages/eslint-plugin/src/rules/no-unsafe-return.ts 97.43% <100.00%> (+0.66%) ⬆️
packages/type-utils/src/predicates.ts 21.21% <0.00%> (-2.13%) ⬇️

@yeonjuan
Copy link
Contributor Author
yeonjuan commented May 1, 2024

CI is failing, I created a separate PR to fix it. #9037

@yeonjuan yeonjuan requested a review from kirkwaiblinger May 3, 2024 11:15
@github-actions github-actions bot removed the awaiting response Issues waiting for a reply from the OP or another party label May 3, 2024
@yeonjuan
Copy link
Contributor Author
yeonjuan commented May 3, 2024

@kirkwaiblinger Thank you for your review. I added test cases and improved messsage.

@bradzacher bradzacher added the enhancement New feature or request 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.

Hi @yeonjuan!

First off, I want to say I am very sorry for how long it's taken me to get to another review pass on this!

This is great stuff. A few more tweaks and questions for iteration, but I feel like we're getting close 🙂

? 'any'
: anyType === AnyType.AnyArray
? 'any[]'
: 'Promise<any>',
Copy link
Member

Choose a reason for hiding this comment

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

Nit - not your fault, but this now runs into an "a" vs "an" issue in the generated string. I get
Unsafe return of an `Promise<any>` typed value

Would you change the wording a bit so that it's grammatically consistent?
For example, Unsafe return of a value of type {{type}}.

@@ -123,24 +124,37 @@ export function isTypeUnknownArrayType(

export enum AnyType {
Any,
PromiseAny,
AnyArray,
Safe,
}
/**
* @returns `AnyType.Any` if the type is `any`, `AnyType.AnyArray` if the type is `any[]` or `readonly any[]`,
Copy link
Member

Choose a reason for hiding this comment

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

nit - comment will need updating

@kirkwaiblinger kirkwaiblinger added the awaiting response Issues waiting for a reply from the OP or another party label Jun 11, 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.

Only blocker remaining is resolution of #8693 (comment), I think

@kirkwaiblinger kirkwaiblinger added the awaiting response Issues waiting for a reply from the OP or another party label Jul 15, 2024
@@ -22,6 +22,10 @@ declare module 'typescript' {
* Return the type of the given property in the given type, or undefined if no such property exists
*/
getTypeOfPropertyOfType(type: Type, propertyName: string): Type | undefined;
/**
* Return the awaited type of the given type.
Copy link
Member

Choose a reason for hiding this comment

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

F438

Maybe add a TODO that this should no longer be necessary in a future TS version and link the TS issue?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure! fcca5ec

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.

Yay! 🚀

@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
Co-authored-by: Josh Goldberg ✨ <git@joshuakgoldberg.com>
{
code: `
declare const value: Promise<any>;
function foo() {
Copy link
Member

Choose a reason for hiding this comment

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

I thought this is supposed to be valid, since it's not an async function, according to #8674 (comment). Did that decision get changed?

Copy link
Member

Choose a reason for hiding this comment

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

Note also that the error message for this case "Unsafe return of an Promise<any> typed value." is problematic, same as #8693 (comment)

Copy link
Contributor Author
@yeonjuan yeonjuan Jul 22, 2024

Choose a reason for hiding this comment

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

I've fixed! I had forgotten about those comments.

@auvred auvred removed the awaiting response Issues waiting for a reply from the OP or another party label Jul 22, 2024
auvred
auvred previously approved these changes Jul 22, 2024
Copy link
Member
@auvred auvred 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!

UPD: There seems to be a merge conflict with #9540.

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.

Left a couple questions

@auvred auvred added the awaiting response Issues waiting for a reply from the OP or another party label Jul 22, 2024
@JoshuaKGoldberg JoshuaKGoldberg removed the 1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge label Jul 29, 2024
@yeonjuan yeonjuan requested a review from kirkwaiblinger August 1, 2024 13:34
@github-actions github-actions bot removed the awaiting response Issues waiting for a reply from the OP or another party label 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.

Yay!

@kirkwaiblinger kirkwaiblinger added the 1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge label Aug 1, 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.

Person in bear costume breakdancing with a boombox on a street

@JoshuaKGoldberg JoshuaKGoldberg merged commit c20bd2f into typescript-eslint:main Aug 12, 2024
67 of 68 checks passed
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 20, 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
Development

Successfully merging this pull request may close these issues.

Enhancement: [no-unsafe-return] Disallow return Promise<any>
5 participants
0