-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patheslint.base.config.mjs
More file actions
705 lines (669 loc) · 24.5 KB
/
eslint.base.config.mjs
File metadata and controls
705 lines (669 loc) · 24.5 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
"use strict";
import eslint from "@eslint/js";
import tseslint from "@typescript-eslint/eslint-plugin";
import eslintParser from "@typescript-eslint/parser";
import eslintPluginImport from "eslint-plugin-import";
import eslintPluginLodash from "eslint-plugin-lodash";
import eslintPluginNoOnlyTests from "eslint-plugin-no-only-tests";
import eslintPluginNode from "eslint-plugin-n";
import eslintPluginReact from "eslint-plugin-react";
import eslintPluginReactHooks from "eslint-plugin-react-hooks";
import eslintPluginTypescriptEnum from "eslint-plugin-typescript-enum";
import eslintPluginUnusedImports from "eslint-plugin-unused-imports";
import globals from "globals";
import stylistic from "@stylistic/eslint-plugin";
// Custom local rules for Slapdash codebase
const localRules = {
rules: {
"zod-prefer-safe-optional": {
meta: {
type: "problem",
docs: {
description:
"Prefer .safeOptional() over .optional(), .nullable(), or .nullish() for LLM response Zod schemas",
category: "Best Practices",
recommended: true,
},
messages: {
preferSafeOptional:
"Use .safeOptional() instead of .{{method}}() for LLM response schemas. " +
".safeOptional() handles both undefined and null values, and it converts null to undefined " +
"to avoid the need for null checks. LLMs sometimes generate null instead of undefined " +
"and vice versa, so .safeOptional() is more robust.",
},
schema: [],
},
create(context) {
return {
MemberExpression(node) {
// Check if this is a method call like .optional(), .nullable(), or .nullish()
if (
node.property &&
node.property.type === "Identifier" &&
["optional", "nullable", "nullish"].includes(node.property.name)
) {
// Check if this is being called (e.g., .optional() not just .optional)
const parent = node.parent;
if (
parent &&
parent.type === "CallExpression" &&
parent.callee === node
) {
// Try to determine if this is a Zod schema
// This is a heuristic: we look for z. in the call chain
let current = node.object;
let isZodSchema = false;
// Walk up the chain looking for z.something
while (current) {
if (
current.type === "MemberExpression" &&
current.object &&
current.object.type === "Identifier" &&
current.object.name === "z"
) {
isZodSchema = true;
break;
}
if (current.type === "CallExpression") {
current = current.callee;
} else if (current.type === "MemberExpression") {
current = current.object;
} else {
break;
}
}
if (isZodSchema) {
context.report({
node: node.property,
messageId: "preferSafeOptional",
data: {
method: node.property.name,
},
});
}
}
}
},
};
},
},
},
};
// Fixing a bug in the eslint-plugin-react library.
const browserGlobals = {
...globals.browser,
AudioWorkletGlobalScope: false, // this is the default,
};
delete browserGlobals["AudioWorkletGlobalScope "];
delete tseslint.configs.recommended["extends"];
function toFlatConfig(plugin, name) {
delete plugin.configs.recommended["extends"];
const parserOptions = plugin.configs.recommended.parserOptions;
delete plugin.configs.recommended["parserOptions"];
return {
...plugin.configs.recommended,
plugins: { [name]: plugin },
...(parserOptions ? { languageOptions: { parserOptions } } : {}),
};
}
export default function createConfig({
projectRoot,
eslintTsConfig,
extraRules,
extraIgnorePatterns,
}) {
return [
{
ignores: [
"**/node_modules/**/*",
"dist/**",
"**/webpack.config.ts",
"**/bin/**",
"**/*.d.ts",
"**/*.sh",
"*.js",
"src/scripts/code_templates/*",
...(extraIgnorePatterns ?? []),
],
},
eslint.configs.recommended,
toFlatConfig(eslintPluginReact, "react"),
toFlatConfig(tseslint, "@typescript-eslint"),
eslintPluginImport.flatConfigs.recommended,
toFlatConfig(eslintPluginLodash, "lodash"),
eslintPluginNode.configs["flat/recommended"],
{
files: ["**/*.ts", "**/*.tsx", "**/*.jsx"],
languageOptions: {
globals: {
...globals.node,
...browserGlobals,
...globals.es6,
...globals.jest,
Atomics: "readonly",
SharedArrayBuffer: "readonly",
NodeJS: "readonly", // For NodeJS.Timeout, NodeJS.Process, etc.
},
parser: eslintParser,
parserOptions: {
ecmaFeatures: {
jsx: true,
},
ecmaVersion: 2018,
sourceType: "module",
tsconfigRootDir: projectRoot,
project: eslintTsConfig,
warnOnUnsupportedTypeScriptVersion: false,
// Reuse editor TS service and slim project to reduce memory and speed up.
EXPERIMENTAL_useProjectService: true,
},
},
plugins: {
eslint,
"@typescript-eslint": tseslint,
lodash: eslintPluginLodash,
"no-only-tests": eslintPluginNoOnlyTests,
node: eslintPluginNode,
react: eslintPluginReact,
"react-hooks": eslintPluginReactHooks,
"typescript-enum": eslintPluginTypescriptEnum,
"unused-imports": eslintPluginUnusedImports,
stylistic,
local: localRules,
},
settings: {
react: {
version: "detect",
},
"import/parsers": {
"@typescript-eslint/parser": [".ts", ".tsx"],
},
"import/resolver": {
typescript: {
project: eslintTsConfig,
alwaysTryTypes: false,
},
node: {
extensions: [".js", ".jsx", ".ts", ".tsx"],
},
},
},
rules: {
// TODO: slowly enable no-extraneous-dependencies rule below. For now, it's
// enforced only for some packages.
//
// In an ideal world, the root package.json should have 0 dependencies, and
// all packages/* should define their own dependencies by themselves,
// independently and locally. The rule below is exactly for that: it ensures
// that all package's dependencies are explicitly mentioned in its
// package.json, and no dependencies are borrowed implicitly from the root
// node_modules.
//
// In real life though, enforcing packages independency is dangerous: we may
// e.g. start accidentally bundle 2 React or 2 Redux versions if we forget
// to sync their versions in different monorepo packages' package.json
// files. (There must be some other lint rule for this hopefully.)
//
// In all cases, we should treat node_modules folders content as something
// secondary and transient. (It's true even now with the new "yarn
// Plug-n-Play" technology which we don't use yet.) The source of truth is
// always package.json (enforced by lint) and yarn.lock (defines the exact
// contents of all node_modules folders, bit by bit). In this schema, it
// doesn't matter at all, does yarn use hoisting or not.
//
// "import/no-extraneous-dependencies": "error";
"arrow-body-style": ["error", "as-needed"],
"lodash/collection-ordering": "off",
"lodash/preferred-alias": "off",
"lodash/chaining": "off",
// Lodash matches property syntax is not typesafe.
"lodash/matches-prop-shorthand": ["warn", "never"],
"lodash/prefer-constant": "off",
"lodash/prefer-flat-map": "off",
"lodash/prefer-immutable-method": "off",
"lodash/prefer-includes": "warn",
"lodash/prefer-reject": "off",
"lodash/identity-shorthand": "off",
"lodash/prefer-is-nil": "off",
"lodash/prefer-lodash-chain": "off",
"lodash/matches-shorthand": "warn",
"lodash/prefer-matches": "off",
"lodash/prefer-noop": "off",
"lodash/prefer-thru": "warn",
"lodash/prefer-startswith": "off",
"lodash/prefer-lodash-method": "off",
"lodash/prefer-lodash-typecheck": "off",
"lodash/prop-shorthand": "off",
"lodash/import-scope": "off", // Doesn't matter much in node.js.
"lodash/unwrap": "off",
"node/prefer-global/process": "error",
"node/prefer-global/console": "error",
"node/prefer-global/buffer": "error",
"node/prefer-global/url-search-params": "error",
"node/prefer-global/url": "error",
// Disable node plugin import checking for TypeScript files since TypeScript handles this.
"n/no-deprecated-api": "warn",
"n/no-missing-import": "off",
"n/no-missing-require": "off",
"n/no-extraneous-import": "off",
"n/no-extraneous-require": "off",
"n/no-process-exit": "off",
"n/hashbang": "warn",
"n/no-unsupported-features/es-syntax": "off",
"n/no-unsupported-features/node-builtins": "off",
"n/no-unpublished-import": "off",
"require-atomic-updates": "off",
"no-prototype-builtins": "off",
"react/prop-types": "off",
"react/no-unescaped-entities": "off",
"react-hooks/rules-of-hooks": "error",
"react-hooks/exhaustive-deps": "warn",
"@typescript-eslint/no-misused-promises": "error",
"@typescript-eslint/promise-function-async": "error",
"@typescript-eslint/await-thenable": "error",
"@typescript-eslint/no-floating-promises": [
"error",
{ ignoreVoid: false },
],
"@typescript-eslint/no-namespace": "off",
"@typescript-eslint/no-this-alias": "off",
"@typescript-eslint/no-unsafe-assignment": "off",
"@typescript-eslint/unbound-method": ["error", { ignoreStatic: true }],
"@typescript-eslint/array-type": ["error", { default: "array-simple" }],
"@typescript-eslint/ban-ts-comment": ["error"],
"@typescript-eslint/no-non-null-asserted-optional-chain": "off",
"@typescript-eslint/no-useless-constructor": ["error"],
"@typescript-eslint/no-empty-object-type": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-require-imports": "off",
"@typescript-eslint/no-unused-expressions": "off",
"@typescript-eslint/no-unsafe-function-type": "off",
"@typescript-eslint/prefer-optional-chain": ["warn"],
"@typescript-eslint/consistent-type-imports": ["error"],
"@typescript-eslint/return-await": "warn",
"@typescript-eslint/require-array-sort-compare": [
"error",
{ ignoreStringArrays: true },
],
"@typescript-eslint/use-unknown-in-catch-callback-variable": "warn",
eqeqeq: ["error"],
"object-shorthand": ["error", "always"],
"typescript-enum/no-const-enum": ["error"], // not supported in SWC
"typescript-enum/no-enum": "off",
"@typescript-eslint/naming-convention": [
"error",
{
selector: "variable",
format: ["camelCase", "PascalCase", "UPPER_CASE"],
leadingUnderscore: "allow",
trailingUnderscore: "allow",
filter: {
regex: "^__webpack",
match: false,
},
},
],
// Disable in favour of @typescript-eslint/no-unused-vars.
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": [
"error",
{
args: "all",
vars: "local", // Only check local variables, ignore imports used only as types.
argsIgnorePattern: "^_",
varsIgnorePattern: "^_",
caughtErrorsIgnorePattern: "^_",
destructuredArrayIgnorePattern: "^_",
ignoreRestSiblings: true,
},
],
// Disable in favour of @typescript-eslint/no-redeclare which understands TS namespaces
"no-redeclare": "off",
"@typescript-eslint/no-redeclare": "off",
"@typescript-eslint/member-ordering": [
"error",
{
//
// ATTENTION: the rules here are not simple, mainly because of this:
// https://github.com/typescript-eslint/typescript-eslint/issues/6133
//
// Besides that, we also want contradictory things, like:
//
// 1. Having constructor close to fields definition (because people
// often define fields in the constructor arguments), although it
// logically should've been below static methods.
// 2. Having all abstract things in the class grouped, irregardless on
// their public/protected/private modifiers.
//
default: [
"signature",
"call-signature",
// Typically, class constants (that's why they're on top).
"public-static-field",
"public-static-get",
"public-static-set",
"protected-static-field",
"protected-static-get",
"protected-static-set",
// All concrete fields. What's interesting is that the order we
// emotionally want here for properties is private-protected-public,
// which is the opposite to the order of methods (which is
// public-protected-private). This is likely because the methods are
// bulky, and properties are lean.
"private-static-field",
"private-instance-field",
"public-instance-field",
"public-abstract-field",
"public-abstract-get",
"public-abstract-set",
// Protected fields and methods are grouped, because eslint currently
// doesn't distinguish fields assigned with a lambda FROM methods, and
// we often times expose abstract protected overridable lambdas:
// https://github.com/typescript-eslint/typescript-eslint/issues/6133
"protected-abstract-field",
"protected-abstract-get",
"protected-abstract-set",
"protected-abstract-method",
"public-abstract-method", // the only exception; it's to group all abstract things too
"protected-instance-field",
"protected-constructor",
"protected-static-method",
"protected-instance-get",
"protected-instance-set",
"protected-instance-method",
// Public constructor, instance methods, static methods.
"public-constructor", // often defines more public/protected/private properties, so should be close to fields
"public-static-method",
"public-instance-get",
"public-instance-set",
"public-instance-method",
// Private constructor, instance methods, static methods.
"private-constructor",
"private-static-method",
"private-instance-get",
"private-instance-set",
"private-instance-method",
"private-static-get",
"private-static-set",
],
},
],
"no-constant-condition": ["error", { checkLoops: false }],
"no-buffer-constructor": ["error"],
"no-console": ["error"],
curly: ["error", "all"],
"no-case-declarations": "off",
"padding-line-between-statements": "off",
"stylistic/padding-line-between-statements": [
"error",
// Force empty lines.
{
blankLine: "always",
prev: [
"block",
"block-like",
"function",
"class",
"interface",
"type",
],
next: "*",
},
{
blankLine: "always",
prev: "import",
next: [
"const",
"if",
"let",
"var",
"export",
"function",
"class",
"interface",
"type",
],
},
{
blankLine: "always",
prev: "*",
next: ["function", "class", "interface", "type"],
},
// Allow one-liner functions without extra spacing (hacky):
{ blankLine: "any", prev: "singleline-const", next: "*" },
{ blankLine: "any", prev: "singleline-var", next: "*" },
{ blankLine: "any", prev: "singleline-let", next: "*" },
],
"no-restricted-properties": [
"error",
{
object: "window",
property: "location",
message:
"We use React Router and History to control the location of our web or desktop app. Prefer `useLocation` in React components and `historyFromContext` in Redux Saga.",
},
...(projectRoot.endsWith("client")
? [
{
object: "window",
property: "document",
message: "Please use `useDocument` from `useDocument.tsx`.",
},
...[
"addEventListener",
"removeEventListener",
"getElementById",
"documentElement",
"activeElement",
"querySelectorAll",
].map((property) => ({
object: "document",
property,
message: "Please use `useDocument` from `useDocument.tsx`.",
})),
]
: []),
],
"no-restricted-globals": [
"warn",
{
name: "location",
message:
"We use React Router and History to control the location of our web or desktop app. Prefer `useLocation` in React components and `historyFromContext` in Redux Saga.",
},
],
"no-restricted-syntax": [
"error",
{
selector: (() => {
const RE_BAD = "/([a-z0-9_ ]|^)E[Ii][Dd]|(^|[-_: ])eid/";
return [
`Identifier[name=${RE_BAD}]`,
`Literal[value=${RE_BAD}]`,
`TemplateElement[value.raw=${RE_BAD}]`,
`TSInterfaceDeclaration[id.name=${RE_BAD}]`,
].join(",");
})(),
message:
'Do not use "eid" or "EID" as a part of a name/field/type. Instead, prefer externalID or external_id.',
},
],
"prefer-const": [
"error",
{
destructuring: "all",
},
],
"no-var": "error",
"no-void": "error",
"react/forbid-dom-props": [
"error",
{
forbid: [
{
propName: "style",
message: "Please use CSS Modules instead",
},
],
},
],
"react/forbid-component-props": [
"error",
{
forbid: [
{
propName: "style",
message: "Please use CSS Modules instead",
},
],
},
],
"no-sequences": ["error"],
"no-undef": "off",
// Too noisy about `react` and other node_modules
"import/default": 0,
// This complains about React.forwardRef, ReactDOM.render, etc.
"import/no-named-as-default-member": 0,
// This complains about "apollo" exporting ApolloClient as a default and as a
// named import at the same time.
"import/no-named-as-default": 0,
// Does not seem to work well with node_modules
"import/named": 0,
"import/newline-after-import": "error",
"import/order": [
"error",
{
groups: ["builtin", "external", "index", "parent", "sibling"],
pathGroups: [
{
pattern: "./**.module.css",
group: "sibling",
position: "after",
},
{
pattern: "./**.module.scss",
group: "sibling",
position: "after",
},
],
alphabetize: {
order: "asc",
caseInsensitive: true,
},
},
],
"import/no-useless-path-segments": ["error", { noUselessIndex: true }],
"unused-imports/no-unused-imports": "error",
"no-restricted-imports": [
"error",
{
patterns: [
{
group: ["react-router"],
message:
"Please use react-router-dom instead, since react-router's useLocation() doesn't work properly with StaticRouter on server side.",
},
{
group: ["braintrust/loadPrompt"],
message:
"You should use DynamicPromptsRepository to load prompts from braintrust.",
},
],
},
],
// Fixes a common mistake: `a ?? b < c` which feels like `(a ?? b) < c`, but
// actually is `a ?? (b < c)`
"no-mixed-operators": [
"error",
{
allowSamePrecedence: false,
groups: [
["??", "+"],
["??", "-"],
["??", "*"],
["??", "/"],
["??", "%"],
["??", "**"],
["??", "&"],
["??", "|"],
["??", "^"],
["??", "~"],
["??", "<<"],
["??", ">>"],
["??", ">>>"],
["??", "=="],
["??", "!="],
["??", "==="],
["??", "!=="],
["??", ">"],
["??", ">="],
["??", "<"],
["??", "<="],
["??", "&&"],
["??", "||"],
["??", "in"],
["??", "instanceof"],
],
},
],
quotes: ["error", "double", { avoidEscape: true }],
"no-only-tests/no-only-tests": "error",
"local/zod-prefer-safe-optional": "warn",
...extraRules,
},
},
{
files: ["**/*.js"],
languageOptions: {
ecmaVersion: 2022,
},
rules: {
"n/hashbang": "off",
"n/no-unsupported-features/node-builtins": "off",
"n/no-extraneous-import": "off",
"@typescript-eslint/no-require-imports": "off",
"lodash/prefer-lodash-method": "off",
"n/no-process-exit": "off",
"n/no-missing-require": "off",
},
},
{
files: ["**/*.mjs"],
languageOptions: {
sourceType: "module",
ecmaVersion: 2020,
},
settings: {
"import/core-modules": ["@stylistic/eslint-plugin", "@eslint/js"],
},
rules: {
"n/hashbang": "off",
"n/no-extraneous-import": "off",
"n/no-unsupported-features/es-syntax": "off",
"n/no-unpublished-import": "off",
"n/no-process-exit": "off",
"lodash/prefer-lodash-method": "off",
// Disable import plugin checks for config files to avoid CJS/ESM export parsing issues
"import/default": "off",
"import/namespace": "off",
"import/no-unresolved": "off",
"import/no-named-as-default": "off",
"import/no-named-as-default-member": "off",
},
},
// Disable zod-prefer-safe-optional for braintrust/eval files since those
// schemas are for validating test configuration, not for guiding LLM output
{
files: [
"**/scripts/evals/**/*.ts",
"**/scripts/evals/**/*.tsx",
"**/braintrust/**/*.ts",
"**/braintrust/**/*.tsx",
],
rules: {
"local/zod-prefer-safe-optional": "off",
"local/comment-punctuation": "off", // Otherwise, it adds a period after every multiline // commit line.
},
},
];
}