@@ -172,7 +172,85 @@ export default function ComponentMap({
172
172
}
173
173
} ;
174
174
175
- collectNodes ( currentSnapshot ) ;
175
+ const shouldIncludeNode = ( node ) => {
176
+ // Return false if node has any context properties
177
+ if ( node ?. componentData ?. context && Object . keys ( node . componentData . context ) . length > 0 ) {
178
+ return false ;
179
+ }
180
+ // Return false if node name ends with 'Provider'
181
+ if ( node ?. name && node . name . endsWith ( 'Provider' ) ) {
182
+ return false ;
183
+ }
184
+ return true ;
185
+ } ;
186
+
187
+ const processTreeData = ( node ) => {
188
+ if ( ! node ) return null ;
189
+
190
+ // Create a new node
191
+ const newNode = { ...node } ;
192
+
193
+ if ( node . children ) {
194
+ // Process all children first
195
+ const processedChildren = node . children
196
+ . map ( ( child ) => processTreeData ( child ) )
197
+ . filter ( Boolean ) ; // Remove null results
198
+
199
+ // For each child that shouldn't be included, replace it with its children
200
+ newNode . children = processedChildren . reduce ( ( acc , child ) => {
201
+ if ( shouldIncludeNode ( child ) ) {
202
+ // If child should be included, add it directly
203
+ acc . push ( child ) ;
204
+ } else {
205
+ // If child should be filtered out, add its children instead
206
+ if ( child . children ) {
207
+ acc . push ( ...child . children ) ;
208
+ }
209
+ }
210
+ return acc ;
211
+ } , [ ] ) ;
212
+ }
213
+
214
+ return newNode ;
215
+ } ;
216
+
217
+ let filtered = processTreeData ( currentSnapshot ) ;
218
+ console . log ( 'filtered' , filtered ) ;
219
+ collectNodes ( filtered ) ;
220
+
221
+ const keepContextAndProviderNodes = ( node ) => {
222
+ if ( ! node ) return null ;
223
+
224
+ // Check if this node should be kept
225
+ const hasContext =
226
+ node ?. componentData ?. context && Object . keys ( node . componentData . context ) . length > 0 ;
227
+ const isProvider = node ?. name && node . name . endsWith ( 'Provider' ) ;
228
+ const shouldKeepNode = hasContext || isProvider ;
229
+
230
+ // Process children first
231
+ let processedChildren = [ ] ;
232
+ if ( node . children ) {
233
+ processedChildren = node . children
234
+ . map ( ( child ) => keepContextAndProviderNodes ( child ) )
235
+ . filter ( Boolean ) ; // Remove null results
236
+ }
237
+
238
+ // If this node should be kept or has kept children, return it
239
+ if ( shouldKeepNode || processedChildren . length > 0 ) {
240
+ return {
241
+ ...node ,
242
+ children : processedChildren ,
243
+ } ;
244
+ }
245
+
246
+ // If neither the node should be kept nor it has kept children, filter it out
247
+ return null ;
248
+ } ;
249
+
250
+ const contextProvidersOnly = keepContextAndProviderNodes ( currentSnapshot ) ;
251
+ console . log ( 'context only' , contextProvidersOnly ) ;
252
+
253
+ collectNodes ( filtered ) ;
176
254
177
255
// @ts
178
256
// find the node that has been selected and use it as the root
0 commit comments