From 57950204031c4465fbba9c2987b2bf841d447fa6 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Fri, 4 Aug 2023 16:08:10 +0800 Subject: [PATCH] style: add no-misused-promises rule (#3547) Co-authored-by: Peng Xiao --- .eslintrc.js | 2 ++ apps/electron/src/helper/db/ensure-db.ts | 6 +++- apps/electron/src/main/index.ts | 8 +++-- apps/electron/src/main/ui/google-auth.ts | 2 +- .../src/main/updater/electron-updater.ts | 6 ++-- apps/electron/src/worker/plugin.ts | 5 +++ .../server/src/modules/workspaces/resolver.ts | 2 +- apps/server/src/prisma/service.ts | 6 ++-- .../components/notification-center/index.tsx | 4 ++- .../page-list/view/collection-bar.tsx | 13 +++++--- .../page-list/view/collection-list.tsx | 31 ++++++++++++------- .../page-list/view/create-collection.tsx | 21 +++++++++---- .../src/components/share-menu/share-page.tsx | 12 +++++-- packages/component/src/ui/toast/toast.ts | 25 +++++++++------ packages/i18n/src/scripts/download.ts | 31 +++++++++---------- packages/i18n/src/scripts/sync.ts | 4 +-- packages/infra/src/preload/electron.ts | 2 +- .../src/providers/sqlite-providers.ts | 15 ++++----- plugins/copilot/src/UI/detail-content.tsx | 9 ++++-- 19 files changed, 130 insertions(+), 74 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index d3285c0b80..155a3ab0dd 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -214,6 +214,7 @@ const config = { ignoreIIFE: false, }, ], + '@typescript-eslint/no-misused-promises': ['error'], }, })), { @@ -239,6 +240,7 @@ const config = { }, ], '@typescript-eslint/no-floating-promises': 0, + '@typescript-eslint/no-misused-promises': 0, }, }, ], diff --git a/apps/electron/src/helper/db/ensure-db.ts b/apps/electron/src/helper/db/ensure-db.ts index 966adfe4e2..dcd099d9ab 100644 --- a/apps/electron/src/helper/db/ensure-db.ts +++ b/apps/electron/src/helper/db/ensure-db.ts @@ -115,7 +115,11 @@ function startPollingSecondaryDB(db: WorkspaceSQLiteDB) { const secondaryDB = new SecondaryWorkspaceSQLiteDB(path, db); return new Observable(subscriber => { subscriber.next(secondaryDB); - return () => secondaryDB.destroy(); + return () => { + secondaryDB.destroy().catch(err => { + subscriber.error(err); + }); + }; }); }), switchMap(secondaryDB => { diff --git a/apps/electron/src/main/index.ts b/apps/electron/src/main/index.ts index 34c61f11d7..499daa3f6c 100644 --- a/apps/electron/src/main/index.ts +++ b/apps/electron/src/main/index.ts @@ -38,7 +38,7 @@ app.on('second-instance', () => { ); }); -app.on('open-url', async (_, _url) => { +app.on('open-url', (_, _url) => { // todo: handle `affine://...` urls }); @@ -54,7 +54,11 @@ app.on('window-all-closed', () => { /** * @see https://www.electronjs.org/docs/v14-x-y/api/app#event-activate-macos Event: 'activate' */ -app.on('activate', restoreOrCreateWindow); +app.on('activate', () => { + restoreOrCreateWindow().catch(err => { + console.error(err); + }); +}); /** * Create app window when background process will be ready diff --git a/apps/electron/src/main/ui/google-auth.ts b/apps/electron/src/main/ui/google-auth.ts index be929f40fd..833220bc89 100644 --- a/apps/electron/src/main/ui/google-auth.ts +++ b/apps/electron/src/main/ui/google-auth.ts @@ -34,7 +34,7 @@ export function getGoogleOauthCode() { logger.error('Failed to open external url', e); reject(e); }); - const handleOpenUrl = async (_: any, url: string) => { + const handleOpenUrl = (_: any, url: string) => { const mainWindow = BrowserWindow.getAllWindows().find( w => !w.isDestroyed() ); diff --git a/apps/electron/src/main/updater/electron-updater.ts b/apps/electron/src/main/updater/electron-updater.ts index 975be9e8c1..b1c2030069 100644 --- a/apps/electron/src/main/updater/electron-updater.ts +++ b/apps/electron/src/main/updater/electron-updater.ts @@ -99,7 +99,9 @@ export const registerUpdater = async () => { }); autoUpdater.forceDevUpdateConfig = isDev; - app.on('activate', async () => { - await checkForUpdates(false); + app.on('activate', () => { + checkForUpdates(false).catch(err => { + console.error(err); + }); }); }; diff --git a/apps/electron/src/worker/plugin.ts b/apps/electron/src/worker/plugin.ts index 3597091860..18323ac6d4 100644 --- a/apps/electron/src/worker/plugin.ts +++ b/apps/electron/src/worker/plugin.ts @@ -19,10 +19,15 @@ const mainThread = AsyncCall<{ channel: new MessageEventChannel(parentPort), }); +// eslint-disable-next-line @typescript-eslint/no-misused-promises globalThis.console.log = mainThread.log; +// eslint-disable-next-line @typescript-eslint/no-misused-promises globalThis.console.error = mainThread.log; +// eslint-disable-next-line @typescript-eslint/no-misused-promises globalThis.console.info = mainThread.log; +// eslint-disable-next-line @typescript-eslint/no-misused-promises globalThis.console.debug = mainThread.log; +// eslint-disable-next-line @typescript-eslint/no-misused-promises globalThis.console.warn = mainThread.log; // eslint-disable-next-line @typescript-eslint/no-var-requires diff --git a/apps/server/src/modules/workspaces/resolver.ts b/apps/server/src/modules/workspaces/resolver.ts index 98d1e1bacf..aae264327d 100644 --- a/apps/server/src/modules/workspaces/resolver.ts +++ b/apps/server/src/modules/workspaces/resolver.ts @@ -77,7 +77,7 @@ export class WorkspaceResolver { return workspace.permission; } - const permission = this.permissionProvider.get(workspace.id, user.id); + const permission = await this.permissionProvider.get(workspace.id, user.id); if (!permission) { throw new ForbiddenException(); diff --git a/apps/server/src/prisma/service.ts b/apps/server/src/prisma/service.ts index 59b9aad992..60e0875cf6 100644 --- a/apps/server/src/prisma/service.ts +++ b/apps/server/src/prisma/service.ts @@ -9,8 +9,10 @@ export class PrismaService extends PrismaClient implements OnModuleInit { } async enableShutdownHooks(app: INestApplication) { - process.on('beforeExit', async () => { - await app.close(); + process.on('beforeExit', () => { + app.close().catch(e => { + console.error(e); + }); }); } } diff --git a/packages/component/src/components/notification-center/index.tsx b/packages/component/src/components/notification-center/index.tsx index d29c062c72..8c21e8a193 100644 --- a/packages/component/src/components/notification-center/index.tsx +++ b/packages/component/src/components/notification-center/index.tsx @@ -181,7 +181,9 @@ function NotificationCard(props: NotificationCardProps): ReactElement { const onClickUndo = useCallback(() => { if (notification.undo) { - return notification.undo(); + notification.undo().catch(err => { + console.error(err); + }); } return void 0; }, [notification]); diff --git a/packages/component/src/components/page-list/view/collection-bar.tsx b/packages/component/src/components/page-list/view/collection-bar.tsx index ae0d2aa882..c5635118c4 100644 --- a/packages/component/src/components/page-list/view/collection-bar.tsx +++ b/packages/component/src/components/page-list/view/collection-bar.tsx @@ -63,10 +63,14 @@ export const CollectionBar = (props: CollectionBarProps) => { : t['com.affine.collection-bar.action.tooltip.pin'](), className: styles.pin, click: () => { - return setting.updateCollection({ - ...collection, - pinned: !collection.pinned, - }); + setting + .updateCollection({ + ...collection, + pinned: !collection.pinned, + }) + .catch(err => { + console.error(err); + }); }, }, { @@ -102,6 +106,7 @@ export const CollectionBar = (props: CollectionBarProps) => { init={collection} open={open} onClose={onClose} + // eslint-disable-next-line @typescript-eslint/no-misused-promises onConfirm={setting.updateCollection} > , name: 'pin', click: () => { - return setting.updateCollection({ - ...collection, - pinned: !collection.pinned, - }); + setting + .updateCollection({ + ...collection, + pinned: !collection.pinned, + }) + .catch(err => { + console.error(err); + }); }, }, { @@ -144,10 +148,14 @@ export const CollectionList = ({ const [collection, setCollection] = useState(); const onChange = useCallback( (filterList: Filter[]) => { - return setting.updateCollection({ - ...setting.currentCollection, - filterList, - }); + setting + .updateCollection({ + ...setting.currentCollection, + filterList, + }) + .catch(err => { + console.error(err); + }); }, [setting] ); @@ -156,10 +164,9 @@ export const CollectionList = ({ [] ); const onConfirm = useCallback( - (view: Collection) => { - return setting.updateCollection(view).then(() => { - closeUpdateCollectionModal(); - }); + async (view: Collection) => { + await setting.updateCollection(view); + closeUpdateCollectionModal(); }, [closeUpdateCollectionModal, setting] ); diff --git a/packages/component/src/components/page-list/view/create-collection.tsx b/packages/component/src/components/page-list/view/create-collection.tsx index 81e92dba71..ec4ea58d53 100644 --- a/packages/component/src/components/page-list/view/create-collection.tsx +++ b/packages/component/src/components/page-list/view/create-collection.tsx @@ -32,7 +32,7 @@ type CreateCollectionProps = { }; type SaveCollectionButtonProps = { - onConfirm: (collection: Collection) => void; + onConfirm: (collection: Collection) => Promise; getPageInfo: GetPageInfoById; propertiesMeta: PropertiesMeta; filterList: Filter[]; @@ -49,7 +49,7 @@ export const EditCollectionModel = ({ title, }: { init?: Collection; - onConfirm: (view: Collection) => void; + onConfirm: (view: Collection) => Promise; open: boolean; onClose: () => void; title?: string; @@ -57,6 +57,18 @@ export const EditCollectionModel = ({ propertiesMeta: PropertiesMeta; }) => { const t = useAFFiNEI18N(); + const onConfirmOnCollection = useCallback( + (view: Collection) => { + onConfirm(view) + .then(() => { + onClose(); + }) + .catch(err => { + console.error(err); + }); + }, + [onClose, onConfirm] + ); return ( { - onConfirm(view); - onClose(); - }} + onConfirm={onConfirmOnCollection} /> ) : null} diff --git a/packages/component/src/components/share-menu/share-page.tsx b/packages/component/src/components/share-menu/share-page.tsx index b182b2b72f..65eaab701c 100644 --- a/packages/component/src/components/share-menu/share-page.tsx +++ b/packages/component/src/components/share-menu/share-page.tsx @@ -47,9 +47,15 @@ export const AffineSharePage: FC = props => { const onClickCreateLink = useCallback(() => { setIsPublic(true); }, [setIsPublic]); - const onClickCopyLink = useCallback(async () => { - await navigator.clipboard.writeText(sharingUrl); - toast(t['Copied link to clipboard']()); + const onClickCopyLink = useCallback(() => { + navigator.clipboard + .writeText(sharingUrl) + .then(() => { + toast(t['Copied link to clipboard']()); + }) + .catch(err => { + console.error(err); + }); }, [sharingUrl, t]); const onDisablePublic = useCallback(() => { setIsPublic(false); diff --git a/packages/component/src/ui/toast/toast.ts b/packages/component/src/ui/toast/toast.ts index 7d21a113d0..b858dcf9d6 100644 --- a/packages/component/src/ui/toast/toast.ts +++ b/packages/component/src/ui/toast/toast.ts @@ -115,21 +115,26 @@ export const toast = ( element.animate(fadeIn, options); - setTimeout(async () => { + setTimeout(() => { const animation = element.animate( // fade out fadeIn.reverse(), options ); - await animation.finished; - element.style.maxHeight = '0'; - element.style.margin = '0'; - element.style.padding = '0'; - // wait for transition - // ToastContainer = null; - element.addEventListener('transitionend', () => { - element.remove(); - }); + animation.finished + .then(() => { + element.style.maxHeight = '0'; + element.style.margin = '0'; + element.style.padding = '0'; + // wait for transition + // ToastContainer = null; + element.addEventListener('transitionend', () => { + element.remove(); + }); + }) + .catch(err => { + console.error(err); + }); }, duration); return element; }; diff --git a/packages/i18n/src/scripts/download.ts b/packages/i18n/src/scripts/download.ts index df652d2e32..ecdcf14b34 100644 --- a/packages/i18n/src/scripts/download.ts +++ b/packages/i18n/src/scripts/download.ts @@ -87,23 +87,22 @@ const main = async () => { language => language.completeRate > 0.4 ); - availableLanguages + for (const language of availableLanguages // skip base language - .filter(i => !i.base) - .forEach(async language => { - await fs.writeFile( - path.resolve(RES_DIR, `${language.tag}.json`), - JSON.stringify( - { - '// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.': - '', - ...flattenTranslation(language.translations), - }, - null, - INDENT - ) + '\n' - ); - }); + .filter(i => !i.base)) { + await fs.writeFile( + path.resolve(RES_DIR, `${language.tag}.json`), + JSON.stringify( + { + '// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.': + '', + ...flattenTranslation(language.translations), + }, + null, + INDENT + ) + '\n' + ); + } console.log('Generating meta data...'); const code = `// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. diff --git a/packages/i18n/src/scripts/sync.ts b/packages/i18n/src/scripts/sync.ts index 755240931a..9415a92b11 100644 --- a/packages/i18n/src/scripts/sync.ts +++ b/packages/i18n/src/scripts/sync.ts @@ -127,11 +127,11 @@ const main = async () => { return; } - diff.add.forEach(async key => { + for (const key of diff.add) { const val = flatLocalTranslations[key]; console.log(`Creating new key: ${key} -> ${val}`); await createsNewKey(key, { [BASE_LANGUAGES]: val }); - }); + } // TODO remove unused tags from used keys diff --git a/packages/infra/src/preload/electron.ts b/packages/infra/src/preload/electron.ts index bd6695d1da..60229a1ee5 100644 --- a/packages/infra/src/preload/electron.ts +++ b/packages/infra/src/preload/electron.ts @@ -116,7 +116,7 @@ function getMainAPIs() { } const helperPort$ = new Promise(resolve => - ipcRenderer.on('helper-connection', async e => { + ipcRenderer.on('helper-connection', e => { console.info('[preload] helper-connection', e); resolve(e.ports[0]); }) diff --git a/packages/workspace/src/providers/sqlite-providers.ts b/packages/workspace/src/providers/sqlite-providers.ts index 29d2915060..a05e9929ba 100644 --- a/packages/workspace/src/providers/sqlite-providers.ts +++ b/packages/workspace/src/providers/sqlite-providers.ts @@ -106,14 +106,15 @@ export const createSQLiteDBDownloadProvider: DocProviderCreator = ( cleanup: () => { // todo }, - sync: async () => { + sync: () => { logger.info('connect sqlite download provider', id); - try { - await syncUpdates(rootDoc); - _resolve(); - } catch (error) { - _reject(error); - } + syncUpdates(rootDoc) + .then(() => { + _resolve(); + }) + .catch(error => { + _reject(error); + }); }, }; }; diff --git a/plugins/copilot/src/UI/detail-content.tsx b/plugins/copilot/src/UI/detail-content.tsx index ed05b87506..4e66e6f6da 100644 --- a/plugins/copilot/src/UI/detail-content.tsx +++ b/plugins/copilot/src/UI/detail-content.tsx @@ -34,9 +34,12 @@ const Actions = () => { /> { - await call(input); - await generateFollowingUp(); + onClick={useCallback(() => { + call(input) + .then(() => generateFollowingUp()) + .catch(e => { + console.error(e); + }); }, [call, generateFollowingUp, input])} >