From ef56d3dcee041e1b8fd3db5dc3c91b53190f497a Mon Sep 17 00:00:00 2001 From: rzvxa Date: Thu, 30 May 2024 21:40:33 +0330 Subject: [PATCH] improvement: better reachablity API. --- .../src/rules/react/rules_of_hooks.rs | 7 +------ crates/oxc_semantic/examples/cfg.rs | 3 ++- crates/oxc_semantic/src/control_flow/mod.rs | 17 ++++++++--------- 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/crates/oxc_linter/src/rules/react/rules_of_hooks.rs b/crates/oxc_linter/src/rules/react/rules_of_hooks.rs index b9fc7c03e00d35..fce19e4f33a5c1 100644 --- a/crates/oxc_linter/src/rules/react/rules_of_hooks.rs +++ b/crates/oxc_linter/src/rules/react/rules_of_hooks.rs @@ -228,12 +228,7 @@ impl Rule for RulesOfHooks { return; } - if !petgraph::algo::has_path_connecting( - &semantic.cfg().graph, - func_cfg_id, - node_cfg_id, - None, - ) { + if !ctx.semantic().cfg().is_reachabale(func_cfg_id, node_cfg_id) { // There should always be a control flow path between a parent and child node. // If there is none it means we always do an early exit before reaching our hook call. // In some cases it might mean that we are operating on an invalid `cfg` but in either diff --git a/crates/oxc_semantic/examples/cfg.rs b/crates/oxc_semantic/examples/cfg.rs index 0ab461b0ad188a..1817d925b84a7c 100644 --- a/crates/oxc_semantic/examples/cfg.rs +++ b/crates/oxc_semantic/examples/cfg.rs @@ -111,7 +111,8 @@ fn main() -> std::io::Result<()> { } }); format!( - "xlabel = \"nodes [{}]\\l\", label = \"bb{}\n{}\"", + "xlabel = \"nodes{} [{}]\\l\", label = \"bb{}\n{}\"", + node.1, nodes, node.1, semantic.semantic.cfg().basic_blocks[*node.1] diff --git a/crates/oxc_semantic/src/control_flow/mod.rs b/crates/oxc_semantic/src/control_flow/mod.rs index 715dea363f7df9..e609ed7c3b7b0b 100644 --- a/crates/oxc_semantic/src/control_flow/mod.rs +++ b/crates/oxc_semantic/src/control_flow/mod.rs @@ -197,17 +197,16 @@ impl ControlFlowGraph { let graph = &self.graph; depth_first_search(&self.graph, Some(from), |event| match event { DfsEvent::TreeEdge(a, b) => { - if b == to { - return Control::Break(true); - } - - let test = graph.edges_connecting(a, b).all(|edge| { - !matches!(edge.weight(), EdgeType::NewFunction | EdgeType::Unreachable) + let unreachable = graph.edges_connecting(a, b).all(|edge| { + matches!(edge.weight(), EdgeType::NewFunction | EdgeType::Unreachable) }); - if test { - Control::Continue - } else { + + if unreachable { Control::Prune + } else if b == to { + return Control::Break(true); + } else { + Control::Continue } } _ => Control::Continue,