Skip to content

Commit

Permalink
improvement: better reachablity API.
Browse files Browse the repository at this point in the history
  • Loading branch information
rzvxa committed May 31, 2024
1 parent 5a1117e commit ae0f550
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 16 deletions.
7 changes: 1 addition & 6 deletions crates/oxc_linter/src/rules/react/rules_of_hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion crates/oxc_semantic/examples/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
17 changes: 8 additions & 9 deletions crates/oxc_semantic/src/control_flow/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit ae0f550

Please sign in to comment.