From 9b969a18a151ea801ecda0ea954a8d33124029ee Mon Sep 17 00:00:00 2001 From: Thomas Date: Tue, 21 Apr 2026 21:13:03 +0200 Subject: [PATCH] [eslint-plugin-react-hooks] Include cycle-entry segment in cyclic set --- .../__tests__/ESLintRulesOfHooks-test.js | 24 +++++++++++++++++++ .../src/rules/RulesOfHooks.ts | 4 ++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin-react-hooks/__tests__/ESLintRulesOfHooks-test.js b/packages/eslint-plugin-react-hooks/__tests__/ESLintRulesOfHooks-test.js index 3d60a36824d2..82bf9ef2059d 100644 --- a/packages/eslint-plugin-react-hooks/__tests__/ESLintRulesOfHooks-test.js +++ b/packages/eslint-plugin-react-hooks/__tests__/ESLintRulesOfHooks-test.js @@ -1164,6 +1164,30 @@ const allTests = { `, errors: [loopError('useHookInsideLoop')], }, + { + code: normalizeIndent` + // Invalid because it's dangerous and might not warn otherwise. + // This *must* be invalid. + function ComponentWithHookInLoopCondition() { + while (useHookInsideLoop()) { + foo(); + } + } + `, + errors: [loopError('useHookInsideLoop')], + }, + { + code: normalizeIndent` + // Invalid because it's dangerous and might not warn otherwise. + // This *must* be invalid. + function ComponentWithHookInForCondition() { + for (; useHookInsideLoop(); ) { + foo(); + } + } + `, + errors: [loopError('useHookInsideLoop')], + }, { code: normalizeIndent` // Invalid because it's dangerous and might not warn otherwise. diff --git a/packages/eslint-plugin-react-hooks/src/rules/RulesOfHooks.ts b/packages/eslint-plugin-react-hooks/src/rules/RulesOfHooks.ts index ca82c99e2f55..aef9089002d9 100644 --- a/packages/eslint-plugin-react-hooks/src/rules/RulesOfHooks.ts +++ b/packages/eslint-plugin-react-hooks/src/rules/RulesOfHooks.ts @@ -348,7 +348,7 @@ const rule = { if (pathList.has(segment.id)) { const pathArray = [...pathList]; const cyclicSegments = pathArray.slice( - pathArray.indexOf(segment.id) + 1, + pathArray.indexOf(segment.id), ); for (const cyclicSegment of cyclicSegments) { cyclic.add(cyclicSegment); @@ -422,7 +422,7 @@ const rule = { if (pathList.has(segment.id)) { const pathArray = Array.from(pathList); const cyclicSegments = pathArray.slice( - pathArray.indexOf(segment.id) + 1, + pathArray.indexOf(segment.id), ); for (const cyclicSegment of cyclicSegments) { cyclic.add(cyclicSegment);