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

reactCompilerCoverage.test.ts

148 lines5.8 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/ui/AndroidAnchoredMenu.tsx": "ref read during render",
21 "src/components/ui/Menu.tsx": "ref read during render",
22 "src/features/diffs/nativeReviewDiffSurface.ts": "ref read during render",
23 "src/features/home/thread-swipe-actions.tsx": "ref read during render",
24 "src/features/terminal/ThreadTerminalRouteScreen.tsx": "ref read during render",
25 "src/features/threads/PendingUserInputCard.tsx": "mutates a frozen value",
26 "src/features/threads/ThreadArrangementSheet.tsx": "ref read during render",
27 "src/features/voice-input/VoiceInputProvider.tsx": "ref read during render",
28 "src/features/voice-input/useVoiceInputController.ts": "ref read during render",
29 "src/native/StackHeader.tsx": "ref read during render",
30 "src/native/T3ComposerEditor.ios.tsx": "ref read during render",
31 "src/native/T3ComposerEditor.native.tsx": "ref read during render",
32};
33​
34// Caller flags Metro passes for an iOS release bundle. supportsReactCompiler is
35// what `experiments.reactCompiler` in app.config.ts turns on.
36const metroCaller = {
37 name: "metro",
38 bundler: "metro",
39 platform: "ios",
40 isDev: false,
41 isServer: false,
42 isNodeModule: false,
43 supportsStaticESM: true,
44 supportsReactCompiler: true,
45};
46​
47const sourceFiles = NodeFS.readdirSync(NodePath.join(mobileRoot, "src"), { recursive: true })
48 .map(String)
49 .filter((file) => /\.tsx?$/.test(file) && !/\.(test\.tsx?|d\.ts)$/.test(file))
50 .map((file) => `src/${file.split(NodePath.sep).join("/")}`)
51 .sort();
52​
53type CompilerLoc = { readonly start?: { readonly line?: number } } | null;
54​
55interface CompilerEvent {
56 readonly kind: string;
57 readonly detail?: {
58 readonly options?: {
59 readonly category?: string;
60 readonly reason?: string;
61 readonly loc?: CompilerLoc;
62 readonly details?: readonly { readonly loc?: CompilerLoc }[];
63 };
64 };
66​
67// babel.config.js is the file Metro loads; the logger rides on the preset's
68// own react-compiler option so the plugin runs with Metro's exact settings.
69function compilerBailouts(file: string): string[] {
70 const events: CompilerEvent[] = [];
71 const babelConfig = requireFromMobile("./babel.config.js")({ cache: () => {} });
72 const presets = babelConfig.presets.map(([name, options]: [string, object]) =>
73 name === "babel-preset-expo"
74 ? [
75 requireFromMobile.resolve(name),
76 {
77 ...options,
78 "react-compiler": {
79 logger: { logEvent: (_: string, event: CompilerEvent) => events.push(event) },
80 },
81 },
82 ]
83 : [name, options],
84 );
85 babel.transformFileSync(NodePath.join(mobileRoot, file), {
86 babelrc: false,
87 configFile: false,
88 cwd: mobileRoot,
89 presets,
90 caller: metroCaller,
91 code: false,
92 });
93 return events
94 .filter((event) => event.kind === "CompileError" || event.kind === "CompileSkip")
95 .map(
96 (event) =>
97 `line ${(event.detail?.options?.loc ?? event.detail?.options?.details?.[0]?.loc)?.start?.line ?? "?"}: ${event.detail?.options?.category ?? event.kind} ${event.detail?.options?.reason ?? ""}`,
98 );
100​
101describe("React Compiler coverage", () => {
102 it("Metro's transformer applies the compiler", async () => {
103 const { transformer } = await metroConfig;
104 const metroTransformer = requireFromMobile(transformer.babelTransformerPath);
105 const filename = NodePath.join(
106 mobileRoot,
107 "src/features/archive/ArchivedThreadsRouteScreen.tsx",
108 );
109 const transformed = (reactCompiler: string) => {
110 const { ast } = metroTransformer.transform({
111 filename,
112 src: NodeFS.readFileSync(filename, "utf8"),
113 plugins: [],
114 options: {
115 dev: false,
116 hot: false,
117 platform: "ios",
118 projectRoot: mobileRoot,
119 type: "module",
120 enableBabelRCLookup: false,
121 experimentalImportSupport: false,
122 customTransformOptions: { reactCompiler, routerRoot: "app" },
123 },
124 });
125 return JSON.stringify(ast.program.body);
126 };
127 expect(transformed("true")).toContain("react/compiler-runtime");
128 expect(transformed("false")).not.toContain("react/compiler-runtime");
129 });
130​
131 it("app.config.ts turns the compiler on for Metro", () => {
132 const appConfig = NodeFS.readFileSync(NodePath.join(mobileRoot, "app.config.ts"), "utf8");
133 expect(appConfig).toMatch(/experiments:\s*\{[^}]*reactCompiler:\s*true/);
134 });
135​
136 it.each(sourceFiles)("compiles %s", (file) => {
137 const bailouts = compilerBailouts(file);
138 if (file in KNOWN_BAILOUTS) {
139 expect(bailouts, `${file} compiles now; remove it from KNOWN_BAILOUTS`).not.toEqual([]);
140 } else {
141 expect(bailouts).toEqual([]);
142 }
143 });
144​
145 it("lists only files that exist", () => {
146 expect(Object.keys(KNOWN_BAILOUTS).filter((file) => !sourceFiles.includes(file))).toEqual([]);
147 });
148}, 300_000);