@@ -5,9 +5,9 @@ use oxc_ast::{
5
5
} ;
6
6
use oxc_macros:: declare_oxc_lint;
7
7
use oxc_semantic:: {
8
- petgraph:: { self , graph :: NodeIndex } ,
8
+ petgraph:: { self } ,
9
9
pg:: neighbors_filtered_by_edge_weight,
10
- AstNodeId , AstNodes , BasicBlockElement , EdgeType , Register ,
10
+ AstNodeId , AstNodes , BasicBlockElement , BasicBlockId , EdgeType , Register ,
11
11
} ;
12
12
use oxc_span:: { Atom , CompactStr } ;
13
13
use oxc_syntax:: operator:: AssignmentOperator ;
@@ -220,18 +220,18 @@ impl Rule for RulesOfHooks {
220
220
return ;
221
221
}
222
222
223
- let node_cfg_ix = node. cfg_ix ( ) ;
224
- let func_cfg_ix = parent_func. cfg_ix ( ) ;
223
+ let node_cfg_id = node. cfg_id ( ) ;
224
+ let func_cfg_id = parent_func. cfg_id ( ) ;
225
225
226
226
// there is no branch between us and our parent function
227
- if node_cfg_ix == func_cfg_ix {
227
+ if node_cfg_id == func_cfg_id {
228
228
return ;
229
229
}
230
230
231
231
if !petgraph:: algo:: has_path_connecting (
232
232
& semantic. cfg ( ) . graph ,
233
- func_cfg_ix ,
234
- node_cfg_ix ,
233
+ func_cfg_id ,
234
+ node_cfg_id ,
235
235
None ,
236
236
) {
237
237
// There should always be a control flow path between a parent and child node.
@@ -246,8 +246,8 @@ impl Rule for RulesOfHooks {
246
246
return ctx. diagnostic ( diagnostics:: loop_hook ( span, hook_name) ) ;
247
247
}
248
248
249
- if self . is_conditional ( ctx, func_cfg_ix , node_cfg_ix )
250
- || self . breaks_early ( ctx, func_cfg_ix , node_cfg_ix )
249
+ if self . is_conditional ( ctx, func_cfg_id , node_cfg_id )
250
+ || self . breaks_early ( ctx, func_cfg_id , node_cfg_id )
251
251
{
252
252
#[ allow( clippy:: needless_return) ]
253
253
return ctx. diagnostic ( diagnostics:: conditional_hook ( span, hook_name) ) ;
@@ -278,42 +278,42 @@ impl RulesOfHooks {
278
278
fn is_conditional (
279
279
& self ,
280
280
ctx : & LintContext ,
281
- func_cfg_ix : NodeIndex ,
282
- node_cfg_ix : NodeIndex ,
281
+ func_cfg_id : BasicBlockId ,
282
+ node_cfg_id : BasicBlockId ,
283
283
) -> bool {
284
284
let graph = & ctx. semantic ( ) . cfg ( ) . graph ;
285
285
// All nodes should be reachable from our hook, Otherwise we have a conditional/branching flow.
286
- petgraph:: algo:: dijkstra ( graph, func_cfg_ix , Some ( node_cfg_ix ) , |e| match e. weight ( ) {
286
+ petgraph:: algo:: dijkstra ( graph, func_cfg_id , Some ( node_cfg_id ) , |e| match e. weight ( ) {
287
287
EdgeType :: NewFunction => 1 ,
288
288
EdgeType :: Backedge | EdgeType :: Normal => 0 ,
289
289
} )
290
290
. into_iter ( )
291
291
. filter ( |( _, val) | * val == 0 )
292
- . any ( |( f, _) | !petgraph:: algo:: has_path_connecting ( graph, f, node_cfg_ix , None ) )
292
+ . any ( |( f, _) | !petgraph:: algo:: has_path_connecting ( graph, f, node_cfg_id , None ) )
293
293
}
294
294
295
295
#[ inline( always) ]
296
296
fn breaks_early (
297
297
& self ,
298
298
ctx : & LintContext ,
299
- func_cfg_ix : NodeIndex ,
300
- node_cfg_ix : NodeIndex ,
299
+ func_cfg_id : BasicBlockId ,
300
+ node_cfg_id : BasicBlockId ,
301
301
) -> bool {
302
302
let cfg = ctx. semantic ( ) . cfg ( ) ;
303
303
neighbors_filtered_by_edge_weight (
304
304
& cfg. graph ,
305
- func_cfg_ix ,
305
+ func_cfg_id ,
306
306
& |e| match e {
307
307
EdgeType :: Normal => None ,
308
308
EdgeType :: Backedge | EdgeType :: NewFunction => Some ( State :: default ( ) ) ,
309
309
} ,
310
- & mut |ix : & NodeIndex , mut state : State | {
311
- if node_cfg_ix == * ix {
310
+ & mut |id : & BasicBlockId , mut state : State | {
311
+ if node_cfg_id == * id {
312
312
return ( state, false ) ;
313
313
}
314
314
315
315
let ( push, keep_walking) = cfg
316
- . basic_block_by_index ( * ix )
316
+ . basic_block ( * id )
317
317
. iter ( )
318
318
. fold_while ( ( false , true ) , |acc, it| match it {
319
319
BasicBlockElement :: Break ( _) => FoldWhile :: Done ( ( true , false ) ) ,
@@ -327,7 +327,7 @@ impl RulesOfHooks {
327
327
. into_inner ( ) ;
328
328
329
329
if push {
330
- state. 0 . push ( * ix ) ;
330
+ state. 0 . push ( * id ) ;
331
331
}
332
332
( state, keep_walking)
333
333
} ,
@@ -340,7 +340,7 @@ impl RulesOfHooks {
340
340
}
341
341
342
342
#[ derive( Debug , Default , Clone ) ]
343
- struct State ( Vec < NodeIndex > ) ;
343
+ struct State ( Vec < BasicBlockId > ) ;
344
344
345
345
fn parent_func < ' a > ( nodes : & ' a AstNodes < ' a > , node : & AstNode ) -> Option < & ' a AstNode < ' a > > {
346
346
nodes. ancestors ( node. id ( ) ) . map ( |id| nodes. get_node ( id) ) . find ( |it| it. kind ( ) . is_function_like ( ) )
@@ -380,10 +380,10 @@ fn is_somewhere_inside_component_or_hook(nodes: &AstNodes, node_id: AstNodeId) -
380
380
} ,
381
381
)
382
382
} )
383
- . any ( |( ix , id ) | {
384
- id . is_some_and ( |name| {
383
+ . any ( |( id , ident ) | {
384
+ ident . is_some_and ( |name| {
385
385
is_react_component_or_hook_name ( name. as_str ( ) )
386
- || is_memo_or_forward_ref_callback ( nodes, ix )
386
+ || is_memo_or_forward_ref_callback ( nodes, id )
387
387
} )
388
388
} )
389
389
}
0 commit comments