Skip to content

Commit 0af2cbd

Browse files
authored
Merge pull request #19 from jfardilha2/eslint/add-new-rules
Eslint/add new rules
2 parents 27c4da8 + 5ed3c77 commit 0af2cbd

File tree

17 files changed

+78
-17
lines changed

17 files changed

+78
-17
lines changed

templates/admin/.eslintrc.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module.exports = {
1414
// => par contre on ne peut pas laisser ce plugin tout le temps
1515
// => car il ne tient ni compte des polyfill, ni des navigateurs configurés dans browserslist
1616
// 'plugin:typescript-compat/recommended',
17+
'plugin:@sayari/recommended'
1718
],
1819
parser: '@typescript-eslint/parser',
1920
parserOptions: {
@@ -26,7 +27,8 @@ module.exports = {
2627
},
2728
plugins: [
2829
'react',
29-
'@typescript-eslint'
30+
'@typescript-eslint',
31+
'react-hooks'
3032
],
3133
settings: {
3234
react: {
@@ -103,5 +105,9 @@ module.exports = {
103105
'variableDeclarationIgnoreFunction': true
104106
}
105107
],
108+
'react-hooks/rules-of-hooks': 'error',
109+
// Add all your custom hooks which have dependencies in the additional hooks
110+
// If you have several hooks, here is the syntax 'additionalHooks': '(hook1|hook2)'
111+
'react-hooks/exhaustive-deps': ['warn', { 'additionalHooks': 'useOnDependenciesChange|useEffectWithSsrSupport|useObservableLoader' }],
106112
},
107113
};

templates/admin/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"validator": "^13.7.0"
2828
},
2929
"devDependencies": {
30+
"@sayari/eslint-plugin": "^0.0.1-rc.4",
3031
"@types/enzyme": "^3.10.12",
3132
"@types/jest": "^29.1.2",
3233
"@types/react": "^18.0.21",

templates/admin/src/components/features/login/Login.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export default function Login() {
3232
if (isAuthenticated) {
3333
navigate({ pathname: HOME });
3434
}
35-
}, [isAuthenticated]);
35+
}, [isAuthenticated, navigate]);
3636

3737
return (
3838
<div className="login-layout">

templates/admin/src/lib/plume-admin-users/pages/UsersEdit.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ export default class UsersEdit {
5252
const isCreation: boolean = userId === undefined;
5353

5454
// small optimization to avoid fetching the current user during each render cycle
55-
const userToEdit: AdminUserDetails | undefined = useMemo(() => findUser(userId, usersWithRoles), [usersWithRoles]);
55+
const userToEdit: AdminUserDetails | undefined = useMemo(
56+
() => findUser(userId, usersWithRoles),
57+
[userId, usersWithRoles],
58+
);
5659

5760
const formContext: UseFormReturn<AdminUserParameters> = useForm<AdminUserParameters>({
5861
defaultValues: userToEdit,
@@ -63,7 +66,7 @@ export default class UsersEdit {
6366
} = formContext;
6467

6568
// when the users are loaded from the upper component, we need update the form with the new values
66-
useOnDependenciesChange(() => reset(userToEdit), [usersWithRoles]);
69+
useOnDependenciesChange(() => reset(userToEdit), [userToEdit, reset]);
6770

6871
// data validation
6972

templates/admin/src/lib/plume-http-react-hook-loader/dataLoaderHook.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export default function useDataLoader<T>(
3636
// mark the component as unmounted
3737
isMountedRef.current = false;
3838
};
39+
// eslint-disable-next-line react-hooks/exhaustive-deps
3940
}, dependencies);
4041

4142
return {

templates/admin/src/lib/plume-http-react-hook-loader/observableLoaderHook.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ export function useObservableLoaderConfigurable<T extends ObservableDataHandler<
107107
.observableSources
108108
// eslint-disable-next-line @typescript-eslint/no-explicit-any
109109
.map((dataObservable: ObservableDataHandler<any>) => {
110+
// the number of observable sources is fixed and cannot change,
111+
// so the number of time useObservable is called will be stable
112+
// eslint-disable-next-line @typescript-eslint/no-explicit-any,react-hooks/rules-of-hooks
110113
const data: T = useObservable(dataObservable.dataObservable);
111114

112115
return {

templates/admin/src/lib/plume-http-react-hook-loader/timeoutHook.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { MutableRefObject, useEffect, useRef } from 'react';
1+
import {
2+
MutableRefObject, useEffect, useRef,
3+
} from 'react';
24

35
/**
46
* Enable to control the timeout in the component where it is used.
@@ -50,7 +52,10 @@ export default function useTimeout(callback: () => void, delayInMillis: number)
5052
useEffect(() => {
5153
startTimeout();
5254
return stopTimeout;
53-
}, [delayInMillis, startTimeout, stopTimeout]);
55+
// eslint-disable-next-line react-hooks/exhaustive-deps
56+
}, []);
57+
// delayInMillis is not in the dependencies. If it changes at runtime, we do not want to reset the hook.
58+
// If delay can change at runtime, this hook is not the best solution to use.
5459

5560
const restartTimeout = () => {
5661
stopTimeout();

templates/admin/src/lib/react-hooks-alias/ReactHooksAlias.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
* @param callback The function that will be called once before the component has not yet been rendered
1313
*/
1414
export function useOnBeforeComponentRendered(callback: () => void): void {
15+
// eslint-disable-next-line react-hooks/exhaustive-deps
1516
useMemo(callback, []);
1617
}
1718

@@ -25,17 +26,17 @@ export function useOnBeforeComponentRendered(callback: () => void): void {
2526
* else this function will be called during unmounting. If this behavior is needed, `useEffect` should be used instead.
2627
*/
2728
export function useOnComponentMounted(callback: () => void): void {
28-
useEffect(
29-
callback,
30-
[],
31-
);
29+
// eslint-disable-next-line react-hooks/exhaustive-deps
30+
useEffect(callback, []);
3231
}
3332

3433
export function useOnComponentUnMounted(callback: () => void): void {
34+
// eslint-disable-next-line react-hooks/exhaustive-deps
3535
useEffect(() => callback, []);
3636
}
3737

3838
export function useOnDependenciesChange(callback: () => void, dependencies: DependencyList): void {
39+
// eslint-disable-next-line react-hooks/exhaustive-deps
3940
useEffect(callback, dependencies);
4041
}
4142

@@ -53,9 +54,11 @@ export function useOnDependenciesChange(callback: () => void, dependencies: Depe
5354
export function useEffectWithSsrSupport(callback: () => void, dependencies: DependencyList = []): void {
5455
if (typeof process !== 'undefined') {
5556
// server context
57+
// eslint-disable-next-line react-hooks/rules-of-hooks
5658
useMemo(callback, dependencies);
5759
} else {
5860
// browser context
61+
// eslint-disable-next-line react-hooks/rules-of-hooks
5962
useEffect(callback, dependencies);
6063
}
6164
}

templates/admin/yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2461,6 +2461,15 @@ __metadata:
24612461
languageName: node
24622462
linkType: hard
24632463

2464+
"@sayari/eslint-plugin@npm:^0.0.1-rc.4":
2465+
version: 0.0.1-rc.4
2466+
resolution: "@sayari/eslint-plugin@npm:0.0.1-rc.4"
2467+
peerDependencies:
2468+
eslint: ^8.5.0
2469+
checksum: c191a91806cf9e9f43a909b838d7274a6dfe6eedb183bd42d8820cf78bf77fe6264a422d81d51dbf28a3ccc38c4c2a9eedde3a12d9dfcf5157b9d16a6689962c
2470+
languageName: node
2471+
linkType: hard
2472+
24642473
"@sinclair/typebox@npm:^0.24.1":
24652474
version: 0.24.46
24662475
resolution: "@sinclair/typebox@npm:0.24.46"
@@ -10033,6 +10042,7 @@ resolve@^2.0.0-next.3:
1003310042
"@mui/styles": ^5.10.9
1003410043
"@mui/system": ^5.10.9
1003510044
"@mui/x-date-pickers": ^5.0.5
10045+
"@sayari/eslint-plugin": ^0.0.1-rc.4
1003610046
"@types/enzyme": ^3.10.12
1003710047
"@types/jest": ^29.1.2
1003810048
"@types/react": ^18.0.21

templates/front/.eslintrc.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module.exports = {
1414
// => par contre on ne peut pas laisser ce plugin tout le temps
1515
// => car il ne tient ni compte des polyfill, ni des navigateurs configurés dans browserslist
1616
// 'plugin:typescript-compat/recommended',
17+
'plugin:@sayari/recommended'
1718
],
1819
parser: '@typescript-eslint/parser',
1920
parserOptions: {
@@ -27,6 +28,7 @@ module.exports = {
2728
plugins: [
2829
'react',
2930
'@typescript-eslint',
31+
'react-hooks'
3032
],
3133
settings: {
3234
react: {
@@ -103,5 +105,9 @@ module.exports = {
103105
'variableDeclarationIgnoreFunction': true
104106
}
105107
],
108+
'react-hooks/rules-of-hooks': 'error',
109+
// Add all your custom hooks which have dependencies in the additional hooks
110+
// If you have several hooks, here is the syntax 'additionalHooks': '(hook1|hook2)'
111+
'react-hooks/exhaustive-deps': ['warn', { 'additionalHooks': 'useOnDependenciesChange|useEffectWithSsrSupport|useObservableLoader' }],
106112
},
107113
};

0 commit comments

Comments
 (0)