Skip to content

Commit 1541032

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

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
@@ -237,12 +237,12 @@ impl Rule for RulesOfHooks {
237237
}
238238

239239
// Is this node cyclic?
240-
if self.is_cyclic(ctx, node, parent_func) {
240+
if semantic.cfg().is_cyclic(node_cfg_id) {
241241
return ctx.diagnostic(diagnostics::loop_hook(span, hook_name));
242242
}
243243

244-
if self.is_conditional(ctx, func_cfg_id, node_cfg_id)
245-
|| self.breaks_early(ctx, func_cfg_id, node_cfg_id)
244+
if Self::is_conditional(ctx, func_cfg_id, node_cfg_id)
245+
|| Self::breaks_early(ctx, func_cfg_id, node_cfg_id)
246246
{
247247
#[allow(clippy::needless_return)]
248248
return ctx.diagnostic(diagnostics::conditional_hook(span, hook_name));
@@ -252,26 +252,8 @@ impl Rule for RulesOfHooks {
252252

253253
// TODO: all `dijkstra` algorithms can be merged together for better performance.
254254
impl RulesOfHooks {
255-
#![allow(clippy::unused_self, clippy::inline_always)]
256-
#[inline(always)]
257-
fn is_cyclic(&self, ctx: &LintContext, node: &AstNode, func: &AstNode) -> bool {
258-
// TODO: use cfg instead
259-
ctx.nodes().ancestors(node.id()).take_while(|id| *id != func.id()).any(|id| {
260-
let maybe_loop = ctx.nodes().kind(id);
261-
matches! {
262-
maybe_loop,
263-
| AstKind::DoWhileStatement(_)
264-
| AstKind::ForInStatement(_)
265-
| AstKind::ForOfStatement(_)
266-
| AstKind::ForStatement(_)
267-
| AstKind::WhileStatement(_)
268-
}
269-
})
270-
}
271-
272-
#[inline(always)]
255+
#[inline]
273256
fn is_conditional(
274-
&self,
275257
ctx: &LintContext,
276258
func_cfg_id: BasicBlockId,
277259
node_cfg_id: BasicBlockId,
@@ -288,9 +270,8 @@ impl RulesOfHooks {
288270
.any(|(f, _)| !cfg.is_reachabale(f, node_cfg_id))
289271
}
290272

291-
#[inline(always)]
273+
#[inline]
292274
fn breaks_early(
293-
&self,
294275
ctx: &LintContext,
295276
func_cfg_id: BasicBlockId,
296277
node_cfg_id: BasicBlockId,

0 commit comments

Comments
 (0)