Skip to content

Commit 4edd79d

Browse files
committed
improvement: better reachablity API.
1 parent 9847599 commit 4edd79d

File tree

3 files changed

+11
-16
lines changed

3 files changed

+11
-16
lines changed

crates/oxc_linter/src/rules/react/rules_of_hooks.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,7 @@ impl Rule for RulesOfHooks {
228228
return;
229229
}
230230

231-
if !petgraph::algo::has_path_connecting(
232-
&semantic.cfg().graph,
233-
func_cfg_id,
234-
node_cfg_id,
235-
None,
236-
) {
231+
if !ctx.semantic().cfg().is_reachabale(func_cfg_id, node_cfg_id) {
237232
// There should always be a control flow path between a parent and child node.
238233
// If there is none it means we always do an early exit before reaching our hook call.
239234
// In some cases it might mean that we are operating on an invalid `cfg` but in either

crates/oxc_semantic/examples/cfg.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ fn main() -> std::io::Result<()> {
111111
}
112112
});
113113
format!(
114-
"xlabel = \"nodes [{}]\\l\", label = \"bb{}\n{}\"",
114+
"xlabel = \"nodes{} [{}]\\l\", label = \"bb{}\n{}\"",
115+
node.1,
115116
nodes,
116117
node.1,
117118
semantic.semantic.cfg().basic_blocks[*node.1]

crates/oxc_semantic/src/control_flow/mod.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -197,17 +197,16 @@ impl ControlFlowGraph {
197197
let graph = &self.graph;
198198
depth_first_search(&self.graph, Some(from), |event| match event {
199199
DfsEvent::TreeEdge(a, b) => {
200-
if b == to {
201-
return Control::Break(true);
202-
}
203-
204-
let test = graph.edges_connecting(a, b).all(|edge| {
205-
!matches!(edge.weight(), EdgeType::NewFunction | EdgeType::Unreachable)
200+
let unreachable = graph.edges_connecting(a, b).all(|edge| {
201+
matches!(edge.weight(), EdgeType::NewFunction | EdgeType::Unreachable)
206202
});
207-
if test {
208-
Control::Continue
209-
} else {
203+
204+
if unreachable {
210205
Control::Prune
206+
} else if b == to {
207+
return Control::Break(true);
208+
} else {
209+
Control::Continue
211210
}
212211
}
213212
_ => Control::Continue,

0 commit comments

Comments
 (0)