fix(linter/react): rules_of_hooks resolve false positives with conditional hooks. (#3299)

related to #3071
This commit is contained in:
rzvxa 2024-05-16 16:16:52 +00:00
parent 7f9d8b71ee
commit c8f1f79d27

View file

@ -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![