-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheslint.config.ts
More file actions
821 lines (795 loc) · 24 KB
/
Copy patheslint.config.ts
File metadata and controls
821 lines (795 loc) · 24 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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
/* eslint-disable */
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import type { Linter } from 'eslint'
import eslint from '@eslint/js'
import astroPlugin from 'eslint-plugin-astro'
import importLitePlugin from 'eslint-plugin-import-lite'
import jsdocPlugin from 'eslint-plugin-jsdoc'
import securityPlugin from 'eslint-plugin-security'
import ymlPlugin from 'eslint-plugin-yml'
import tsPlugin from 'typescript-eslint'
import restrictedGlobals from 'confusing-browser-globals'
import globals from 'globals'
import enforceCentralizedEventsRule from './test/eslint/enforce-centralized-events-rule'
import noHtmlElementAssertionsRule from './test/eslint/no-html-element-assertions-rule'
import noQuerySelectorOutsideSelectorsRule from './test/eslint/no-query-selector-outside-selectors-rule'
const level: Linter.StringSeverity = 'error'
const astroParser = (astroPlugin as unknown as { parser: Linter.Parser }).parser
const tsconfigRootDir = path.dirname(fileURLToPath(import.meta.url))
export default [
eslint.configs.recommended,
...tsPlugin.configs.recommended,
...astroPlugin.configs.recommended,
importLitePlugin.configs.recommended,
jsdocPlugin.configs['flat/recommended-typescript'],
securityPlugin.configs.recommended,
...ymlPlugin.configs['flat/recommended'],
{
plugins: {
'custom-rules': {
rules: {
'enforce-centralized-events': enforceCentralizedEventsRule,
'no-html-element-assertions': noHtmlElementAssertionsRule,
'no-query-selector-outside-selectors': noQuerySelectorOutsideSelectorsRule,
},
},
},
languageOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
globals: {
...globals.node, // Covers 'process', 'Buffer', etc.
NodeJS: 'readonly', // Covers TS namespaces
console: 'readonly', // Specific fix for 'console'
},
},
rules: {
/**
* Custom ESLint rules
*/
'custom-rules/enforce-centralized-events': 'error',
'custom-rules/no-html-element-assertions': 'error',
'custom-rules/no-query-selector-outside-selectors': 'off',
/**
* Common rule settings for all linted files
*/
'@typescript-eslint/ban-ts-comment': 'off',
'@typescript-eslint/consistent-type-assertions': [
level,
{ assertionStyle: 'as', objectLiteralTypeAssertions: 'allow-as-parameter' },
],
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-unsafe-argument': 'off',
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/no-unsafe-assignment': 'off',
'@typescript-eslint/no-unsafe-call': 'off',
'@typescript-eslint/no-unsafe-function-type': level,
'@typescript-eslint/no-unsafe-member-access': 'off',
'@typescript-eslint/no-unsafe-return': 'off',
'@typescript-eslint/no-unused-vars': [
level,
{ varsIgnorePattern: '^_', argsIgnorePattern: '^_|^this$' },
],
'@typescript-eslint/no-empty-object-type': level,
'@typescript-eslint/no-var-requires': level,
'@typescript-eslint/no-unnecessary-type-assertion': 'off',
'@typescript-eslint/no-wrapper-object-types': level,
'@typescript-eslint/restrict-template-expressions': 'off',
'@typescript-eslint/triple-slash-reference': 'off',
/** Issue with Prettier https://github.com/prettier/eslint-plugin-prettier/issues/65: */
'arrow-body-style': 'off',
'camelcase': [level],
'curly': 'off',
'import-lite/no-duplicates': level,
'jsdoc/check-indentation': level,
'jsdoc/check-line-alignment': 'off',
'jsdoc/check-param-names': 'off',
'jsdoc/check-values': 'off',
'jsdoc/multiline-blocks': 'off',
'jsdoc/newline-after-description': 'off',
'jsdoc/no-bad-blocks': level,
'jsdoc/no-defaults': level,
'jsdoc/no-types': 'off',
'jsdoc/require-jsdoc': 'off',
'jsdoc/tag-lines': 'off',
'jsdoc/require-param': 'off',
'jsdoc/require-param-type': 'off',
'jsdoc/require-returns': 'off',
'jsdoc/require-returns-type': 'off',
'jsdoc/valid-types': 'off',
'new-cap': [level, { newIsCap: true, capIsNew: false }],
'no-empty-pattern': [level, { "allowObjectPatternsAsParameters": true }],
'no-new': level,
'no-restricted-globals': ['error'].concat(restrictedGlobals),
'no-restricted-imports': [
'error', {
patterns: [
{
group: ['*'],
importNames: ['*'],
message: 'Wildcard imports are not allowed. Use named imports instead with object destructuring.',
},
],
},
],
'no-unused-expressions': [level, { allowShortCircuit: true, allowTernary: true }],
'no-unused-vars': [level, { varsIgnorePattern: '^_', argsIgnorePattern: '^_|^this$' }],
'no-useless-escape': 'off',
/** Issue with Prettier https://github.com/prettier/eslint-plugin-prettier/issues/65: */
'prefer-arrow-callback': 'off',
'prefer-object-spread': level,
'prefer-spread': level,
'security/detect-non-literal-fs-filename': 'off',
'security/detect-no-csrf-before-method-override': 'off',
'security/detect-object-injection': 'off',
'security/detect-unsafe-regex': 'off',
'semi': ['error', 'never'],
},
},
/**
* =================================================================================================
*
* Enforce centralized DOM querying in client-side component code.
* querySelector/querySelectorAll are only allowed in `selectors.ts` or `*.spec.ts`.
*
* =================================================================================================
*/
{
files: [
'src/components/**/*.{ts,astro}',
'src/components/scripts/**/*.{ts,astro}',
],
rules: {
'custom-rules/no-query-selector-outside-selectors': [
level,
{
allowedFiles: [
'src/components/Head/ThemeInit.astro',
'src/components/Copy/client/utils.ts',
'src/components/scripts/store/themes.ts',
],
},
],
},
},
/**
* =================================================================================================
*
* Restrict error instantiation to the correct directories. Restricts usage of the built-in
* Error class to custom error classes only. Makes sure custom error classes are used in
* appropriate contexts - BuildError, ClientScriptError, TestError.
*
* =================================================================================================
*/
{
/** Disable `new Error` calls */
files: [
'**/*.astro',
'**/*.ts',
'**/*.tsx',
],
rules: {
'no-restricted-syntax': [
level,
{
selector: 'NewExpression[callee.name="Error"]',
message: 'Use project-specific error classes instead of the base Error constructor.',
},
],
},
},
{
/** BuildError only in server-only or integration contexts */
files: [
'**/*.astro',
'**/*.ts',
'**/*.tsx',
],
ignores: [
'src/integrations/**/*',
'src/**/server/**/*',
'src/lib/**/*.ts',
],
rules: {
'no-restricted-syntax': [
level,
{
selector: 'NewExpression[callee.name="BuildError"]',
message: 'BuildError can only be instantiated in server-only or integration contexts.',
},
],
},
},
{
/** ClientScriptError only in client directories and the client scripts directory */
files: [
'**/*.astro',
'**/*.ts',
'**/*.tsx',
],
ignores: [
'src/**/client/**/*',
'src/components/scripts/**/*',
],
rules: {
'no-restricted-syntax': [
level,
{
selector: 'NewExpression[callee.name="ClientScriptError"]',
message: 'ClientScriptError can only be instantiated within client-side component directories.',
},
],
},
},
{
/** TestError only in unit or e2e test directories */
files: [
'**/*.astro',
'**/*.ts',
'**/*.tsx',
],
ignores: [
'src/**/__tests__/**',
'test/e2e/specs/**/*',
],
rules: {
'no-restricted-syntax': [
level,
{
selector: 'NewExpression[callee.name="TestError"]',
message: 'TestError can only be instantiated in unit or e2e test directories.',
},
],
},
},
{
/** Directories defining custom errors can use any error class. */
files: [
'**/*.astro',
'**/*.ts',
'**/*.tsx',
],
ignores: [
'test/e2e/helpers/pageObjectModels/**/*',
'test/e2e/specs/**',
],
rules: {
'no-restricted-syntax': [
level,
{
selector: 'NewExpression[callee.name="EvaluationError"]',
message: 'EvaluationError can only be instantiated in e2e test directories.',
},
],
},
},
{
files: [
'src/lib/errors/**/*',
'src/components/scripts/errors/**/*',
'test/errors/**/*',
'src/pages/api/_errors/ApiFunctionError.ts',
],
rules: {
'no-restricted-syntax': 'off',
},
},
/**
* =================================================================================================
*
* Test files need some relaxed rules to allow "any" types and "as" type assertions
*
* =================================================================================================
*/
{
files: ['**/*.spec.ts'],
rules: {
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/consistent-type-assertions': 'off',
},
},
/**
* =================================================================================================
*
* Allow CSS module class access via dot-notation in Astro files. CSS modules expose an index
* signature which normally forces bracket notation; this keeps those files readable while
* retaining the stricter behavior elsewhere.
*
* =================================================================================================
*/
{
files: [
'src/components/**/*.astro',
'src/pages/**/*.astro',
],
languageOptions: {
parser: astroParser,
parserOptions: {
parser: tsPlugin.parser,
project: './tsconfig.json',
tsconfigRootDir,
extraFileExtensions: ['.astro'],
programs: undefined,
},
},
rules: {
'@typescript-eslint/dot-notation': [level, { allowIndexSignaturePropertyAccess: true }],
},
},
/**
* =================================================================================================
*
* These files implement type guards and legitimately needs type assertions
*
* =================================================================================================
*/
{
files: [
'**/*error.spec.ts',
'src/components/scripts/assertions/elements.ts',
'test/e2e/assertions/index.ts',
],
rules: {
'custom-rules/no-html-element-assertions': 'off',
},
},
/**
* =================================================================================================
*
* Exceptions to the project custom rule that prevents direct use of addEventListener in
* favor of centralized utilities
*
* =================================================================================================
*/
{
files: [
/** Centralized utilities providing event listener management */
'src/components/scripts/elementListeners/index.ts',
/** Needs direct addEventListener for pause/resume/reset lifecycle */
'src/components/Social/Shares/client.ts',
],
rules: {
'custom-rules/enforce-centralized-events': 'off',
},
},
/**
* =================================================================================================
*
* Database files that use snake_case for database field names
*
* =================================================================================================
*/
{
files: [
'.github/actions/**/*',
'src/actions/**/*',
'src/components/scripts/sentry/__tests__/helpers.spec.ts',
'src/components/scripts/store/__tests__/socialEmbeds.spec.ts',
'src/lib/config/pwa.ts',
'src/lib/config/serviceWorker.ts',
'src/pages/api/**/*',
'src/pages/manifest.json.ts',
'test/e2e/specs/15-cron/cron.spec.ts',
],
rules: {
camelcase: 'off',
},
},
/**
* =================================================================================================
*
* No process.env use in general. This is to prevent mis-use in client code
* and ensure expected environmental variables are available in code context.
*
* =================================================================================================
*/
{
files: [
'**/*.astro',
'**/*.ts',
'**/*.tsx',
],
rules: {
'no-process-env': level,
},
},
{
/** These directories can use process.env, which is forbidden in other files */
files: [
'.eslintrc.js',
'.github/actions/**/*',
'astro.config.ts',
'playwright.config.ts',
'vitest.config.ts',
'vitest.setup.ts',
'src/components/scripts/utils/environmentClient.ts',
'src/lib/config/**/*',
'src/pages/api/_utils/environment/**/*',
'test/e2e/config/runtime/database.ts',
'test/e2e/config/global-setup.ts',
'test/e2e/config/runtime/mockState.ts',
'test/e2e/db/libsqlClient.ts',
'test/e2e/helpers/pageObjectModels/**/*',
'test/e2e/helpers/mockServices.ts',
'test/e2e/specs/05-api/cron.spec.ts',
],
rules: {
'no-process-env': 'off',
},
},
/**
* =================================================================================================
*
* No import.meta.env use in general, for the same reason as the restriction on use of
* process.env. This is to prevent mis-use in client code and ensure expected environmental
* variables are available in code context.
*
* =================================================================================================
*/
{
files: [
'src/components/**/*',
'src/layouts/**/*',
'src/pages/**/*',
],
rules: {
'no-restricted-syntax': [
level,
{
selector: 'MemberExpression[property.name="env"] > MetaProperty[meta.name="import"][property.name="meta"]',
message: 'Do not use import.meta.env directly. See docs/ENVIRONMENT_VARIABLES.',
},
],
},
},
{
/** These files can use import.meta.env */
files: [
'src/components/scripts/utils/environmentClient.ts',
'src/components/scripts/utils/siteUrlClient.ts',
'src/lib/config/environmentServer.ts',
'src/lib/config/siteUrlServer.ts',
'src/pages/api/_utils/environment/environmentApi.ts',
'test/e2e/config/global-setup.ts',
],
rules: {
'no-restricted-syntax': 'off',
},
},
/**
* =================================================================================================
*
* Enforce use of path aliases in the project over relative imports
*
* =================================================================================================
*/
{
files: [
'**/*.astro',
'**/*.ts',
'**/*.tsx',
],
rules: {
'no-restricted-imports': [
level,
{
patterns: [
{
group: ['../*'],
message: 'Usage of relative imports is not allowed. Use path aliases.',
},
],
},
],
},
},
{
files: [
'src/integrations/**/*',
'src/lib/config/**/*',
],
rules: {
'no-restricted-imports': [
level, {
patterns: [
{
group: ['@/*'],
message: 'Path aliases cannot be used in files that are imported by astro.config.ts. See notes in that config file for the reasons why.',
},
],
},
],
},
},
/**
* =================================================================================================
*
* The following rules are designed to enforce separation of client-side, SSR,
* and build-time server code so that Vite does not incorrectly bundle code.
*
* =================================================================================================
*/
/**
* Restrict the server directory in components, layouts, and pages to
* importing only from src/components/scripts
*/
{
files: [
'src/**/client/**/*.{astro,ts,tsx}',
],
ignores: [
'src/pages/api/_environment/environmentApi.ts',
'src/pages/api/_logger/index.ts',
],
rules: {
'no-restricted-imports': [
level,
{
patterns: [
{
group: ['src/lib/**/*', '@lib/**/*'],
message: 'The src/lib directory is for build-time code only.',
},
],
},
],
},
},
/**
* Restrict the client directory in components, layouts, and pages to importing only from src/lib
*/
{
files: [
'src/**/server/**/*.{astro,ts,tsx}',
],
ignores: [
'src/pages/api/_environment/environmentApi.ts',
'src/pages/api/_logger/index.ts',
],
rules: {
'no-restricted-imports': [
level,
{
patterns: [
{
group: [
'src/components/scripts/**/*',
'@components/scripts/**/*',
],
message: 'The src/components/scripts directory is for client bundle code only.',
},
],
},
],
},
},
/**
* Test files can import from server code in src/lib, but must use server-side helpers
*/
{
files: [
'src/**/__tests__/**/*'
],
ignores: [
'src/components/scripts/utils/__tests__/siteUrlClient.spec.ts',
],
rules: {
'no-restricted-imports': [
level,
{
paths: [
{
name: 'src/components/scripts/utils/environmentClient.ts',
message: 'use src/lib/config/environmentServer.ts in test files, not environmentClient.',
},
{
name: '@components/scripts/utils/environmentClient',
message: 'use src/lib/config/environmentServer.ts in test files, not environmentClient.',
},
{
name: 'src/components/scripts/utils/siteUrlClient.ts',
message: 'use src/lib/config/siteUrlServer.ts in test files, not siteUrlClient.',
},
{
name: '@components/scripts/utils/siteUrlClient',
message: 'use src/lib/config/siteUrlServer.ts in test files, not siteUrlClient.',
},
],
},
],
},
},
/**
* =================================================================================================
*
* Control use of "astro:env" helper for environmental variable imports.
*
* =================================================================================================
*/
{
files: [
'src/lib/**/*.ts',
'src/pages/api/**/*',
],
rules: {
'no-restricted-imports': [
level, {
patterns: [
{
group: ['astro:env/client'],
message: 'SSR API routes and server code must use astro:env/server, not astro:env/client.',
},
],
},
],
},
},
{
files: [
'src/components/**/*',
'src/layouts/**/*',
'src/pages/**/*',
],
rules: {
'no-restricted-imports': [
level, {
patterns: [
{
group: ['astro:env/server'],
message: 'Client code must use astro:env/client, not astro:env/server.',
},
],
},
],
},
},
/**
* =================================================================================================
*
* Control use of environment helpers based on project area (client, API, build-time, test)
*
* =================================================================================================
*/
{
files: [
'**/*.astro',
'**/*.ts',
'**/*.tsx',
],
ignores: [
'src/**/client/**',
'src/components/scripts/**/*',
'src/pages/testing/**/*',
],
rules: {
'no-restricted-imports': [
level,
{
paths: [
{
name: '@components/scripts/utils/environmentClient',
message: 'Server code must not import client-only helpers. Use server-side env utilities instead.',
},
],
},
],
},
},
{
files: [
'**/*.astro',
'**/*.ts',
'**/*.tsx',
],
ignores: [
'src/integrations/**/*',
'src/**/__tests__/**/*',
'src/**/server/**/*',
'src/lib/**/*.ts',
'src/pages/**/*.astro',
'src/pages/api/_utils/environment/environmentApi.ts',
],
rules: {
'no-restricted-imports': [
level,
{
paths: [
{
name: '@lib/config/environmentServer',
message: 'Client code must not import server-only helpers. Use client-side env utilities instead.',
},
],
},
],
},
},
{
files: [
'**/*.astro',
'**/*.ts',
'**/*.tsx',
],
ignores: [
'src/pages/api/**/*',
],
rules: {
'no-restricted-imports': [
level,
{
paths: [
{
name: '@pages/api/_utils/environment/environmentApi.ts',
message: 'Non-API code must not import API-only helpers. Use either client-side or server-side env utilities as appropriate instead.',
},
],
},
],
},
},
{
files: [
'**/*.astro',
'**/*.ts',
'**/*.tsx',
],
ignores: [
'src/**/client/**',
'src/components/scripts/**/*',
'src/pages/testing/**/*',
'src/components/scripts/utils/__tests__/siteUrlClient.spec.ts',
],
rules: {
'no-restricted-imports': [
level,
{
paths: [
{
name: '@components/scripts/utils/siteUrlClient',
message: 'Only browser-executed client code may import @components/scripts/utils/siteUrlClient. SSR, Astro frontmatter, actions, API routes, and other server-side code must resolve site URLs from runtime-safe server helpers or request context instead.',
},
],
},
],
},
},
{
files: [
'**/*.astro',
],
ignores: [
'src/pages/testing/**/*',
],
rules: {
'no-restricted-imports': [
level,
{
paths: [
{
name: '@components/scripts/utils/siteUrlClient',
message: 'Astro files must not import @components/scripts/utils/siteUrlClient. Browser-only site URL helpers are unsafe in Astro frontmatter because those files can execute during SSR. Resolve site URLs from Astro context or use a runtime-safe server helper instead.',
},
{
name: '@lib/config/siteUrlServer',
message: 'Astro files must not import @lib/config/siteUrlServer. Resolve site URLs from Astro context or use a runtime-safe helper instead.',
},
],
},
],
},
},
/**
* =================================================================================================
*
* Test files using dynamic imports need to include the file extension
*
* =================================================================================================
*/
{
files: [
'test/e2e/specs/14-system/package-release.spec.ts',
'test/e2e/specs/14-system/privacy-policy-version.spec.ts',
],
rules: {},
},
]