Skip to content

Commit

Permalink
Handle DoWhileStatement separately
Browse files Browse the repository at this point in the history
  • Loading branch information
tyxla committed Dec 10, 2024
1 parent 4cd7c76 commit 9932384
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions packages/eslint-plugin-react-hooks/src/RulesOfHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ function isInsideComponentOrHook(node) {
return false;
}

function isInsideDoWhileLoop(node) {
while (node) {
if ( node.type === 'DoWhileStatement' ) {
return true;
}
node = node.parent;
}
return false;
}

function isUseEffectEventIdentifier(node) {
if (__EXPERIMENTAL__) {
return node.type === 'Identifier' && node.name === 'useEffectEvent';
Expand Down Expand Up @@ -295,7 +305,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 Expand Up @@ -485,7 +495,7 @@ export default {
for (const hook of reactHooks) {
// Report an error if a hook may be called more then once.
// `use(...)` can be called in loops.
if (cycled && !isUseIdentifier(hook)) {
if ( (cycled || isInsideDoWhileLoop(hook)) && !isUseIdentifier(hook)) {
context.report({
node: hook,
message:
Expand Down Expand Up @@ -520,7 +530,8 @@ export default {
if (
!cycled &&
pathsFromStartToEnd !== allPathsFromStartToEnd &&
!isUseIdentifier(hook) // `use(...)` can be called conditionally.
!isUseIdentifier(hook) && // `use(...)` can be called conditionally.
!isInsideDoWhileLoop(hook) // wrapping do/while loops are checked separately.
) {
const message =
`React Hook "${getSource(hook)}" is called ` +
Expand Down

0 comments on commit 9932384

Please sign in to comment.