Skip to content

Commit

Permalink
fix(linter/react): rules_of_hooks resolve false positives with cond…
Browse files Browse the repository at this point in the history
…itional hooks. (#3299)

related to #3071
  • Loading branch information
rzvxa committed May 16, 2024
1 parent 7f9d8b7 commit c8f1f79
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions crates/oxc_linter/src/rules/react/rules_of_hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,13 @@ impl RulesOfHooks {
) -> bool {
let graph = &ctx.semantic().cfg().graph;
// All nodes should be reachable from our hook, Otherwise we have a conditional/branching flow.
petgraph::algo::dijkstra(graph, func_cfg_ix, Some(node_cfg_ix), |_| 0)
.into_iter()
.any(|(f, _)| !petgraph::algo::has_path_connecting(graph, f, node_cfg_ix, None))
petgraph::algo::dijkstra(graph, func_cfg_ix, Some(node_cfg_ix), |e| match e.weight() {
EdgeType::NewFunction => 1,
EdgeType::Backedge | EdgeType::Normal => 0,
})
.into_iter()
.filter(|(_, val)| *val == 0)
.any(|(f, _)| !petgraph::algo::has_path_connecting(graph, f, node_cfg_ix, None))
}

#[inline(always)]
Expand Down Expand Up @@ -874,6 +878,16 @@ fn test() {
return <div>test</div>;
};
",
"
function useLabeledBlock() {
let x = () => {
if (some) {
noop();
}
};
useHook();
}
",
];

let fail = vec![
Expand Down

0 comments on commit c8f1f79

Please sign in to comment.