Skip to content

Commit

Permalink
Don't lint against Hooks after conditional throw (#14040)
Browse files Browse the repository at this point in the history
Seems like this should be OK. Fixes #14038.

Now when tracking paths, we completely ignore segments that end in a throw.
  • Loading branch information
sophiebits authored Oct 31, 2018
1 parent 169f935 commit 3db8b80
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,15 @@ eslintTester.run('react-hooks', ReactHooksESLintRule, {
useState();
}
`,
`
// Valid because exceptions abort rendering
function RegressionTest() {
if (page == null) {
throw new Error('oh no!');
}
useState();
}
`,
],
invalid: [
{
Expand Down
8 changes: 6 additions & 2 deletions packages/eslint-plugin-react-hooks/src/RulesOfHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ export default {

// Compute `paths` and cache it. Guarding against cycles.
cache.set(segment.id, null);
if (segment.prevSegments.length === 0) {
if (codePath.thrownSegments.includes(segment)) {
paths = 0;
} else if (segment.prevSegments.length === 0) {
paths = 1;
} else {
paths = 0;
Expand Down Expand Up @@ -199,7 +201,9 @@ export default {

// Compute `paths` and cache it. Guarding against cycles.
cache.set(segment.id, null);
if (segment.nextSegments.length === 0) {
if (codePath.thrownSegments.includes(segment)) {
paths = 0;
} else if (segment.nextSegments.length === 0) {
paths = 1;
} else {
paths = 0;
Expand Down

0 comments on commit 3db8b80

Please sign in to comment.