Skip to content

Commit

Permalink
refactor: add eslint rule for unused async / await (toeverything#6231)
Browse files Browse the repository at this point in the history
  • Loading branch information
AyushAgrawal-A2 authored Feb 20, 2024
1 parent 2201798 commit 0c0a8b1
Show file tree
Hide file tree
Showing 104 changed files with 489 additions and 526 deletions.
7 changes: 6 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,13 @@ module.exports = {
ignoreRestSiblings: true,
},
],
'@typescript-eslint/no-floating-promises': 'error',
'@typescript-eslint/no-non-null-asserted-nullish-coalescing': 'error',
'no-return-await': 'off',
'@typescript-eslint/return-await': 'error',
'require-await': 'off',
'@typescript-eslint/require-await': 'error',
'@typescript-eslint/await-thenable': 'error',
'@typescript-eslint/no-floating-promises': 'error',
'@typescript-eslint/no-misused-promises': 'error',
'@typescript-eslint/no-namespace': [
'error',
Expand Down
8 changes: 4 additions & 4 deletions packages/blocks/src/__tests__/database/database.unit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ function createTestOptions() {
return { id: 'test-workspace', idGenerator, schema };
}

async function createTestPage(pageId = 'page0') {
function createTestPage(pageId = 'page0') {
const options = createTestOptions();
const workspace = new Workspace(options);
const page = workspace.createPage({ id: pageId });
await page.load();
page.load();
return page;
}

Expand All @@ -57,10 +57,10 @@ describe('DatabaseManager', () => {
{ id: '3', value: 'WIP', color: 'var(--affine-tag-blue)' },
];

beforeEach(async () => {
beforeEach(() => {
vi.useFakeTimers({ toFake: ['requestIdleCallback'] });

page = await createTestPage();
page = createTestPage();

pageBlockId = page.addBlock('affine:page', {
title: new page.Text('database test'),
Expand Down
4 changes: 2 additions & 2 deletions packages/blocks/src/_common/adapters/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ export class HtmlAdapter extends BaseAdapter<Html> {
}
}
});
walker.setLeave(async (o, context) => {
walker.setLeave((o, context) => {
switch (o.node.flavour) {
case 'affine:page': {
context.closeNode().closeNode().closeNode();
Expand Down Expand Up @@ -991,7 +991,7 @@ export class HtmlAdapter extends BaseAdapter<Html> {
}
}
});
walker.setLeave(async (o, context) => {
walker.setLeave((o, context) => {
if (o.node.type !== 'element') {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/blocks/src/_common/adapters/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ export class MarkdownAdapter extends BaseAdapter<Markdown> {
}
}
});
walker.setLeave(async (o, context) => {
walker.setLeave((o, context) => {
const currentTNode = context.currentNode();
const previousTNode = context.previousNode();
switch (o.node.flavour) {
Expand Down Expand Up @@ -992,7 +992,7 @@ export class MarkdownAdapter extends BaseAdapter<Markdown> {
}
}
});
walker.setLeave(async (o, context) => {
walker.setLeave((o, context) => {
switch (o.node.type) {
case 'listItem': {
context.closeNode();
Expand Down
10 changes: 3 additions & 7 deletions packages/blocks/src/_common/adapters/mix-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@ export class MixTextAdapter extends BaseAdapter<MixText> {
};
}

async toPageSnapshot(
payload: ToPageSnapshotPayload<MixText>
): Promise<PageSnapshot> {
toPageSnapshot(payload: ToPageSnapshotPayload<MixText>): PageSnapshot {
payload.file = payload.file.replaceAll('\r', '');
return {
type: 'page',
Expand Down Expand Up @@ -157,9 +155,7 @@ export class MixTextAdapter extends BaseAdapter<MixText> {
};
}

async toBlockSnapshot(
payload: ToBlockSnapshotPayload<MixText>
): Promise<BlockSnapshot> {
toBlockSnapshot(payload: ToBlockSnapshotPayload<MixText>): BlockSnapshot {
payload.file = payload.file.replaceAll('\r', '');
return {
type: 'block',
Expand Down Expand Up @@ -222,7 +218,7 @@ export class MixTextAdapter extends BaseAdapter<MixText> {
(node): node is BlockSnapshot =>
BlockSnapshotSchema.safeParse(node).success
);
walker.setEnter(async o => {
walker.setEnter(o => {
const text = (o.node.props.text ?? { delta: [] }) as {
delta: DeltaInsert[];
};
Expand Down
2 changes: 1 addition & 1 deletion packages/blocks/src/_common/adapters/notion-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,7 @@ export class NotionHtmlAdapter extends BaseAdapter<NotionHtml> {
}
}
});
walker.setLeave(async (o, context) => {
walker.setLeave((o, context) => {
if (o.node.type !== 'element') {
return;
}
Expand Down
14 changes: 5 additions & 9 deletions packages/blocks/src/_common/adapters/plain-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@ export class PlainTextAdapter extends BaseAdapter<PlainText> {
};
}

async toPageSnapshot(
payload: ToPageSnapshotPayload<PlainText>
): Promise<PageSnapshot> {
toPageSnapshot(payload: ToPageSnapshotPayload<PlainText>): PageSnapshot {
payload.file = payload.file.replaceAll('\r', '');
return {
type: 'page',
Expand Down Expand Up @@ -157,9 +155,7 @@ export class PlainTextAdapter extends BaseAdapter<PlainText> {
};
}

async toBlockSnapshot(
payload: ToBlockSnapshotPayload<PlainText>
): Promise<BlockSnapshot> {
toBlockSnapshot(payload: ToBlockSnapshotPayload<PlainText>): BlockSnapshot {
payload.file = payload.file.replaceAll('\r', '');
return {
type: 'block',
Expand Down Expand Up @@ -194,9 +190,9 @@ export class PlainTextAdapter extends BaseAdapter<PlainText> {
};
}

async toSliceSnapshot(
toSliceSnapshot(
payload: PlainTextToSliceSnapshotPayload
): Promise<SliceSnapshot | null> {
): SliceSnapshot | null {
this._markdownAdapter.applyConfigs(this.configs);
if (payload.file.trim().length === 0) {
return null;
Expand Down Expand Up @@ -252,7 +248,7 @@ export class PlainTextAdapter extends BaseAdapter<PlainText> {
(node): node is BlockSnapshot =>
BlockSnapshotSchema.safeParse(node).success
);
walker.setEnter(async o => {
walker.setEnter(o => {
const text = (o.node.props.text ?? { delta: [] }) as {
delta: DeltaInsert[];
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,17 +401,14 @@ export const bindContainerHotkey = (blockElement: BlockElement) => {
});
inlineEditor.setInlineRange({ index: inlineRange.index - 1, length: 0 });

createDefaultPage(blockElement.page.workspace, {
const page = createDefaultPage(blockElement.page.workspace, {
title: pageName,
})
.then(page => {
insertLinkedNode({
editorHost: blockElement.host,
model: blockElement.model,
pageId: page.id,
});
})
.catch(e => console.error(e));
});
insertLinkedNode({
editorHost: blockElement.host,
model: blockElement.model,
pageId: page.id,
});
return true;
}

Expand Down Expand Up @@ -443,7 +440,7 @@ export const bindContainerHotkey = (blockElement: BlockElement) => {
// [[Selected text]] should automatically be converted to a Linked page with the title "Selected text".
// See https://github.com/toeverything/blocksuite/issues/2730
const success = tryConvertToLinkedDoc();
if (success) return;
if (success) return true;
}
inlineEditor.insertText(
inlineRange,
Expand Down
34 changes: 16 additions & 18 deletions packages/blocks/src/_common/export-manager/export-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class ExportManager {
}
}, 100);
});
return await promise;
return promise;
}

private _drawEdgelessBackground(
Expand Down Expand Up @@ -157,7 +157,7 @@ export class ExportManager {
}
});

await this._replaceRichTextWithSvgElement(element);
this._replaceRichTextWithSvgElement(element);
await this.replaceImgSrcWithSvg(element);
},
useCORS: this._exportOptions.imageProxyEndpoint ? false : true,
Expand Down Expand Up @@ -352,7 +352,7 @@ export class ExportManager {
},
onclone: async function (_documentClone: Document, element: HTMLElement) {
element.style.height = `${viewportHeight}px`;
await replaceRichTextWithSvgElement(element);
replaceRichTextWithSvgElement(element);
await replaceImgSrcWithSvg(element);
},
backgroundColor: window.getComputedStyle(viewportElement).backgroundColor,
Expand All @@ -371,22 +371,20 @@ export class ExportManager {
return data;
}

private _replaceRichTextWithSvgElement = async (element: HTMLElement) => {
private _replaceRichTextWithSvgElement = (element: HTMLElement) => {
const richList = Array.from(element.querySelectorAll('.inline-editor'));
await Promise.all(
richList.map(async rich => {
const svgEle = await this._elementToSvgElement(
rich.cloneNode(true) as HTMLElement,
rich.clientWidth,
rich.clientHeight + 1
);
rich.parentElement?.appendChild(svgEle);
rich.parentElement?.removeChild(rich);
})
);
richList.map(rich => {
const svgEle = this._elementToSvgElement(
rich.cloneNode(true) as HTMLElement,
rich.clientWidth,
rich.clientHeight + 1
);
rich.parentElement?.appendChild(svgEle);
rich.parentElement?.removeChild(rich);
});
};

private async _elementToSvgElement(
private _elementToSvgElement(
node: HTMLElement,
width: number,
height: number
Expand Down Expand Up @@ -423,7 +421,7 @@ export class ExportManager {
await this._checkReady();

if (isInsideDocEditor(this.editorHost)) {
return await this._docToCanvas();
return this._docToCanvas();
} else {
const root = this.page.root;
if (!root) return;
Expand All @@ -434,7 +432,7 @@ export class ExportManager {
) as EdgelessPageBlockComponent;
const bound = edgeless.getElementsBound();
assertExists(bound);
return await this.edgelessToCanvas(
return this.edgelessToCanvas(
edgeless.surface.renderer,
bound,
(model: BlockModel) => blockElementGetter(model, this.editorHost.view),
Expand Down
2 changes: 1 addition & 1 deletion packages/blocks/src/_common/utils/init.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Workspace } from '@blocksuite/store';

export async function createDefaultPage(
export function createDefaultPage(
workspace: Workspace,
options: { id?: string; title?: string } = {}
) {
Expand Down
8 changes: 4 additions & 4 deletions packages/blocks/src/_common/utils/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ function setEndRange(editableContainer: Element) {
return newRange;
}

async function setNewTop(y: number, editableContainer: Element, zoom = 1) {
function setNewTop(y: number, editableContainer: Element, zoom = 1) {
const SCROLL_THRESHOLD = 100;

const scrollContainer = editableContainer.closest('.affine-doc-viewport');
Expand Down Expand Up @@ -177,7 +177,7 @@ export function focusTitle(editorHost: EditorHost, index = Infinity, len = 0) {
titleInlineEditor.setInlineRange({ index, length: len });
}

async function focusRichText(
function focusRichText(
editableContainer: Element,
position: SelectionPosition = 'end',
zoom = 1
Expand All @@ -203,7 +203,7 @@ async function focusRichText(
default: {
const { x, y } = position;
let newLeft = x;
const newTop = await setNewTop(y, editableContainer, zoom);
const newTop = setNewTop(y, editableContainer, zoom);
if (x <= left) {
newLeft = left + 1;
}
Expand Down Expand Up @@ -240,7 +240,7 @@ export function focusBlockByModel(
assertExists(element);
const editableContainer = element?.querySelector('[contenteditable]');
if (editableContainer) {
focusRichText(editableContainer, position, zoom).catch(console.error);
focusRichText(editableContainer, position, zoom);
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/blocks/src/attachment-block/attachment-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export class AttachmentBlockComponent extends BlockElement<
};

download = () => {
downloadAttachmentBlob(this).catch(console.error);
downloadAttachmentBlob(this);
};

refreshData = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { AttachmentBlockProps } from './attachment-model.js';

export class AttachmentBlockTransformer extends BaseBlockTransformer<AttachmentBlockProps> {
override async toSnapshot(payload: ToSnapshotPayload<AttachmentBlockProps>) {
const snapshot = await super.toSnapshot(payload);
const snapshot = super.toSnapshot(payload);
const sourceId = payload.model.sourceId;
if (sourceId) await payload.assets.readFromBlob(sourceId);

Expand Down
2 changes: 1 addition & 1 deletion packages/blocks/src/attachment-block/embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export function renderEmbedView(model: AttachmentBlockModel, blobUrl: string) {
/**
* Turn the attachment block into an image block.
*/
export async function turnIntoImageBlock(model: AttachmentBlockModel) {
export function turnIntoImageBlock(model: AttachmentBlockModel) {
if (!model.page.schema.flavourSchemaMap.has('affine:image'))
throw new Error('The image flavour is not supported!');

Expand Down
2 changes: 1 addition & 1 deletion packages/blocks/src/attachment-block/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export async function checkAttachmentBlob(block: AttachmentBlockComponent) {
* Since the size of the attachment may be very large,
* the download process may take a long time!
*/
export async function downloadAttachmentBlob(block: AttachmentBlockComponent) {
export function downloadAttachmentBlob(block: AttachmentBlockComponent) {
const { host, model, loading, error, downloading, blobUrl } = block;
if (downloading) {
toast(host, 'Download in progress...');
Expand Down
4 changes: 3 additions & 1 deletion packages/blocks/src/code-block/components/lang-list.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import '../../_common/components/button.js';

import type { Placement } from '@floating-ui/dom';
import { baseTheme } from '@toeverything/theme';
import { css, html, LitElement, nothing, unsafeCSS } from 'lit';
Expand Down Expand Up @@ -143,7 +145,7 @@ export class LangList extends LitElement {
@query('#filter-input')
filterInput!: HTMLInputElement;

override async connectedCallback() {
override connectedCallback() {
super.connectedCallback();

setTimeout(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class KanbanClipboardController implements ReactiveController {
const kanbanSelection = this.host.selectionController.selection;
if (!kanbanSelection) return false;

this._onCopy(ctx, kanbanSelection).catch(console.error);
this._onCopy(ctx, kanbanSelection);
return true;
})
);
Expand All @@ -34,7 +34,7 @@ export class KanbanClipboardController implements ReactiveController {
);
}

private _onCopy = async (
private _onCopy = (
_context: UIEventStateContext,
_kanbanSelection: KanbanViewSelectionWithType
) => {
Expand Down
Loading

0 comments on commit 0c0a8b1

Please sign in to comment.