-
Notifications
You must be signed in to change notification settings - Fork 995
/
.eslintrc.js
393 lines (386 loc) · 12.4 KB
/
.eslintrc.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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
const { findUp } = require('@redwoodjs/project-config')
// Framework Babel config is monorepo root ./babel.config.js
// `yarn lint` runs for each workspace, which needs findUp for path to root
const findBabelConfig = (cwd = process.cwd()) => {
const configPath = findUp('babel.config.js', cwd)
if (!configPath) {
throw new Error(`Eslint-parser could not find a "babel.config.js" file`)
}
return configPath
}
module.exports = {
extends: [
'eslint:recommended',
'plugin:react/recommended',
'plugin:react-hooks/recommended',
'plugin:jest-dom/recommended',
],
parser: '@babel/eslint-parser',
parserOptions: {
ecmaVersion: 'latest',
ecmaFeatures: {
jsx: true,
},
babelOptions: {
configFile: findBabelConfig(),
},
},
plugins: [
'unused-imports',
'@babel',
'import',
'jsx-a11y',
'react',
'react-hooks',
'jest-dom',
'@redwoodjs',
],
// Prevents unused eslint-disable comments
reportUnusedDisableDirectives: true,
settings: {
react: {
version: 'detect',
},
// For the import/order rule. Configures how it tells if an import is "internal" or not.
// An "internal" import is basically just one that's aliased.
//
// See...
// - https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/order.md#groups-array
// - https://github.com/import-js/eslint-plugin-import/blob/main/README.md#importinternal-regex
'import/internal-regex': '^src/',
},
ignorePatterns: [
'node_modules',
'dist',
'fixtures',
'packages/babel-config/src/plugins/__tests__/__fixtures__/**/*',
'packages/babel-config/src/__tests__/__fixtures__/**/*',
'packages/codemods/**/__testfixtures__/**/*',
'packages/cli/**/__testfixtures__/**/*',
'packages/storage/src/__tests__/prisma-client/*',
],
rules: {
curly: 'error',
'unused-imports/no-unused-imports': 'error',
'@redwoodjs/process-env-computed': 'error',
'no-console': 'off',
'no-extra-semi': 'off',
'prefer-object-spread': 'warn',
'prefer-spread': 'warn',
'no-unused-expressions': [
'error',
{ allowShortCircuit: true, allowTernary: true },
],
'no-useless-escape': 'off',
camelcase: ['warn', { properties: 'never' }],
'no-new': 'warn',
'new-cap': ['error', { newIsCap: true, capIsNew: false }],
'no-unused-vars': [
'error',
{ varsIgnorePattern: '^_', argsIgnorePattern: '^_' },
],
// React rules
'react/prop-types': 'off',
'react/display-name': 'off',
'react-hooks/exhaustive-deps': 'warn',
'import/order': [
'error',
{
'newlines-between': 'always',
// We set this to an empty array to override the default value, which is `['builtin', 'external', 'object']`.
// Judging by the number of issues on the repo, this option seems to be notoriously tricky to understand.
// From what I can tell, if the value of this is `['builtin']` that means it won't sort builtins.
// But we have a rule for builtins below (react), so that's not what we want.
//
// See...
// - https://github.com/import-js/eslint-plugin-import/pull/1570
// - https://github.com/import-js/eslint-plugin-import/issues/1565
pathGroupsExcludedImportTypes: [],
// Only doing this to add internal. The order here maters.
// See https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/order.md#groups-array
groups: [
'builtin',
'external',
'internal',
'parent',
'sibling',
'index',
],
pathGroups: [
{
pattern: 'react',
group: 'builtin',
position: 'after',
},
{
pattern: '@redwoodjs/**',
group: 'external',
position: 'after',
},
{
// Matches...
// - src/directives/**/*.{js,ts}
// - src/services/**/*.{js,ts}
// - src/graphql/**/*.sdl.{js,ts}
//
// Uses https://github.com/isaacs/minimatch under the hood
// See https://github.com/isaacs/node-glob#glob-primer for syntax
pattern: 'src/*/**/*.?(sdl.){js,ts}',
patternOptions: {
nobrace: true,
noglobstar: true,
},
group: 'internal',
position: 'before',
},
],
alphabetize: {
order: 'asc',
caseInsensitive: true,
},
},
],
},
env: {
// We use the most modern environment available. Then we rely on Babel to
// transpile it to something that can run on all node versions we support
es2022: true,
},
overrides: [
// We disable react-hooks/rules-of-hooks for packages which do not deal with React code
{
files: [
'packages/api-server/**/*.ts',
'packages/graphql-server/**/*.ts',
'packages/realtime/**/*.ts',
],
rules: {
'react-hooks/rules-of-hooks': 'off',
},
},
// TypeScript specific linting
{
files: ['*.ts', '*.mts', '*.tsx'],
parser: '@typescript-eslint/parser',
parserOptions: {
project: './tsconfig.eslint.json',
tsconfigRootDir: __dirname,
},
extends: [
'plugin:@typescript-eslint/recommended-type-checked',
'plugin:@typescript-eslint/stylistic-type-checked',
],
rules: {
// This is disabled for now because of our legacy usage of `require`. It should be enabled in the future.
'@typescript-eslint/no-require-imports': 'off',
// This is disabled for now because of our vast usage of `any`. It should be enabled in the future.
'@typescript-eslint/no-explicit-any': 'off',
// We allow exceptions to the no-unused-vars rule for variables that start with an underscore
'no-unused-vars': 'off',
'@typescript-eslint/no-unused-vars': [
'error',
{ varsIgnorePattern: '^_', argsIgnorePattern: '^_' },
],
// We want consistent `import type {} from '...'`
'@typescript-eslint/consistent-type-imports': 'error',
// We want consistent curly brackets
curly: 'error',
// Stylistic rules we have disabled
'@typescript-eslint/consistent-indexed-object-style': 'off',
'@typescript-eslint/consistent-type-definitions': 'off',
'@typescript-eslint/no-empty-function': 'off',
'@typescript-eslint/prefer-function-type': 'off',
camelcase: 'off',
// TODO(jgmw): Work through these and either keep disabled or fix and re-enable
'@typescript-eslint/no-unsafe-call': 'off',
'@typescript-eslint/no-unsafe-assignment': 'off',
'@typescript-eslint/require-await': 'off',
'@typescript-eslint/prefer-nullish-coalescing': 'off',
'@typescript-eslint/no-unsafe-member-access': 'off',
'@typescript-eslint/dot-notation': 'off',
'@typescript-eslint/no-unsafe-argument': 'off',
'@typescript-eslint/no-unsafe-return': 'off',
'@typescript-eslint/prefer-promise-reject-errors': 'off',
'@typescript-eslint/no-redundant-type-constituents': 'off',
'@typescript-eslint/restrict-plus-operands': 'off',
'@typescript-eslint/no-misused-promises': 'off',
'@typescript-eslint/no-floating-promises': 'off',
'@typescript-eslint/prefer-regexp-exec': 'off',
'@typescript-eslint/restrict-template-expressions': 'off',
'@typescript-eslint/non-nullable-type-assertion-style': 'off',
'@typescript-eslint/no-base-to-string': 'off',
'@typescript-eslint/unbound-method': 'off',
},
},
{
files: ['*.test.*', '**/__mocks__/**'],
env: {
node: true,
es6: true,
commonjs: true,
jest: true,
},
},
// Set the correct environment for this eslint config file
{
files: ['.eslintrc.js'],
env: {
node: true,
commonjs: true,
},
},
// Set the correct environment for Jest config files
{
files: ['jest.config.js', 'jest.setup.js'],
env: {
node: true,
commonjs: true,
jest: true,
},
},
// Browser Context
//
// We prevent "window" from being used, and instead require "global".
// This is because prerender runs in the NodeJS context it's undefined.
{
files: [
'packages/auth/src/**',
'packages/forms/src/**',
'packages/prerender/src/browserUtils/**',
'packages/router/src/**',
'packages/web/src/**',
],
env: {
browser: true,
},
globals: {
window: 'off', // Developers should use `global` instead of window. Since window is undefined in NodeJS.
},
},
// Prevent @redwoodjs/internal imports in runtime (web+api) packages
{
files: [
'packages/auth/src/**',
'packages/forms/src/**',
'packages/prerender/src/browserUtils/**',
'packages/router/src/**',
'packages/web/src/**',
'packages/api/src/**',
'packages/graphql-server/src/**',
'packages/record/src/**',
'packages/project-config/src/**',
],
rules: {
'no-restricted-imports': [
'error',
{
patterns: [
{
group: ['@redwoodjs/internal', '@redwoodjs/internal/*'],
message:
'Do not import "@redwoodjs/internal" or subpackages in runtime modules, because it leads to MASSIVE bundle sizes',
},
{
group: ['@redwoodjs/structure', '@redwoodjs/structure/*'],
message:
'Do not import "@redwoodjs/structure" or subpackages in runtime modules, because it leads to MASSIVE bundle sizes',
},
],
},
],
},
},
// Entry.js rules
{
files: ['packages/web/src/entry/index.jsx'],
env: {
browser: true,
},
globals: {
React: 'readonly',
},
},
// NodeJS Context
{
files: [
'packages/api/src/**',
'packages/api-server/src/**',
'packages/cli/src/**',
'packages/create-redwood-app/src/*.js',
'packages/internal/src/**',
'packages/prerender/src/**',
'packages/structure/src/**',
'packages/testing/src/**',
'packages/testing/config/**',
'packages/eslint-config/*.js',
'packages/record/src/**',
'packages/telemetry/src/**',
'packages/vite/bins/**',
],
env: {
node: true,
},
},
// Prevent bad imports in Node packages - cli and api packages
{
files: [
'packages/api/src/**',
'packages/api-server/src/**',
'packages/cli/src/**',
'packages/internal/src/**',
'packages/prerender/src/**',
'packages/structure/src/**',
'packages/testing/src/**',
'packages/testing/config/**',
'packages/eslint-config/*.js',
'packages/record/src/**',
'packages/telemetry/src/**',
],
rules: {
'no-restricted-imports': [
// for import x from ('@redwoodjs/internal')
'error',
{
name: '@redwoodjs/internal',
message:
'To prevent bloat in CLI, do not import "@redwoodjs/internal" directly. Instead import like @redwoodjs/internal/dist/<file>, or await import',
},
],
'no-restricted-modules': [
// for require('@redwoodjs/internal')
'error',
{
name: '@redwoodjs/internal',
message:
'To prevent bloat in CLI, do not require "@redwoodjs/internal" directly. Instead require like @redwoodjs/internal/dist/<file>',
},
],
},
},
// Allow computed member access on process.env in NodeJS contexts and tests
{
files: ['packages/testing/**', 'packages/vite/src/index.ts'],
rules: {
'@redwoodjs/process-env-computed': 'off',
},
},
{
files: ['packages/project-config/**'],
excludedFiles: [
'**/__tests__/**',
'**/*.test.ts?(x)',
'**/*.spec.ts?(x)',
],
rules: {
'import/no-extraneous-dependencies': [
'error',
{
devDependencies: false,
optionalDependencies: false,
peerDependencies: true,
},
],
},
},
],
}