Skip to content

Commit 3be52df

Browse files
chore(lint): fail lint when the React Compiler skips a listed file
The compiler silently skips any component or hook it can't prove follows React's rules, so manual useMemo/useCallback stays load-bearing and nobody notices. Files listed in a new override must compile: the compiler's bailout rules are errors there, and the new t3code/no-manual-memo rule rejects useMemo and useCallback. The terminal hook and the persistent terminal drawer and panel start the list; their manual memoization is gone. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 02e6fe9 commit 3be52df

6 files changed

Lines changed: 198 additions & 181 deletions

File tree

‎apps/web/src/components/PersistentThreadTerminals.tsx‎

Lines changed: 92 additions & 181 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
import { scopeProjectRef } from "@t3tools/client-runtime/environment";
99
import { projectScriptCwd, projectScriptRuntimeEnv } from "@t3tools/shared/projectScripts";
1010
import { resolveTerminalSessionLabel } from "@t3tools/shared/terminalLabels";
11-
import { memo, useCallback, useEffect, useMemo, useState } from "react";
11+
import { memo, useEffect, useState } from "react";
1212
import { useComposerDraftStore } from "../composerDraftStore";
1313
import { useStableCallback } from "../hooks/useStableCallback";
1414
import type { TerminalContextSelection } from "../lib/terminalContext";
@@ -70,6 +70,12 @@ function serverTerminalIdsStrictSubsetOfClient(
7070
return true;
7171
}
7272

73+
interface TerminalLaunchLocationWithEnv {
74+
readonly cwd: string;
75+
readonly worktreePath: string | null;
76+
readonly runtimeEnv: Record<string, string>;
77+
}
78+
7379
interface PersistentThreadTerminalDrawerProps {
7480
threadRef: { environmentId: EnvironmentId; threadId: ThreadId };
7581
threadId: ThreadId;
@@ -118,67 +124,36 @@ export const PersistentThreadTerminalDrawer = memo(function PersistentThreadTerm
118124
const panelSurfaces = useRightPanelStore(
119125
(state) => selectThreadRightPanelState(state.byThreadKey, threadRef).surfaces,
120126
);
121-
const panelTerminalIds = useMemo(
122-
() =>
123-
new Set(
124-
panelSurfaces.flatMap((surface) =>
125-
surface.kind === "terminal" ? surface.terminalIds : [],
126-
),
127-
),
128-
[panelSurfaces],
129-
);
130-
const drawerTerminalSessions = useMemo(
131-
() =>
132-
knownTerminalSessions?.filter(
133-
(session) => !panelTerminalIds.has(session.target.terminalId),
134-
) ?? [],
135-
[knownTerminalSessions, panelTerminalIds],
127+
const panelTerminalIds = new Set(
128+
panelSurfaces.flatMap((surface) => (surface.kind === "terminal" ? surface.terminalIds : [])),
136129
);
137-
const terminalLabelsById = useMemo(() => {
138-
const next = new Map<string, string>();
139-
for (const session of drawerTerminalSessions) {
140-
next.set(
141-
session.target.terminalId,
142-
resolveTerminalSessionLabel(session.target.terminalId, session.state.summary),
143-
);
144-
}
145-
return next;
146-
}, [drawerTerminalSessions]);
147-
const terminalLaunchLocationsById = useMemo(() => {
148-
const next = new Map<
149-
string,
150-
{
151-
readonly cwd: string;
152-
readonly worktreePath: string | null;
153-
readonly runtimeEnv: Record<string, string>;
154-
}
155-
>();
156-
if (!project) {
157-
return next;
158-
}
159-
160-
for (const session of drawerTerminalSessions) {
161-
const summary = session.state.summary;
162-
if (!summary) {
163-
continue;
164-
}
165-
const worktreePathForLaunch =
166-
launchContext !== null ? launchContext.worktreePath : summary.worktreePath;
167-
next.set(session.target.terminalId, {
168-
cwd: launchContext?.cwd ?? summary.cwd,
130+
const drawerTerminalSessions =
131+
knownTerminalSessions?.filter((session) => !panelTerminalIds.has(session.target.terminalId)) ??
132+
[];
133+
const terminalLabelsById = new Map<string, string>();
134+
for (const session of drawerTerminalSessions) {
135+
terminalLabelsById.set(
136+
session.target.terminalId,
137+
resolveTerminalSessionLabel(session.target.terminalId, session.state.summary),
138+
);
139+
}
140+
const terminalLaunchLocationsById = new Map<string, TerminalLaunchLocationWithEnv>();
141+
for (const session of drawerTerminalSessions) {
142+
const summary = session.state.summary;
143+
if (!project || !summary) continue;
144+
const worktreePathForLaunch =
145+
launchContext !== null ? launchContext.worktreePath : summary.worktreePath;
146+
terminalLaunchLocationsById.set(session.target.terminalId, {
147+
cwd: launchContext?.cwd ?? summary.cwd,
148+
worktreePath: worktreePathForLaunch,
149+
runtimeEnv: projectScriptRuntimeEnv({
150+
project: { cwd: project.workspaceRoot },
169151
worktreePath: worktreePathForLaunch,
170-
runtimeEnv: projectScriptRuntimeEnv({
171-
project: { cwd: project.workspaceRoot },
172-
worktreePath: worktreePathForLaunch,
173-
}),
174-
});
175-
}
176-
177-
return next;
178-
}, [drawerTerminalSessions, launchContext, project]);
179-
const serverOrderedTerminalIds = useMemo(
180-
() => drawerTerminalSessions.map((session) => session.target.terminalId),
181-
[drawerTerminalSessions],
152+
}),
153+
});
154+
}
155+
const serverOrderedTerminalIds = drawerTerminalSessions.map(
156+
(session) => session.target.terminalId,
182157
);
183158
const storeSetTerminalHeight = useTerminalUiStateStore((state) => state.setTerminalHeight);
184159
const storeSplitTerminal = useTerminalUiStateStore((state) => state.splitTerminal);
@@ -203,47 +178,32 @@ export const PersistentThreadTerminalDrawer = memo(function PersistentThreadTerm
203178
}, [reconcileTerminalIds, serverOrderedTerminalIds, terminalUiState.terminalIds, threadRef]);
204179
const [localFocusRequestId, setLocalFocusRequestId] = useState(0);
205180
const worktreePath = serverThread?.worktreePath ?? draftThread?.worktreePath ?? null;
206-
const effectiveWorktreePath = useMemo(() => {
207-
if (launchContext !== null) {
208-
return launchContext.worktreePath;
209-
}
210-
return worktreePath;
211-
}, [launchContext, worktreePath]);
212-
const cwd = useMemo(
213-
() =>
214-
launchContext?.cwd ??
215-
(project
216-
? projectScriptCwd({
217-
project: { cwd: project.workspaceRoot },
218-
worktreePath: effectiveWorktreePath,
219-
})
220-
: null),
221-
[effectiveWorktreePath, launchContext?.cwd, project],
222-
);
223-
const runtimeEnv = useMemo(
224-
() =>
225-
project
226-
? projectScriptRuntimeEnv({
227-
project: { cwd: project.workspaceRoot },
228-
worktreePath: effectiveWorktreePath,
229-
})
230-
: {},
231-
[effectiveWorktreePath, project],
232-
);
181+
const effectiveWorktreePath = launchContext !== null ? launchContext.worktreePath : worktreePath;
182+
const cwd =
183+
launchContext?.cwd ??
184+
(project
185+
? projectScriptCwd({
186+
project: { cwd: project.workspaceRoot },
187+
worktreePath: effectiveWorktreePath,
188+
})
189+
: null);
190+
const runtimeEnv = project
191+
? projectScriptRuntimeEnv({
192+
project: { cwd: project.workspaceRoot },
193+
worktreePath: effectiveWorktreePath,
194+
})
195+
: {};
233196

234-
const bumpFocusRequestId = useCallback(() => {
197+
const bumpFocusRequestId = () => {
235198
if (!visible) {
236199
return;
237200
}
238201
setLocalFocusRequestId((value) => value + 1);
239-
}, [visible]);
202+
};
240203

241-
const setTerminalHeight = useCallback(
242-
(height: number) => {
243-
storeSetTerminalHeight(threadRef, height);
244-
},
245-
[storeSetTerminalHeight, threadRef],
246-
);
204+
const setTerminalHeight = (height: number) => {
205+
storeSetTerminalHeight(threadRef, height);
206+
};
247207

248208
const launchDrawerTerminal = (place: (terminalId: string) => void) => {
249209
if (!cwd) return;
@@ -265,13 +225,10 @@ export const PersistentThreadTerminalDrawer = memo(function PersistentThreadTerm
265225
launchDrawerTerminal((terminalId) => storeNewTerminal(threadRef, terminalId)),
266226
);
267227

268-
const activateTerminal = useCallback(
269-
(terminalId: string) => {
270-
storeSetActiveTerminal(threadRef, terminalId);
271-
bumpFocusRequestId();
272-
},
273-
[bumpFocusRequestId, storeSetActiveTerminal, threadRef],
274-
);
228+
const activateTerminal = (terminalId: string) => {
229+
storeSetActiveTerminal(threadRef, terminalId);
230+
bumpFocusRequestId();
231+
};
275232

276233
const closeTerminal = useStableCallback((terminalId: string) => {
277234
if (!terminalActions.canWrite()) return;
@@ -280,15 +237,12 @@ export const PersistentThreadTerminalDrawer = memo(function PersistentThreadTerm
280237
bumpFocusRequestId();
281238
});
282239

283-
const handleAddTerminalContext = useCallback(
284-
(selection: TerminalContextSelection) => {
285-
if (!visible) {
286-
return;
287-
}
288-
onAddTerminalContext(selection);
289-
},
290-
[onAddTerminalContext, visible],
291-
);
240+
const handleAddTerminalContext = (selection: TerminalContextSelection) => {
241+
if (!visible) {
242+
return;
243+
}
244+
onAddTerminalContext(selection);
245+
};
292246

293247
if (!project || (!terminalUiState.terminalOpen && !active) || !cwd) {
294248
return null;
@@ -394,81 +348,38 @@ export const PersistentThreadTerminalPanel = memo(function PersistentThreadTermi
394348
?.state.summary ?? null;
395349
const worktreePath =
396350
launchContext?.worktreePath ?? activeSummary?.worktreePath ?? threadWorktreePath;
397-
const cwd = useMemo(
398-
() =>
399-
launchContext?.cwd ??
400-
activeSummary?.cwd ??
401-
(project
402-
? projectScriptCwd({
403-
project: { cwd: project.workspaceRoot },
404-
worktreePath,
405-
})
406-
: null),
407-
[activeSummary?.cwd, launchContext?.cwd, project, worktreePath],
408-
);
409-
const runtimeEnv = useMemo(
410-
() =>
411-
project
412-
? projectScriptRuntimeEnv({
413-
project: { cwd: project.workspaceRoot },
414-
worktreePath,
415-
})
416-
: {},
417-
[project, worktreePath],
418-
);
419-
const terminalLabelsById = useMemo(() => {
420-
const labels = new Map<string, string>();
421-
for (const terminalId of surface.terminalIds) {
422-
const summary =
423-
knownTerminalSessions?.find((session) => session.target.terminalId === terminalId)?.state
424-
.summary ?? null;
425-
labels.set(terminalId, resolveTerminalSessionLabel(terminalId, summary));
426-
}
427-
return labels;
428-
}, [knownTerminalSessions, surface.terminalIds]);
429-
const terminalLaunchLocationsById = useMemo(() => {
430-
const locations = new Map<
431-
string,
432-
{
433-
readonly cwd: string;
434-
readonly worktreePath: string | null;
435-
readonly runtimeEnv: Record<string, string>;
436-
}
437-
>();
438-
for (const terminalId of surface.terminalIds) {
439-
const summary =
440-
knownTerminalSessions?.find((session) => session.target.terminalId === terminalId)?.state
441-
.summary ?? null;
442-
const terminalWorktreePath =
443-
launchContext?.worktreePath ?? summary?.worktreePath ?? threadWorktreePath;
444-
const terminalCwd =
351+
const cwd =
352+
launchContext?.cwd ??
353+
activeSummary?.cwd ??
354+
(project ? projectScriptCwd({ project: { cwd: project.workspaceRoot }, worktreePath }) : null);
355+
const runtimeEnv = project
356+
? projectScriptRuntimeEnv({ project: { cwd: project.workspaceRoot }, worktreePath })
357+
: {};
358+
const terminalLabelsById = new Map<string, string>();
359+
const terminalLaunchLocationsById = new Map<string, TerminalLaunchLocationWithEnv>();
360+
for (const terminalId of surface.terminalIds) {
361+
const summary =
362+
knownTerminalSessions?.find((session) => session.target.terminalId === terminalId)?.state
363+
.summary ?? null;
364+
terminalLabelsById.set(terminalId, resolveTerminalSessionLabel(terminalId, summary));
365+
if (!project) continue;
366+
const terminalWorktreePath =
367+
launchContext?.worktreePath ?? summary?.worktreePath ?? threadWorktreePath;
368+
terminalLaunchLocationsById.set(terminalId, {
369+
cwd:
445370
launchContext?.cwd ??
446371
summary?.cwd ??
447-
(project
448-
? projectScriptCwd({
449-
project: { cwd: project.workspaceRoot },
450-
worktreePath: terminalWorktreePath,
451-
})
452-
: null);
453-
if (!terminalCwd || !project) continue;
454-
locations.set(terminalId, {
455-
cwd: terminalCwd,
456-
worktreePath: terminalWorktreePath,
457-
runtimeEnv: projectScriptRuntimeEnv({
372+
projectScriptCwd({
458373
project: { cwd: project.workspaceRoot },
459374
worktreePath: terminalWorktreePath,
460375
}),
461-
});
462-
}
463-
return locations;
464-
}, [
465-
knownTerminalSessions,
466-
launchContext?.cwd,
467-
launchContext?.worktreePath,
468-
project,
469-
surface.terminalIds,
470-
threadWorktreePath,
471-
]);
376+
worktreePath: terminalWorktreePath,
377+
runtimeEnv: projectScriptRuntimeEnv({
378+
project: { cwd: project.workspaceRoot },
379+
worktreePath: terminalWorktreePath,
380+
}),
381+
});
382+
}
472383

473384
if (!project || !cwd) return null;
474385

‎docs/internals/web-ui.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,16 @@ Calls inside the component keep the plain function, so work that resumes after a
5353
acts on the render it started in. A function that a child calls during render, or after an
5454
`await`, comes from a module-level factory memoized on its inputs, so its closure keeps only
5555
those inputs.
56+
57+
## React Compiler
58+
59+
The compiler memoizes a component or hook only when it can prove the code follows React's rules.
60+
Anything it can't prove, such as reading a ref during render or calling `setState` from an effect,
61+
makes it skip the whole function without a build error. Lint reports each skip as a
62+
`react(...)` warning with the note "React Compiler skipped optimizing this component or hook".
63+
64+
Files listed in the React Compiler override in [`vite.config.ts`](../../vite.config.ts) must
65+
compile. There those rules are errors, and `t3code/no-manual-memo` rejects `useMemo` and
66+
`useCallback`, which the compiler makes redundant. `useStableCallback` still marks handlers
67+
that leave a large component, as described above. Add a file to the list once its skips are
68+
fixed, and drop its manual memoization in the same change.

‎oxlint-plugin-t3code/index.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import noGlobalProcessRuntime from "./rules/no-global-process-runtime.ts";
55
import noHermesUnsupportedApis from "./rules/no-hermes-unsupported-apis.ts";
66
import noInlineSchemaCompile from "./rules/no-inline-schema-compile.ts";
77
import noLowValueComments from "./rules/no-low-value-comments.ts";
8+
import noManualMemo from "./rules/no-manual-memo.ts";
89
import noManualEffectRuntimeInTests from "./rules/no-manual-effect-runtime-in-tests.ts";
910
import noMobileUniwindThemeEscapeHatches from "./rules/no-mobile-uniwind-theme-escape-hatches.ts";
1011
import noNativeTitleTooltip from "./rules/no-native-title-tooltip.ts";
@@ -29,6 +30,7 @@ export default definePlugin({
2930
"no-inline-schema-compile": noInlineSchemaCompile,
3031
"no-low-value-comments": noLowValueComments,
3132
"no-manual-effect-runtime-in-tests": noManualEffectRuntimeInTests,
33+
"no-manual-memo": noManualMemo,
3234
"no-mobile-uniwind-theme-escape-hatches": noMobileUniwindThemeEscapeHatches,
3335
"no-native-title-tooltip": noNativeTitleTooltip,
3436
"no-outset-state-indicators": noOutsetStateIndicators,
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { describe } from "@effect/vitest";
2+
3+
import { createOxlintRuleHarness } from "../test/utils.ts";
4+
5+
const rule = createOxlintRuleHarness("t3code/no-manual-memo", {
6+
filename: "fixture.tsx",
7+
});
8+
9+
describe("t3code/no-manual-memo", () => {
10+
rule.valid("allows plain derived values", `const total = items.length * 2;`);
11+
12+
rule.valid("allows useStableCallback", `const onClick = useStableCallback(() => save());`);
13+
14+
rule.valid("allows other React members", `const ref = React.useRef(null);`);
15+
16+
rule.invalid("reports useMemo", `const total = useMemo(() => items.length, [items]);`);
17+
18+
rule.invalid("reports useCallback", `const onClick = useCallback(() => save(), []);`);
19+
20+
rule.invalid(
21+
"reports React.useMemo",
22+
`const total = React.useMemo(() => items.length, [items]);`,
23+
);
24+
});

0 commit comments

Comments
 (0)