Skip to content

Commit 393541f

Browse files
committed
improvement(linter/react): use CFG for detecting cyclic subgraphs in rules-of-hooks.
1 parent 6cdd652 commit 393541f

File tree

1 file changed

+5
-24
lines changed

1 file changed

+5
-24
lines changed

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

+5-24
Original file line numberDiff line numberDiff line change
@@ -242,12 +242,12 @@ impl Rule for RulesOfHooks {
242242
}
243243

244244
// Is this node cyclic?
245-
if self.is_cyclic(ctx, node, parent_func) {
245+
if semantic.cfg().is_cyclic(node_cfg_id) {
246246
return ctx.diagnostic(diagnostics::loop_hook(span, hook_name));
247247
}
248248

249-
if self.is_conditional(ctx, func_cfg_id, node_cfg_id)
250-
|| self.breaks_early(ctx, func_cfg_id, node_cfg_id)
249+
if Self::is_conditional(ctx, func_cfg_id, node_cfg_id)
250+
|| Self::breaks_early(ctx, func_cfg_id, node_cfg_id)
251251
{
252252
#[allow(clippy::needless_return)]
253253
return ctx.diagnostic(diagnostics::conditional_hook(span, hook_name));
@@ -257,26 +257,8 @@ impl Rule for RulesOfHooks {
257257

258258
// TODO: all `dijkstra` algorithms can be merged together for better performance.
259259
impl RulesOfHooks {
260-
#![allow(clippy::unused_self, clippy::inline_always)]
261-
#[inline(always)]
262-
fn is_cyclic(&self, ctx: &LintContext, node: &AstNode, func: &AstNode) -> bool {
263-
// TODO: use cfg instead
264-
ctx.nodes().ancestors(node.id()).take_while(|id| *id != func.id()).any(|id| {
265-
let maybe_loop = ctx.nodes().kind(id);
266-
matches! {
267-
maybe_loop,
268-
| AstKind::DoWhileStatement(_)
269-
| AstKind::ForInStatement(_)
270-
| AstKind::ForOfStatement(_)
271-
| AstKind::ForStatement(_)
272-
| AstKind::WhileStatement(_)
273-
}
274-
})
275-
}
276-
277-
#[inline(always)]
260+
#[inline]
278261
fn is_conditional(
279-
&self,
280262
ctx: &LintContext,
281263
func_cfg_id: BasicBlockId,
282264
node_cfg_id: BasicBlockId,
@@ -293,9 +275,8 @@ impl RulesOfHooks {
293275
.any(|(f, _)| !cfg.is_reachabale(f, node_cfg_id))
294276
}
295277

296-
#[inline(always)]
278+
#[inline]
297279
fn breaks_early(
298-
&self,
299280
ctx: &LintContext,
300281
func_cfg_id: BasicBlockId,
301282
node_cfg_id: BasicBlockId,

0 commit comments

Comments
 (0)