From 4dddbbdaee2c8efc591bab229b6037f330eaf39c Mon Sep 17 00:00:00 2001 From: LongYinan Date: Wed, 29 Nov 2023 04:43:50 +0000 Subject: [PATCH] fix: add prefer-dom-node-remove rule (#5112) --- .eslintrc.js | 1 + .../frontend/component/src/components/block-hub/index.tsx | 2 +- .../component/src/components/block-suite-editor/index.tsx | 2 +- .../frontend/core/src/components/page-detail-editor.tsx | 6 +++--- .../core/src/components/pure/plugin-header/index.tsx | 2 +- packages/plugins/image-preview/src/component/index.tsx | 2 +- 6 files changed, 8 insertions(+), 7 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index fd49734c02..6bbfc4aaa1 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -209,6 +209,7 @@ const config = { 'unicorn/no-useless-fallback-in-spread': 'error', 'unicorn/prefer-dom-node-dataset': 'error', 'unicorn/prefer-dom-node-append': 'error', + 'unicorn/prefer-dom-node-remove': 'error', 'unicorn/prefer-array-some': 'error', 'unicorn/no-useless-promise-resolve-reject': 'error', 'sonarjs/no-all-duplicated-branches': 'error', diff --git a/packages/frontend/component/src/components/block-hub/index.tsx b/packages/frontend/component/src/components/block-hub/index.tsx index d189399a9e..a6f27b9937 100644 --- a/packages/frontend/component/src/components/block-hub/index.tsx +++ b/packages/frontend/component/src/components/block-hub/index.tsx @@ -10,7 +10,7 @@ export const RootBlockHub = () => { const div = ref.current; if (blockHub) { if (div.hasChildNodes()) { - div.removeChild(div.firstChild as ChildNode); + (div.firstChild as ChildNode).remove(); } div.append(blockHub); } diff --git a/packages/frontend/component/src/components/block-suite-editor/index.tsx b/packages/frontend/component/src/components/block-suite-editor/index.tsx index 3e6d433ab0..d8b40e2a48 100644 --- a/packages/frontend/component/src/components/block-suite-editor/index.tsx +++ b/packages/frontend/component/src/components/block-suite-editor/index.tsx @@ -136,7 +136,7 @@ const BlockSuiteEditorImpl = ({ } container.append(editor); return () => { - container.removeChild(editor); + editor.remove(); }; }, [editor]); diff --git a/packages/frontend/core/src/components/page-detail-editor.tsx b/packages/frontend/core/src/components/page-detail-editor.tsx index f61d623768..ca3dc916d9 100644 --- a/packages/frontend/core/src/components/page-detail-editor.tsx +++ b/packages/frontend/core/src/components/page-detail-editor.tsx @@ -177,7 +177,7 @@ const EditorWrapper = memo(function EditorWrapper({ document.body.append(div); return () => { cleanup(); - document.body.removeChild(div); + div.remove(); }; }); }); @@ -238,11 +238,11 @@ const PluginContentAdapter = memo( root.append(div); if (abortController.signal.aborted) { cleanup(); - root.removeChild(div); + div.remove(); } else { const cl = () => { cleanup(); - root.removeChild(div); + div.remove(); }; const dispose = addCleanup(pluginName, cl); abortController.signal.addEventListener('abort', () => { diff --git a/packages/frontend/core/src/components/pure/plugin-header/index.tsx b/packages/frontend/core/src/components/pure/plugin-header/index.tsx index 1080b39eaa..6156691546 100644 --- a/packages/frontend/core/src/components/pure/plugin-header/index.tsx +++ b/packages/frontend/core/src/components/pure/plugin-header/index.tsx @@ -30,7 +30,7 @@ export const PluginHeader = () => { pluginsRef.current = pluginsRef.current.filter( name => name !== pluginName ); - root.removeChild(div); + div.remove(); cleanup(); }); }); diff --git a/packages/plugins/image-preview/src/component/index.tsx b/packages/plugins/image-preview/src/component/index.tsx index 8b5e62ff1d..853ddd52a2 100644 --- a/packages/plugins/image-preview/src/component/index.tsx +++ b/packages/plugins/image-preview/src/component/index.tsx @@ -233,7 +233,7 @@ const ImagePreviewModalImpl = ( a.download = block.id ?? 'image'; document.body.append(a); a.click(); - document.body.removeChild(a); + a.remove(); } }, [props.pageId, props.workspace]