Skip to content

Commit 20a96e7

Browse files
sophiebitsjetoneza
authored andcommitted
Don't lint against Hooks after conditional throw (facebook#14040)
Seems like this should be OK. Fixes facebook#14038. Now when tracking paths, we completely ignore segments that end in a throw.
1 parent 2aa0991 commit 20a96e7

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

packages/eslint-plugin-react-hooks/__tests__/ESLintRulesOfHooks-test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,15 @@ eslintTester.run('react-hooks', ReactHooksESLintRule, {
261261
useState();
262262
}
263263
`,
264+
`
265+
// Valid because exceptions abort rendering
266+
function RegressionTest() {
267+
if (page == null) {
268+
throw new Error('oh no!');
269+
}
270+
useState();
271+
}
272+
`,
264273
],
265274
invalid: [
266275
{

packages/eslint-plugin-react-hooks/src/RulesOfHooks.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,9 @@ export default {
139139

140140
// Compute `paths` and cache it. Guarding against cycles.
141141
cache.set(segment.id, null);
142-
if (segment.prevSegments.length === 0) {
142+
if (codePath.thrownSegments.includes(segment)) {
143+
paths = 0;
144+
} else if (segment.prevSegments.length === 0) {
143145
paths = 1;
144146
} else {
145147
paths = 0;
@@ -199,7 +201,9 @@ export default {
199201

200202
// Compute `paths` and cache it. Guarding against cycles.
201203
cache.set(segment.id, null);
202-
if (segment.nextSegments.length === 0) {
204+
if (codePath.thrownSegments.includes(segment)) {
205+
paths = 0;
206+
} else if (segment.nextSegments.length === 0) {
203207
paths = 1;
204208
} else {
205209
paths = 0;

0 commit comments

Comments
 (0)