-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patheslint.config.js
242 lines (232 loc) · 8.09 KB
/
eslint.config.js
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
import js from '@eslint/js'
import globals from 'globals'
import importPlugin from 'eslint-plugin-import'
import reactHooks from 'eslint-plugin-react-hooks'
import reactRefresh from 'eslint-plugin-react-refresh'
import tseslint from 'typescript-eslint'
import tailwindPlugin from 'eslint-plugin-tailwindcss'
import path from 'path'
import fs from 'fs'
const FEATURES_DIR = './src/features'
/**
* Traverse the features directory and return an array of restricted paths for
* use in the `import/no-restricted-paths` rule.
*
* @example
* ```js
* [
* {
* except: [ './dependencies' ],
* from: './src/features',
* target: './src/features/dependencies'
* },
* {
* except: [ './versions' ],
* from: './src/features',
* target: './src/features/versions'
* },
* {
* except: [ './vulnerabilities' ],
* from: './src/features',
* target: './src/features/vulnerabilities'
* }
* ]
* ```
*/
const getRestrictedPathsForFeatureDir = () => {
const featureDirPath = path.resolve(FEATURES_DIR)
/**
* @type {Array<{except: `./${string}`[], from: './src/features', target: string}>}
*/
const restrictedPaths = []
try {
const featureDirs = fs.readdirSync(featureDirPath)
featureDirs.forEach((featureDir) => {
const subPath = path.join(featureDirPath, featureDir)
if (fs.lstatSync(subPath).isDirectory()) {
restrictedPaths.push({
except: [`./${featureDir}`],
from: FEATURES_DIR,
target: path.join(FEATURES_DIR, featureDir),
})
}
})
} catch (error) {
console.error('Error reading features directory:', error)
}
return restrictedPaths
}
const restrictedSyntax = {
reactQuery: {
useQuery: (v) =>
`CallExpression[callee.name='useQuery'] > ObjectExpression:first-child > Property[key.name='${v}']`,
useQueries: (v) =>
`CallExpression[callee.name='useQueries'] > ObjectExpression:first-child > Property[key.name='queries'] > ArrayExpression > ObjectExpression > Property[key.name='${v}']`,
},
}
export default tseslint.config(
{ ignores: ['dist'] },
{
extends: [
js.configs.recommended,
...tseslint.configs.recommended,
...tailwindPlugin.configs['flat/recommended'],
],
files: ['**/*.{ts,tsx}'],
languageOptions: {
parserOptions: {
projectService: true,
tsconfigRootDir: import.meta.dirname,
ecmaFeatures: {
jsx: true,
},
},
globals: {
...globals.browser,
...globals.node,
},
},
plugins: {
'react-hooks': reactHooks,
'react-refresh': reactRefresh,
import: importPlugin,
},
settings: {
'import/resolver': {
typescript: true,
node: true,
},
tailwindcss: {
callees: ['tv', 'twMerge'],
config: './tailwind.config.ts',
},
},
rules: {
...reactHooks.configs.recommended.rules,
'react-refresh/only-export-components': [
'warn',
{ allowConstantExport: true },
],
'tailwindcss/enforces-negative-arbitrary-values': 'error',
'tailwindcss/enforces-shorthand': 'error',
'tailwindcss/classnames-order': 'off', // handled by prettier
'tailwindcss/no-contradicting-classname': 'error',
'tailwindcss/no-custom-classname': [
'error',
{
callees: ['tv', 'twMerge'],
whitelist: [
'theme\\-(minder|trusty)',
'light',
'dark',
'scrollbar-thin',
'font-default',
'font-title',
'font-code',
'subhead-bold',
'subhead-regular',
],
},
],
'no-restricted-syntax': [
'error',
{
selector: [
restrictedSyntax.reactQuery.useQuery('staleTime'),
restrictedSyntax.reactQuery.useQuery('gcTime'),
restrictedSyntax.reactQuery.useQueries('staleTime'),
restrictedSyntax.reactQuery.useQueries('gcTime'),
].join(', '),
message:
'`staleTime` & `gcTime` should be managed via the `getQueryCacheConfig` util instead.',
},
{
selector: [
restrictedSyntax.reactQuery.useQuery('queryKey'),
restrictedSyntax.reactQuery.useQuery('queryFn'),
restrictedSyntax.reactQuery.useQueries('queryKey'),
restrictedSyntax.reactQuery.useQueries('queryFn'),
].join(', '),
message:
"'queryKey' & 'queryFn' should be managed by openapi-ts react-query integration instead. This allows standardized management of query keys & cache invalidation.",
},
{
selector: [
restrictedSyntax.reactQuery.useQuery('refetchOnMount'),
restrictedSyntax.reactQuery.useQuery('refetchOnReconnect'),
restrictedSyntax.reactQuery.useQuery('refetchOnWindowFocus'),
restrictedSyntax.reactQuery.useQueries('refetchOnMount'),
restrictedSyntax.reactQuery.useQueries('refetchOnReconnect'),
restrictedSyntax.reactQuery.useQueries('refetchOnWindowFocus'),
].join(', '),
message:
'`refetchOnMount`, `refetchOnReconnect` & `refetchOnWindowFocus` should be managed centrally in the react-query provider',
},
{
selector:
"CallExpression > MemberExpression[property.name='invalidateQueries']",
message:
'Do not directly call `invalidateQueries`. Instead, use the `invalidateQueries` helper function.',
},
{
selector: [
"CallExpression[callee.object.name='http'][callee.property.name='all'] > Literal:first-child",
"CallExpression[callee.object.name='http'][callee.property.name='head'] > Literal:first-child",
"CallExpression[callee.object.name='http'][callee.property.name='get'] > Literal:first-child",
"CallExpression[callee.object.name='http'][callee.property.name='post'] > Literal:first-child",
"CallExpression[callee.object.name='http'][callee.property.name='put'] > Literal:first-child",
"CallExpression[callee.object.name='http'][callee.property.name='delete'] > Literal:first-child",
"CallExpression[callee.object.name='http'][callee.property.name='patch'] > Literal:first-child",
"CallExpression[callee.object.name='http'][callee.property.name='options'] > Literal:first-child",
].join(', '),
message:
"Do not pass a string as the first argument to methods on Mock Service Worker's `http`. Use the `mswEndpoint` helper function instead, which provides type-safe routes based on the OpenAPI spec and the API base URL.",
},
],
'no-restricted-imports': [
'error',
{
paths: [
{
importNames: ['useMutation'],
message: 'Use the custom `useToastMutation` instead',
name: '@tanstack/react-query',
},
],
},
],
'import/no-restricted-paths': [
'error',
{
zones: [
// disables cross-feature imports:
// eg. src/features/dashboard-alerts should not import from src/features/dashboard-messages, etc.
...getRestrictedPathsForFeatureDir(),
// enforce unidirectional codebase:
// e.g. src/routes can import from src/features but not the other way around
{
from: './src/routes',
target: './src/features',
},
// enforce unidirectional codebase:
// e.g src/features and src/routes can import from these shared modules but not the other way around
{
from: ['./src/features', './src/routes'],
target: [
'./src/components',
'./src/constants',
'./src/hooks',
'./src/i18n',
'./src/lib',
'./src/mocks',
'./src/trusty-api',
'./src/types',
'./src/utils',
],
},
],
},
],
},
}
)