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

fix: only warn about literals and undefined on no-return-await #17363

Closed

Conversation

clshortfuse
Copy link
Contributor

In async functions, returning awaited promises no longer has any performance penalty and is now faster than returning promises directly.

Because we cannot detect what the result type of a CallExpression or the type of a MemberExpression, or the type of an Identifier, warnings related to whether an actual Promise is being returned cannot be processed. Strictly from at an AST level, we can detect Literal node types and undefined.

  • Detect await being used on Literal node type
  • Detect await being used on an Identifier named undefined
  • Detect await being used on a ConditionalExpression that returns either a Literal type or undefined.
  • Detect await being used on a void operation (results in undefined)

Futher changes can be made to detect syntaxes where a Literal type would be returned, such as

  • await (4 + 3)
  • await ('foo' + 'bar')

Future changes may also, suggest rewriting awaited CondtionExpression nodes such as:

  • async function foo(value) { return await (value ? true : bar()) }

This would require more extensive testing.

Fixes #17345

See: https://v8.dev/blog/fast-async

Prerequisites checklist

What is the purpose of this pull request? (put an "X" next to an item)

[ ] Documentation update
[ ] Bug fix (template)
[ ] New rule (template)
[x] Changes an existing rule (template)
[ ] Add autofix to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:

What changes did you make? (Give an overview)

(see commit message)

Is there anything you'd like reviewers to focus on?

It's impossible to properly assess the return type, but we can at least check for Literal nodes and undefined.

This keeps the rule being a suggestion, but relaxes type-related checks.

@clshortfuse clshortfuse requested a review from a team as a code owner July 13, 2023 15:02
@eslint-github-bot
Copy link

Hi @clshortfuse!, thanks for the Pull Request

The first commit message isn't properly formatted. We ask that you update the message to match this format, as we use it to generate changelogs and automate releases.

  • The commit message tag wasn't recognized. Did you mean "docs", "fix", or "feat"?
  • There should be a space following the initial tag and colon, for example 'feat: Message'.

To Fix: You can fix this problem by running git commit --amend, editing your commit message, and then running git push -f to update this pull request.

Read more about contributing to ESLint here

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Jul 13, 2023

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: clshortfuse / name: Carlos Lopez (4faa777)

@netlify
Copy link

netlify bot commented Jul 13, 2023

Deploy Preview for docs-eslint canceled.

Name Link
🔨 Latest commit 4faa777
🔍 Latest deploy log https://app.netlify.com/sites/docs-eslint/deploys/64b012b42b47440008aa5568

In async functions, returning awaited promises no longer has
any performance penalty and is now faster than returning promises
directly.

Because we cannot detect what the result type of a `CallExpression` or
the type of a `MemberExpression`, or the type of an `Identifier`,
warnings related to whether an actual Promise is being returned cannot
be processed. Strictly from at an AST level, we can detect `Literal`
node types and `undefined`.

* Detect await being used on `Literal` node type
* Detect await being used on an `Identifier` named `undefined`
* Detect await being used on a `ConditionalExpression` that returns
either a `Literal` type or `undefined`.
* Detect await being used on a void operation (results in `undefined`)

Futher changes can be made to detect syntaxes where a `Literal` type
would be returned, such as

* await (4 + 3)
* await ('foo' + 'bar')

Future changes may also, suggest rewriting awaited `CondtionExpression`
nodes such as:

* async function foo(value) { return await (value ? true : bar()) }

This would require more extensive testing.

See: https://v8.dev/blog/fast-async

Fixes eslint#17345
@eslint-github-bot eslint-github-bot bot added the bug ESLint is working incorrectly label Jul 13, 2023
@clshortfuse clshortfuse changed the title fix(no-return-await): only warn about literals and undefined fix: only warn about literals and undefined on no-return-await Jul 13, 2023
@nzakas
Copy link
Member

nzakas commented Jul 13, 2023

@clshortfuse thanks for the PR. In the issue you reference (#17345) it was agreed to make a documentation change, not a change to how the rule works. We'd happily accept a documentation change explaining the points you made. Please revert the changes to the rule itself and update the docs accordingly.

@github-actions
Copy link

Hi everyone, it looks like we lost track of this pull request. Please review and see what the next steps are. This pull request will auto-close in 7 days without an update.

@github-actions github-actions bot added the Stale label Jul 23, 2023
@mdjermanovic
Copy link
Member

Closing in favor of #17417

@eslint-github-bot eslint-github-bot bot locked and limited conversation to collaborators Jan 22, 2024
@eslint-github-bot eslint-github-bot bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Jan 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly Stale
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rule Change: return await is both faster and allows better error handling
3 participants