Skip to content

Commit

Permalink
Don't lint against Hooks after conditional throw
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. In https://eslint.org/docs/developer-guide/code-path-analysis I don't see a way to detect throws other than manually tracking them, so that's what I've done.
  • Loading branch information
sophiebits committed Oct 31, 2018
1 parent 169f935 commit fcd4703
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
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
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 fcd4703

Please sign in to comment.