Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat(eslint-plugin): [await-thenable] check for-await loop iteree #10008

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

Conversation

kirkwaiblinger
Copy link
Member

@kirkwaiblinger kirkwaiblinger commented Sep 17, 2024

PR Checklist

Overview

Just ask the type checker if a Symbol.asyncIterator is present, ezpz. Add tests and docs (less ezpz).

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @kirkwaiblinger!

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.

## Async Iteration (`for await...of` Loops)

This rule also inspects [`for await...of` statements](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of), which are designed for iterating over async-iterable objects.
If the value being iterated over is not async-iterable, an ordinary `for...of` statement is preferable, even if the value is an iterable of Thenables.
Copy link
Member

Choose a reason for hiding this comment

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

This is debatable. Banning iterables of thenables should be an option.

Copy link
Member Author

Choose a reason for hiding this comment

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

Eh, kind of... in any case, I was going off of #8858 (comment). It's easy enough to add an option to allow iterables of thenables, though I'm -0.5 on doing so.

Copy link
Member

Choose a reason for hiding this comment

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

In lieu of an option, can we add more motivation to the docs (as what I said in that comment)?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point! Made some changes around this 👍

Copy link

codecov bot commented Sep 17, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 86.03%. Comparing base (4d31ebe) to head (7cd8ba7).
Report is 57 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #10008      +/-   ##
==========================================
+ Coverage   86.00%   86.03%   +0.02%     
==========================================
  Files         425      428       +3     
  Lines       14810    14930     +120     
  Branches     4308     4329      +21     
==========================================
+ Hits        12738    12845     +107     
- Misses       1723     1741      +18     
+ Partials      349      344       -5     
Flag Coverage Δ
unittest 86.03% <100.00%> (+0.02%) ⬆️

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

Files with missing lines Coverage Δ
packages/eslint-plugin/src/rules/await-thenable.ts 100.00% <100.00%> (ø)

... and 21 files with indirect coverage changes

Copy link

netlify bot commented Sep 17, 2024

Deploy Preview for typescript-eslint failed.

Name Link
🔨 Latest commit 353bb30
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/66e9fc172d3e3c0008abbb17

Copy link

netlify bot commented Sep 17, 2024

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit 7cd8ba7
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/66facd2e21b039000844ab8d
😎 Deploy Preview https://deploy-preview-10008--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 1 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.

// This suggestion causes broken code for sync iterables of promises, since
// the loop variable will not be awaited.
//
// Ideally, if the iterable yields promises, we would offer a suggestion to
Copy link
Member Author

Choose a reason for hiding this comment

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

bleh - I guess this isn't always a good idea, particularly with possibly infinite iterables. But, then again, why would you have an infinite sync iterable of promises? Thinking out loud.

@kirkwaiblinger kirkwaiblinger added the enhancement New feature or request label Sep 18, 2024
context.report({
loc: getForStatementHeadLoc(context.sourceCode, node),
messageId: 'forAwaitOfNonThenable',
suggest: [
Copy link
Member

Choose a reason for hiding this comment

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

Can we only offer this suggestion if the iterator result is not thenable?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think so because we'd need getIterationTypesOfIterable() or getIteratedTypeOrElementType or similar from TS none of which is exposed on the checker.

If we check arrays and tuples specifically, i.e. known iterables, this is silly since

for (const promisedValue of await Promise.all(promises)) {
}

is a much better suggestion in those cases (since awaiting in the loop is generally problematic for exception handling given a non-lazy array of promises)

Whereas with lazy sync-iterables of promises, which I don't know if we can detect, it may be better to do

for (const promise of yieldPromises()) {
    const promisedValue = await promise;
}

I guess the only thing that's unambiguous here is that if you have an array or tuple of non-thenables, this suggestion is good. Maybe that's the only situation where we give a suggestion?

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 am wholly satisfied with this, except for the one line of missing test coverage. Then we're good to go IMO! 🚀

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

🚢

@JoshuaKGoldberg JoshuaKGoldberg merged commit b121bd9 into typescript-eslint:main Sep 30, 2024
60 of 61 checks passed
@kirkwaiblinger kirkwaiblinger deleted the for-await-thenable branch September 30, 2024 16:59
@phaux
Copy link
Contributor

phaux commented Oct 2, 2024

wrong rule in title lol

@kirkwaiblinger
Copy link
Member Author

wrong rule in title lol

smh 😞. Changing the title and release notes, thanks for pointing this out!

@kirkwaiblinger kirkwaiblinger changed the title feat(eslint-plugin): [return-await] check for-await loop iteree feat(eslint-plugin): [await-thenable] check for-await loop iteree Oct 2, 2024
@haines
Copy link
Contributor

haines commented Oct 7, 2024

I think this can result in false positives: #10111

Edit: sorry, I see you're already aware of this from #10080

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enhancement: [await-thenable] should check that for-await loop is used on an async iterable
5 participants