internal: Add lints for promises (#3733)

* Add lints for floating and misued promises

* Add logging async errors in main

* Add async error catch in test-utils

* Change any to unknown

* Trap promise errors and ignore more await warnings

* Add more ignores and toSync helper

* Fix more lint warnings

* Add more ignores and fixes

* Add more reject reporting

* Add accepting arbitrary parameters to toSync()

* Fix more lints

* Revert unintentional change to non-arrow function

* Revert unintentional change to use arrow function

* Fix new warnings in main with auto updater

* Fix formatting

* Change lints to error

This is what the recommended type checked rules do.

* Fix to properly report promise rejections

* Fix formatting

* Fix formatting

* Remove unused import

* Remove unused convenience function

* Move type helpers

* Fix to not return promise when caller doesn't expect it

* Add ignores to lsp code
This commit is contained in:
Jonathan Tran
2024-09-09 18:17:45 -04:00
committed by GitHub
parent 0a72d7a39a
commit 25443eba31
59 changed files with 786 additions and 528 deletions

View File

@ -3,6 +3,7 @@ import { SourceRange } from '../lang/wasm'
import { v4 } from 'uuid'
import { isDesktop } from './isDesktop'
import { AnyMachineSnapshot } from 'xstate'
import { AsyncFn } from './types'
export const uuidv4 = v4
@ -106,6 +107,28 @@ export function deferExecution<T>(func: (args: T) => any, wait: number) {
return deferred
}
/**
* Wrap an async function so that it can be called in a sync context, catching
* rejections.
*
* It's common to want to run an async function in a sync context, like an event
* handler or callback. But we want to catch errors.
*
* Note: The returned function doesn't block. This isn't magic.
*
* @param onReject This callback type is from Promise.prototype.catch.
*/
export function toSync<F extends AsyncFn<F>>(
fn: F,
onReject: (
reason: any
) => void | PromiseLike<void | null | undefined> | null | undefined
): (...args: Parameters<F>) => void {
return (...args: Parameters<F>) => {
fn(...args).catch(onReject)
}
}
export function getNormalisedCoordinates({
clientX,
clientY,