[ESLint rule] prevent useRecoilCallback without a dependency array (#4411)

Co-authored-by: gitstart-twenty <gitstart-twenty@users.noreply.github.com>
Co-authored-by: Matheus <matheus_benini@hotmail.com>
Co-authored-by: v1b3m <vibenjamin6@gmail.com>
This commit is contained in:
gitstart-app[bot] 2024-03-12 15:12:17 +01:00 committed by GitHub
parent 41c7cd8cf7
commit 60598bf235
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 174 additions and 52 deletions

View File

@ -51,6 +51,7 @@ module.exports = {
'@nx/workspace-component-props-naming': 'error', '@nx/workspace-component-props-naming': 'error',
'@nx/workspace-explicit-boolean-predicates-in-if': 'error', '@nx/workspace-explicit-boolean-predicates-in-if': 'error',
'@nx/workspace-use-getLoadable-and-getValue-to-get-atoms': 'error', '@nx/workspace-use-getLoadable-and-getValue-to-get-atoms': 'error',
'@nx/workspace-useRecoilCallback-has-dependency-array': 'error',
'react/no-unescaped-entities': 'off', 'react/no-unescaped-entities': 'off',
'react/prop-types': 'off', 'react/prop-types': 'off',

View File

@ -24,10 +24,10 @@ export const useCommandMenu = () => {
goBackToPreviousHotkeyScope, goBackToPreviousHotkeyScope,
} = usePreviousHotkeyScope(); } = usePreviousHotkeyScope();
const openCommandMenu = () => { const openCommandMenu = useCallback(() => {
setIsCommandMenuOpened(true); setIsCommandMenuOpened(true);
setHotkeyScopeAndMemorizePreviousScope(AppHotkeyScope.CommandMenuOpen); setHotkeyScopeAndMemorizePreviousScope(AppHotkeyScope.CommandMenuOpen);
}; }, [setHotkeyScopeAndMemorizePreviousScope, setIsCommandMenuOpened]);
const closeCommandMenu = useRecoilCallback( const closeCommandMenu = useRecoilCallback(
({ snapshot }) => ({ snapshot }) =>
@ -60,6 +60,7 @@ export const useCommandMenu = () => {
openCommandMenu(); openCommandMenu();
} }
}, },
[closeCommandMenu, openCommandMenu],
); );
const addToCommandMenu = useCallback( const addToCommandMenu = useCallback(

View File

@ -45,29 +45,40 @@ export const useRecordActionBar = ({
objectNameSingular: objectMetadataItem.nameSingular, objectNameSingular: objectMetadataItem.nameSingular,
}); });
const handleFavoriteButtonClick = useRecoilCallback(({ snapshot }) => () => { const handleFavoriteButtonClick = useRecoilCallback(
if (selectedRecordIds.length > 1) { ({ snapshot }) =>
return; () => {
} if (selectedRecordIds.length > 1) {
return;
}
const selectedRecordId = selectedRecordIds[0]; const selectedRecordId = selectedRecordIds[0];
const selectedRecord = snapshot const selectedRecord = snapshot
.getLoadable(recordStoreFamilyState(selectedRecordId)) .getLoadable(recordStoreFamilyState(selectedRecordId))
.getValue(); .getValue();
const foundFavorite = favorites?.find( const foundFavorite = favorites?.find(
(favorite) => favorite.recordId === selectedRecordId, (favorite) => favorite.recordId === selectedRecordId,
); );
const isFavorite = !!selectedRecordId && !!foundFavorite; const isFavorite = !!selectedRecordId && !!foundFavorite;
if (isFavorite) { if (isFavorite) {
deleteFavorite(foundFavorite.id); deleteFavorite(foundFavorite.id);
} else if (isDefined(selectedRecord)) { } else if (isDefined(selectedRecord)) {
createFavorite(selectedRecord, objectMetadataItem.nameSingular); createFavorite(selectedRecord, objectMetadataItem.nameSingular);
} }
callback?.(); callback?.();
}); },
[
callback,
createFavorite,
deleteFavorite,
favorites,
objectMetadataItem.nameSingular,
selectedRecordIds,
],
);
const handleDeleteClick = useCallback(async () => { const handleDeleteClick = useCallback(async () => {
callback?.(); callback?.();

View File

@ -4,9 +4,11 @@ import { useRecoilCallback, useRecoilValue } from 'recoil';
import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem';
import { useDeleteOneRecord } from '@/object-record/hooks/useDeleteOneRecord'; import { useDeleteOneRecord } from '@/object-record/hooks/useDeleteOneRecord';
import { FieldMetadata } from '@/object-record/record-field/types/FieldMetadata';
import { RecordTable } from '@/object-record/record-table/components/RecordTable'; import { RecordTable } from '@/object-record/record-table/components/RecordTable';
import { EntityDeleteContext } from '@/object-record/record-table/contexts/EntityDeleteHookContext'; import { EntityDeleteContext } from '@/object-record/record-table/contexts/EntityDeleteHookContext';
import { useRecordTableStates } from '@/object-record/record-table/hooks/internal/useRecordTableStates'; import { useRecordTableStates } from '@/object-record/record-table/hooks/internal/useRecordTableStates';
import { ColumnDefinition } from '@/object-record/record-table/types/ColumnDefinition';
import { IconPlus } from '@/ui/display/icon'; import { IconPlus } from '@/ui/display/icon';
import { Button } from '@/ui/input/button/components/Button'; import { Button } from '@/ui/input/button/components/Button';
import AnimatedPlaceholder from '@/ui/layout/animated-placeholder/components/AnimatedPlaceholder'; import AnimatedPlaceholder from '@/ui/layout/animated-placeholder/components/AnimatedPlaceholder';
@ -92,11 +94,16 @@ export const RecordTableWithWrappers = ({
<RecordTable <RecordTable
recordTableId={recordTableId} recordTableId={recordTableId}
objectNameSingular={objectNameSingular} objectNameSingular={objectNameSingular}
onColumnsChange={useRecoilCallback(() => (columns) => { onColumnsChange={useRecoilCallback(
persistViewFields( () => (columns) => {
mapColumnDefinitionsToViewFields(columns), persistViewFields(
); mapColumnDefinitionsToViewFields(
})} columns as ColumnDefinition<FieldMetadata>[],
),
);
},
[persistViewFields],
)}
createRecord={createRecord} createRecord={createRecord}
/> />
<DragSelect <DragSelect

View File

@ -5,7 +5,11 @@ import { useRecordTableStates } from '@/object-record/record-table/hooks/interna
export const useSetRowSelectedState = (recordTableId?: string) => { export const useSetRowSelectedState = (recordTableId?: string) => {
const { isRowSelectedFamilyState } = useRecordTableStates(recordTableId); const { isRowSelectedFamilyState } = useRecordTableStates(recordTableId);
return useRecoilCallback(({ set }) => (rowId: string, selected: boolean) => { return useRecoilCallback(
set(isRowSelectedFamilyState(rowId), selected); ({ set }) =>
}); (rowId: string, selected: boolean) => {
set(isRowSelectedFamilyState(rowId), selected);
},
[isRowSelectedFamilyState],
);
}; };

View File

@ -8,5 +8,6 @@ export const useGeneratedApiKeys = () => {
(apiKeyId: string, apiKey: string | null) => { (apiKeyId: string, apiKey: string | null) => {
set(generatedApiKeyFamilyState(apiKeyId), apiKey); set(generatedApiKeyFamilyState(apiKeyId), apiKey);
}, },
[],
); );
}; };

View File

@ -14,12 +14,16 @@ export const useSnackBar = () => {
SnackBarManagerScopeInternalContext, SnackBarManagerScopeInternalContext,
); );
const handleSnackBarClose = useRecoilCallback(({ set }) => (id: string) => { const handleSnackBarClose = useRecoilCallback(
set(snackBarInternalScopedState({ scopeId }), (prevState) => ({ ({ set }) =>
...prevState, (id: string) => {
queue: prevState.queue.filter((snackBar) => snackBar.id !== id), set(snackBarInternalScopedState({ scopeId }), (prevState) => ({
})); ...prevState,
}); queue: prevState.queue.filter((snackBar) => snackBar.id !== id),
}));
},
[scopeId],
);
const setSnackBarQueue = useRecoilCallback( const setSnackBarQueue = useRecoilCallback(
({ set }) => ({ set }) =>

View File

@ -12,28 +12,40 @@ export const useListenScroll = <T extends Element>({
}: { }: {
scrollableRef: React.RefObject<T>; scrollableRef: React.RefObject<T>;
}) => { }) => {
const hideScrollBarsCallback = useRecoilCallback(({ snapshot }) => () => { const hideScrollBarsCallback = useRecoilCallback(
const isScrolling = snapshot.getLoadable(isScrollingState()).getValue(); ({ snapshot }) =>
() => {
const isScrolling = snapshot.getLoadable(isScrollingState()).getValue();
if (!isScrolling) { if (!isScrolling) {
scrollableRef.current?.classList.remove('scrolling'); scrollableRef.current?.classList.remove('scrolling');
} }
}); },
[scrollableRef],
);
const handleScrollStart = useRecoilCallback(({ set }) => (event: Event) => { const handleScrollStart = useRecoilCallback(
set(isScrollingState(), true); ({ set }) =>
scrollableRef.current?.classList.add('scrolling'); (event: Event) => {
set(isScrollingState(), true);
scrollableRef.current?.classList.add('scrolling');
const target = event.target as HTMLElement; const target = event.target as HTMLElement;
set(scrollTopState(), target.scrollTop); set(scrollTopState(), target.scrollTop);
set(scrollLeftState(), target.scrollLeft); set(scrollLeftState(), target.scrollLeft);
}); },
[scrollableRef],
);
const handleScrollEnd = useRecoilCallback(({ set }) => () => { const handleScrollEnd = useRecoilCallback(
set(isScrollingState(), false); ({ set }) =>
debounce(hideScrollBarsCallback, 1000)(); () => {
}); set(isScrollingState(), false);
debounce(hideScrollBarsCallback, 1000)();
},
[hideScrollBarsCallback],
);
useEffect(() => { useEffect(() => {
const refTarget = scrollableRef.current; const refTarget = scrollableRef.current;

View File

@ -38,6 +38,10 @@ import {
rule as useGetLoadableAndGetValueToGetAtoms, rule as useGetLoadableAndGetValueToGetAtoms,
RULE_NAME as useGetLoadableAndGetValueToGetAtomsName, RULE_NAME as useGetLoadableAndGetValueToGetAtomsName,
} from './rules/use-getLoadable-and-getValue-to-get-atoms'; } from './rules/use-getLoadable-and-getValue-to-get-atoms';
import {
rule as useRecoilCallbackHasDependencyArray,
RULE_NAME as useRecoilCallbackHasDependencyArrayName,
} from './rules/useRecoilCallback-has-dependency-array';
/** /**
* Import your custom workspace rules at the top of this file. * Import your custom workspace rules at the top of this file.
@ -77,5 +81,7 @@ module.exports = {
[useGetLoadableAndGetValueToGetAtomsName]: [useGetLoadableAndGetValueToGetAtomsName]:
useGetLoadableAndGetValueToGetAtoms, useGetLoadableAndGetValueToGetAtoms,
[maxConstsPerFileName]: maxConstsPerFile, [maxConstsPerFileName]: maxConstsPerFile,
[useRecoilCallbackHasDependencyArrayName]:
useRecoilCallbackHasDependencyArray,
}, },
}; };

View File

@ -0,0 +1,29 @@
import { TSESLint } from '@typescript-eslint/utils';
import { rule, RULE_NAME } from './useRecoilCallback-has-dependency-array';
const ruleTester = new TSESLint.RuleTester({
parser: require.resolve('@typescript-eslint/parser'),
});
ruleTester.run(RULE_NAME, rule, {
valid: [
{
code: 'const someValue = useRecoilCallback(() => () => {}, []);',
},
{
code: 'const someValue = useRecoilCallback(() => () => {}, [dependency]);',
},
],
invalid: [
{
code: 'const someValue = useRecoilCallback(({}) => () => {});',
errors: [
{
messageId: 'isNecessaryDependencyArray',
},
],
output: 'const someValue = useRecoilCallback(({}) => () => {}, []);',
},
],
});

View File

@ -0,0 +1,46 @@
import { ESLintUtils } from '@typescript-eslint/utils';
// NOTE: The rule will be available in ESLint configs as "@nx/workspace-useRecoilCallback-has-dependency-array"
export const RULE_NAME = 'useRecoilCallback-has-dependency-array';
export const rule = ESLintUtils.RuleCreator(() => __filename)({
name: RULE_NAME,
meta: {
type: 'problem',
docs: {
description: 'Ensure `useRecoilCallback` is used with a dependency array',
recommended: 'recommended',
},
schema: [],
messages: {
isNecessaryDependencyArray:
'Is necessary dependency array with useRecoilCallback',
},
fixable: 'code',
},
defaultOptions: [],
create: (context) => {
return {
CallExpression: (node) => {
const { callee } = node;
if (
callee.type === 'Identifier' &&
callee.name === 'useRecoilCallback'
) {
const depsArg = node.arguments;
if (depsArg.length === 1) {
context.report({
node: callee,
messageId: 'isNecessaryDependencyArray',
data: {
callee,
deps: depsArg[0],
},
fix: (fixer) => fixer.insertTextAfter(depsArg[0], ', []'),
});
}
}
},
};
},
});