@@ -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,45 @@ 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 inside an `if ($[n] !== dep)`
127+ // cache 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 ) =>
138+ ancestor . type === "IfStatement" && JSON . stringify ( ancestor . test ) . includes ( '"name":"$"' ) ,
139+ ) ,
140+ ) ;
141+ } ) ;
142+ return cached ;
143+ }
144+
101145describe ( "React Compiler coverage" , ( ) => {
102146 it ( "Metro's transformer applies the compiler" , async ( ) => {
103147 const { transformer } = await metroConfig ;
@@ -137,6 +181,14 @@ describe("React Compiler coverage", () => {
137181 }
138182 } ) ;
139183
184+ // The compiler once silently dropped this cache, so every
185+ // ThreadFeed render rebuilt the feed and handed LegendList a new data array.
186+ it ( "caches the thread feed derivation" , ( ) => {
187+ expect (
188+ compilerCachedCalls ( "src/features/threads/ThreadFeed.tsx" , "deriveThreadFeedPresentation" ) ,
189+ ) . toEqual ( [ true ] ) ;
190+ } ) ;
191+
140192 it ( "lists only files that exist" , ( ) => {
141193 expect ( Object . keys ( KNOWN_BAILOUTS ) . filter ( ( file ) => ! sourceFiles . includes ( file ) ) ) . toEqual ( [ ] ) ;
142194 } ) ;
0 commit comments