mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +00:00
fix(linter/react): rules_of_hooks resolve false positives with conditional hooks. (#3299)
related to #3071
This commit is contained in:
parent
7f9d8b71ee
commit
c8f1f79d27
1 changed files with 17 additions and 3 deletions
|
|
@ -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)]
|
||||
|
|
@ -874,6 +878,16 @@ fn test() {
|
|||
return <div>test</div>;
|
||||
};
|
||||
",
|
||||
"
|
||||
function useLabeledBlock() {
|
||||
let x = () => {
|
||||
if (some) {
|
||||
noop();
|
||||
}
|
||||
};
|
||||
useHook();
|
||||
}
|
||||
",
|
||||
];
|
||||
|
||||
let fail = vec![
|
||||
|
|
|
|||
Loading…
Reference in a new issue