From d26c4b3309eb5ab322a7e29bb9919b9639024600 Mon Sep 17 00:00:00 2001 From: sahil-jassal-sf Date: Wed, 13 Dec 2023 20:02:57 +0530 Subject: [PATCH] refactor(core): ARC-69 ignore console statements sonar code smells ignore sonar code smells related to unexpected console statments in handling errors --- configGenerator.js | 12 +++++++----- src/Components/Forms/Form/Form.tsx | 6 ++++-- .../ProtectedRouteWrapper.test.tsx | 5 +++-- .../ProtectedRouteWrapper.tsx | 4 ++-- src/Components/TransferList/TransferList.tsx | 16 ++++++++-------- src/Helpers/authorizationFunctions.ts | 9 +++++---- src/Hooks/useAuth.ts | 2 +- src/Hooks/useLocalStorage.ts | 4 ++-- src/Providers/ErrorBoundary.tsx | 4 ++-- src/Providers/OnlineStatusProvider.tsx | 2 +- src/redux/apiSlice.ts | 1 - 11 files changed, 35 insertions(+), 30 deletions(-) diff --git a/configGenerator.js b/configGenerator.js index f5b87ba..89082aa 100644 --- a/configGenerator.js +++ b/configGenerator.js @@ -1,9 +1,9 @@ /* eslint-env node */ +import dotenv from 'dotenv'; import fs from 'fs'; import path, {dirname} from 'path'; -import yargs from 'yargs'; -import dotenv from 'dotenv'; import {fileURLToPath} from 'url'; +import yargs from 'yargs'; // Get the directory name and filename const currentDir = dirname(fileURLToPath(import.meta.url)); @@ -51,12 +51,14 @@ try { // Write the substituted data to config.json try { fs.writeFileSync(currentConfigFilePath, substitutedData, 'utf-8'); - console.log('Configuration file written in current directory.'); + // eslint-disable-next-line no-console + console.log('Configuration file written in current directory.'); // NOSONAR // Copy config.json to the public directory try { fs.copyFileSync(currentConfigFilePath, outConfigFilePath); - console.log('Configuration file copied to public directory.'); + // eslint-disable-next-line no-console + console.log('Configuration file copied to public directory.'); // NOSONAR } catch (copyError) { handleError(`Error copying config file: ${copyError.message}`); } @@ -68,6 +70,6 @@ try { } function handleError(message) { - console.error(message); + console.error(message); // NOSONAR process.exit(1); } diff --git a/src/Components/Forms/Form/Form.tsx b/src/Components/Forms/Form/Form.tsx index eb0f73d..e42b51c 100755 --- a/src/Components/Forms/Form/Form.tsx +++ b/src/Components/Forms/Form/Form.tsx @@ -2,9 +2,11 @@ import {Form as FormikForm, Formik} from 'formik'; import {ForwardedRef, forwardRef, ReactNode} from 'react'; import * as yup from 'yup'; +type AnyObject = Record; // NOSONAR + interface Props { - initialValues: any; - onSubmit: any; + initialValues: AnyObject; + onSubmit: (...args: any) => void; // NOSONAR validationSchema?: ReturnType; id?: string; enableReinitialize?: boolean; diff --git a/src/Components/ProtectedRouteWrapper/ProtectedRouteWrapper.test.tsx b/src/Components/ProtectedRouteWrapper/ProtectedRouteWrapper.test.tsx index caa0c1f..7eaa624 100644 --- a/src/Components/ProtectedRouteWrapper/ProtectedRouteWrapper.test.tsx +++ b/src/Components/ProtectedRouteWrapper/ProtectedRouteWrapper.test.tsx @@ -4,7 +4,7 @@ import {MemoryRouter, RouteObject, useRoutes} from 'react-router-dom'; import {ProtectedRouteWrapper} from './ProtectedRouteWrapper'; interface TestAppProps { - isAuthorized: (...args: any[]) => boolean; + isAuthorized: (...args: any[]) => boolean; // NOSONAR } const TestApp: React.FC = ({isAuthorized}) => { @@ -22,7 +22,8 @@ const TestApp: React.FC = ({isAuthorized}) => { ), }, ]; - return useRoutes(routesConfig); + const routes = useRoutes(routesConfig); + return <>{routes}; }; describe('ProtectedRouteWrapper', () => { diff --git a/src/Components/ProtectedRouteWrapper/ProtectedRouteWrapper.tsx b/src/Components/ProtectedRouteWrapper/ProtectedRouteWrapper.tsx index bffca35..5f22122 100644 --- a/src/Components/ProtectedRouteWrapper/ProtectedRouteWrapper.tsx +++ b/src/Components/ProtectedRouteWrapper/ProtectedRouteWrapper.tsx @@ -3,7 +3,7 @@ import {Navigate, useLocation} from 'react-router-dom'; export interface ProtectedRouteWrapperProps { children: JSX.Element; - isAuthorized?: (...args: any[]) => boolean; + isAuthorized?: (...args: any[]) => boolean; // NOSONAR } /** @@ -20,5 +20,5 @@ export const ProtectedRouteWrapper = ({ children, }: ProtectedRouteWrapperProps) => { const location = useLocation(); - return isAuthorized() ? children : ; + return <>{isAuthorized() ? children : }; }; diff --git a/src/Components/TransferList/TransferList.tsx b/src/Components/TransferList/TransferList.tsx index c2010f0..c3c3a46 100755 --- a/src/Components/TransferList/TransferList.tsx +++ b/src/Components/TransferList/TransferList.tsx @@ -32,6 +32,14 @@ function intersection(arr1: {label: string; value: string}[], arr2: {label: stri return arr1.filter(item1 => arr2.some(item2 => item2.value === item1.value)); } +const IconButton = ({text, ...rest}: {text: ReactNode; onClick: () => void; disabled: boolean}) => ( + +); + const TransferList: React.FC = ({ left = [], setLeft, @@ -87,14 +95,6 @@ const TransferList: React.FC = ({ setChecked([]); }; - const IconButton = ({text, ...rest}: {text: ReactNode; onClick: () => void; disabled: boolean}) => ( - - ); - const customList = ({ items, title, diff --git a/src/Helpers/authorizationFunctions.ts b/src/Helpers/authorizationFunctions.ts index 013cef7..4f256f9 100644 --- a/src/Helpers/authorizationFunctions.ts +++ b/src/Helpers/authorizationFunctions.ts @@ -1,14 +1,15 @@ import useAuth from 'Hooks/useAuth'; +export interface AnyFunctions { + [key: string]: (...args: any[]) => boolean; // NOSONAR +} + const IsAuthenticated = () => { + // NOSONAR typescript:S100 const {isLoggedIn} = useAuth(); return !!isLoggedIn; }; -export interface AnyFunctions { - [key: string]: (...args: any[]) => boolean; // NOSONAR -} - export const authorizationFunctions: AnyFunctions = { isAuthenticated: IsAuthenticated, }; diff --git a/src/Hooks/useAuth.ts b/src/Hooks/useAuth.ts index 0e50772..aec62b3 100644 --- a/src/Hooks/useAuth.ts +++ b/src/Hooks/useAuth.ts @@ -73,7 +73,7 @@ export default function useAuth() { await logoutApi(refreshToken).unwrap(); dispatch(unsetCredentials()); } catch (err) { - console.error(err); + console.error(err); // NOSONAR } }; diff --git a/src/Hooks/useLocalStorage.ts b/src/Hooks/useLocalStorage.ts index 36f4e8a..1bc50f9 100644 --- a/src/Hooks/useLocalStorage.ts +++ b/src/Hooks/useLocalStorage.ts @@ -9,7 +9,7 @@ const useLocalStorage = (key: string, initialValue: T): [T, (value: T | ((pre const item = window.localStorage.getItem(key); return item ? (JSON.parse(item) as T) : initialValue; } catch (error) { - console.error('error while setting useLocalStorage initial value = ', error); + console.error('error while setting useLocalStorage initial value = ', error); // NOSONAR return initialValue; } }); @@ -21,7 +21,7 @@ const useLocalStorage = (key: string, initialValue: T): [T, (value: T | ((pre window.localStorage.setItem(key, JSON.stringify(valueToStore)); } } catch (error) { - console.error('error while setting useLocalStorage value = ', error); + console.error('error while setting useLocalStorage value = ', error); // NOSONAR } }; return [storedValue, setValue]; diff --git a/src/Providers/ErrorBoundary.tsx b/src/Providers/ErrorBoundary.tsx index 4e29e94..a6e6a41 100644 --- a/src/Providers/ErrorBoundary.tsx +++ b/src/Providers/ErrorBoundary.tsx @@ -17,13 +17,13 @@ class ErrorBoundary extends React.Component { static getDerivedStateFromError(error: Error) { // Update state so the next render will show the fallback UI. - console.error(error); + console.error(error); // NOSONAR return {hasError: true}; } componentDidCatch(error: Error, errorInfo: ErrorInfo) { // You can also log the error to an error reporting service - console.error(error, errorInfo); + console.error(error, errorInfo); // NOSONAR } render(): ReactNode { diff --git a/src/Providers/OnlineStatusProvider.tsx b/src/Providers/OnlineStatusProvider.tsx index d1d0101..be6f45f 100644 --- a/src/Providers/OnlineStatusProvider.tsx +++ b/src/Providers/OnlineStatusProvider.tsx @@ -38,7 +38,7 @@ const checkOnlineStatus = async () => { return true; } catch (error) { // Error Log - console.error(error); + console.error(error); // NOSONAR // This can be because of request timed out // so we abort the request for any case diff --git a/src/redux/apiSlice.ts b/src/redux/apiSlice.ts index 6d07038..6708272 100644 --- a/src/redux/apiSlice.ts +++ b/src/redux/apiSlice.ts @@ -34,7 +34,6 @@ const baseQueryWithReauth: BaseQueryFn