fix: add eslint-plugin-sonarjs and rules (#2767)

This commit is contained in:
LongYinan 2023-06-14 10:45:14 +08:00 committed by GitHub
parent 1c8f1a05d0
commit 3996955e3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 50 additions and 35 deletions

View File

@ -80,15 +80,19 @@ const config = {
'react', 'react',
'@typescript-eslint', '@typescript-eslint',
'simple-import-sort', 'simple-import-sort',
'sonarjs',
'import', 'import',
'unused-imports', 'unused-imports',
'unicorn', 'unicorn',
], ],
rules: { rules: {
'array-callback-return': 'error',
'no-undef': 'off', 'no-undef': 'off',
'no-empty': 'off', 'no-empty': 'off',
'no-func-assign': 'off', 'no-func-assign': 'off',
'no-cond-assign': 'off', 'no-cond-assign': 'off',
'no-constant-binary-expression': 'error',
'no-constructor-return': 'error',
'react/prop-types': 'off', 'react/prop-types': 'off',
'@typescript-eslint/consistent-type-imports': 'error', '@typescript-eslint/consistent-type-imports': 'error',
'@typescript-eslint/no-non-null-assertion': 'error', '@typescript-eslint/no-non-null-assertion': 'error',
@ -138,6 +142,21 @@ const config = {
ignore: ['^\\[[a-zA-Z0-9-_]+\\]\\.tsx$'], ignore: ['^\\[[a-zA-Z0-9-_]+\\]\\.tsx$'],
}, },
], ],
'sonarjs/no-all-duplicated-branches': 'error',
'sonarjs/no-element-overwrite': 'error',
'sonarjs/no-empty-collection': 'error',
'sonarjs/no-extra-arguments': 'error',
'sonarjs/no-identical-conditions': 'error',
'sonarjs/no-identical-expressions': 'error',
'sonarjs/no-ignored-return': 'error',
'sonarjs/no-one-iteration-loop': 'error',
'sonarjs/no-use-of-empty-return-value': 'error',
'sonarjs/non-existent-operator': 'error',
'sonarjs/no-collapsible-if': 'error',
'sonarjs/no-same-line-conditional': 'error',
'sonarjs/no-duplicated-branches': 'error',
'sonarjs/no-collection-size-mischeck': 'error',
'sonarjs/no-useless-catch': 'error',
}, },
overrides: [ overrides: [
{ {

View File

@ -109,7 +109,7 @@ const EditorWrapper = memo(function EditorWrapper({
); );
const disposes = uiDecorators.map(ui => ui(editor)); const disposes = uiDecorators.map(ui => ui(editor));
return () => { return () => {
disposes.map(fn => fn()); disposes.forEach(fn => fn());
dispose(); dispose();
}; };
}, },

View File

@ -80,15 +80,10 @@ export function WorkspaceHeader({
{t['Workspace Settings']()} {t['Workspace Settings']()}
</WorkspaceTitle> </WorkspaceTitle>
); );
} else if (currentEntry.subPath === WorkspaceSubPath.SHARED) { } else if (
return ( currentEntry.subPath === WorkspaceSubPath.SHARED ||
<WorkspaceModeFilterTab currentEntry.subPath === WorkspaceSubPath.TRASH
workspace={currentWorkspace} ) {
currentPage={null}
isPublic={false}
/>
);
} else if (currentEntry.subPath === WorkspaceSubPath.TRASH) {
return ( return (
<WorkspaceModeFilterTab <WorkspaceModeFilterTab
workspace={currentWorkspace} workspace={currentWorkspace}

View File

@ -39,8 +39,9 @@ function useTabRouterSync(
? router.query.currentTab ? router.query.currentTab
: null; : null;
if ( if (
queryCurrentTab !== null && (queryCurrentTab !== null &&
settingPanelValues.indexOf(queryCurrentTab as SettingPanel) === -1 settingPanelValues.indexOf(queryCurrentTab as SettingPanel) === -1) ||
settingPanelValues.indexOf(currentTab as SettingPanel) === -1
) { ) {
setCurrentTab(settingPanel.General); setCurrentTab(settingPanel.General);
router router
@ -52,17 +53,6 @@ function useTabRouterSync(
}, },
}) })
.catch(console.error); .catch(console.error);
} else if (settingPanelValues.indexOf(currentTab as SettingPanel) === -1) {
setCurrentTab(settingPanel.General);
router
.replace({
pathname: router.pathname,
query: {
...router.query,
currentTab: settingPanel.General,
},
})
.catch(console.error);
} else if (queryCurrentTab !== currentTab) { } else if (queryCurrentTab !== currentTab) {
router router
.replace({ .replace({
@ -109,11 +99,11 @@ const SettingPage: NextPageWithLayout = () => {
return helper.deleteWorkspace(workspaceId); return helper.deleteWorkspace(workspaceId);
}, [currentWorkspace, helper]); }, [currentWorkspace, helper]);
const onTransformWorkspace = useOnTransformWorkspace(); const onTransformWorkspace = useOnTransformWorkspace();
if (!router.isReady) { if (
return <PageLoading />; !router.isReady ||
} else if (currentWorkspace === null) { currentWorkspace === null ||
return <PageLoading />; settingPanelValues.indexOf(currentTab as SettingPanel) === -1
} else if (settingPanelValues.indexOf(currentTab as SettingPanel) === -1) { ) {
return <PageLoading />; return <PageLoading />;
} }
const { SettingsDetail, Header } = getUIAdapter(currentWorkspace.flavour); const { SettingsDetail, Header } = getUIAdapter(currentWorkspace.flavour);

View File

@ -31,9 +31,7 @@ const TrashPage: NextPageWithLayout = () => {
}, },
[currentWorkspace, jumpToPage] [currentWorkspace, jumpToPage]
); );
if (!router.isReady) { if (!router.isReady || currentWorkspace === null) {
return <PageLoading />;
} else if (currentWorkspace === null) {
return <PageLoading />; return <PageLoading />;
} }
// todo(himself65): refactor to plugin // todo(himself65): refactor to plugin

View File

@ -72,6 +72,7 @@
"eslint-plugin-react": "^7.32.2", "eslint-plugin-react": "^7.32.2",
"eslint-plugin-react-hooks": "^4.6.0", "eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-simple-import-sort": "^10.0.0", "eslint-plugin-simple-import-sort": "^10.0.0",
"eslint-plugin-sonarjs": "^0.19.0",
"eslint-plugin-unicorn": "^47.0.0", "eslint-plugin-unicorn": "^47.0.0",
"eslint-plugin-unused-imports": "^2.0.0", "eslint-plugin-unused-imports": "^2.0.0",
"fake-indexeddb": "4.0.1", "fake-indexeddb": "4.0.1",

View File

@ -207,10 +207,12 @@ export function createAffineAuth(prefix = '/') {
}), }),
}).then(r => r.json()) as Promise<LoginResponse>; }).then(r => r.json()) as Promise<LoginResponse>;
} catch (error) { } catch (error) {
if (error instanceof Error && 'code' in error) { if (
if (error.code === 'auth/popup-closed-by-user') { error instanceof Error &&
return null; 'code' in error &&
} error.code === 'auth/popup-closed-by-user'
) {
return null;
} }
logger.error('Failed to sign in', error); logger.error('Failed to sign in', error);
} }

View File

@ -296,6 +296,7 @@ __metadata:
eslint-plugin-react: ^7.32.2 eslint-plugin-react: ^7.32.2
eslint-plugin-react-hooks: ^4.6.0 eslint-plugin-react-hooks: ^4.6.0
eslint-plugin-simple-import-sort: ^10.0.0 eslint-plugin-simple-import-sort: ^10.0.0
eslint-plugin-sonarjs: ^0.19.0
eslint-plugin-unicorn: ^47.0.0 eslint-plugin-unicorn: ^47.0.0
eslint-plugin-unused-imports: ^2.0.0 eslint-plugin-unused-imports: ^2.0.0
fake-indexeddb: 4.0.1 fake-indexeddb: 4.0.1
@ -15275,6 +15276,15 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"eslint-plugin-sonarjs@npm:^0.19.0":
version: 0.19.0
resolution: "eslint-plugin-sonarjs@npm:0.19.0"
peerDependencies:
eslint: ^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0
checksum: 893640583f62ce55584c6ddd481aa0fd6fa15fe0fffc32ac92b17f3fadde8eaf32414183bb80b612455212e9bb14400236398af6279ca04e8992f008e011926c
languageName: node
linkType: hard
"eslint-plugin-unicorn@npm:^47.0.0": "eslint-plugin-unicorn@npm:^47.0.0":
version: 47.0.0 version: 47.0.0
resolution: "eslint-plugin-unicorn@npm:47.0.0" resolution: "eslint-plugin-unicorn@npm:47.0.0"