Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[eslint-plugin-react-hooks] Fix cyclic caching for loops containing a… #16853

Merged
merged 4 commits into from
Feb 25, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -347,6 +347,18 @@ const tests = {
useHook();
}
`,
`
// Valid because the neither the condition nor the loop affect the hook call.
function App(props) {
const someObject = {propA: true};
for (const propName in someObject) {
if (propName === true) {
} else {
}
}
const [myState, setMyState] = useState(null);
}
`,
],
invalid: [
{
Expand Down Expand Up @@ -556,14 +568,7 @@ const tests = {
}
}
`,
errors: [
loopError('useHook1'),

// NOTE: Small imprecision in error reporting due to caching means we
// have a conditional error here instead of a loop error. However,
// we will always get an error so this is acceptable.
conditionalError('useHook2', true),
],
errors: [loopError('useHook1'), loopError('useHook2', true)],
},
{
code: `
Expand Down
70 changes: 37 additions & 33 deletions packages/eslint-plugin-react-hooks/src/RulesOfHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,39 +114,41 @@ export default {
* Populates `cyclic` with cyclic segments.
*/

function countPathsFromStart(segment) {
function countPathsFromStart(segment, pathHistory) {
const {cache} = countPathsFromStart;
let paths = cache.get(segment.id);

// If `paths` is null then we've found a cycle! Add it to `cyclic` and
// any other segments which are a part of this cycle.
if (paths === null) {
if (cyclic.has(segment.id)) {
return 0;
} else {
cyclic.add(segment.id);
for (const prevSegment of segment.prevSegments) {
countPathsFromStart(prevSegment);
}
return 0;
let pathList = new Set(pathHistory);
M-Izadmehr marked this conversation as resolved.
Show resolved Hide resolved

// If `pathList` includes the current segment then we've found a cycle!
// We need to fill `cyclic` with all segments inside cycle
if (pathList.has(segment.id)) {
const pathArray = [...pathList];
const cyclicSegments = pathArray.slice(
pathArray.indexOf(segment.id) + 1,
);
for (const cyclicSegment of cyclicSegments) {
cyclic.add(cyclicSegment);
}

return 0;
}

// add the current segment to pathList
pathList.add(segment.id);

// We have a cached `paths`. Return it.
if (paths !== undefined) {
return paths;
}

// Compute `paths` and cache it. Guarding against cycles.
cache.set(segment.id, null);
if (codePath.thrownSegments.includes(segment)) {
paths = 0;
} else if (segment.prevSegments.length === 0) {
paths = 1;
} else {
paths = 0;
for (const prevSegment of segment.prevSegments) {
paths += countPathsFromStart(prevSegment);
paths += countPathsFromStart(prevSegment, pathList);
}
}

Expand Down Expand Up @@ -183,43 +185,45 @@ export default {
* Populates `cyclic` with cyclic segments.
*/

function countPathsToEnd(segment) {
function countPathsToEnd(segment, pathHistory) {
const {cache} = countPathsToEnd;
let paths = cache.get(segment.id);

// If `paths` is null then we've found a cycle! Add it to `cyclic` and
// any other segments which are a part of this cycle.
if (paths === null) {
if (cyclic.has(segment.id)) {
return 0;
} else {
cyclic.add(segment.id);
for (const nextSegment of segment.nextSegments) {
countPathsToEnd(nextSegment);
}
return 0;
let pathList = new Set(pathHistory);

// If `pathList` includes the current segment then we've found a cycle!
// We need to fill `cyclic` with all segments inside cycle
if (pathList.has(segment.id)) {
const pathArray = Array.from(pathList);
const cyclicSegments = pathArray.slice(
pathArray.indexOf(segment.id) + 1,
);
for (const cyclicSegment of cyclicSegments) {
cyclic.add(cyclicSegment);
}

return 0;
}

// add the current segment to pathList
pathList.add(segment.id);

// We have a cached `paths`. Return it.
if (paths !== undefined) {
return paths;
}

// Compute `paths` and cache it. Guarding against cycles.
cache.set(segment.id, null);
if (codePath.thrownSegments.includes(segment)) {
paths = 0;
} else if (segment.nextSegments.length === 0) {
paths = 1;
} else {
paths = 0;
for (const nextSegment of segment.nextSegments) {
paths += countPathsToEnd(nextSegment);
paths += countPathsToEnd(nextSegment, pathList);
}
}
cache.set(segment.id, paths);

cache.set(segment.id, paths);
Copy link

Choose a reason for hiding this comment

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

I think unnecessary diff in here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am not sure why, but I am not sure why it considers this line as changed:)

return paths;
}

Expand Down