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 @@ -550,6 +550,18 @@ const tests = {
// TODO: this should error but doesn't.
// errors: [genericError('useState')],
},
{
code: normalizeIndent`
// Valid because the hook is outside of the loop
const Component = () => {
const [state, setState] = useState(0);
for (let i = 0; i < 10; i++) {
Comment on lines +557 to +558
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the hook is called in a valid way, outside of the loop.

console.log(i);
}
return <div></div>;
};
`,
},
],
invalid: [
{
Expand Down
20 changes: 17 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,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverts the change from #28714 as it was introducing false positives (see #31687).

);
for (const cyclicSegment of cyclicSegments) {
cyclic.add(cyclicSegment);
Expand Down Expand Up @@ -485,7 +495,10 @@ 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 +533,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.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specifically excluding hooks inside do/while calls because we're handling those cases separately in the above check.

) {
const message =
`React Hook "${getSource(hook)}" is called ` +
Expand Down
Loading