Skip to content
TypeScript

chore(lint): fail lint when the React Compiler skips a listed file (#20292)

Julius Marminge48a16040891d
diff --git a/apps/web/src/components/PersistentThreadTerminals.tsx b/apps/web/src/components/PersistentThreadTerminals.tsx
index 9dfd67bceab..7e539e22995 100644
--- a/apps/web/src/components/PersistentThreadTerminals.tsx
+++ b/apps/web/src/components/PersistentThreadTerminals.tsx
@@ -8,7 +8,7 @@ import {
import { scopeProjectRef } from "@t3tools/client-runtime/environment";
import { projectScriptCwd, projectScriptRuntimeEnv } from "@t3tools/shared/projectScripts";
import { resolveTerminalSessionLabel } from "@t3tools/shared/terminalLabels";
-import { memo, useCallback, useEffect, useMemo, useState } from "react";
+import { memo, useEffect, useState } from "react";
import { useComposerDraftStore } from "../composerDraftStore";
import { useStableCallback } from "../hooks/useStableCallback";
import type { TerminalContextSelection } from "../lib/terminalContext";
@@ -70,6 +70,12 @@ function serverTerminalIdsStrictSubsetOfClient(
return true;
}
+interface TerminalLaunchLocationWithEnv {
+ readonly cwd: string;
+ readonly worktreePath: string | null;
+ readonly runtimeEnv: Record<string, string>;
+}
+
interface PersistentThreadTerminalDrawerProps {
threadRef: { environmentId: EnvironmentId; threadId: ThreadId };
threadId: ThreadId;
@@ -118,67 +124,36 @@ export const PersistentThreadTerminalDrawer = memo(function PersistentThreadTerm
const panelSurfaces = useRightPanelStore(
(state) => selectThreadRightPanelState(state.byThreadKey, threadRef).surfaces,
);
- const panelTerminalIds = useMemo(
- () =>
- new Set(
- panelSurfaces.flatMap((surface) =>
- surface.kind === "terminal" ? surface.terminalIds : [],
- ),
- ),
- [panelSurfaces],
- );
- const drawerTerminalSessions = useMemo(
- () =>
- knownTerminalSessions?.filter(
- (session) => !panelTerminalIds.has(session.target.terminalId),
- ) ?? [],
- [knownTerminalSessions, panelTerminalIds],
+ const panelTerminalIds = new Set(
+ panelSurfaces.flatMap((surface) => (surface.kind === "terminal" ? surface.terminalIds : [])),
);
- const terminalLabelsById = useMemo(() => {
- const next = new Map<string, string>();
- for (const session of drawerTerminalSessions) {
- next.set(
- session.target.terminalId,
- resolveTerminalSessionLabel(session.target.terminalId, session.state.summary),
- );
- }
- return next;
- }, [drawerTerminalSessions]);
- const terminalLaunchLocationsById = useMemo(() => {
- const next = new Map<
- string,
- {
- readonly cwd: string;
- readonly worktreePath: string | null;
- readonly runtimeEnv: Record<string, string>;
- }
- >();
- if (!project) {
- return next;
- }
-
- for (const session of drawerTerminalSessions) {
- const summary = session.state.summary;
- if (!summary) {
- continue;
- }
- const worktreePathForLaunch =
- launchContext !== null ? launchContext.worktreePath : summary.worktreePath;
- next.set(session.target.terminalId, {
- cwd: launchContext?.cwd ?? summary.cwd,
+ const drawerTerminalSessions =
+ knownTerminalSessions?.filter((session) => !panelTerminalIds.has(session.target.terminalId)) ??
+ [];
+ const terminalLabelsById = new Map<string, string>();
+ for (const session of drawerTerminalSessions) {
+ terminalLabelsById.set(
+ session.target.terminalId,
+ resolveTerminalSessionLabel(session.target.terminalId, session.state.summary),
+ );
+ }
+ const terminalLaunchLocationsById = new Map<string, TerminalLaunchLocationWithEnv>();
+ for (const session of drawerTerminalSessions) {
+ const summary = session.state.summary;
+ if (!project || !summary) continue;
+ const worktreePathForLaunch =
+ launchContext !== null ? launchContext.worktreePath : summary.worktreePath;
+ terminalLaunchLocationsById.set(session.target.terminalId, {
+ cwd: launchContext?.cwd ?? summary.cwd,
+ worktreePath: worktreePathForLaunch,
+ runtimeEnv: projectScriptRuntimeEnv({
+ project: { cwd: project.workspaceRoot },
worktreePath: worktreePathForLaunch,
- runtimeEnv: projectScriptRuntimeEnv({
- project: { cwd: project.workspaceRoot },
- worktreePath: worktreePathForLaunch,
- }),
- });
- }
-
- return next;
- }, [drawerTerminalSessions, launchContext, project]);
- const serverOrderedTerminalIds = useMemo(
- () => drawerTerminalSessions.map((session) => session.target.terminalId),
- [drawerTerminalSessions],
+ }),
+ });
+ }
+ const serverOrderedTerminalIds = drawerTerminalSessions.map(
+ (session) => session.target.terminalId,
);
const storeSetTerminalHeight = useTerminalUiStateStore((state) => state.setTerminalHeight);
const storeSplitTerminal = useTerminalUiStateStore((state) => state.splitTerminal);
@@ -203,47 +178,32 @@ export const PersistentThreadTerminalDrawer = memo(function PersistentThreadTerm
}, [reconcileTerminalIds, serverOrderedTerminalIds, terminalUiState.terminalIds, threadRef]);
const [localFocusRequestId, setLocalFocusRequestId] = useState(0);
const worktreePath = serverThread?.worktreePath ?? draftThread?.worktreePath ?? null;
- const effectiveWorktreePath = useMemo(() => {
- if (launchContext !== null) {
- return launchContext.worktreePath;
- }
- return worktreePath;
- }, [launchContext, worktreePath]);
- const cwd = useMemo(
- () =>
- launchContext?.cwd ??
- (project
- ? projectScriptCwd({
- project: { cwd: project.workspaceRoot },
- worktreePath: effectiveWorktreePath,
- })
- : null),
- [effectiveWorktreePath, launchContext?.cwd, project],
- );
- const runtimeEnv = useMemo(
- () =>
- project
- ? projectScriptRuntimeEnv({
- project: { cwd: project.workspaceRoot },
- worktreePath: effectiveWorktreePath,
- })
- : {},
- [effectiveWorktreePath, project],
- );
+ const effectiveWorktreePath = launchContext !== null ? launchContext.worktreePath : worktreePath;
+ const cwd =
+ launchContext?.cwd ??
+ (project
+ ? projectScriptCwd({
+ project: { cwd: project.workspaceRoot },
+ worktreePath: effectiveWorktreePath,
+ })
+ : null);
+ const runtimeEnv = project
+ ? projectScriptRuntimeEnv({
+ project: { cwd: project.workspaceRoot },
+ worktreePath: effectiveWorktreePath,
+ })
+ : {};
- const bumpFocusRequestId = useCallback(() => {
+ const bumpFocusRequestId = () => {
if (!visible) {
return;
}
setLocalFocusRequestId((value) => value + 1);
- }, [visible]);
+ };
- const setTerminalHeight = useCallback(
- (height: number) => {
- storeSetTerminalHeight(threadRef, height);
- },
- [storeSetTerminalHeight, threadRef],
- );
+ const setTerminalHeight = (height: number) => {
+ storeSetTerminalHeight(threadRef, height);
+ };
const launchDrawerTerminal = (place: (terminalId: string) => void) => {
if (!cwd) return;
@@ -265,13 +225,10 @@ export const PersistentThreadTerminalDrawer = memo(function PersistentThreadTerm
launchDrawerTerminal((terminalId) => storeNewTerminal(threadRef, terminalId)),
);
- const activateTerminal = useCallback(
- (terminalId: string) => {
- storeSetActiveTerminal(threadRef, terminalId);
- bumpFocusRequestId();
- },
- [bumpFocusRequestId, storeSetActiveTerminal, threadRef],
- );
+ const activateTerminal = (terminalId: string) => {
+ storeSetActiveTerminal(threadRef, terminalId);
+ bumpFocusRequestId();
+ };
const closeTerminal = useStableCallback((terminalId: string) => {
if (!terminalActions.canWrite()) return;
@@ -280,15 +237,12 @@ export const PersistentThreadTerminalDrawer = memo(function PersistentThreadTerm
bumpFocusRequestId();
});
- const handleAddTerminalContext = useCallback(
- (selection: TerminalContextSelection) => {
- if (!visible) {
- return;
- }
- onAddTerminalContext(selection);
- },
- [onAddTerminalContext, visible],
- );
+ const handleAddTerminalContext = (selection: TerminalContextSelection) => {
+ if (!visible) {
+ return;
+ }
+ onAddTerminalContext(selection);
+ };
if (!project || (!terminalUiState.terminalOpen && !active) || !cwd) {
return null;
@@ -394,81 +348,38 @@ export const PersistentThreadTerminalPanel = memo(function PersistentThreadTermi
?.state.summary ?? null;
const worktreePath =
launchContext?.worktreePath ?? activeSummary?.worktreePath ?? threadWorktreePath;
- const cwd = useMemo(
- () =>
- launchContext?.cwd ??
- activeSummary?.cwd ??
- (project
- ? projectScriptCwd({
- project: { cwd: project.workspaceRoot },
- worktreePath,
- })
- : null),
- [activeSummary?.cwd, launchContext?.cwd, project, worktreePath],
- );
- const runtimeEnv = useMemo(
- () =>
- project
- ? projectScriptRuntimeEnv({
- project: { cwd: project.workspaceRoot },
- worktreePath,
- })
- : {},
- [project, worktreePath],
- );
- const terminalLabelsById = useMemo(() => {
- const labels = new Map<string, string>();
- for (const terminalId of surface.terminalIds) {
- const summary =
- knownTerminalSessions?.find((session) => session.target.terminalId === terminalId)?.state
- .summary ?? null;
- labels.set(terminalId, resolveTerminalSessionLabel(terminalId, summary));
- }
- return labels;
- }, [knownTerminalSessions, surface.terminalIds]);
- const terminalLaunchLocationsById = useMemo(() => {
- const locations = new Map<
- string,
- {
- readonly cwd: string;
- readonly worktreePath: string | null;
- readonly runtimeEnv: Record<string, string>;
- }
- >();
- for (const terminalId of surface.terminalIds) {
- const summary =
- knownTerminalSessions?.find((session) => session.target.terminalId === terminalId)?.state
- .summary ?? null;
- const terminalWorktreePath =
- launchContext?.worktreePath ?? summary?.worktreePath ?? threadWorktreePath;
- const terminalCwd =
+ const cwd =
+ launchContext?.cwd ??
+ activeSummary?.cwd ??
+ (project ? projectScriptCwd({ project: { cwd: project.workspaceRoot }, worktreePath }) : null);
+ const runtimeEnv = project
+ ? projectScriptRuntimeEnv({ project: { cwd: project.workspaceRoot }, worktreePath })
+ : {};
+ const terminalLabelsById = new Map<string, string>();
+ const terminalLaunchLocationsById = new Map<string, TerminalLaunchLocationWithEnv>();
+ for (const terminalId of surface.terminalIds) {
+ const summary =
+ knownTerminalSessions?.find((session) => session.target.terminalId === terminalId)?.state
+ .summary ?? null;
+ terminalLabelsById.set(terminalId, resolveTerminalSessionLabel(terminalId, summary));
+ if (!project) continue;
+ const terminalWorktreePath =
+ launchContext?.worktreePath ?? summary?.worktreePath ?? threadWorktreePath;
+ terminalLaunchLocationsById.set(terminalId, {
+ cwd:
launchContext?.cwd ??
summary?.cwd ??
- (project
- ? projectScriptCwd({
- project: { cwd: project.workspaceRoot },
- worktreePath: terminalWorktreePath,
- })
- : null);
- if (!terminalCwd || !project) continue;
- locations.set(terminalId, {
- cwd: terminalCwd,
- worktreePath: terminalWorktreePath,
- runtimeEnv: projectScriptRuntimeEnv({
+ projectScriptCwd({
project: { cwd: project.workspaceRoot },
worktreePath: terminalWorktreePath,
}),
- });
- }
- return locations;
- }, [
- knownTerminalSessions,
- launchContext?.cwd,
- launchContext?.worktreePath,
- project,
- surface.terminalIds,
- threadWorktreePath,
- ]);
+ worktreePath: terminalWorktreePath,
+ runtimeEnv: projectScriptRuntimeEnv({
+ project: { cwd: project.workspaceRoot },
+ worktreePath: terminalWorktreePath,
+ }),
+ });
+ }
if (!project || !cwd) return null;
diff --git a/docs/internals/web-ui.md b/docs/internals/web-ui.md
index 8eba6c04495..ac12b56f29f 100644
--- a/docs/internals/web-ui.md
+++ b/docs/internals/web-ui.md
@@ -53,3 +53,16 @@ Calls inside the component keep the plain function, so work that resumes after a
acts on the render it started in. A function that a child calls during render, or after an
`await`, comes from a module-level factory memoized on its inputs, so its closure keeps only
those inputs.
+
+## React Compiler
+
+The compiler memoizes a component or hook only when it can prove the code follows React's rules.
+Anything it can't prove, such as reading a ref during render or calling `setState` from an effect,
+makes it skip the whole function without a build error. Lint reports each skip as a
+`react(...)` warning with the note "React Compiler skipped optimizing this component or hook".
+
+Files listed in the React Compiler override in [`vite.config.ts`](../../vite.config.ts) must
+compile. There those rules are errors, and `t3code/no-manual-memo` rejects `useMemo` and
+`useCallback`, which the compiler makes redundant. `useStableCallback` still marks handlers
+that leave a large component, as described above. Add a file to the list once its skips are
+fixed, and drop its manual memoization in the same change.
diff --git a/oxlint-plugin-t3code/index.ts b/oxlint-plugin-t3code/index.ts
index 19ebfb6df8a..e67cc86a99a 100644
--- a/oxlint-plugin-t3code/index.ts
+++ b/oxlint-plugin-t3code/index.ts
@@ -5,6 +5,7 @@ import noGlobalProcessRuntime from "./rules/no-global-process-runtime.ts";
import noHermesUnsupportedApis from "./rules/no-hermes-unsupported-apis.ts";
import noInlineSchemaCompile from "./rules/no-inline-schema-compile.ts";
import noLowValueComments from "./rules/no-low-value-comments.ts";
+import noManualMemo from "./rules/no-manual-memo.ts";
import noManualEffectRuntimeInTests from "./rules/no-manual-effect-runtime-in-tests.ts";
import noMobileUniwindThemeEscapeHatches from "./rules/no-mobile-uniwind-theme-escape-hatches.ts";
import noNativeTitleTooltip from "./rules/no-native-title-tooltip.ts";
@@ -29,6 +30,7 @@ export default definePlugin({
"no-inline-schema-compile": noInlineSchemaCompile,
"no-low-value-comments": noLowValueComments,
"no-manual-effect-runtime-in-tests": noManualEffectRuntimeInTests,
+ "no-manual-memo": noManualMemo,
"no-mobile-uniwind-theme-escape-hatches": noMobileUniwindThemeEscapeHatches,
"no-native-title-tooltip": noNativeTitleTooltip,
"no-outset-state-indicators": noOutsetStateIndicators,
diff --git a/oxlint-plugin-t3code/rules/no-manual-memo.test.ts b/oxlint-plugin-t3code/rules/no-manual-memo.test.ts
new file mode 100644
index 00000000000..8044d698998
--- /dev/null
+++ b/oxlint-plugin-t3code/rules/no-manual-memo.test.ts
@@ -0,0 +1,24 @@
+import { describe } from "@effect/vitest";
+
+import { createOxlintRuleHarness } from "../test/utils.ts";
+
+const rule = createOxlintRuleHarness("t3code/no-manual-memo", {
+ filename: "fixture.tsx",
+});
+
+describe("t3code/no-manual-memo", () => {
+ rule.valid("allows plain derived values", `const total = items.length * 2;`);
+
+ rule.valid("allows useStableCallback", `const onClick = useStableCallback(() => save());`);
+
+ rule.valid("allows other React members", `const ref = React.useRef(null);`);
+
+ rule.invalid("reports useMemo", `const total = useMemo(() => items.length, [items]);`);
+
+ rule.invalid("reports useCallback", `const onClick = useCallback(() => save(), []);`);
+
+ rule.invalid(
+ "reports React.useMemo",
+ `const total = React.useMemo(() => items.length, [items]);`,
+ );
+});
diff --git a/oxlint-plugin-t3code/rules/no-manual-memo.ts b/oxlint-plugin-t3code/rules/no-manual-memo.ts
new file mode 100644
index 00000000000..73cdf0fc8a4
--- /dev/null
+++ b/oxlint-plugin-t3code/rules/no-manual-memo.ts
@@ -0,0 +1,42 @@
+import { defineRule, type ESTree } from "@oxlint/plugins";
+
+const MANUAL_MEMO_HOOKS = new Set(["useCallback", "useMemo"]);
+
+function manualMemoHookName(callee: ESTree.Node): string | null {
+ if (callee.type === "Identifier") {
+ return MANUAL_MEMO_HOOKS.has(callee.name) ? callee.name : null;
+ }
+ if (
+ callee.type === "MemberExpression" &&
+ !callee.computed &&
+ callee.object.type === "Identifier" &&
+ callee.object.name === "React" &&
+ callee.property.type === "Identifier" &&
+ MANUAL_MEMO_HOOKS.has(callee.property.name)
+ ) {
+ return callee.property.name;
+ }
+ return null;
+}
+
+export default defineRule({
+ meta: {
+ type: "suggestion",
+ docs: {
+ description:
+ "Disallow useCallback and useMemo in files the React Compiler must compile; it memoizes them.",
+ },
+ },
+ create(context) {
+ return {
+ CallExpression(node) {
+ const name = manualMemoHookName(node.callee);
+ if (name === null) return;
+ context.report({
+ node: node.callee,
+ message: `Drop ${name}: the React Compiler memoizes this file. Write the plain value or function, and use useStableCallback only where a handler must keep one identity across renders.`,
+ });
+ },
+ };
+ },
+});
diff --git a/vite.config.ts b/vite.config.ts
index 293b6873dcd..f140ca60fbd 100644
--- a/vite.config.ts
+++ b/vite.config.ts
@@ -320,6 +320,31 @@ export default defineConfig({
],
},
},
+ {
+ // Files the React Compiler must compile. Any bailout (a ref read in render, a
+ // setState in an effect, a manual memo it cannot preserve) fails lint instead of
+ // silently leaving the file unoptimized, and manual memoization is redundant here.
+ // Add a file once it compiles cleanly. ChatView is being split toward this list.
+ files: [
+ "apps/web/src/components/PersistentThreadTerminals.tsx",
+ "apps/web/src/components/terminal/**",
+ ],
+ rules: {
+ "react/exhaustive-effect-dependencies": "error",
+ "react/globals": "error",
+ "react/hooks": "error",
+ "react/immutability": "error",
+ "react/memo-dependencies": "error",
+ "react/preserve-manual-memoization": "error",
+ "react/purity": "error",
+ "react/refs": "error",
+ "react/set-state-in-effect": "error",
+ "react/set-state-in-render": "error",
+ "react/static-components": "error",
+ "react/unsupported-syntax": "error",
+ "t3code/no-manual-memo": "error",
+ },
+ },
{
// The sign-in masthead is T3 brand artwork: fixed gradients, not theme surfaces.
files: ["apps/web/src/components/auth/AuthSurfaceShell.tsx"],