Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

reactCompilerCoverage.test.ts

222 lines11.2 KB

1// @effect-diagnostics nodeBuiltinImport:off - compiles real source files on disk.
2import * as NodeFS from "node:fs";
3import * as NodeModule from "node:module";
4import * as NodePath from "node:path";
5import { describe, expect, it } from "vite-plus/test";
6​
7const mobileRoot = NodePath.resolve(__dirname, "..");
8const requireFromMobile = NodeModule.createRequire(NodePath.join(mobileRoot, "package.json"));
9const babel: {
10 transformFileSync: (file: string, options: Record<string, unknown>) => unknown;
11} = requireFromMobile("@babel/core");
12const metroConfig: Promise<{ transformer: { babelTransformerPath: string } }> =
13 requireFromMobile("./metro.config.js");
14​
15// Files the compiler skips today. A skipped file still ships, just unmemoized.
16// This list only shrinks: the test fails when a file here starts compiling, so
17// delete its entry (and its manual useMemo/useCallback/memo) in the same change.
18const KNOWN_BAILOUTS: Record<string, string> = {
19 "src/components/ComposerAttachmentStrip.tsx": "unsupported syntax",
20 "src/components/ComposerContextAttachment.tsx":
21 "throw inside try, try without catch, try/finally",
22 "src/components/ComposerEditor.tsx": "try/finally",
23 "src/components/VideoPreviewModal.ios.tsx": "try without catch",
24 "src/components/VideoPreviewModal.tsx": "ref read during render",
25 "src/components/ui/AndroidAnchoredMenu.tsx": "ref read during render",
26 "src/components/ui/Menu.tsx": "ref read during render",
27 "src/components/ui/OverlayPortal.tsx": "unsupported syntax",
28 "src/features/connection/ConnectionsNewRouteScreen.tsx": "try without catch",
29 "src/features/diffs/nativeReviewDiffSurface.ts": "ref read during render",
30 "src/features/files/AttachmentFileScreen.tsx": "manual memo not preserved",
31 "src/features/files/FileMarkdownPreview.tsx": "try without catch",
32 "src/features/files/FileTreeBrowser.tsx": "ref read during render",
33 "src/features/files/SourceFileSurface.tsx": "try without catch",
34 "src/features/files/ThreadFilesRouteScreen.tsx": "manual memo not preserved",
35 "src/features/files/useFileTreeEntries.ts": "manual memo not preserved",
36 "src/features/home/HomeScreen.tsx": "ref read during render",
37 "src/features/home/WorkspaceConnectionTitle.tsx": "ref read during render",
38 "src/features/home/home-list-options.ts": "manual memo not preserved",
39 "src/features/home/thread-swipe-actions.tsx": "ref read during render",
40 "src/features/home/useThreadListActions.ts": "try without catch",
41 "src/features/layout/AdaptiveWorkspaceLayout.tsx": "ref read during render",
42 "src/features/layout/workspace-inspector-pane.tsx": "mutates a frozen value",
43 "src/features/layout/workspace-pane-divider.tsx": "ref read during render",
44 "src/features/projects/AddProjectScreen.tsx": "try without catch",
45 "src/features/pull-requests/PullRequestDiscussionScreen.tsx": "try/finally",
46 "src/features/pull-requests/PullRequestInlineCommentScreen.tsx": "try without catch",
47 "src/features/pull-requests/PullRequestMergeControls.tsx": "throw inside try, try/finally",
48 "src/features/pull-requests/PullRequestReviewSheet.tsx": "try/finally",
49 "src/features/pull-requests/PullRequestReviewersScreen.tsx": "throw inside try, try/finally",
50 "src/features/pull-requests/PullRequestRouteScreen.tsx": "manual memo not preserved",
51 "src/features/pull-requests/guide/PullRequestGuideScreen.tsx": "conditional inside try",
52 "src/features/pull-requests/guide/ReviewGuideExcerpt.tsx": "conditional inside try",
53 "src/features/pull-requests/guide/useReviewGuide.ts": "try without catch",
54 "src/features/pull-requests/usePullRequestAction.ts": "throw inside try, try/finally",
55 "src/features/pull-requests/usePullRequestListActions.ts": "throw inside try, try/finally",
56 "src/features/pull-requests/usePullRequestListControls.ts": "unsupported syntax",
57 "src/features/pull-requests/usePullRequestThreads.ts": "throw inside try, try/finally",
58 "src/features/review/ReviewSheet.tsx": "try without catch",
59 "src/features/review/reviewDiffRendering.tsx": "mutates a frozen value",
60 "src/features/review/useNativeReviewDiffHighlighting.ts": "conditional inside try",
61 "src/features/settings/SettingsAboutRouteScreen.tsx": "try without catch",
62 "src/features/settings/SettingsAuthRouteScreen.tsx": "ref read during render",
63 "src/features/settings/SettingsEnvironmentDetailRouteScreen.tsx":
64 "try without catch, try/finally",
65 "src/features/settings/SettingsEnvironmentsRouteScreen.tsx": "try without catch",
66 "src/features/settings/SettingsNotificationsRouteScreen.tsx": "try without catch",
67 "src/features/settings/SettingsProjectOverviewRouteScreen.tsx": "try without catch",
68 "src/features/settings/SettingsProviderAccountsRouteScreen.tsx": "conditional inside try",
69 "src/features/settings/SettingsScheduledTasksRouteScreen.tsx": "unsupported syntax",
70 "src/features/settings/components/SettingsDragHandle.tsx": "ref read during render",
71 "src/features/terminal/ThreadTerminalRouteScreen.tsx": "ref read during render",
72 "src/features/threads/NewTaskContextPickerScreens.tsx":
73 "manual memo not preserved, try without catch",
74 "src/features/threads/NewTaskDraftScreen.tsx": "try/finally",
75 "src/features/threads/NewTaskRouteScreen.tsx": "try without catch",
76 "src/features/threads/PendingUserInputCard.tsx": "mutates a frozen value",
77 "src/features/threads/QuestionAttachments.tsx": "try/finally",
78 "src/features/threads/ThreadArrangementSheet.tsx": "ref read during render",
79 "src/features/threads/ThreadComposer.tsx": "try without catch",
80 "src/features/threads/ThreadDetailScreen.tsx": "mutates a frozen value",
81 "src/features/threads/ThreadFeed.tsx": "ref read during render, throw inside try, try/finally",
82 "src/features/threads/ThreadGitControls.tsx": "manual memo not preserved",
83 "src/features/threads/ThreadNavigationSidebar.tsx": "ref read during render",
84 "src/features/threads/ThreadQueueControl.tsx": "ref read during render, try without catch",
85 "src/features/threads/ThreadRouteScreen.tsx": "try without catch",
86 "src/features/threads/ThreadSettingsSheet.tsx": "manual memo not preserved, try without catch",
87 "src/features/threads/UsageLimitRecoveryCard.tsx": "throw inside try, try/finally",
88 "src/features/threads/git/GitCommitSheet.tsx": "manual memo not preserved",
89 "src/features/threads/git/GitOverviewSheet.tsx": "try without catch",
90 "src/features/threads/new-task-flow-provider.tsx": "try without catch",
91 "src/features/threads/thread-work-log.tsx": "manual memo not preserved",
92 "src/features/threads/use-composer-command-menu.ts": "manual memo not preserved",
93 "src/features/threads/use-thread-list-v2-shelf-preferences.ts": "ref read during render",
94 "src/features/voice-input/VoiceInputProvider.tsx": "ref read during render",
95 "src/features/voice-input/useVoiceInputController.ts": "ref read during render",
96 "src/lib/attachmentDocument.ts": "try/finally",
97 "src/native/StackHeader.tsx": "ref read during render",
98 "src/native/T3ComposerEditor.ios.tsx": "ref read during render",
99 "src/native/T3ComposerEditor.native.tsx": "ref read during render",
100 "src/state/composer-attachment-uploads.ts": "try without catch",
101 "src/state/mediaActions.ts": "try without catch",
102 "src/state/queries.ts": "manual memo not preserved",
103 "src/state/use-thread-composer-state.ts": "try without catch",
104 "src/state/use-thread-selection.ts": "ref read during render",
105 "src/state/use-vcs-action-state.ts": "manual memo not preserved",
106};
107​
108// Caller flags Metro passes for an iOS release bundle. supportsReactCompiler is
109// what `experiments.reactCompiler` in app.config.ts turns on.
110const metroCaller = {
111 name: "metro",
112 bundler: "metro",
113 platform: "ios",
114 isDev: false,
115 isServer: false,
116 isNodeModule: false,
117 supportsStaticESM: true,
118 supportsReactCompiler: true,
119};
120​
121const sourceFiles = NodeFS.readdirSync(NodePath.join(mobileRoot, "src"), { recursive: true })
122 .map(String)
123 .filter((file) => /\.tsx?$/.test(file) && !/\.(test\.tsx?|d\.ts)$/.test(file))
124 .map((file) => `src/${file.split(NodePath.sep).join("/")}`)
125 .sort();
126​
127type CompilerLoc = { readonly start?: { readonly line?: number } } | null;
128​
129interface CompilerEvent {
130 readonly kind: string;
131 readonly detail?: {
132 readonly options?: {
133 readonly category?: string;
134 readonly reason?: string;
135 readonly loc?: CompilerLoc;
136 readonly details?: readonly { readonly loc?: CompilerLoc }[];
137 };
138 };
140​
141// babel.config.js is the file Metro loads; the logger rides on the preset's
142// own react-compiler option so the plugin runs with Metro's exact settings.
143function compilerBailouts(file: string): string[] {
144 const events: CompilerEvent[] = [];
145 const babelConfig = requireFromMobile("./babel.config.js")({ cache: () => {} });
146 const presets = babelConfig.presets.map(([name, options]: [string, object]) =>
147 name === "babel-preset-expo"
148 ? [
149 requireFromMobile.resolve(name),
150 {
151 ...options,
152 "react-compiler": {
153 logger: { logEvent: (_: string, event: CompilerEvent) => events.push(event) },
154 },
155 },
156 ]
157 : [name, options],
158 );
159 babel.transformFileSync(NodePath.join(mobileRoot, file), {
160 babelrc: false,
161 configFile: false,
162 cwd: mobileRoot,
163 presets,
164 caller: metroCaller,
165 code: false,
166 });
167 return events
168 .filter((event) => event.kind === "CompileError" || event.kind === "CompileSkip")
169 .map(
170 (event) =>
171 `line ${(event.detail?.options?.loc ?? event.detail?.options?.details?.[0]?.loc)?.start?.line ?? "?"}: ${event.detail?.options?.category ?? event.kind} ${event.detail?.options?.reason ?? ""}`,
172 );
174​
175describe("React Compiler coverage", () => {
176 it("Metro's transformer applies the compiler", async () => {
177 const { transformer } = await metroConfig;
178 const metroTransformer = requireFromMobile(transformer.babelTransformerPath);
179 const filename = NodePath.join(
180 mobileRoot,
181 "src/features/archive/ArchivedThreadsRouteScreen.tsx",
182 );
183 const transformed = (reactCompiler: string) => {
184 const { ast } = metroTransformer.transform({
185 filename,
186 src: NodeFS.readFileSync(filename, "utf8"),
187 plugins: [],
188 options: {
189 dev: false,
190 hot: false,
191 platform: "ios",
192 projectRoot: mobileRoot,
193 type: "module",
194 enableBabelRCLookup: false,
195 experimentalImportSupport: false,
196 customTransformOptions: { reactCompiler, routerRoot: "app" },
197 },
198 });
199 return JSON.stringify(ast.program.body);
200 };
201 expect(transformed("true")).toContain("react/compiler-runtime");
202 expect(transformed("false")).not.toContain("react/compiler-runtime");
203 });
204​
205 it("app.config.ts turns the compiler on for Metro", () => {
206 const appConfig = NodeFS.readFileSync(NodePath.join(mobileRoot, "app.config.ts"), "utf8");
207 expect(appConfig).toMatch(/experiments:\s*\{[^}]*reactCompiler:\s*true/);
208 });
209​
210 it.each(sourceFiles)("compiles %s", (file) => {
211 const bailouts = compilerBailouts(file);
212 if (file in KNOWN_BAILOUTS) {
213 expect(bailouts, `${file} compiles now; remove it from KNOWN_BAILOUTS`).not.toEqual([]);
214 } else {
215 expect(bailouts).toEqual([]);
216 }
217 });
218​
219 it("lists only files that exist", () => {
220 expect(Object.keys(KNOWN_BAILOUTS).filter((file) => !sourceFiles.includes(file))).toEqual([]);
221 });
222}, 300_000);