-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy patheslint.config.mjs
More file actions
767 lines (717 loc) · 24.3 KB
/
eslint.config.mjs
File metadata and controls
767 lines (717 loc) · 24.3 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
// For more info, see https://github.com/storybookjs/eslint-plugin-storybook#configuration-flat-config-format
import storybook from "eslint-plugin-storybook";
import js from "@eslint/js";
import { defineConfig } from "eslint/config";
import react from "eslint-plugin-react";
import reactHooks from "eslint-plugin-react-hooks";
import tailwindcss from "eslint-plugin-tailwindcss";
import tseslint from "typescript-eslint";
/**
* Custom ESLint plugin for safe Node.js patterns
* Enforces safe child_process and filesystem patterns
*/
const localPlugin = {
rules: {
"no-unsafe-child-process": {
meta: {
type: "problem",
docs: {
description: "Prevent unsafe child_process usage that can cause zombie processes",
},
messages: {
unsafePromisifyExec:
"Do not use promisify(exec) directly. Use DisposableExec wrapper with 'using' declaration to prevent zombie processes.",
},
},
create(context) {
return {
CallExpression(node) {
// Ban promisify(exec)
if (
node.callee.type === "Identifier" &&
node.callee.name === "promisify" &&
node.arguments.length > 0 &&
node.arguments[0].type === "Identifier" &&
node.arguments[0].name === "exec"
) {
context.report({
node,
messageId: "unsafePromisifyExec",
});
}
},
};
},
},
"no-sync-fs-methods": {
meta: {
type: "problem",
docs: {
description: "Prevent synchronous filesystem operations",
},
messages: {
syncFsMethod:
"Do not use synchronous fs methods ({{method}}). Use async version instead: {{asyncMethod}}",
},
},
create(context) {
// Map of sync methods to their async equivalents
const syncMethods = {
statSync: "stat",
readFileSync: "readFile",
writeFileSync: "writeFile",
readdirSync: "readdir",
mkdirSync: "mkdir",
unlinkSync: "unlink",
rmdirSync: "rmdir",
existsSync: "access or stat",
accessSync: "access",
copyFileSync: "copyFile",
renameSync: "rename",
chmodSync: "chmod",
chownSync: "chown",
lstatSync: "lstat",
linkSync: "link",
symlinkSync: "symlink",
readlinkSync: "readlink",
realpathSync: "realpath",
truncateSync: "truncate",
fstatSync: "fstat",
appendFileSync: "appendFile",
};
return {
MemberExpression(node) {
// Only flag if it's a property access on 'fs' or imported fs methods
if (
node.property &&
node.property.type === "Identifier" &&
syncMethods[node.property.name] &&
node.object &&
node.object.type === "Identifier" &&
(node.object.name === "fs" || node.object.name === "fsPromises")
) {
context.report({
node,
messageId: "syncFsMethod",
data: {
method: node.property.name,
asyncMethod: syncMethods[node.property.name],
},
});
}
},
};
},
},
"no-cross-boundary-imports": {
meta: {
type: "problem",
docs: {
description: "Enforce folder boundaries to prevent architectural violations",
},
messages: {
browserToNode:
"browser/ cannot import from node/. Move shared code to common/ or use IPC.",
nodeToDesktop:
"node/ cannot import from desktop/. Move shared code to common/ or use dependency injection.",
nodeToCli: "node/ cannot import from cli/. Move shared code to common/.",
cliToBrowser: "cli/ cannot import from browser/. Move shared code to common/.",
desktopToBrowser: "desktop/ cannot import from browser/. Move shared code to common/.",
},
},
create(context) {
return {
ImportDeclaration(node) {
// Allow type-only imports (for DI patterns)
if (node.importKind === "type") {
return;
}
const sourceFile = context.filename;
const importPath = node.source.value;
// Extract folder from source file (browser, node, desktop, cli, common)
const sourceFolderMatch = sourceFile.match(
/\/src\/(browser|node|desktop|cli|common)\//
);
if (!sourceFolderMatch) return;
const sourceFolder = sourceFolderMatch[1];
// Extract folder from import target
// Handle relative imports (e.g., '../node/...')
let targetFolder = null;
if (importPath.startsWith("../")) {
const targetMatch = importPath.match(/\.\.\/(browser|node|desktop|cli|common)\//);
if (targetMatch) {
targetFolder = targetMatch[1];
}
} else if (importPath.startsWith("@/")) {
// Handle alias imports (e.g., '@/node/...')
const targetMatch = importPath.match(/@\/(browser|node|desktop|cli|common)\//);
if (targetMatch) {
targetFolder = targetMatch[1];
}
}
if (!targetFolder) return;
// Allow imports from common
if (targetFolder === "common") return;
// Check for violations
if (sourceFolder === "browser" && targetFolder === "node") {
context.report({
node,
messageId: "browserToNode",
});
} else if (sourceFolder === "node" && targetFolder === "desktop") {
context.report({
node,
messageId: "nodeToDesktop",
});
} else if (sourceFolder === "node" && targetFolder === "cli") {
context.report({
node,
messageId: "nodeToCli",
});
} else if (sourceFolder === "cli" && targetFolder === "browser") {
context.report({
node,
messageId: "cliToBrowser",
});
} else if (sourceFolder === "desktop" && targetFolder === "browser") {
context.report({
node,
messageId: "desktopToBrowser",
});
}
},
};
},
},
"no-native-interactive-tooltips": {
meta: {
type: "problem",
docs: {
description:
"Disallow native title tooltips on interactive controls when the content is long or dynamic",
},
messages: {
useTooltip:
"Native title tooltips on raw interactive elements truncate easily when the content is long or dynamic. Use the shared Tooltip surface instead, or pass `title` through a shared control that intercepts it.",
},
},
create(context) {
const MAX_NATIVE_TOOLTIP_LENGTH = 20;
const INTERACTIVE_TAGS = new Set(["button", "input", "select", "textarea"]);
const getAttribute = (node, attributeName) =>
node.attributes.find(
(attribute) =>
attribute.type === "JSXAttribute" &&
attribute.name.type === "JSXIdentifier" &&
attribute.name.name === attributeName
) ?? null;
const getStaticString = (expression) => {
if (!expression) {
return null;
}
if (expression.type === "Literal") {
return typeof expression.value === "string" ? expression.value : null;
}
if (expression.type === "TemplateLiteral" && expression.expressions.length === 0) {
return expression.quasis
.map((quasi) => quasi.value.cooked ?? quasi.value.raw)
.join("");
}
return null;
};
const isProblematicTitleExpression = (expression) => {
if (!expression) {
return false;
}
if (expression.type === "Literal") {
if (expression.value == null) {
return false;
}
return (
typeof expression.value === "string" &&
(expression.value.includes("\n") ||
expression.value.length > MAX_NATIVE_TOOLTIP_LENGTH)
);
}
if (expression.type === "TemplateLiteral") {
if (expression.expressions.length > 0) {
return true;
}
const value = getStaticString(expression);
return value !== null
? value.includes("\n") || value.length > MAX_NATIVE_TOOLTIP_LENGTH
: true;
}
if (expression.type === "ConditionalExpression") {
return (
isProblematicTitleExpression(expression.consequent) ||
isProblematicTitleExpression(expression.alternate)
);
}
const staticValue = getStaticString(expression);
if (staticValue !== null) {
return (
staticValue.includes("\n") || staticValue.length > MAX_NATIVE_TOOLTIP_LENGTH
);
}
return true;
};
return {
JSXOpeningElement(node) {
if (node.name.type !== "JSXIdentifier") {
return;
}
const elementName = node.name.name;
if (elementName !== elementName.toLowerCase()) {
return;
}
const onClickAttribute = getAttribute(node, "onClick");
const hrefAttribute = getAttribute(node, "href");
const roleAttribute = getAttribute(node, "role");
const roleValue =
roleAttribute &&
roleAttribute.value?.type === "Literal" &&
typeof roleAttribute.value.value === "string"
? roleAttribute.value.value
: null;
const isInteractive =
INTERACTIVE_TAGS.has(elementName) ||
(elementName === "a" && hrefAttribute !== null) ||
onClickAttribute !== null ||
roleValue === "button" ||
roleValue === "link" ||
roleValue === "switch";
if (!isInteractive) {
return;
}
const titleAttribute = getAttribute(node, "title");
if (!titleAttribute || !titleAttribute.value) {
return;
}
// Normalize: Literal value nodes and JSXExpressionContainer inner
// expressions are both valid AST expression nodes that
// isProblematicTitleExpression already handles.
let expression;
if (titleAttribute.value.type === "Literal") {
expression = titleAttribute.value;
} else if (titleAttribute.value.type === "JSXExpressionContainer") {
expression = titleAttribute.value.expression;
} else {
return;
}
if (isProblematicTitleExpression(expression)) {
context.report({
node: titleAttribute,
messageId: "useTooltip",
});
}
},
};
},
},
},
};
export default defineConfig([
{
ignores: [
"dist/",
"build/",
"node_modules/",
"*.js",
"*.cjs",
"*.mjs",
"!eslint.config.mjs",
"vite.config.ts",
"electron.vite.config.ts",
"src/browser/main.tsx",
],
},
js.configs.recommended,
...tseslint.configs.recommendedTypeChecked,
...tseslint.configs.stylisticTypeChecked,
{
files: ["src/**/*.{ts,tsx}"],
languageOptions: {
parserOptions: {
projectService: true,
tsconfigRootDir: import.meta.dirname,
},
globals: {
console: "readonly",
process: "readonly",
Buffer: "readonly",
__dirname: "readonly",
__filename: "readonly",
exports: "writable",
module: "writable",
require: "readonly",
global: "readonly",
window: "readonly",
document: "readonly",
requestAnimationFrame: "readonly",
setTimeout: "readonly",
clearTimeout: "readonly",
setInterval: "readonly",
clearInterval: "readonly",
navigator: "readonly",
alert: "readonly",
},
},
plugins: {
react,
"react-hooks": reactHooks,
tailwindcss,
local: localPlugin,
},
settings: {
react: {
version: "detect",
},
tailwindcss: {
// Don't try to load Tailwind config (v4 doesn't export resolveConfig)
config: false,
// CSS files to check
cssFiles: ["**/*.css", "!**/node_modules", "!**/.*", "!**/dist", "!**/build"],
// Disable callees check to avoid resolving config
callees: [],
},
},
rules: {
...react.configs.recommended.rules,
// Use recommended-latest to get React Compiler lint rules
...reactHooks.configs["recommended-latest"].rules,
// Flag unused variables, parameters, and imports
"@typescript-eslint/no-unused-vars": [
"error",
{
vars: "all",
args: "after-used",
ignoreRestSiblings: true,
argsIgnorePattern: "^_",
varsIgnorePattern: "^_",
caughtErrors: "all",
},
],
// Prohibit 'as any' type assertions
"@typescript-eslint/no-explicit-any": "error",
// Additional rule to catch 'as any' specifically
"@typescript-eslint/consistent-type-assertions": [
"error",
{
assertionStyle: "as",
objectLiteralTypeAssertions: "allow-as-parameter",
},
],
// Enforce shorthand array notation, e.g. Foo[] instead of Array<Foo>
"@typescript-eslint/array-type": [
"error",
{
default: "array-simple",
readonly: "array-simple",
},
],
// Keep type-only imports explicit to avoid runtime inclusion
"@typescript-eslint/consistent-type-imports": [
"error",
{
prefer: "type-imports",
disallowTypeAnnotations: true,
},
],
// Require handling Promises instead of letting them float
"@typescript-eslint/no-floating-promises": [
"error",
{
ignoreVoid: true,
ignoreIIFE: true,
},
],
// Highlight unnecessary assertions to keep code idiomatic
"@typescript-eslint/no-unnecessary-type-assertion": "error",
// Encourage readonly where possible to surface unintended mutations
"@typescript-eslint/prefer-readonly": [
"error",
{
onlyInlineLambdas: true,
},
],
// Prevent using any type at all
"@typescript-eslint/no-unsafe-assignment": "error",
"@typescript-eslint/no-unsafe-member-access": "error",
"@typescript-eslint/no-unsafe-call": "error",
"@typescript-eslint/no-unsafe-return": "error",
"@typescript-eslint/no-unsafe-argument": "error",
// React specific
"react/react-in-jsx-scope": "off",
"react/prop-types": "off",
// Tailwind CSS
"tailwindcss/classnames-order": "warn",
"tailwindcss/enforces-negative-arbitrary-values": "warn",
"tailwindcss/enforces-shorthand": "warn",
"tailwindcss/migration-from-tailwind-2": "warn",
"tailwindcss/no-arbitrary-value": "off",
"tailwindcss/no-contradicting-classname": "error",
"tailwindcss/no-custom-classname": "off",
// Safe Node.js patterns
"local/no-unsafe-child-process": "error",
"local/no-sync-fs-methods": "error",
"local/no-cross-boundary-imports": "error",
"local/no-native-interactive-tooltips": "error",
// Allow console for this app (it's a dev tool)
"no-console": "off",
// Allow require in specific contexts
"@typescript-eslint/no-var-requires": "off",
// Enforce absolute imports with @/ alias for cross-directory imports
"no-restricted-imports": [
"error",
{
patterns: [
{
group: ["../!(tests)*", "../../!(tests)*"],
message:
"Use absolute imports with @/ instead of relative parent imports. Same-directory imports (./foo) are allowed.",
},
],
},
],
// Warn on TODO comments
"no-warning-comments": [
"off",
{
terms: ["TODO", "FIXME", "XXX", "HACK"],
location: "start",
},
],
// Enable TypeScript deprecation warnings
"@typescript-eslint/prefer-ts-expect-error": "error",
// Ban @ts-ignore comments and suggest @ts-expect-error instead
"@typescript-eslint/ban-ts-comment": [
"error",
{
"ts-expect-error": "allow-with-description",
"ts-ignore": true,
"ts-nocheck": true,
"ts-check": false,
minimumDescriptionLength: 3,
},
],
// Ban dynamic imports - they hide circular dependencies and should be avoided
"no-restricted-syntax": [
"error",
{
selector: "ImportExpression",
message:
"Dynamic imports are not allowed. Use static imports at the top of the file instead. Dynamic imports hide circular dependencies and improper module structure.",
},
],
// Prevent accidentally interpolating undefined/null in template literals and JSX
"@typescript-eslint/restrict-template-expressions": [
"error",
{
allowNumber: true,
allowBoolean: true,
allowAny: false,
allowNullish: false, // Catch undefined/null interpolations
allowRegExp: false,
},
],
},
},
{
// Allow dynamic imports for lazy-loading (startup optimization / platform compat)
files: [
"src/services/aiService.ts",
"src/utils/tools/tools.ts",
"src/utils/ai/providerFactory.ts",
"src/utils/main/tokenizer.ts",
"src/node/runtime/SSH2ConnectionPool.ts",
],
rules: {
"no-restricted-syntax": "off",
},
},
{
// Temporarily allow sync fs methods in files with existing usage
// TODO: Gradually migrate these to async operations
files: [
"src/node/config.ts",
"src/cli/debug/**/*.ts",
"src/node/git.ts",
"src/desktop/main.ts",
"src/node/config.test.ts",
"src/node/services/gitService.ts",
"src/node/services/log.ts",
"src/node/services/streamManager.ts",
"src/node/services/tempDir.ts",
"src/node/services/tools/bash.ts",
"src/node/services/tools/bash.test.ts",
"src/node/services/tools/testHelpers.ts",
"src/node/utils/providerRequirements.ts",
],
rules: {
"local/no-sync-fs-methods": "off",
},
},
{
// Frontend architectural boundary - prevent services and tokenizer imports
// Note: src/browser/utils/** and src/browser/stores/** are not included because:
// - Some utils are shared between main/renderer (e.g., utils/tools registry)
// - Stores can import from utils/messages which is renderer-safe
// - Type-only imports from services are safe (types live in src/common/types/)
files: [
"src/browser/components/**",
"src/browser/contexts/**",
"src/browser/hooks/**",
"src/browser/App.tsx",
],
rules: {
"no-restricted-imports": [
"error",
{
patterns: [
{
group: ["**/services/**", "../services/**", "../../services/**"],
message:
"Frontend code cannot import from services/. Use IPC or move shared code to utils/.",
},
{
group: ["**/tokens/tokenizer", "**/tokens/tokenStatsCalculator"],
message:
"Frontend code cannot import tokenizer (2MB+ encodings). Use @/utils/tokens/usageAggregator for aggregation or @/utils/tokens/modelStats for pricing.",
},
{
group: ["**/utils/main/**", "@/utils/main/**"],
message:
"Frontend code cannot import from utils/main/ (contains Node.js APIs). Move shared code to utils/ or use IPC.",
},
],
},
],
},
},
{
// Shiki must only be imported in the highlight worker to avoid blocking main thread
// Type-only imports are allowed (erased at compile time)
files: ["src/**/*.ts", "src/**/*.tsx"],
ignores: ["src/browser/workers/highlightWorker.ts"],
rules: {
"no-restricted-imports": [
"error",
{
patterns: [
{
group: ["shiki"],
importNamePattern: "^(?!type\\s)",
allowTypeImports: true,
message:
"Shiki must only be imported in highlightWorker.ts to avoid blocking the main thread. Use highlightCode() from highlightWorkerClient.ts instead.",
},
],
},
],
},
},
{
// ORPC must import config schemas via direct file paths, never the schemas barrel
files: ["src/common/orpc/**/*.ts"],
rules: {
"no-restricted-imports": [
"error",
{
paths: [
{
name: "@/common/config/schemas",
message:
"Import config schemas via direct file paths (e.g., @/common/config/schemas/appConfigOnDisk), not the barrel.",
},
{
name: "@/common/config/schemas/index",
message:
"Import config schemas via direct file paths (e.g., @/common/config/schemas/appConfigOnDisk), not the barrel.",
},
],
},
],
},
},
{
// Config schemas must remain independent from ORPC schema definitions
files: ["src/common/config/schemas/**/*.ts"],
rules: {
"no-restricted-imports": [
"error",
{
patterns: [
{
group: ["@/common/orpc/schemas/*", "**/orpc/schemas/*"],
message:
"Config schemas must not import from ORPC; use @/common/schemas/* or @/common/config/schemas/* instead.",
},
],
},
],
},
},
{
// Renderer process (frontend) architectural boundary - prevent Node.js API usage
files: ["src/**/*.ts", "src/**/*.tsx"],
ignores: [
"src/cli/**",
"src/desktop/**",
"src/node/**",
"**/*.test.ts",
"**/*.test.tsx",
// This file is only used by Node.js code (cli/debug) but lives in common/
// TODO: Consider moving to node/utils/
"src/common/utils/providers/ensureProvidersConfig.ts",
// Telemetry uses defensive process checks for test environments
"src/common/telemetry/**",
],
rules: {
"no-restricted-globals": [
"error",
{
name: "process",
message:
"Renderer code cannot access 'process' global (not available in renderer). Use IPC to communicate with main process or use constants for environment-agnostic values.",
},
],
"no-restricted-syntax": [
"error",
{
selector: "MemberExpression[object.name='process'][property.name='env']",
message:
"Renderer code cannot access process.env (not available in renderer). Use IPC to get environment variables from main process or use constants.",
},
],
},
},
{
// Test file configuration
files: ["**/*.test.ts", "**/*.test.tsx"],
languageOptions: {
globals: {
describe: "readonly",
it: "readonly",
test: "readonly",
expect: "readonly",
jest: "readonly",
beforeEach: "readonly",
afterEach: "readonly",
beforeAll: "readonly",
afterAll: "readonly",
},
},
},
{
// Storybook story files - disable type-aware rules for Storybook 10 barrel exports
files: ["**/*.stories.ts", "**/*.stories.tsx", ".storybook/**/*.ts", ".storybook/**/*.tsx"],
rules: {
"@typescript-eslint/no-unsafe-assignment": "off",
"@typescript-eslint/no-unsafe-call": "off",
"@typescript-eslint/no-unsafe-member-access": "off",
"@typescript-eslint/no-unsafe-return": "off",
},
},
...storybook.configs["flat/recommended"],
]);