Repository navigation
Expand file tree
/
Copy patheslint.config.js
More file actions
271 lines (258 loc) · 11.5 KB
/
Copy patheslint.config.js
File metadata and controls
271 lines (258 loc) · 11.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
/**
* Juno's frontend lint gate. `bun run lint` locally; CI runs it as the first
* step of the Frontend job, before vitest, because it is the cheapest gate.
*
* It lints exactly two things, because everything else is already covered:
*
* 1. React hooks correctness (eslint-plugin-react-hooks). This is the one
* class of bug neither `tsc` nor vitest can see: a hook called
* conditionally, or state set during render, typechecks, passes its
* tests, and then tears under React 19's concurrent rendering.
*
* 2. Juno's architectural boundary (CLAUDE.md: "Backend Owns Logic, Frontend
* is Display-Only"). The frontend may not record audio, play audio,
* persist settings, or open sockets. That rule has lived in prose across
* three CLAUDE.md files and been enforced by code review, which has not
* worked — every `eslint-disable-next-line` in the tree referencing this
* file is a place it was broken. These rules make it a gate, so the debt
* stops growing while the existing violations are paid off.
*
* ## This config is a ratchet, and that is deliberate
*
* eslint-plugin-react-hooks v7 ships the React Compiler's full rule set, and
* `configs.recommended` reports 226 findings on Juno today. Turning that on
* wholesale gives you a red CI check that nobody can make green, which is the
* same as having no gate at all — worse, because people learn to ignore it.
*
* So the enforced set below is the subset that reports ZERO findings today. It
* costs nothing, it can never be "temporarily" lowered to land a branch, and it
* still contains the two rules that catch the worst React bugs there are
* (`rules-of-hooks`, `set-state-in-render`). The deferred rules are listed by
* name with their finding count and their issue, so the map is visible rather
* than implied by what is missing. Turn each one on in its own PR as the
* findings get fixed; never turn one on and suppress the findings.
*
* Rules are enumerated rather than spread from `configs.recommended` on
* purpose: a plugin upgrade that adds a rule should not turn CI red on a branch
* that changed no frontend code.
*
* Deliberately NOT here:
* - A formatter. The repo has never had one, and adding one means reformatting
* 377 files in the same PR that adds the gate: an unreviewable diff.
* - typescript-eslint's rule sets. `bun run build` is `tsc && vite build` with
* `strict`, `noUnusedLocals` and `noUnusedParameters` on, so type-aware lint
* would re-report what CI already fails on, at several times the runtime.
* - eslint-plugin-react. Its rules are mostly JSX style, and the ones that
* were not died with React 19's automatic JSX transform.
* - `no-undef`. `tsc` owns undefined identifiers and already knows the DOM
* lib; ESLint would need a `globals` dependency to be told the same thing.
*
* ## Why @babel/eslint-parser and not @typescript-eslint/parser
*
* Juno is on TypeScript 7, and no published typescript-eslint parses it — it
* refuses at import with "typescript-eslint does not support TS 7.0"
* (github.com/typescript-eslint/typescript-eslint/issues/10940). Rather than
* pin a second, side-by-side TypeScript 6 just to parse, this uses Babel's
* parser, which is the better fit anyway:
*
* - The React Compiler that powers these hooks rules IS a Babel plugin.
* Babel's AST is its native input.
* - It has no dependency on the `typescript` package, so it cannot be broken
* by a TypeScript major — which is the exact failure we are routing around.
* - The only thing typescript-eslint's parser adds over it is type
* information for type-aware rules, and those are deliberately out (above).
*
* Syntax plugins go in `parserOpts.plugins`, NOT in `presets`. Babel 8's
* eslint-parser only parses, so preset-derived syntax never reaches
* @babel/parser and every TypeScript file fails on `import type`.
*/
import babelParser from "@babel/eslint-parser";
import reactHooks from "eslint-plugin-react-hooks";
/** Shared Babel options; `plugins` differs between .ts and .tsx (see below). */
const babelOptionsFor = (plugins) => ({
requireConfigFile: false,
babelOptions: { babelrc: false, configFile: false, parserOpts: { plugins } },
});
/**
* The enforced hooks rules: every rule in `configs.recommended` that reports
* zero findings on Juno today. Promoted to `error` where recommended says
* `warn`, because a gate that can emit a warning accumulates warnings.
*/
const HOOKS_ENFORCED = {
// A hook called conditionally, in a loop, or outside a component. The
// original rule, and still the one that causes the most damage.
"react-hooks/rules-of-hooks": "error",
// setState during render: an infinite render loop, or a torn first paint.
"react-hooks/set-state-in-render": "error",
// Errors thrown where no boundary can catch them.
"react-hooks/error-boundaries": "error",
// Module-level mutable state read during render.
"react-hooks/globals": "error",
// A library used in a way the compiler cannot make safe.
"react-hooks/incompatible-library": "error",
// Malformed "use memo" / "use no memo" directives, and the compiler's own
// gating and config directives. Cheap, and they catch typos that would
// otherwise silently disable the compiler for a whole file.
"react-hooks/config": "error",
"react-hooks/gating": "error",
"react-hooks/unsupported-syntax": "error",
};
/**
* Deferred, with today's finding count. Each is a real correctness rule and
* each needs a sweep of its own. Fix the findings, then turn the rule on in
* that same PR — see LAC-4500.
*
* Every entry is `"off"`, which is already ESLint's default, so this object
* changes no behaviour. It is here on purpose: it makes the ratchet's remaining
* work visible in the file that enforces it rather than only in an issue, and
* it makes enabling a rule a one-word diff instead of a new line. Do not
* "simplify" it away — the counts are the map.
*
* react-hooks/set-state-in-effect 72
* react-hooks/refs 64
* react-hooks/exhaustive-deps 33
* react-hooks/immutability 16
* react-hooks/preserve-manual-memoization 13
* react-hooks/purity 7
* react-hooks/static-components 4
* react-hooks/use-memo 1
*/
const HOOKS_DEFERRED = {
"react-hooks/set-state-in-effect": "off",
"react-hooks/refs": "off",
"react-hooks/exhaustive-deps": "off",
"react-hooks/immutability": "off",
"react-hooks/preserve-manual-memoization": "off",
"react-hooks/purity": "off",
"react-hooks/static-components": "off",
"react-hooks/use-memo": "off",
};
/**
* Browser APIs the frontend must not touch, and the Rust that owns each. Every
* message names the owner, because "banned" without "use this instead" only
* teaches people to reach for a disable comment.
*/
const BANNED_GLOBALS = [
{
name: "localStorage",
message:
"Settings persistence is Rust's (Tauri Store, tauri_plugin_store::StoreExt). The frontend reads cached settings through invoke(), never localStorage — the backend has to run headlessly.",
},
{
name: "sessionStorage",
message:
"Settings persistence is Rust's (Tauri Store, tauri_plugin_store::StoreExt). The frontend reads cached settings through invoke(), never sessionStorage.",
},
{
name: "WebSocket",
message:
"WebSockets are Rust's (cloud/connector.rs, tokio-tungstenite). The frontend listens for Tauri events; it never opens a socket.",
},
{
name: "AudioContext",
message:
"Audio playback is Rust's (tts/, the `say` command, ElevenLabs). The Web Audio API is banned in the frontend.",
},
{
name: "webkitAudioContext",
message:
"Audio playback is Rust's (tts/, the `say` command, ElevenLabs). The Web Audio API is banned in the frontend.",
},
];
/** The boundary rules. Identical for .ts and .tsx. */
const BOUNDARY_RULES = {
"no-restricted-globals": ["error", ...BANNED_GLOBALS],
"no-restricted-imports": [
"error",
{
paths: [
{
name: "@tauri-apps/plugin-websocket",
message:
"Importing this breaks the build. WebSockets are Rust's (cloud/connector.rs); the frontend listens for Tauri events.",
},
],
},
],
"no-restricted-syntax": [
"error",
{
// `navigator.mediaDevices` in any form. Microphone capture lives in
// tauri-plugin-voice-transcription so the native permission prompt, the
// device selection and the Whisper pipeline are all one thing.
selector:
"MemberExpression[object.name='navigator'][property.name='mediaDevices']",
message:
"Microphone capture is Rust's (tauri-plugin-voice-transcription). navigator.mediaDevices / getUserMedia are banned in the frontend.",
},
{
// Banned outright: unreliable on macOS with `transparent: true` +
// `decorations: false`, which is every Juno window. Dragging goes
// through the useDragWindow hook.
selector: "JSXAttribute[name.name='data-tauri-drag-region']",
message:
"Banned on macOS transparent/undecorated windows — it silently drops the drag. Use the useDragWindow hook (startDragging()) instead.",
},
],
};
/** One rule block, shared by the .ts and .tsx configs below. */
const RULES = { ...HOOKS_ENFORCED, ...HOOKS_DEFERRED, ...BOUNDARY_RULES };
export default [
{
ignores: [
"dist/**",
"target/**",
"src-tauri/**",
"backend-server/**",
"marketing/**",
"packages/**",
"crates/**",
"scripts/**",
// Written by `bun run generate-constants` from the Rust constants. A
// finding here is a codegen bug, and the fix belongs in the generator.
"src/lib/constants.generated.ts",
],
},
{
// Off, and coupled to the deferred rules above rather than to taste.
//
// The tree already carries 18 `eslint-disable-next-line
// react-hooks/exhaustive-deps` comments, written against a lint setup that
// never reached CI. With `exhaustive-deps` deferred, ESLint correctly reads
// all 18 as suppressing nothing — so turning this on means either 18 errors
// or deleting 18 comments that will be needed again the moment the rule is
// enabled. Both are worse than leaving it off.
//
// Flip it to "error" in the same PR that enables `exhaustive-deps`
// (LAC-4500), where the directives become meaningful again. Until then the
// safeguard on the suppressions added below is that each one names its
// issue, so the sweep that fixes the violation finds the comment.
linterOptions: { reportUnusedDisableDirectives: "off" },
},
// .ts — no `jsx` syntax plugin, so angle-bracket type assertions (`<T>x`)
// still parse. Enabling jsx here would read them as an unclosed element.
{
files: ["src/**/*.ts", "types/**/*.ts", "vite.config.ts"],
languageOptions: {
parser: babelParser,
ecmaVersion: 2022,
sourceType: "module",
parserOptions: babelOptionsFor([["typescript", {}]]),
},
plugins: { "react-hooks": reactHooks },
rules: RULES,
},
// .tsx — typescript + jsx. TSX has no angle-bracket assertion syntax, so
// there is nothing for the jsx plugin to collide with.
{
files: ["src/**/*.tsx"],
languageOptions: {
parser: babelParser,
ecmaVersion: 2022,
sourceType: "module",
parserOptions: babelOptionsFor([["typescript", {}], "jsx"]),
},
plugins: { "react-hooks": reactHooks },
rules: RULES,
},
];