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