@@ -66,8 +66,7 @@ interface CompilerEvent {
6666
6767// babel.config.js is the file Metro loads; the logger rides on the preset's
6868// own react-compiler option so the plugin runs with Metro's exact settings.
69- function compilerBailouts ( file : string ) : string [ ] {
70- const events : CompilerEvent [ ] = [ ] ;
69+ function compileLikeMetro ( file : string , events : CompilerEvent [ ] , output : Record < string , unknown > ) {
7170 const babelConfig = requireFromMobile ( "./babel.config.js" ) ( { cache : ( ) => { } } ) ;
7271 const presets = babelConfig . presets . map ( ( [ name , options ] : [ string , object ] ) =>
7372 name === "babel-preset-expo"
@@ -82,14 +81,20 @@ function compilerBailouts(file: string): string[] {
8281 ]
8382 : [ name , options ] ,
8483 ) ;
85- babel . transformFileSync ( NodePath . join ( mobileRoot , file ) , {
84+ return babel . transformFileSync ( NodePath . join ( mobileRoot , file ) , {
8685 babelrc : false ,
8786 configFile : false ,
8887 cwd : mobileRoot ,
8988 presets,
9089 caller : metroCaller ,
9190 code : false ,
91+ ...output ,
9292 } ) ;
93+ }
94+
95+ function compilerBailouts ( file : string ) : string [ ] {
96+ const events : CompilerEvent [ ] = [ ] ;
97+ compileLikeMetro ( file , events , { } ) ;
9398 return events
9499 . filter ( ( event ) => event . kind === "CompileError" || event . kind === "CompileSkip" )
95100 . map (
@@ -98,6 +103,47 @@ function compilerBailouts(file: string): string[] {
98103 ) ;
99104}
100105
106+ interface AstNode {
107+ readonly type : string ;
108+ readonly [ key : string ] : unknown ;
109+ }
110+
111+ const isAstNode = ( value : unknown ) : value is AstNode =>
112+ typeof value === "object" && value !== null && "type" in value && typeof value . type === "string" ;
113+
114+ function walkAst (
115+ value : unknown ,
116+ visit : ( node : AstNode , ancestors : readonly AstNode [ ] ) => void ,
117+ ancestors : readonly AstNode [ ] = [ ] ,
118+ ) {
119+ if ( typeof value !== "object" || value === null ) return ;
120+ const node = isAstNode ( value ) ? value : null ;
121+ if ( node ) visit ( node , ancestors ) ;
122+ const childAncestors = node ? [ ...ancestors , node ] : ancestors ;
123+ for ( const child of Object . values ( value ) ) walkAst ( child , visit , childAncestors ) ;
124+ }
125+
126+ // For each call to `callee`, whether it runs only on the cache-miss branch of
127+ // an `if ($[n] !== dep)` block rather than on every render.
128+ function compilerCachedCalls ( file : string , callee : string ) : boolean [ ] {
129+ const result = compileLikeMetro ( file , [ ] , { ast : true } ) ;
130+ const cached : boolean [ ] = [ ] ;
131+ if ( typeof result !== "object" || result === null || ! ( "ast" in result ) ) return cached ;
132+ walkAst ( result . ast , ( node , ancestors ) => {
133+ if ( node . type !== "CallExpression" || ! isAstNode ( node . callee ) ) return ;
134+ if ( node . callee . type !== "Identifier" || node . callee . name !== callee ) return ;
135+ cached . push (
136+ ancestors . some (
137+ ( ancestor , index ) =>
138+ ancestor . type === "IfStatement" &&
139+ ancestors [ index + 1 ] === ancestor . consequent &&
140+ JSON . stringify ( ancestor . test ) . includes ( '"name":"$"' ) ,
141+ ) ,
142+ ) ;
143+ } ) ;
144+ return cached ;
145+ }
146+
101147describe ( "React Compiler coverage" , ( ) => {
102148 it ( "Metro's transformer applies the compiler" , async ( ) => {
103149 const { transformer } = await metroConfig ;
@@ -137,6 +183,14 @@ describe("React Compiler coverage", () => {
137183 }
138184 } ) ;
139185
186+ // The compiler once silently dropped this cache, so every
187+ // ThreadFeed render rebuilt the feed and handed LegendList a new data array.
188+ it ( "caches the thread feed derivation" , ( ) => {
189+ expect (
190+ compilerCachedCalls ( "src/features/threads/ThreadFeed.tsx" , "deriveThreadFeedPresentation" ) ,
191+ ) . toEqual ( [ true ] ) ;
192+ } ) ;
193+
140194 it ( "lists only files that exist" , ( ) => {
141195 expect ( Object . keys ( KNOWN_BAILOUTS ) . filter ( ( file ) => ! sourceFiles . includes ( file ) ) ) . toEqual ( [ ] ) ;
142196 } ) ;
0 commit comments