forked from arbisoft/session-portal-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheslint.config.mjs
More file actions
271 lines (260 loc) · 12.1 KB
/
eslint.config.mjs
File metadata and controls
271 lines (260 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import js from "@eslint/js";
import nextPlugin from "@next/eslint-plugin-next";
import { defineConfig } from "eslint/config";
import importPlugin from "eslint-plugin-import";
import prettierPlugin from "eslint-plugin-prettier";
import reactPlugin from "eslint-plugin-react";
import reactHooksPlugin from "eslint-plugin-react-hooks";
import globals from "globals";
import tseslint from "typescript-eslint";
export default defineConfig([
js.configs.recommended,
...tseslint.configs.recommended,
reactPlugin.configs.flat.recommended,
{
ignores: [".next/", "node_modules/", "dist/", "build/", "coverage/", "public/", "next-env.d.ts"],
},
{
files: ["**/*.{js,mjs,cjs,ts,mts,cts,jsx,tsx}"],
languageOptions: {
parser: tseslint.parser,
parserOptions: {
project: "./tsconfig.json",
tsconfigRootDir: import.meta.dirname,
sourceType: "module",
ecmaFeatures: { jsx: true },
},
globals: {
...globals.browser,
...globals.node,
},
},
plugins: {
react: reactPlugin,
"react-hooks": reactHooksPlugin,
"@typescript-eslint": tseslint.plugin,
import: importPlugin,
prettier: prettierPlugin,
"@next/next": nextPlugin,
},
settings: {
react: { version: "detect" },
"import/resolver": {
node: { extensions: [".js", ".jsx", ".ts", ".tsx"] },
typescript: { project: "./tsconfig.json", alwaysTryTypes: true },
},
},
rules: {
// === General Code Style ===
quotes: ["error", "double"],
eqeqeq: "error",
"array-callback-return": "error",
"no-alert": "error",
"no-return-assign": "error",
"react/react-in-jsx-scope": "off",
"react/prop-types": "off",
"react-hooks/rules-of-hooks": "error",
"react-hooks/exhaustive-deps": "off",
"max-len": ["error", { code: 130 }],
"no-console": ["error", { allow: ["warn", "error"] }],
"no-nested-ternary": "warn",
"no-shadow": "error",
"eol-last": ["error", "always"],
// === TypeScript Rules ===
"@typescript-eslint/no-unused-vars": "error",
"@typescript-eslint/ban-types": "off",
"@typescript-eslint/no-namespace": "off",
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/ban-ts-comment": "off",
"@typescript-eslint/no-empty-object-type": "off",
"@typescript-eslint/no-unused-expressions": ["error", { allowShortCircuit: true }],
// === Import Rules ===
"import/no-unresolved": "error",
"import/order": [
"error",
{
groups: ["builtin", "external", "internal", "parent", "sibling", "index"],
pathGroups: [
{ pattern: "react", group: "external", position: "before" },
{ pattern: "@/**", group: "internal", position: "after" },
],
pathGroupsExcludedImportTypes: ["react"],
"newlines-between": "always",
alphabetize: { order: "asc", caseInsensitive: true },
},
],
"import/extensions": [
"error",
"ignorePackages",
{
js: "never",
jsx: "never",
ts: "never",
tsx: "never",
css: "never",
},
],
"import/prefer-default-export": "off",
"import/named": "off",
"import/no-named-as-default": "off",
// === Next.js ===
"@next/next/no-img-element": "off",
// === Misc ===
"prefer-destructuring": ["error", { array: false, object: true }, { enforceForRenamedProperties: false }],
"no-empty-function": "off",
// === React Hook Form performance ===
/* eslint-disable max-len */
"no-restricted-syntax": [
"error",
{
selector: "CallExpression[callee.name=watch], MemberExpression[object.name=methods][property.name=watch]",
message: "watch re-render the whole form component. Use hook useWatch instead.",
},
{
selector: "VariableDeclarator > ObjectPattern > Property[key.name=formState]",
message: "formState re-render the whole form component. Use hook useFormState instead.",
},
{
selector: "VariableDeclarator[init.callee.name=useFormState][id.type=Identifier]",
message:
"Use destructuring assignment for useFormState. Example: const { isDirty } = useFormState(). Returned formState is wrapped with Proxy to improve render performance and skip extra computation if specific state is not subscribed, so make sure you deconstruct or read it before render in order to enable the subscription. More info here https://react-hook-form.com/docs/useformstate#rules",
},
{
selector: "MemberExpression[object.name=control]",
message:
"Do not access any of the properties inside this object directly. It's for internal usage only. More info here: https://react-hook-form.com/docs/useform/control",
},
{
selector:
"VariableDeclaration[declarations.0.init.callee.name=useForm] ~ VariableDeclaration[declarations.0.init.callee.name=useWatch]",
message:
"useWatch in main form component (which use useForm) will re-render the whole form component. Move your useWatch's logic to separate component.",
},
{
selector:
"VariableDeclaration[declarations.0.init.callee.name=useForm] ~ VariableDeclaration[declarations.0.init.callee.name=useFieldArray]",
message:
"useFieldArray in main form component (which use useForm) will re-render the whole form component. Move your useFieldArray's logic to separate component.",
},
{
selector:
"VariableDeclaration[declarations.0.init.callee.name=useForm] ~ VariableDeclaration[declarations.0.init.callee.name=useController]",
message:
"useController in main form component (which use useForm) will re-render the whole form component. Move your useController's logic to separate component.",
},
{
selector:
"VariableDeclaration[declarations.0.init.callee.name=useForm] ~ VariableDeclaration[declarations.0.init.callee.name=useFormContext]",
message:
"useFormContext in main form component (which use useForm) will re-render the whole form component. Move your useFormContext's logic to separate component.",
},
{
selector:
"VariableDeclaration[declarations.0.init.callee.name=useForm] ~ VariableDeclaration[declarations.0.init.callee.name=useFormState]",
message:
"useFormState in main form component (which use useForm) will re-render the whole form component. Move your useFormState's logic to separate component.",
},
{
selector:
"CallExpression[callee.name=useForm][arguments.length=0], CallExpression[callee.name=useForm][arguments.length=1]:not(:has(Property[key.name=defaultValues]))",
message:
"Pass object with defaultValues for correct formState behavior. More info here: https://react-hook-form.com/api/useform/formstate#main",
},
{
selector:
"VariableDeclaration[declarations.0.init.callee.name=useState] ~ ReturnStatement JSXIdentifier[name=Controller]",
message:
"Don't use Controller with useState. Move you useState and another logic to separate component and wrap this component via Controller. Reason: This approach will rise wrong logic for handle input field. Example: src/components/form/text-input/form-text-input.tsx",
},
{
selector:
"VariableDeclaration[declarations.0.init.callee.name=useState] ~ VariableDeclaration[declarations.0.init.callee.name=useController], VariableDeclaration[declarations.0.init.callee.name=useController] ~ VariableDeclaration[declarations.0.init.callee.name=useState]",
message:
"Don't use useController with useState. Move you useState and another logic to separate component and wrap this component into another component with useController. Reason: This approach will rise wrong logic for handle input filed",
},
{
selector:
"CallExpression[callee.name=useForm][arguments.length=1] Property[key.name=defaultValues][value.properties.length=0]",
message:
"defaultValues can not be empty object for correct formState behavior. More info here: https://react-hook-form.com/api/useform/formstate#main",
},
{
selector: "MemberExpression[object.name=React][property.name=/^use/]",
message:
"Use hooks without React prefix. For avoid using both import styles. Example: useEffect instead of React.useEffect.",
},
{
selector: "CallExpression[callee.type=MemberExpression][callee.property.name=forEach]:has(AwaitExpression)",
message:
"Do not use in forEach async code, because logic will not be awaited. use map with Promise.all, for example await Promise.all(ARRAY_OF_ITEM.map(...)) or use regular for loop.",
},
{
selector:
"ConditionalExpression[consequent.type=Literal][consequent.value=true][alternate.type=Literal][alternate.value=false]",
message: "Do not use condition ? true : false. Simplify someVariable === 42 ? true : false to someVariable === 42",
},
{
selector: "JSXElement[openingElement.name.property.name=Provider] JSXElement[openingElement.name.name]",
message:
"Do not put your regular components inside Context .Provider. Create new component, for example ComponentProvider. Put Provider's logic to ComponentProvider. Render {children} instead of regular component. Wrap regular component via new ComponentProvider. Example: src/services/auth/auth-provider",
},
{
selector:
"Property[key.name=/^(padding|margin|paddingLeft|paddingRight|paddingTop|paddingBottom|paddingVertical|marginLeft|marginRight|marginTop|marginBottom|marginVertical)$/][value.type=/^(Literal|UnaryExpression)$/]:not([value.value='0 !important']):not([value.value='0']):not([value.value='0 auto']):not([value.value='auto'])",
message: "Use theme.spacing() instead of literal.",
},
{
selector:
"CallExpression[callee.name=/^(useQuery|useInfiniteQuery)$/] Property[key.name=queryKey]:not(:has(Identifier[name=key]))",
message: "Use key created via createQueryKeys function instead of your solution",
},
{
selector: "CallExpression[callee.name=refresh]",
message:
"Do not use refresh() function for update or change result in react-query. Use queryClient.resetQueries or pass new filter data to queryKey.",
},
{
selector:
"ExpressionStatement[expression.callee.object.name=JSON][expression.callee.property.name=parse][expression.arguments.0.callee.object.name=JSON][expression.arguments.0.callee.property.name=stringify]",
message: "Do not use JSON.parse(JSON.stringify(...)) for deep copy. Use structuredClone instead.",
},
{
selector: "CallExpression[callee.name=test][arguments.0.value!=/^should/]",
message: "test should start with should",
},
],
/* eslint-enable max-len */
// === MUI Imports ===
"no-restricted-imports": [
"error",
{
paths: [
{
name: "@mui/material",
message: "Please use \"import ComponentName from '@mui/material/ComponentName'\" instead.",
},
{
name: "@mui/icons-material",
message: "Please use \"import IconName from '@mui/icons-material/IconName'\" instead.",
},
],
},
],
// === Prettier ===
"prettier/prettier": [
"error",
{
printWidth: 130,
singleQuote: false,
},
],
},
},
{
files: ["**/*.test.{ts,tsx}"],
rules: {
"@typescript-eslint/no-require-imports": "off",
},
},
]);