-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy patheslint.config.mjs
More file actions
478 lines (454 loc) · 14.6 KB
/
eslint.config.mjs
File metadata and controls
478 lines (454 loc) · 14.6 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
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
import babelParser from '@babel/eslint-parser'
import babelPlugin from '@babel/eslint-plugin'
import js from '@eslint/js'
import importPlugin from 'eslint-plugin-import'
import jestDomPlugin from 'eslint-plugin-jest-dom'
import jsxA11yPlugin from 'eslint-plugin-jsx-a11y'
import reactPlugin from 'eslint-plugin-react'
import reactHooksPlugin from 'eslint-plugin-react-hooks'
import unusedImportsPlugin from 'eslint-plugin-unused-imports'
import globals from 'globals'
import tseslint from 'typescript-eslint'
import cedarjsPlugin from '@cedarjs/eslint-plugin'
import { findUp } from '@cedarjs/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
}
export default [
// Global ignores
{
ignores: [
'**/node_modules/**',
'**/dist/**',
'**/fixtures/**',
'**/__fixtures__/**',
'**/__testfixtures__/**',
'**/*.template',
'packages/babel-config/src/plugins/__tests__/__fixtures__/**/*',
'packages/babel-config/src/__tests__/__fixtures__/**/*',
'packages/codemods/**/__testfixtures__/**/*',
'packages/cli/**/__testfixtures__/**/*',
'packages/internal/src/__tests__/__fixtures__/**/*',
'packages/prerender/**/__tests__/__fixtures__/**/*',
'packages/storage/src/__tests__/prisma-client/*',
'packages/testing/config',
'packages/testing/**/__fixtures__/**/*',
'packages/vite/src/plugins/__tests__/__fixtures__/**/*',
'packages/create-cedar-rsc-app/**',
'packages/create-cedar-app/templates/**',
],
},
// Configuration for this eslint config file itself
{
files: ['eslint.config.mjs'],
languageOptions: {
globals: {
...globals.node,
},
},
},
// Base configuration for all files
js.configs.recommended,
{
plugins: {
'@babel': babelPlugin,
import: importPlugin,
'jsx-a11y': jsxA11yPlugin,
react: reactPlugin,
'react-hooks': reactHooksPlugin,
'jest-dom': jestDomPlugin,
'unused-imports': unusedImportsPlugin,
'@cedarjs': cedarjsPlugin,
},
linterOptions: {
reportUnusedDisableDirectives: 'error',
},
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/',
},
rules: {
// React recommended rules
...reactPlugin.configs.flat.recommended.rules,
...reactPlugin.configs.flat['jsx-runtime'].rules,
// React hooks recommended rules
...reactHooksPlugin.configs.recommended.rules,
// Jest DOM recommended rules
...jestDomPlugin.configs.recommended.rules,
curly: 'error',
'unused-imports/no-unused-imports': '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: '@cedarjs/**',
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,
},
},
],
},
},
// JavaScript files specific configuration
{
files: ['**/*.js', '**/*.jsx', '**/*.cjs', '**/*.mjs'],
languageOptions: {
parser: babelParser,
ecmaVersion: 'latest',
sourceType: 'module',
parserOptions: {
ecmaFeatures: {
jsx: true,
},
babelOptions: {
configFile: findBabelConfig(),
},
},
globals: {
...globals.es2022,
},
},
},
// 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
...tseslint.config({
files: ['**/*.ts', '**/*.mts', '**/*.tsx'],
extends: [
...tseslint.configs.recommendedTypeChecked,
...tseslint.configs.stylisticTypeChecked,
],
languageOptions: {
parserOptions: {
project: './tsconfig.eslint.json',
tsconfigRootDir: import.meta.dirname,
},
},
rules: {
// Disable unused-imports for TypeScript files, use TypeScript's own rule instead
'unused-imports/no-unused-imports': 'off',
// 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',
},
}),
// Test files
{
files: ['*.test.*', '**/__mocks__/**', '**/*.test.*'],
languageOptions: {
globals: {
...globals.node,
...globals.commonjs,
...globals.jest,
},
},
},
// Config files (.babelrc.js, jest.config.js, etc.) and CJS wrapper files
{
files: [
'**/.babelrc.js',
'**/.babelrc.cjs',
'**/babel.config.js',
'**/jest.config.js',
'**/jest.setup.js',
'**/*.config.js',
'**/*.config.cjs',
'**/*.config.mjs',
'docs/sidebars.js',
'packages/web/apollo/index.js',
'packages/web/toast/index.js',
'packages/auth-providers/dbAuth/web/webAuthn/index.js',
],
languageOptions: {
globals: {
...globals.node,
...globals.commonjs,
},
sourceType: 'commonjs',
},
},
// 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/**',
],
languageOptions: {
globals: {
...globals.browser,
window: 'off', // Developers should use `global` instead of window. Since window is undefined in NodeJS.
},
},
},
// Prevent @cedarjs/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: ['@cedarjs/internal', '@cedarjs/internal/*'],
message:
'Do not import "@cedarjs/internal" or subpackages in runtime modules, because it leads to MASSIVE bundle sizes',
},
{
group: ['@cedarjs/structure', '@cedarjs/structure/*'],
message:
'Do not import "@cedarjs/structure" or subpackages in runtime modules, because it leads to MASSIVE bundle sizes',
},
],
},
],
},
},
// Entry.js rules
{
files: ['packages/web/src/entry/index.jsx'],
languageOptions: {
globals: {
...globals.browser,
React: 'readonly',
},
},
},
// NodeJS Context
{
files: [
'.github/**',
'docs/ignore_build.mjs',
'packages/api/src/**',
'packages/api-server/src/**',
'packages/cli/src/**',
'packages/create-cedar-app/src/*.js',
'packages/create-cedar-app/scripts/**',
'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/**',
'packages/cli-packages/**/*.mjs',
'packages/codemods/tasks/**',
'packages/ogimage-gen/cjsWrappers/**',
'tasks/**',
],
languageOptions: {
globals: {
...globals.node,
},
},
},
// 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 ('@cedarjs/internal')
'error',
{
name: '@cedarjs/internal',
message:
'To prevent bloat in CLI, do not import "@cedarjs/internal" directly. Instead import like @cedarjs/internal/dist/<file>, or await import',
},
],
'no-restricted-modules': [
// for require('@cedarjs/internal')
'error',
{
name: '@cedarjs/internal',
message:
'To prevent bloat in CLI, do not require "@cedarjs/internal" directly. Instead require like @cedarjs/internal/dist/<file>',
},
],
},
},
// Allow computed member access on process.env in NodeJS contexts and tests
{
files: [
'packages/project-config/src/envVarDefinitions.ts',
'packages/testing/**',
'packages/vite/src/plugins/vite-plugin-cedar-html-env.ts',
'.github/**',
],
rules: {
'@cedarjs/process-env-computed': 'off',
},
},
// project-config package specific rules
{
files: ['packages/project-config/**'],
ignores: ['**/__tests__/**', '**/*.test.ts?(x)', '**/*.spec.ts?(x)'],
plugins: {
import: importPlugin,
},
rules: {
'import/no-extraneous-dependencies': [
'error',
{
devDependencies: false,
optionalDependencies: false,
peerDependencies: true,
},
],
},
},
]