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

Enhancement: [promise-function-async] flag non-async functions returning unions with non-Promise types #6329

Closed
4 tasks done
ericbf opened this issue Jan 11, 2023 · 3 comments · Fixed by #6330
Closed
4 tasks done
Assignees
Labels
enhancement: plugin rule option New rule option for an existing eslint-plugin rule package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look

Comments

@ericbf
Copy link
Contributor

ericbf commented Jan 11, 2023

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

My proposal is suitable for this project

  • I believe my proposal would be useful to the broader TypeScript community (meaning it is not a niche proposal).

Link to the rule's documentation

https://typescript-eslint.io/rules/promise-function-async/

Description

I am finding cases in my code where it accidentally is returning undefined or some other guard type (null, etc) from a function that we expected to be async, but the rule promise-function-async is not flagging those as I expected it to (allowing us to catch those mistakes).

Please add an option to promise-function-async that will allow it to flag any function that returns promises at all, either as the main return type, or as a union containing promises.

Fail

function getSomething(id?: string) {
    if (!id) {
        return undefined
    }

    return db.getByID(id)
}

Pass

async function getSomething(id?: string) {
    if (!id) {
        return undefined
    }

    return db.getByID(id)
}

Additional Info

There is an issue for the opposite of my request, #1005. I’d like that feature back (probably behind an option, seeing as it was designated as a bug before).

@ericbf ericbf added enhancement: plugin rule option New rule option for an existing eslint-plugin rule package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look labels Jan 11, 2023
@bradzacher
Copy link
Member

I wonder if there's a heuristic we can apply here to avoid the need for an option.

Like "if the function has an explicit or contextual return type that is not Promise<T>, then ignore it, else if the implicit return type includes Promise at all, report it"

@ericbf
Copy link
Contributor Author

ericbf commented Jan 11, 2023

If it has an explicit type that includes non-promises, that would make sense, sure. The real issue, I suppose, is when the implicit type includes a union with non-promises, it should still be flagged, imo.

ericbf added a commit to ericbf/typescript-eslint that referenced this issue Jan 12, 2023
When we return a union containing a promise from a function
implicitly, it's often a mistake. This commit makes it so if the
return type is explicit, any `Promise` in the return type (whether
it's part of a union or not) will flag the function. If it is
intentional, make the return type explicit.

Fixes typescript-eslint#6329
@ericbf
Copy link
Contributor Author

ericbf commented Jan 12, 2023

@bradzacher I put up a PR implementing the heuristic you suggested. Take a look and let me know what you think, or of any suggestions.

@ericbf ericbf changed the title Enhancement: [promise-function-async] option to report when returning union containing promise Enhancement: [promise-function-async] flag non-async functions returning unions with non-Promise types Jan 12, 2023
ericbf added a commit to ericbf/typescript-eslint that referenced this issue Jan 12, 2023
When we return a union containing a promise from a function
implicitly, it's often a mistake. This commit makes it so if the
return type is explicit, any `Promise` in the return type (whether
it's part of a union or not) will flag the function. If it is
intentional, make the return type explicit.

Fixes typescript-eslint#6329
ericbf added a commit to ericbf/typescript-eslint that referenced this issue Feb 10, 2023
When we return a union containing a promise from a function
implicitly, it's often a mistake. This commit makes it so if the
return type is explicit, any `Promise` in the return type (whether
it's part of a union or not) will flag the function. If it is
intentional, make the return type explicit.

Fixes typescript-eslint#6329
bradzacher pushed a commit that referenced this issue Feb 20, 2023
…mplicit return types (#6330)

* [promise-function-async] Only allow unions in explicit return types

When we return a union containing a promise from a function
implicitly, it's often a mistake. This commit makes it so if the
return type is explicit, any `Promise` in the return type (whether
it's part of a union or not) will flag the function. If it is
intentional, make the return type explicit.

Fixes #6329

* Refrain from renaming the type-util, instead add an optional fourth param.
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 28, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement: plugin rule option New rule option for an existing eslint-plugin rule package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look
Projects
None yet
2 participants