Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,30 @@ const tests = {
`,
errors: [loopError('useHookInsideLoop')],
},
{
code: normalizeIndent`
// Invalid because it's dangerous and might not warn otherwise.
// This *must* be invalid.
function ComponentWithHookInsideLoop() {
do {
useHookInsideLoop();
} while (cond);
}
`,
errors: [loopError('useHookInsideLoop')],
},
{
code: normalizeIndent`
// Invalid because it's dangerous and might not warn otherwise.
// This *must* be invalid.
function ComponentWithHookInsideLoop() {
do {
foo();
} while (useHookInsideLoop());
}
`,
errors: [loopError('useHookInsideLoop')],
},
{
code: normalizeIndent`
// Invalid because it's dangerous and might not warn otherwise.
Expand Down Expand Up @@ -853,6 +877,45 @@ const tests = {
`,
errors: [loopError('useHook1'), loopError('useHook2', true)],
},
{
code: normalizeIndent`
// Invalid because it's dangerous and might not warn otherwise.
// This *must* be invalid.
function useHookInLoops() {
do {
useHook1();
if (a) return;
useHook2();
} while (b);

do {
useHook3();
if (c) return;
useHook4();
} while (d)
}
`,
errors: [
loopError('useHook1'),
loopError('useHook2'),
loopError('useHook3'),
loopError('useHook4'),
],
},
{
code: normalizeIndent`
// Invalid because it's dangerous and might not warn otherwise.
// This *must* be invalid.
function useHookInLoops() {
do {
useHook1();
if (a) continue;
useHook2();
} while (b);
}
`,
errors: [loopError('useHook1'), loopError('useHook2', true)],
},
{
code: normalizeIndent`
// Invalid because it's dangerous and might not warn otherwise.
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin-react-hooks/src/RulesOfHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ export default {
if (pathList.has(segment.id)) {
const pathArray = Array.from(pathList);
const cyclicSegments = pathArray.slice(
pathArray.indexOf(segment.id) + 1,
pathArray.indexOf(segment.id) - 1,
);
for (const cyclicSegment of cyclicSegments) {
cyclic.add(cyclicSegment);
Expand Down
Loading