Skip to content

Rule proposal: check that function is not returning promise and non-promise #9296

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

Open
6 tasks done
benmccann opened this issue Jun 6, 2024 · 4 comments
Open
6 tasks done
Labels
accepting prs Go ahead, send a pull request that resolves this issue enhancement: new plugin rule New rule request for eslint-plugin package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@benmccann
Copy link
Contributor

Before You File a Proposal Please Confirm You Have Done The Following...

My proposal is suitable for this project

  • My proposal specifically checks TypeScript syntax, or it proposes a check that requires type information to be accurate.
  • My proposal is not a "formatting rule"; meaning it does not just enforce how code is formatted (whitespace, brace placement, etc).
  • I believe my proposal would be useful to the broader TypeScript community (meaning it is not a niche proposal).

Description

We have a number of public APIs in our project that return things like string | Promise<string>. In fact, we have so many of these that we have a helper type called MaybePromise. I believe it would be better if these functions always returned a Promise. It would make a nicer API for callers because they would only have to worry about handling rejected promise errors and would not have to worry about errors that are thrown as well

I probably wouldn't want to apply this check to our whole project as internally we can have errors propagate in many functions, but I'd love to apply it to our public type definition file (.d.ts).

Fail Cases

export interface Emulator {
	platform(details: { config: any; prerender: PrerenderOption }): App.Platform | Promise<App.Platform>;
}

Pass Cases

export interface Emulator {
	platform(details: { config: any; prerender: PrerenderOption }): Promise<App.Platform>;
}

Additional Info

promise-function-async has a somewhat similar goal, but doesn't work for me because it ignores methods with explicit return types, which is one of the things I'd like to detect and disallow. It also has a ton of false positives on my existing codebase where it's almost always fine for me to just propagate errors upwards

@benmccann benmccann added enhancement: new plugin rule New rule request for eslint-plugin package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Jun 6, 2024
@bradzacher
Copy link
Member

You could probably get most of the way there with no-restricted-syntax eg playground

@Josh-Cena
Copy link
Member

Maybe this can be an option of promise-function-async? I also think promise-function-async is generally useful beyond error handling, as it prevents a function to be maybe-synchronous, which is often bad for side effect ordering.

@bradzacher
Copy link
Member

promise-function-async will do this for function implementations if and only if the return type isn't annotated.

But it doesn't do it for function types or method signatures - only implementations.

A separate rule might be good here to just enforce globally for all function cases. It might be good to have two modes - one where it just looks for | Promise<T> in a return type and errors (eg the super strict "if you return a promise at all the entire function must only return a promise") and one where its a bit looser and only looks for T | Promise<T>.

@kirkwaiblinger kirkwaiblinger added accepting prs Go ahead, send a pull request that resolves this issue and removed triage Waiting for team members to take a look labels Jun 11, 2024
@kirkwaiblinger
Copy link
Member

Looks like there's generally positive response from 3 team members, so marking as accepting PRs 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepting prs Go ahead, send a pull request that resolves this issue enhancement: new plugin rule New rule request for eslint-plugin package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

No branches or pull requests

4 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy