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);