Skip to content

Commit f4d764e

Browse files
committed
fix: better throw control flow.
1 parent b3e0e1e commit f4d764e

File tree

5 files changed

+51
-4
lines changed

5 files changed

+51
-4
lines changed

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

+34-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ use oxc_ast::{
33
AstKind,
44
};
55
use oxc_macros::declare_oxc_lint;
6-
use oxc_semantic::{AstNodeId, AstNodes};
6+
use oxc_semantic::{
7+
algo, petgraph::visit::Control, AstNodeId, AstNodes, BasicBlockId, EdgeType, InstructionKind,
8+
};
79
use oxc_span::{Atom, CompactStr};
810
use oxc_syntax::operator::AssignmentOperator;
911

@@ -236,13 +238,43 @@ impl Rule for RulesOfHooks {
236238
return ctx.diagnostic(diagnostics::loop_hook(span, hook_name));
237239
}
238240

239-
if semantic.cfg().has_conditional_path(func_cfg_id, node_cfg_id) {
241+
if has_conditional_path_accept_throw(ctx, func_cfg_id, node_cfg_id) {
240242
#[allow(clippy::needless_return)]
241243
return ctx.diagnostic(diagnostics::conditional_hook(span, hook_name));
242244
}
243245
}
244246
}
245247

248+
fn has_conditional_path_accept_throw(
249+
ctx: &LintContext<'_>,
250+
from: BasicBlockId,
251+
to: BasicBlockId,
252+
) -> bool {
253+
let cfg = ctx.semantic().cfg();
254+
let graph = &cfg.graph;
255+
// All nodes should be able to reach the hook node, Otherwise we have a conditional/branching flow.
256+
algo::dijkstra(graph, from, Some(to), |e| match e.weight() {
257+
EdgeType::NewFunction => 1,
258+
EdgeType::Jump | EdgeType::Unreachable | EdgeType::Backedge | EdgeType::Normal => 0,
259+
})
260+
.into_iter()
261+
.filter(|(_, val)| *val == 0)
262+
.any(|(f, _)| {
263+
!cfg.is_reachabale_filtered(f, to, |it| {
264+
if cfg
265+
.basic_block(it)
266+
.instructions()
267+
.iter()
268+
.any(|i| matches!(i.kind, InstructionKind::Throw))
269+
{
270+
Control::Break(true)
271+
} else {
272+
Control::Continue
273+
}
274+
})
275+
})
276+
}
277+
246278
fn parent_func<'a>(nodes: &'a AstNodes<'a>, node: &AstNode) -> Option<&'a AstNode<'a>> {
247279
nodes.ancestors(node.id()).map(|id| nodes.get_node(id)).find(|it| it.kind().is_function_like())
248280
}

crates/oxc_semantic/src/builder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1114,7 +1114,7 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
11141114
// todo - append unreachable after throw statement
11151115

11161116
/* cfg */
1117-
self.cfg.push_throw(node_id);
1117+
self.cfg.append_throw(node_id);
11181118
/* cfg */
11191119

11201120
self.leave_node(kind);

crates/oxc_semantic/src/control_flow/builder/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,9 @@ impl<'a> ControlFlowGraphBuilder<'a> {
9696
self.push_instruction(InstructionKind::Return(kind), Some(node));
9797
}
9898

99-
pub fn push_throw(&mut self, node: AstNodeId) {
99+
pub fn append_throw(&mut self, node: AstNodeId) {
100100
self.push_instruction(InstructionKind::Throw, Some(node));
101+
self.append_unreachable();
101102
}
102103

103104
pub fn append_break(&mut self, node: AstNodeId, label: Option<&'a str>) {

crates/oxc_semantic/src/control_flow/mod.rs

+13
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,25 @@ impl ControlFlowGraph {
191191
}
192192

193193
pub fn is_reachabale(&self, from: BasicBlockId, to: BasicBlockId) -> bool {
194+
self.is_reachabale_filtered(from, to, |_| Control::Continue)
195+
}
196+
197+
pub fn is_reachabale_filtered<F: Fn(BasicBlockId) -> Control<bool>>(
198+
&self,
199+
from: BasicBlockId,
200+
to: BasicBlockId,
201+
filter: F,
202+
) -> bool {
194203
if from == to {
195204
return true;
196205
}
197206
let graph = &self.graph;
198207
depth_first_search(&self.graph, Some(from), |event| match event {
199208
DfsEvent::TreeEdge(a, b) => {
209+
let filter_result = filter(a);
210+
if !matches!(filter_result, Control::Continue) {
211+
return filter_result;
212+
}
200213
let unreachable = graph.edges_connecting(a, b).all(|edge| {
201214
matches!(edge.weight(), EdgeType::NewFunction | EdgeType::Unreachable)
202215
});

crates/oxc_semantic/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ mod symbol;
1616
use std::{rc::Rc, sync::Arc};
1717

1818
pub use petgraph;
19+
pub use petgraph::algo;
1920

2021
pub use builder::{SemanticBuilder, SemanticBuilderReturn};
2122
use class::ClassTable;

0 commit comments

Comments
 (0)