refactor(editor): remove assertExists (#10615)

This commit is contained in:
Saul-Mirone
2025-03-05 00:13:08 +00:00
parent a6692f70aa
commit b8ecfbdae6
106 changed files with 863 additions and 517 deletions

View File

@@ -1,5 +1,4 @@
import { apis } from '@affine/electron-api';
import { assertExists } from '@blocksuite/affine/global/utils';
import type { AppConfigSchema } from '@toeverything/infra';
import { AppConfigStorage, defaultAppConfig } from '@toeverything/infra';
import type { Dispatch } from 'react';
@@ -12,12 +11,16 @@ class AppConfigProxy {
value: AppConfigSchema = defaultAppConfig;
async getSync(): Promise<AppConfigSchema> {
assertExists(apis);
if (!apis) {
throw new Error('electron apis is not found');
}
return (this.value = await apis.configStorage.get());
}
async setSync(): Promise<void> {
assertExists(apis);
if (!apis) {
throw new Error('electron apis is not found');
}
await apis.configStorage.set(this.value);
}

View File

@@ -11,7 +11,6 @@ import type {
VariableMap,
} from '@affine/env/filter';
import { getOrCreateI18n, I18nextProvider } from '@affine/i18n';
import { assertExists } from '@blocksuite/affine/global/utils';
import { render } from '@testing-library/react';
import type { ReactElement } from 'react';
import { useState } from 'react';
@@ -77,7 +76,9 @@ describe('match filter', () => {
describe('eval filter', () => {
test('before', async () => {
const before = filterMatcher.findData(v => v.name === 'before');
assertExists(before);
if (!before) {
throw new Error('before is not found');
}
const filter1 = filter(before, ref('Created'), [
new Date(2023, 5, 28).getTime(),
]);
@@ -96,7 +97,9 @@ describe('eval filter', () => {
});
test('after', async () => {
const after = filterMatcher.findData(v => v.name === 'after');
assertExists(after);
if (!after) {
throw new Error('after is not found');
}
const filter1 = filter(after, ref('Created'), [
new Date(2023, 5, 28).getTime(),
]);
@@ -115,7 +118,9 @@ describe('eval filter', () => {
});
test('is', async () => {
const is = filterMatcher.findData(v => v.name === 'is');
assertExists(is);
if (!is) {
throw new Error('is is not found');
}
const filter1 = filter(is, ref('Is Favourited'), [false]);
const filter2 = filter(is, ref('Is Favourited'), [true]);
const varMap = mockVariableMap({
@@ -130,7 +135,9 @@ describe('render filter', () => {
test('boolean condition value change', async () => {
const is = filterMatcher.match(tBoolean.create());
const i18n = getOrCreateI18n();
assertExists(is);
if (!is) {
throw new Error('is is not found');
}
const Wrapper = () => {
const [value, onChange] = useState(
filter(is, ref('Is Favourited'), [true])
@@ -169,7 +176,9 @@ describe('render filter', () => {
test('date condition function change', async () => {
const dateFunction = filterMatcher.match(tDate.create());
assertExists(dateFunction);
if (!dateFunction) {
throw new Error('dateFunction is not found');
}
const Wrapper = WrapperCreator(dateFunction);
const result = render(<Wrapper />);
const dom = await result.findByTestId('filter-name');
@@ -179,7 +188,9 @@ describe('render filter', () => {
});
test('date condition variable change', async () => {
const dateFunction = filterMatcher.match(tDate.create());
assertExists(dateFunction);
if (!dateFunction) {
throw new Error('dateFunction is not found');
}
const Wrapper = WrapperCreator(dateFunction);
const result = render(<Wrapper />);
const dom = await result.findByTestId('variable-name');

View File

@@ -4,7 +4,6 @@
import 'fake-indexeddb/auto';
import { StoreExtensions } from '@blocksuite/affine/blocks';
import { assertExists } from '@blocksuite/affine/global/utils';
import { type Store, Text } from '@blocksuite/affine/store';
import { TestWorkspace } from '@blocksuite/affine/store/test';
import { renderHook } from '@testing-library/react';
@@ -23,7 +22,6 @@ beforeEach(async () => {
const initPage = async (page: Store) => {
page.load();
expect(page).not.toBeNull();
assertExists(page);
const pageBlockId = page.addBlock('affine:page', {
title: new Text(''),
});

View File

@@ -2,7 +2,6 @@ import { shallowEqual } from '@affine/component';
import { DocDisplayMetaService } from '@affine/core/modules/doc-display-meta';
import type { Tag } from '@affine/env/filter';
import { useI18n } from '@affine/i18n';
import { assertExists } from '@blocksuite/affine/global/utils';
import type { DocMeta, Workspace } from '@blocksuite/affine/store';
import { ToggleRightIcon, ViewLayersIcon } from '@blocksuite/icons/rc';
import * as Collapsible from '@radix-ui/react-collapsible';
@@ -301,7 +300,9 @@ function pageMetaToListItemProp(
): PageListItemProps {
const toggleSelection = props.onSelectedIdsChange
? () => {
assertExists(props.selectedIds);
if (!props.selectedIds) {
throw new Error('selectedIds is not found');
}
const prevSelected = props.selectedIds.includes(item.id);
const shouldAdd = !prevSelected;
const shouldRemove = prevSelected;
@@ -345,7 +346,9 @@ function collectionMetaToListItemProp(
): CollectionListItemProps {
const toggleSelection = props.onSelectedIdsChange
? () => {
assertExists(props.selectedIds);
if (!props.selectedIds) {
throw new Error('selectedIds is not found');
}
const prevSelected = props.selectedIds.includes(item.id);
const shouldAdd = !prevSelected;
const shouldRemove = prevSelected;
@@ -382,7 +385,9 @@ function tagMetaToListItemProp(
): TagListItemProps {
const toggleSelection = props.onSelectedIdsChange
? () => {
assertExists(props.selectedIds);
if (!props.selectedIds) {
throw new Error('selectedIds is not found');
}
const prevSelected = props.selectedIds.includes(item.id);
const shouldAdd = !prevSelected;
const shouldRemove = prevSelected;

View File

@@ -1,6 +1,5 @@
import { notify } from '@affine/component';
import { GraphQLError } from '@affine/graphql';
import { assertExists } from '@blocksuite/affine/global/utils';
import type { PropsWithChildren, ReactNode } from 'react';
import { useCallback } from 'react';
import type { SWRConfiguration } from 'swr';
@@ -12,7 +11,9 @@ const swrConfig: SWRConfiguration = {
useSWRNext => (key, fetcher, config) => {
const fetcherWrapper = useCallback(
async (...args: any[]) => {
assertExists(fetcher);
if (!fetcher) {
throw new Error('fetcher is not found');
}
const d = fetcher(...args);
if (d instanceof Promise) {
return d.catch(e => {