Don't lint against Hooks after conditional throw (#14040)
Seems like this should be OK. Fixes #14038. Now when tracking paths, we completely ignore segments that end in a throw.
Sophie Alpert committed
Oct 30, 2018 at 17:13 UTC
3db8b80e1501b161b213b0b5405590e4325a0414
2 files changed
+15
-2
packages/eslint-plugin-react-hooks/__tests__/ESLintRulesOfHooks-test.js
+9
@@ -261,6 +261,15 @@ eslintTester.run('react-hooks', ReactHooksESLintRule, {
261
useState();
262
}
263
`,
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
+ `,
273
],
274
invalid: [
275
{
packages/eslint-plugin-react-hooks/src/RulesOfHooks.js
+6
-2
@@ -139,7 +139,9 @@ export default {
139
140
// Compute `paths` and cache it. Guarding against cycles.
141
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) {
145
paths = 1;
146
} else {
147
paths = 0;
@@ -199,7 +201,9 @@ export default {
201
202
// Compute `paths` and cache it. Guarding against cycles.
203
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) {
207
paths = 1;
208
} else {
209
paths = 0;