Skip to content
TypeScript

chore(web): lint rejects "use memo" directives (#20843)

Julius Marminge2fbd4318fc5b
diff --git a/apps/web/src/components/CommandPalette.tsx b/apps/web/src/components/CommandPalette.tsx
index 123e436a3b7..1564207a2d7 100644
--- a/apps/web/src/components/CommandPalette.tsx
+++ b/apps/web/src/components/CommandPalette.tsx
@@ -366,13 +366,15 @@ function notifyThemeSaveFailure(): void {
);
}
-// Kept as "use memo" hooks: inlined, each result reaches the dialog's returned
-// <CommandPaletteContent> through the action and thread item arrays built after
-// the browse hooks, and the compiler leaves all of them uncached.
+// These helpers keep "use memo" until the palette's item building moves out of this
+// component: inlined, each result reaches the returned <CommandPaletteContent> through
+// the action and thread item arrays built after the browse hooks, and the compiler
+// leaves all of them uncached.
function useThemeCards(
customThemes: ReturnType<typeof useCustomThemes>,
environmentThemes: ReturnType<typeof useEnvironmentThemeDefinitions>,
) {
+ // oxlint-disable-next-line t3code/no-manual-memo -- Kept until the palette's item building moves out of this component (see above).
"use memo";
const seen = new Set<string>();
return [
@@ -388,6 +390,7 @@ function useThemeCards(
}
function useThreadContentMatchByKey(matches: ReturnType<typeof useThreadSearch>["matches"]) {
+ // oxlint-disable-next-line t3code/no-manual-memo -- Kept until the palette's item building moves out of this component (see above).
"use memo";
return new Map(
matches.flatMap((match) =>
@@ -399,6 +402,7 @@ function useThreadContentMatchByKey(matches: ReturnType<typeof useThreadSearch>[
}
function useEnvironmentLookups(environments: ReturnType<typeof useEnvironments>["environments"]) {
+ // oxlint-disable-next-line t3code/no-manual-memo -- Kept until the palette's item building moves out of this component (see above).
"use memo";
const environmentLabelById = new Map(
environments.map((environment) => [environment.environmentId, environment.label] as const),
@@ -421,6 +425,7 @@ function useEnvironmentLookups(environments: ReturnType<typeof useEnvironments>[
}
function useProjectLookups(projects: ReturnType<typeof useProjects>) {
+ // oxlint-disable-next-line t3code/no-manual-memo -- Kept until the palette's item building moves out of this component (see above).
"use memo";
const projectCwdById = new Map<ProjectId, string>(
projects.map((project) => [project.id, project.workspaceRoot]),
@@ -449,6 +454,7 @@ function useSortedProjectGroups({
readonly environmentLabelById: ReadonlyMap<EnvironmentId, string>;
readonly threads: ReturnType<typeof useThreadShells>;
}) {
+ // oxlint-disable-next-line t3code/no-manual-memo -- Kept until the palette's item building moves out of this component (see above).
"use memo";
const projectGroupingSettings = selectProjectGroupingSettings(clientSettings);
const orderedProjects = orderItemsByPreferredIds({
diff --git a/docs/internals/web-ui.md b/docs/internals/web-ui.md
index 7e00521c2fa..c1975a4f98e 100644
--- a/docs/internals/web-ui.md
+++ b/docs/internals/web-ui.md
@@ -63,7 +63,10 @@ makes it skip the whole function without a build error. Lint reports each skip a
Every file under `apps/web/src` must compile. Lint makes those rules errors there, and
`t3code/no-manual-memo` rejects `useMemo` and `useCallback`, which the compiler makes redundant;
-no upstream rule bans them outright. Lint misses some bailouts, notably `try` with `finally` and
+no upstream rule bans them outright. It also rejects `"use memo"`. The compiler withholds a cache
+scope when a value reaches plain function calls on both sides of a hook call. A `"use memo"`
+helper forces one, but hides the ordering problem. Fix the order instead: plain uses of a value go
+before the hook, or the helper is inlined. Lint misses some bailouts, notably `try` with `finally` and
`throw` inside `try`, so [`reactCompilerCoverage.test.ts`](../../apps/web/src/reactCompilerCoverage.test.ts)
compiles every file and fails on any skip. Async handlers rarely need `try`: an
atom command settles into a result, so cleanup can follow the `await`. `useStableCallback` still
diff --git a/oxlint-plugin-t3code/rules/no-manual-memo.test.ts b/oxlint-plugin-t3code/rules/no-manual-memo.test.ts
index 8044d698998..cd6d26dba1b 100644
--- a/oxlint-plugin-t3code/rules/no-manual-memo.test.ts
+++ b/oxlint-plugin-t3code/rules/no-manual-memo.test.ts
@@ -13,6 +13,13 @@ describe("t3code/no-manual-memo", () => {
rule.valid("allows other React members", `const ref = React.useRef(null);`);
+ rule.valid("allows other directives", `function Panel() { "use client"; return null; }`);
+
+ rule.invalid(
+ 'reports a "use memo" directive',
+ `function useTotal(items) { "use memo"; return items.length; }`,
+ );
+
rule.invalid("reports useMemo", `const total = useMemo(() => items.length, [items]);`);
rule.invalid("reports useCallback", `const onClick = useCallback(() => save(), []);`);
diff --git a/oxlint-plugin-t3code/rules/no-manual-memo.ts b/oxlint-plugin-t3code/rules/no-manual-memo.ts
index 73cdf0fc8a4..fd8e61b653c 100644
--- a/oxlint-plugin-t3code/rules/no-manual-memo.ts
+++ b/oxlint-plugin-t3code/rules/no-manual-memo.ts
@@ -24,11 +24,21 @@ export default defineRule({
type: "suggestion",
docs: {
description:
- "Disallow useCallback and useMemo in files the React Compiler must compile; it memoizes them.",
+ 'Disallow useCallback, useMemo and "use memo" directives in files the React Compiler must compile; it memoizes them.',
},
},
create(context) {
return {
+ // A "use memo" helper only exists to force a cache scope the compiler withheld,
+ // which hides the ordering or structure problem that withheld it.
+ ExpressionStatement(node) {
+ if (node.directive !== "use memo") return;
+ context.report({
+ node,
+ message:
+ 'Drop "use memo": restructure so the compiler caches this on its own, e.g. move plain uses of a value before the hook call that splits its scope, or inline the helper.',
+ });
+ },
CallExpression(node) {
const name = manualMemoHookName(node.callee);
if (name === null) return;