1+ use core:: panic;
12use std:: {
23 collections:: { BinaryHeap , VecDeque , hash_map:: Entry } ,
34 future:: Future ,
@@ -871,6 +872,14 @@ impl ModuleGraphRef {
871872 . context ( "Expected graph node" )
872873 }
873874
875+ fn should_visit_node ( & self , node : & SingleModuleGraphNode ) -> bool {
876+ if self . skip_visited_module_children {
877+ !matches ! ( node, SingleModuleGraphNode :: VisitedModule { .. } )
878+ } else {
879+ true
880+ }
881+ }
882+
874883 /// Returns a map of all modules in the graphs to their identifiers.
875884 /// This is primarily useful for debugging.
876885 pub async fn get_ids ( & self ) -> Result < FxHashMap < ResolvedVc < Box < dyn Module > > , ReadRef < RcStr > > > {
@@ -887,10 +896,12 @@ impl ModuleGraphRef {
887896
888897 /// Traverses all reachable nodes exactly once and calls the visitor.
889898 ///
890- /// * `entry` - The entry module to start the traversal from
891- /// * `visitor` - Called before visiting the children of a node.
892- /// - Receives &SingleModuleGraphNode
899+ /// * `entries` - The entry modules to start the traversal from
900+ /// * `state` mutable state to be shared across the visitors
901+ /// * `visit_preorder` - Called before visiting the children of a node.
902+ /// - Receives the module and the `state`
893903 /// - Can return [GraphTraversalAction]s to control the traversal
904+ /// * `visit_postorder` - Called after visiting children of a node.
894905 pub fn traverse_nodes_from_entries_dfs < S > (
895906 & self ,
896907 entries : impl IntoIterator < Item = ResolvedVc < Box < dyn Module > > > ,
@@ -899,7 +910,6 @@ impl ModuleGraphRef {
899910 mut visit_postorder : impl FnMut ( ResolvedVc < Box < dyn Module > > , & mut S ) -> Result < ( ) > ,
900911 ) -> Result < ( ) > {
901912 let graphs = & self . graphs ;
902- let skip_visited_module_children = self . skip_visited_module_children ;
903913
904914 let entries = entries. into_iter ( ) . collect :: < Vec < _ > > ( ) ;
905915
@@ -928,8 +938,7 @@ impl ModuleGraphRef {
928938 }
929939 stack. push ( ( Pass :: Visit , current) ) ;
930940 if action == GraphTraversalAction :: Continue
931- && !( skip_visited_module_children
932- && matches ! ( current_node, SingleModuleGraphNode :: VisitedModule { .. } ) )
941+ && self . should_visit_node ( current_node)
933942 {
934943 let current = current_node. target_idx ( ) . unwrap_or ( current) ;
935944 stack. extend (
@@ -963,7 +972,6 @@ impl ModuleGraphRef {
963972 ) -> Result < GraphTraversalAction > ,
964973 ) -> Result < ( ) > {
965974 let graphs = & self . graphs ;
966- let skip_visited_module_children = self . skip_visited_module_children ;
967975
968976 let mut queue = VecDeque :: from (
969977 entries
@@ -986,9 +994,7 @@ impl ModuleGraphRef {
986994 Some ( ( node_weight. module ( ) , edge_weight) ) ,
987995 succ_weight. module ( ) ,
988996 ) ?;
989- if skip_visited_module_children
990- && matches ! ( succ_weight, SingleModuleGraphNode :: VisitedModule { .. } )
991- {
997+ if !self . should_visit_node ( succ_weight) {
992998 continue ;
993999 }
9941000 let succ = succ_weight. target_idx ( ) . unwrap_or ( succ) ;
@@ -1021,7 +1027,6 @@ impl ModuleGraphRef {
10211027 ) -> GraphTraversalAction ,
10221028 ) -> Result < ( ) > {
10231029 let graphs = & self . graphs ;
1024- let skip_visited_module_children = self . skip_visited_module_children ;
10251030
10261031 let mut stack = entries
10271032 . into_iter ( )
@@ -1042,9 +1047,7 @@ impl ModuleGraphRef {
10421047 Some ( ( node_weight. module ( ) , edge_weight) ) ,
10431048 succ_weight. module ( ) ,
10441049 ) ;
1045- if skip_visited_module_children
1046- && matches ! ( succ_weight, SingleModuleGraphNode :: VisitedModule { .. } )
1047- {
1050+ if !self . should_visit_node ( succ_weight) {
10481051 continue ;
10491052 }
10501053 let succ = succ_weight. target_idx ( ) . unwrap_or ( succ) ;
@@ -1102,7 +1105,6 @@ impl ModuleGraphRef {
11021105 /// * `visit_postorder` - Called after visiting the children of a node. Return
11031106 /// - Receives: (originating &SingleModuleGraphNode, edge &ChunkingType), target
11041107 /// &SingleModuleGraphNode, state &S
1105- /// - Can return [GraphTraversalAction]s to control the traversal
11061108 pub fn traverse_edges_from_entries_dfs < S > (
11071109 & self ,
11081110 entries : impl IntoIterator < Item = ResolvedVc < Box < dyn Module > > > ,
@@ -1119,7 +1121,6 @@ impl ModuleGraphRef {
11191121 ) -> Result < ( ) > ,
11201122 ) -> Result < ( ) > {
11211123 let graphs = & self . graphs ;
1122- let skip_visited_module_children = self . skip_visited_module_children ;
11231124
11241125 let entries = entries. into_iter ( ) . collect :: < Vec < _ > > ( ) ;
11251126
@@ -1158,8 +1159,7 @@ impl ModuleGraphRef {
11581159 stack. push ( ( Pass :: Visit , parent, current) ) ;
11591160 if action == GraphTraversalAction :: Continue
11601161 && expanded. insert ( current)
1161- && !( skip_visited_module_children
1162- && matches ! ( current_node, SingleModuleGraphNode :: VisitedModule { .. } ) )
1162+ && self . should_visit_node ( current_node)
11631163 {
11641164 let current = current_node. target_idx ( ) . unwrap_or ( current) ;
11651165 stack. extend ( iter_graphs_neighbors_rev ( graphs, current) . map (
@@ -1220,7 +1220,9 @@ impl ModuleGraphRef {
12201220 ) -> Result < usize > {
12211221 let graphs = & self . graphs ;
12221222 if self . skip_visited_module_children {
1223- bail ! ( "traverse_edges_fixed_point_with_priority musn't be called on individual graphs" ) ;
1223+ panic ! (
1224+ "traverse_edges_fixed_point_with_priority musn't be called on individual graphs"
1225+ ) ;
12241226 }
12251227
12261228 #[ derive( PartialEq , Eq ) ]
0 commit comments