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

chore: validate promise handling #11842

Merged
merged 10 commits into from
Jun 4, 2024
Merged

chore: validate promise handling #11842

merged 10 commits into from
Jun 4, 2024

Conversation

benmccann
Copy link
Member

@benmccann benmccann commented May 30, 2024

I don't know whether console.error is the best way to deal with these or not. Please feel free to fix this up if you have better ideas

https://typescript-eslint.io/rules/no-floating-promises/

I wanted to turn these rules on because they catch tons of errors in SvelteKit. Turning them on here as well will let us add them to our common config. I fixed several dozen errors in SvelteKit with several dozen left to go in sveltejs/kit#12284

Copy link

changeset-bot bot commented May 30, 2024

⚠️ No Changeset found

Latest commit: 7d5f7bb

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Member

@dummdidumm dummdidumm left a comment

Choose a reason for hiding this comment

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

Everything inside the Svelte packages should be left untouched. This is inside a browser. I don't really think we need that stuff there, it can't bring down your server or anything, it just increases bundle size for everyone for no real gain.

@dominikg
Copy link
Member

if we wanted to add a way to detect unhandled promise rejections, would it have to be in form of a hook? forced console.error are things people don't tend to like and then have to bring in bundlers that filter them.

@benmccann
Copy link
Member Author

benmccann commented Jun 1, 2024

While it can't bring down the server, it would make it pretty hard to deal if there was ever an error and it wasn't reported in any way. I ignored instances of failing that check for now. Is it worth filing an issue for the hook that @dominikg mentioned?

@benmccann benmccann force-pushed the floating-promises branch from 16aaa2a to 73eff42 Compare June 1, 2024 06:17
@benmccann benmccann changed the title chore: don't swallow rejected promise errors chore: validate promise handling Jun 1, 2024
@benmccann benmccann force-pushed the floating-promises branch from d758361 to 10ac6aa Compare June 1, 2024 16:30
@@ -20,7 +20,7 @@ export function slice_source(code_slice, offset, { file_basename, filename, get_
* @param {(...match: any[]) => Promise<MappedCode>} get_replacement
* @param {string} source
*/
function calculate_replacements(re, get_replacement, source) {
async function calculate_replacements(re, get_replacement, source) {
Copy link
Member

Choose a reason for hiding this comment

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

I don't understand. What is this for?

Copy link
Member Author

Choose a reason for hiding this comment

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

Because it returns a promise

Copy link
Member

Choose a reason for hiding this comment

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

so?

Copy link
Member

Choose a reason for hiding this comment

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

(to be clear: I understand that eslint fails because of @typescript-eslint/promise-function-async, but that rule seems particularly pointless even by the standards of the genre)

Copy link
Member

Choose a reason for hiding this comment

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

(in fact i'm pretty sure that making a function async unnecessarily creates runtime overhead. i just have no idea why we would do this to ourselves)

Copy link
Member

@Conduitry Conduitry Jun 3, 2024

Choose a reason for hiding this comment

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

All of the ones that would still return a promise but don't have an await in them, probably, yeah.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, absolutely. But to be honest I question the value of any of it. We need less naggy ESLint bullshit, not more. The fact that you have to add this...

// eslint-disable-next-line @typescript-eslint/no-floating-promises

...to tell the plugin that Promise.resolve() — of all things! — doesn't need a .catch handler tells you all you need to know about how much value it adds.

Copy link
Member

Choose a reason for hiding this comment

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

I still don't understand the benefit of leaving it off

As mentioned, it creates runtime overhead. AFAIK this will result in four promises being created rather than two:

async function get(thing) {
  return fetch(`${base}/${thing}`).then(async (r) => r.json());
}

Copy link
Member Author

Choose a reason for hiding this comment

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

...to tell the plugin that Promise.resolve() — of all things! — doesn't need a .catch handler tells you all you need to know about how much value it adds.

I believe that the issue isn't Promise.resolve(), but that an exception could be thrown in .then() and then it would be unhandled

Copy link
Member

Choose a reason for hiding this comment

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

Either way it's an annoying rule that we really don't need. I got rid of the ones that don't provide any value (or provide negative value, as in the case of promise-function-async)

@benmccann
Copy link
Member Author

@typescript-eslint/no-misused-promises and @typescript-eslint/await-thenable catch tons of stuff that TypeScript doesn't. They're not really catching anything in this project, but are catching tons in SvelteKit

@Rich-Harris
Copy link
Member

This is the sample from no-misused-promises with TypeScript by itself — it seems pretty redundant?

image

I can concede that await-thenable might occasionally catch issues, my initial understanding (because of the name) was that it was enforcing that thenables were awaited rather than that awaited things were thenables

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants