eslint-plugin-react-hooks 2.4.0-alpha.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -34,6 +34,17 @@ Then add it to your ESLint configuration:
34
34
  }
35
35
  ```
36
36
 
37
+ Or use the recommended config:
38
+
39
+ ```js
40
+ {
41
+ "extends": [
42
+ // ...
43
+ "plugin:react-hooks/recommended"
44
+ ]
45
+ }
46
+ ```
47
+
37
48
  ## Valid and Invalid Examples
38
49
 
39
50
  Please refer to the [Rules of Hooks](https://reactjs.org/docs/hooks-rules.html) documentation and the [Hooks FAQ](https://reactjs.org/docs/hooks-faq.html#what-exactly-do-the-lint-rules-enforce) to learn more about this rule.
@@ -16,7 +16,6 @@ if (process.env.NODE_ENV !== "production") {
16
16
  'use strict';
17
17
 
18
18
  /* eslint-disable no-for-of-loops/no-for-of-loops */
19
-
20
19
  /**
21
20
  * Catch all identifiers that begin with "use" followed by an uppercase Latin
22
21
  * character to exclude identifiers like "user".
@@ -151,51 +150,48 @@ var RuleOfHooks = {
151
150
  * Populates `cyclic` with cyclic segments.
152
151
  */
153
152
 
154
- function countPathsFromStart(segment) {
153
+ function countPathsFromStart(segment, pathHistory) {
155
154
  var cache = countPathsFromStart.cache;
156
- var paths = cache.get(segment.id); // If `paths` is null then we've found a cycle! Add it to `cyclic` and
157
- // any other segments which are a part of this cycle.
155
+ var paths = cache.get(segment.id);
156
+ var pathList = new Set(pathHistory); // If `pathList` includes the current segment then we've found a cycle!
157
+ // We need to fill `cyclic` with all segments inside cycle
158
158
 
159
- if (paths === null) {
160
- if (cyclic.has(segment.id)) {
161
- return 0;
162
- } else {
163
- cyclic.add(segment.id);
164
- var _iteratorNormalCompletion = true;
165
- var _didIteratorError = false;
166
- var _iteratorError = undefined;
159
+ if (pathList.has(segment.id)) {
160
+ var pathArray = [].concat(pathList);
161
+ var cyclicSegments = pathArray.slice(pathArray.indexOf(segment.id) + 1);
162
+ var _iteratorNormalCompletion = true;
163
+ var _didIteratorError = false;
164
+ var _iteratorError = undefined;
167
165
 
166
+ try {
167
+ for (var _iterator = cyclicSegments[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
168
+ var cyclicSegment = _step.value;
169
+ cyclic.add(cyclicSegment);
170
+ }
171
+ } catch (err) {
172
+ _didIteratorError = true;
173
+ _iteratorError = err;
174
+ } finally {
168
175
  try {
169
- for (var _iterator = segment.prevSegments[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
170
- var prevSegment = _step.value;
171
- countPathsFromStart(prevSegment);
176
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
177
+ _iterator.return();
172
178
  }
173
- } catch (err) {
174
- _didIteratorError = true;
175
- _iteratorError = err;
176
179
  } finally {
177
- try {
178
- if (!_iteratorNormalCompletion && _iterator.return != null) {
179
- _iterator.return();
180
- }
181
- } finally {
182
- if (_didIteratorError) {
183
- throw _iteratorError;
184
- }
180
+ if (_didIteratorError) {
181
+ throw _iteratorError;
185
182
  }
186
183
  }
187
-
188
- return 0;
189
184
  }
190
- } // We have a cached `paths`. Return it.
191
185
 
186
+ return 0;
187
+ } // add the current segment to pathList
192
188
 
193
- if (paths !== undefined) {
194
- return paths;
195
- } // Compute `paths` and cache it. Guarding against cycles.
196
189
 
190
+ pathList.add(segment.id); // We have a cached `paths`. Return it.
197
191
 
198
- cache.set(segment.id, null);
192
+ if (paths !== undefined) {
193
+ return paths;
194
+ }
199
195
 
200
196
  if (codePath.thrownSegments.includes(segment)) {
201
197
  paths = 0;
@@ -209,8 +205,8 @@ var RuleOfHooks = {
209
205
 
210
206
  try {
211
207
  for (var _iterator2 = segment.prevSegments[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
212
- var _prevSegment = _step2.value;
213
- paths += countPathsFromStart(_prevSegment);
208
+ var prevSegment = _step2.value;
209
+ paths += countPathsFromStart(prevSegment, pathList);
214
210
  }
215
211
  } catch (err) {
216
212
  _didIteratorError2 = true;
@@ -261,51 +257,48 @@ var RuleOfHooks = {
261
257
  */
262
258
 
263
259
 
264
- function countPathsToEnd(segment) {
260
+ function countPathsToEnd(segment, pathHistory) {
265
261
  var cache = countPathsToEnd.cache;
266
- var paths = cache.get(segment.id); // If `paths` is null then we've found a cycle! Add it to `cyclic` and
267
- // any other segments which are a part of this cycle.
262
+ var paths = cache.get(segment.id);
263
+ var pathList = new Set(pathHistory); // If `pathList` includes the current segment then we've found a cycle!
264
+ // We need to fill `cyclic` with all segments inside cycle
268
265
 
269
- if (paths === null) {
270
- if (cyclic.has(segment.id)) {
271
- return 0;
272
- } else {
273
- cyclic.add(segment.id);
274
- var _iteratorNormalCompletion3 = true;
275
- var _didIteratorError3 = false;
276
- var _iteratorError3 = undefined;
266
+ if (pathList.has(segment.id)) {
267
+ var pathArray = Array.from(pathList);
268
+ var cyclicSegments = pathArray.slice(pathArray.indexOf(segment.id) + 1);
269
+ var _iteratorNormalCompletion3 = true;
270
+ var _didIteratorError3 = false;
271
+ var _iteratorError3 = undefined;
277
272
 
273
+ try {
274
+ for (var _iterator3 = cyclicSegments[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
275
+ var cyclicSegment = _step3.value;
276
+ cyclic.add(cyclicSegment);
277
+ }
278
+ } catch (err) {
279
+ _didIteratorError3 = true;
280
+ _iteratorError3 = err;
281
+ } finally {
278
282
  try {
279
- for (var _iterator3 = segment.nextSegments[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
280
- var nextSegment = _step3.value;
281
- countPathsToEnd(nextSegment);
283
+ if (!_iteratorNormalCompletion3 && _iterator3.return != null) {
284
+ _iterator3.return();
282
285
  }
283
- } catch (err) {
284
- _didIteratorError3 = true;
285
- _iteratorError3 = err;
286
286
  } finally {
287
- try {
288
- if (!_iteratorNormalCompletion3 && _iterator3.return != null) {
289
- _iterator3.return();
290
- }
291
- } finally {
292
- if (_didIteratorError3) {
293
- throw _iteratorError3;
294
- }
287
+ if (_didIteratorError3) {
288
+ throw _iteratorError3;
295
289
  }
296
290
  }
297
-
298
- return 0;
299
291
  }
300
- } // We have a cached `paths`. Return it.
301
292
 
293
+ return 0;
294
+ } // add the current segment to pathList
302
295
 
303
- if (paths !== undefined) {
304
- return paths;
305
- } // Compute `paths` and cache it. Guarding against cycles.
306
296
 
297
+ pathList.add(segment.id); // We have a cached `paths`. Return it.
307
298
 
308
- cache.set(segment.id, null);
299
+ if (paths !== undefined) {
300
+ return paths;
301
+ }
309
302
 
310
303
  if (codePath.thrownSegments.includes(segment)) {
311
304
  paths = 0;
@@ -319,8 +312,8 @@ var RuleOfHooks = {
319
312
 
320
313
  try {
321
314
  for (var _iterator4 = segment.nextSegments[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
322
- var _nextSegment = _step4.value;
323
- paths += countPathsToEnd(_nextSegment);
315
+ var nextSegment = _step4.value;
316
+ paths += countPathsToEnd(nextSegment, pathList);
324
317
  }
325
318
  } catch (err) {
326
319
  _didIteratorError4 = true;
@@ -570,26 +563,29 @@ var RuleOfHooks = {
570
563
  message: message
571
564
  });
572
565
  }
573
- } else if (codePathNode.parent && (codePathNode.parent.type === 'MethodDefinition' || codePathNode.parent.type === 'ClassProperty') && codePathNode.parent.value === codePathNode) {// Ignore class methods for now because they produce too many
574
- // false positives due to feature flag checks. We're less
575
- // sensitive to them in classes because hooks would produce
576
- // runtime errors in classes anyway, and because a use*()
577
- // call in a class, if it works, is unambiguously *not* a hook.
566
+ } else if (codePathNode.parent && (codePathNode.parent.type === 'MethodDefinition' || codePathNode.parent.type === 'ClassProperty') && codePathNode.parent.value === codePathNode) {
567
+ // Custom message for hooks inside a class
568
+ var _message = "React Hook \"" + context.getSource(hook) + "\" cannot be called " + 'in a class component. React Hooks must be called in a ' + 'React function component or a custom React Hook function.';
569
+
570
+ context.report({
571
+ node: hook,
572
+ message: _message
573
+ });
578
574
  } else if (codePathFunctionName) {
579
575
  // Custom message if we found an invalid function name.
580
- var _message = "React Hook \"" + context.getSource(hook) + "\" is called in " + ("function \"" + context.getSource(codePathFunctionName) + "\" ") + 'that is neither a React function component nor a custom ' + 'React Hook function.';
576
+ var _message2 = "React Hook \"" + context.getSource(hook) + "\" is called in " + ("function \"" + context.getSource(codePathFunctionName) + "\" ") + 'that is neither a React function component nor a custom ' + 'React Hook function.';
581
577
 
582
578
  context.report({
583
579
  node: hook,
584
- message: _message
580
+ message: _message2
585
581
  });
586
582
  } else if (codePathNode.type === 'Program') {
587
583
  // These are dangerous if you have inline requires enabled.
588
- var _message2 = "React Hook \"" + context.getSource(hook) + "\" cannot be called " + 'at the top level. React Hooks must be called in a ' + 'React function component or a custom React Hook function.';
584
+ var _message3 = "React Hook \"" + context.getSource(hook) + "\" cannot be called " + 'at the top level. React Hooks must be called in a ' + 'React function component or a custom React Hook function.';
589
585
 
590
586
  context.report({
591
587
  node: hook,
592
- message: _message2
588
+ message: _message3
593
589
  });
594
590
  } else {
595
591
  // Assume in all other cases the user called a hook in some
@@ -598,11 +594,11 @@ var RuleOfHooks = {
598
594
  // enough in the common case that the incorrect message in
599
595
  // uncommon cases doesn't matter.
600
596
  if (isSomewhereInsideComponentOrHook) {
601
- var _message3 = "React Hook \"" + context.getSource(hook) + "\" cannot be called " + 'inside a callback. React Hooks must be called in a ' + 'React function component or a custom React Hook function.';
597
+ var _message4 = "React Hook \"" + context.getSource(hook) + "\" cannot be called " + 'inside a callback. React Hooks must be called in a ' + 'React function component or a custom React Hook function.';
602
598
 
603
599
  context.report({
604
600
  node: hook,
605
- message: _message3
601
+ message: _message4
606
602
  });
607
603
  }
608
604
  }
@@ -717,7 +713,6 @@ function last(array) {
717
713
 
718
714
  /* eslint-disable no-for-of-loops/no-for-of-loops */
719
715
 
720
-
721
716
  var ExhaustiveDeps = {
722
717
  meta: {
723
718
  schema: [{
@@ -2146,17 +2141,21 @@ function isAncestorNodeOf(a, b) {
2146
2141
  return a.range[0] <= b.range[0] && a.range[1] >= b.range[1];
2147
2142
  }
2148
2143
 
2144
+ var configs = {
2145
+ recommended: {
2146
+ plugins: ['react-hooks'],
2147
+ rules: {
2148
+ 'react-hooks/rules-of-hooks': 'error',
2149
+ 'react-hooks/exhaustive-deps': 'warn'
2150
+ }
2151
+ }
2152
+ };
2149
2153
  var rules = {
2150
2154
  'rules-of-hooks': RuleOfHooks,
2151
2155
  'exhaustive-deps': ExhaustiveDeps
2152
2156
  };
2153
2157
 
2154
- var src = Object.freeze({
2155
- rules: rules
2156
- });
2157
-
2158
- var eslintPluginReactHooks = src;
2159
-
2160
- module.exports = eslintPluginReactHooks;
2158
+ exports.configs = configs;
2159
+ exports.rules = rules;
2161
2160
  })();
2162
2161
  }
@@ -7,46 +7,47 @@
7
7
  * LICENSE file in the root directory of this source tree.
8
8
  */
9
9
 
10
- 'use strict';function T(a){return"Identifier"===a.type?/^use[A-Z0-9].*$/.test(a.name):"MemberExpression"===a.type&&!a.computed&&T(a.property)?(a=a.object,"Identifier"===a.type&&"React"===a.name):!1}function ba(a){return"Identifier"===a.type?!/^[a-z]/.test(a.name):!1}function ca(a,e){return a.name===e||"MemberExpression"===a.type&&"React"===a.object.name&&a.property.name===e}function da(a){return!!(a.parent&&a.parent.callee&&ca(a.parent.callee,"forwardRef"))}
11
- function ea(a){return!!(a.parent&&a.parent.callee&&ca(a.parent.callee,"memo"))}function fa(a){for(;a;){var e=ha(a);if(e&&(ba(e)||T(e))||da(a)||ea(a))return!0;a=a.parent}return!1}
10
+ 'use strict';function U(a){return"Identifier"===a.type?/^use[A-Z0-9].*$/.test(a.name):"MemberExpression"===a.type&&!a.computed&&U(a.property)?(a=a.object,"Identifier"===a.type&&"React"===a.name):!1}function ba(a){return"Identifier"===a.type?!/^[a-z]/.test(a.name):!1}function ca(a,f){return a.name===f||"MemberExpression"===a.type&&"React"===a.object.name&&a.property.name===f}function da(a){return!!(a.parent&&a.parent.callee&&ca(a.parent.callee,"forwardRef"))}
11
+ function ea(a){return!!(a.parent&&a.parent.callee&&ca(a.parent.callee,"memo"))}function fa(a){for(;a;){var f=ha(a);if(f&&(ba(f)||U(f))||da(a)||ea(a))return!0;a=a.parent}return!1}
12
12
  function ha(a){if("FunctionDeclaration"===a.type||"FunctionExpression"===a.type&&a.id)return a.id;if("FunctionExpression"===a.type||"ArrowFunctionExpression"===a.type)return"VariableDeclarator"===a.parent.type&&a.parent.init===a?a.parent.id:"AssignmentExpression"===a.parent.type&&a.parent.right===a&&"="===a.parent.operator?a.parent.left:"Property"!==a.parent.type||a.parent.value!==a||a.parent.computed?"AssignmentPattern"!==a.parent.type||a.parent.right!==a||a.parent.computed?void 0:a.parent.left:
13
13
  a.parent.key}
14
- function V(a){function e(){return{isRequired:!1,isSatisfiedRecursively:!1,hasRequiredNodesBelow:!1,children:new Map}}function d(a,c){c=c.split(".");var m=!0,k=!1,d=void 0;try{for(var h=c[Symbol.iterator](),p;!(m=(p=h.next()).done);m=!0){var z=p.value,g=a.children.get(z);g||(g=e(),a.children.set(z,g));a=g}}catch(E){k=!0,d=E}finally{try{m||null==h.return||h.return()}finally{if(k)throw d;}}return a}function h(a,c,e){c=c.split(".");var m=!0,k=!1,d=void 0;try{for(var h=c[Symbol.iterator](),g;!(m=(g=h.next()).done);m=
15
- !0){var z=a.children.get(g.value);if(!z)break;e(z);a=z}}catch(E){k=!0,d=E}finally{try{m||null==h.return||h.return()}finally{if(k)throw d;}}}function c(a,e,k,h){a.children.forEach(function(a,m){var d=h(m);a.isSatisfiedRecursively?a.hasRequiredNodesBelow&&k.add(d):a.isRequired?e.add(d):c(a,e,k,function(a){return d+"."+a})})}var z=a.dependencies,l=a.declaredDependencies,v=a.optionalDependencies,k=a.externalDependencies,C=a.isEffect,F=e();z.forEach(function(a,c){d(F,c).isRequired=!0;h(F,c,function(a){a.hasRequiredNodesBelow=
16
- !0})});l.forEach(function(a){d(F,a.key).isSatisfiedRecursively=!0});v.forEach(function(a){d(F,a).isSatisfiedRecursively=!0});a=new Set;var g=new Set;c(F,a,g,function(a){return a});var u=[],x=new Set,A=new Set;l.forEach(function(a){a=a.key;g.has(a)?-1===u.indexOf(a)?u.push(a):A.add(a):!C||a.endsWith(".current")||k.has(a)?x.add(a):-1===u.indexOf(a)&&u.push(a)});a.forEach(function(a){u.push(a)});return{suggestedDependencies:u,unnecessaryDependencies:x,duplicateDependencies:A,missingDependencies:a}}
17
- function ia(a){var e=a.declaredDependenciesNode,d=a.componentScope,h=a.scope;return a.declaredDependencies.map(function(a){a=d.set.get(a.key);if(null==a)return null;var c=a.defs[0];return null==c?null:"Variable"===c.type&&"VariableDeclarator"===c.node.type&&null!=c.node.init&&("ArrowFunctionExpression"===c.node.init.type||"FunctionExpression"===c.node.init.type)||"FunctionName"===c.type&&"FunctionDeclaration"===c.node.type?a:null}).filter(Boolean).map(function(a){var c=a.defs[0];a:{for(var d=!1,v=
18
- 0;v<a.references.length;v++){var k=a.references[v];if(k.writeExpr)if(d){a=!0;break a}else{d=!0;continue}for(var C=k.from;C!==h&&null!=C;)C=C.upper;if(C!==h&&!ja(e,k.identifier)){a=!0;break a}}a=!1}return{fn:c,suggestUseCallback:a}})}function ka(a){return"MemberExpression"!==a.parent.type||a.parent.object!==a||"current"===a.parent.property.name||a.parent.computed||null!=a.parent.parent&&"CallExpression"===a.parent.parent.type&&a.parent.parent.callee===a.parent?a:ka(a.parent)}
19
- function X(a){if("Identifier"===a.type)return a.name;if("MemberExpression"!==a.type||a.computed)throw Error("Unsupported node type: "+a.type);var e=X(a.object);a=X(a.property);return e+"."+a}function ma(a){return"MemberExpression"!==a.type||"Identifier"!==a.object.type||"React"!==a.object.name||"Identifier"!==a.property.type||a.computed?a:a.property}
20
- function na(a,e){var d=ma(a);if("Identifier"!==d.type)return null;switch(d.name){case "useEffect":case "useLayoutEffect":case "useCallback":case "useMemo":return 0;case "useImperativeHandle":return 1;default:if(d===a&&e&&e.additionalHooks){try{var h=X(d)}catch(c){if(/Unsupported node type/.test(c.message))return 0;throw c;}return e.additionalHooks.test(h)?0:-1}return-1}}
21
- function Y(a,e){for(var d=[a],h=null;d.length;){h=d.shift();if("Identifier"===h.type&&h.name===e.name&&h.range[0]===e.range[0]&&h.range[1]===e.range[1])return h;if(ja(h,e)){a=0;for(var c=Object.entries(h);a<c.length;a++){var z=c[a],l=z[1];"parent"!==z[0]&&(oa(l)?(l.parent=h,d.push(l)):Array.isArray(l)&&l.forEach(function(a){oa(a)&&(a.parent=h,d.push(a))}))}}}return null}
22
- function pa(a){for(var e="",d=0;d<a.length;d++)e+=a[d],0===d&&2===a.length?e+=" and ":d===a.length-2&&2<a.length?e+=", and ":d<a.length-1&&(e+=", ");return e}function oa(a){return"object"===typeof a&&null!==a&&!Array.isArray(a)&&"string"===typeof a.type}function ja(a,e){return a.range[0]<=e.range[0]&&a.range[1]>=e.range[1]}
23
- module.exports={rules:{"rules-of-hooks":{create:function(a){var e=[],d=[];return{onCodePathSegmentStart:function(a){return d.push(a)},onCodePathSegmentEnd:function(){return d.pop()},onCodePathStart:function(){return e.push(new Map)},onCodePathEnd:function(d,c){function h(a){var c=h.cache,b=c.get(a.id);if(null===b){if(!C.has(a.id)){C.add(a.id);c=!0;b=!1;var n=void 0;try{for(var f=a.prevSegments[Symbol.iterator](),B;!(c=(B=f.next()).done);c=!0)h(B.value)}catch(L){b=!0,n=L}finally{try{c||null==f.return||
24
- f.return()}finally{if(b)throw n;}}}return 0}if(void 0!==b)return b;c.set(a.id,null);if(d.thrownSegments.includes(a))b=0;else if(0===a.prevSegments.length)b=1;else{b=0;f=!0;B=!1;var e=void 0;try{n=a.prevSegments[Symbol.iterator]();for(var k;!(f=(k=n.next()).done);f=!0)b+=h(k.value)}catch(L){B=!0,e=L}finally{try{f||null==n.return||n.return()}finally{if(B)throw e;}}}a.reachable&&0===b?c.delete(a.id):c.set(a.id,b);return b}function l(a){var c=l.cache,b=c.get(a.id);if(null===b){if(!C.has(a.id)){C.add(a.id);
25
- c=!0;b=!1;var n=void 0;try{for(var f=a.nextSegments[Symbol.iterator](),B;!(c=(B=f.next()).done);c=!0)l(B.value)}catch(L){b=!0,n=L}finally{try{c||null==f.return||f.return()}finally{if(b)throw n;}}}return 0}if(void 0!==b)return b;c.set(a.id,null);if(d.thrownSegments.includes(a))b=0;else if(0===a.nextSegments.length)b=1;else{b=0;f=!0;B=!1;var e=void 0;try{n=a.nextSegments[Symbol.iterator]();for(var k;!(f=(k=n.next()).done);f=!0)b+=l(k.value)}catch(L){B=!0,e=L}finally{try{f||null==n.return||n.return()}finally{if(B)throw e;
26
- }}}c.set(a.id,b);return b}function v(a){var c=v.cache,b=c.get(a.id);if(null===b)return Infinity;if(void 0!==b)return b;c.set(a.id,null);if(0===a.prevSegments.length)b=1;else{b=Infinity;var n=!0,f=!1,B=void 0;try{for(var e=a.prevSegments[Symbol.iterator](),d;!(n=(d=e.next()).done);n=!0){var k=v(d.value);k<b&&(b=k)}}catch(qa){f=!0,B=qa}finally{try{n||null==e.return||e.return()}finally{if(f)throw B;}}b+=1}c.set(a.id,b);return b}var k=e.pop();if(0!==k.size){var C=new Set;h.cache=new Map;l.cache=new Map;
27
- v.cache=new Map;var F=l(d.initialSegment),g=ha(c),u=fa(c),x=g?ba(g)||T(g):da(c)||ea(c),A=Infinity,m=!0,p=!1,I=void 0;try{for(var t=d.finalSegments[Symbol.iterator](),K;!(m=(K=t.next()).done);m=!0){var W=K.value;if(W.reachable){var S=v(W);S<A&&(A=S)}}}catch(G){p=!0,I=G}finally{try{m||null==t.return||t.return()}finally{if(p)throw I;}}m=!0;p=!1;I=void 0;try{for(var y=k[Symbol.iterator](),P;!(m=(P=y.next()).done);m=!0){var E=P.value,D=E[0],M=E[1];if(D.reachable){var J=0===D.nextSegments.length?A<=v(D):
28
- A<v(D),Q=h(D)*l(D),H=C.has(D.id);k=!0;t=!1;K=void 0;try{for(var w=M[Symbol.iterator](),N;!(k=(N=w.next()).done);k=!0){var r=N.value;H&&a.report({node:r,message:'React Hook "'+a.getSource(r)+'" may be executed more than once. Possibly because it is called in a loop. React Hooks must be called in the exact same order in every component render.'});if(x){if(!H&&Q!==F){var U='React Hook "'+a.getSource(r)+'" is called conditionally. React Hooks must be called in the exact same order in every component render.'+
29
- (J?" Did you accidentally call a React Hook after an early return?":"");a.report({node:r,message:U})}}else if(!c.parent||"MethodDefinition"!==c.parent.type&&"ClassProperty"!==c.parent.type||c.parent.value!==c)if(g){var O='React Hook "'+a.getSource(r)+'" is called in function "'+(a.getSource(g)+'" that is neither a React function component nor a custom React Hook function.');a.report({node:r,message:O})}else if("Program"===c.type){var R='React Hook "'+a.getSource(r)+'" cannot be called at the top level. React Hooks must be called in a React function component or a custom React Hook function.';
30
- a.report({node:r,message:R})}else if(u){var q='React Hook "'+a.getSource(r)+'" cannot be called inside a callback. React Hooks must be called in a React function component or a custom React Hook function.';a.report({node:r,message:q})}}}catch(G){t=!0,K=G}finally{try{k||null==w.return||w.return()}finally{if(t)throw K;}}}}}catch(G){p=!0,I=G}finally{try{m||null==y.return||y.return()}finally{if(p)throw I;}}}},CallExpression:function(a){if(T(a.callee)){var c=e[e.length-1],h=d[d.length-1],l=c.get(h);l||
31
- (l=[],c.set(h,l));l.push(a.callee)}}}}},"exhaustive-deps":{meta:{schema:[{type:"object",additionalProperties:!1,properties:{additionalHooks:{type:"string"}}}]},create:function(a){function e(a,c){return function(e){if(c.has(e))return c.get(e);var d=a(e);c.set(e,d);return d}}function d(d){function k(a){var b=!0,f=!1,c=void 0;try{for(var e=a.references[Symbol.iterator](),h;!(b=(h=e.next()).done);b=!0){var g=h.value;if(g.resolved&&I.has(g.resolved.scope)){var m=Y(d,g.identifier),l=ka(m),p=X(l),r;if(r=
32
- A&&"Identifier"===l.type&&"MemberExpression"===l.parent.type&&!l.parent.computed&&"Identifier"===l.parent.property.type&&"current"===l.parent.property.name){for(var q=g.from,u=!1;q.block!==d;)"function"===q.type&&(u=null!=q.block.parent&&"ReturnStatement"===q.block.parent.type),q=q.upper;r=u}r&&S.set(p,{reference:g,dependencyNode:l});var t=g.resolved.defs[0];if(null!=t&&(null==t.node||t.node.init!==d.parent)&&"TypeParameter"!==t.type)if(y.has(p))y.get(p).references.push(g);else{var w=g.resolved,z=
33
- K(w)||W(w);y.set(p,{isStatic:z,references:[g]})}}}}catch(aa){f=!0,c=aa}finally{try{b||null==e.return||e.return()}finally{if(f)throw c;}}b=!0;f=!1;c=void 0;try{for(var v=a.childScopes[Symbol.iterator](),x;!(b=(x=v.next()).done);b=!0)k(x.value)}catch(aa){f=!0,c=aa}finally{try{b||null==v.return||v.return()}finally{if(f)throw c;}}}function F(a,n,f,c){return 0===a.size?null:(1<a.size?"":n+" ")+f+" "+(1<a.size?"dependencies":"dependency")+": "+pa(Array.from(a).sort().map(function(a){return"'"+a+"'"}))+
34
- (". Either "+c+" "+(1<a.size?"them":"it")+" or remove the dependency array.")}if(("FunctionExpression"===d.type||"ArrowFunctionExpression"===d.type)&&"CallExpression"===d.parent.type){var g=na(d.parent.callee,h);if(d.parent.arguments[g]===d){var u=d.parent.callee,x=ma(u).name,A=/Effect($|[^a-z])/g.test(x),m=d.parent.arguments[g+1];if(m||A){A&&d.async&&a.report({node:d,message:"Effect callbacks are synchronous to prevent race conditions. Put the async function inside:\n\nuseEffect(() => {\n async function fetchData() {\n // You can await here\n const response = await MyAPI.getData(someId);\n // ...\n }\n fetchData();\n}, [someId]); // Or [] if effect doesn't need props or state\n\nLearn more about data fetching with Hooks: https://fb.me/react-hooks-data-fetching"});
35
- var p=a.getScope(),I=new Set,t=null;for(g=p.upper;g;){I.add(g);if("function"===g.type)break;g=g.upper}if(g){t=g;var K=e(function(a){if(!Array.isArray(a.defs))return!1;var b=a.defs[0];if(null==b||"VariableDeclarator"!==b.node.type)return!1;var f=b.node.init;if(null==f)return!1;var d=b.node.parent;if(null==d&&(Y(t.block,b.node.id),d=b.node.parent,null==d))return!1;if("const"===d.kind&&"Literal"===f.type&&("string"===typeof f.value||"number"===typeof f.value||null===f.value))return!0;if("CallExpression"!==
36
- f.type)return!1;f=f.callee;"MemberExpression"!==f.type||"React"!==f.object.name||null==f.property||f.computed||(f=f.property);if("Identifier"!==f.type)return!1;b=b.node.id;f=f.name;if("useRef"===f&&"Identifier"===b.type)return!0;if(("useState"===f||"useReducer"===f)&&"ArrayPattern"===b.type&&2===b.elements.length&&Array.isArray(a.identifiers)){if(b.elements[1]===a.identifiers[0]){if("useState"===f)for(a=a.references,f=0;f<a.length;f++)c.set(a[f].identifier,b.elements[0]);return!0}if(b.elements[0]===
37
- a.identifiers[0]&&"useState"===f)for(a=a.references,b=0;b<a.length;b++)z.add(a[b].identifier)}return!1},l),W=e(function(a){if(!Array.isArray(a.defs))return!1;a=a.defs[0];if(null==a||null==a.node||null==a.node.id)return!1;var b=a.node,f=t.childScopes;a=null;var c;for(c=0;c<f.length;c++){var d=f[c],e=d.block;if("FunctionDeclaration"===b.type&&e===b||"VariableDeclarator"===b.type&&e.parent===b){a=d;break}}if(null==a)return!1;for(c=0;c<a.through.length;c++)if(b=a.through[c],null!=b.resolved&&I.has(b.resolved.scope)&&
38
- !K(b.resolved))return!1;return!0},v),S=new Map,y=new Map;k(p);S.forEach(function(b,c){var f=b.dependencyNode;b=b.reference.resolved.references;for(var d=!1,e=0;e<b.length;e++){var n=b[e].identifier.parent;if(null!=n&&"MemberExpression"===n.type&&!n.computed&&"Identifier"===n.property.type&&"current"===n.property.name&&"AssignmentExpression"===n.parent.type&&n.parent.left===n){d=!0;break}}d||a.report({node:f.parent.property,message:"The ref value '"+c+".current' will likely have changed by the time this effect cleanup function runs. If this ref points to a node rendered by React, copy '"+
39
- (c+".current' to a variable inside the effect, and use that variable in the cleanup function.")})});var P=new Set,E=new Set;y.forEach(function(b,c){var f=b.references;b.isStatic&&E.add(c);f.forEach(function(b){b.writeExpr&&(b=b.writeExpr,P.has(c)||(P.add(c),a.report({node:b,message:"Assignments to the '"+c+"' variable from inside React Hook "+(a.getSource(u)+" will be lost after each render. To preserve the value over time, store it in a useRef Hook and keep the mutable value in the '.current' property. Otherwise, you can move this variable directly inside ")+
40
- (a.getSource(u)+".")})))})});if(!(0<P.size))if(m){var D=[],M=new Set;"ArrayExpression"!==m.type?a.report({node:m,message:"React Hook "+a.getSource(u)+" was passed a dependency list that is not an array literal. This means we can't statically verify whether you've passed the correct dependencies."}):m.elements.forEach(function(b){if(null!==b)if("SpreadElement"===b.type)a.report({node:b,message:"React Hook "+a.getSource(u)+" has a spread element in its dependency array. This means we can't statically verify whether you've passed the correct dependencies."});
41
- else{try{var c=X(b)}catch(la){if(/Unsupported node type/.test(la.message)){"Literal"===b.type?y.has(b.value)?a.report({node:b,message:"The "+b.raw+" literal is not a valid dependency because it never changes. Did you mean to include "+(b.value+" in the array instead?")}):a.report({node:b,message:"The "+b.raw+" literal is not a valid dependency because it never changes. You can safely remove it."}):a.report({node:b,message:"React Hook "+a.getSource(u)+" has a complex expression in the dependency array. Extract it to a separate variable so it can be statically checked."});
42
- return}throw la;}for(var f=b;"MemberExpression"===f.type;)f=f.object;var d=!t.through.some(function(a){return a.identifier===f});D.push({key:c,node:b});d||M.add(c)}});var J=V({dependencies:y,declaredDependencies:D,optionalDependencies:E,externalDependencies:M,isEffect:A}),Q=J.suggestedDependencies;g=J.unnecessaryDependencies;var H=J.missingDependencies;J=J.duplicateDependencies;if(0===J.size+H.size+g.size)ia({declaredDependencies:D,declaredDependenciesNode:m,componentScope:t,scope:p}).forEach(function(b){var c=
43
- b.fn;b=b.suggestUseCallback;var f="The '"+c.name.name+"' function makes the dependencies of "+(x+" Hook (at line "+m.loc.start.line+") change on every render.");f=b?f+(" To fix this, wrap the '"+(c.name.name+"' definition into its own useCallback() Hook.")):f+(" Move it inside the "+x+" callback. Alternatively, wrap the '"+(c.name.name+"' definition into its own useCallback() Hook."));var d;b&&"Variable"===c.type&&(d=[{desc:"Wrap the '"+c.name.name+"' definition into its own useCallback() Hook.",
44
- fix:function(a){return[a.insertTextBefore(c.node.init,"useCallback("),a.insertTextAfter(c.node.init,")")]}}]);a.report({node:c.node,message:f,suggest:d})});else{!A&&0<H.size&&(Q=V({dependencies:y,declaredDependencies:[],optionalDependencies:E,externalDependencies:M,isEffect:A}).suggestedDependencies);(function(){if(0===D.length)return!0;var a=D.map(function(a){return a.key}),c=a.slice().sort();return a.join(",")===c.join(",")})()&&Q.sort();var w="";if(0<g.size){var N=null;Array.from(g.keys()).forEach(function(a){null===
45
- N&&a.endsWith(".current")&&(N=a)});if(null!==N)w=" Mutable values like '"+N+"' aren't valid dependencies because mutating them doesn't re-render the component.";else if(0<M.size){var r=Array.from(M)[0];p.set.has(r)||(w=" Outer scope values like '"+r+"' aren't valid dependencies because mutating them doesn't re-render the component.")}}if(!w&&H.has("props")){p=y.get("props");if(null==p)return;p=p.references;if(!Array.isArray(p))return;r=!0;for(var U=0;U<p.length;U++){var O=Y(t.block,p[U].identifier);
46
- if(!O){r=!1;break}O=O.parent;if(null==O){r=!1;break}if("MemberExpression"!==O.type){r=!1;break}}r&&(w=" However, 'props' will change when *any* prop changes, so the preferred fix is to destructure the 'props' object outside of the "+(x+" call and refer to those specific props inside ")+(a.getSource(u)+"."))}if(!w&&0<H.size){var R=null;H.forEach(function(a){if(!R){var b=t.set.get(a),c=y.get(a);if(c.references[0].resolved===b&&(b=b.defs[0],null!=b&&null!=b.name&&"Parameter"===b.type)){b=!1;for(var d,
47
- e=0;e<c.references.length;e++)if(d=c.references[e].identifier,null!=d&&null!=d.parent&&"CallExpression"===d.parent.type&&d.parent.callee===d){b=!0;break}b&&(R=a)}}});null!==R&&(w=" If '"+R+"' changes too often, find the parent component that defines it and wrap that definition in useCallback.")}if(!w&&0<H.size){var q=null;H.forEach(function(a){if(null===q)for(var b=y.get(a).references,d,e,g=0;g<b.length;g++){d=b[g].identifier;for(e=d.parent;null!=e&&e!==t.block;){if("CallExpression"===e.type){var h=
48
- c.get(e.callee);if(null!=h){h.name===a?q={missingDep:a,setter:e.callee.name,form:"updater"}:z.has(d)?q={missingDep:a,setter:e.callee.name,form:"reducer"}:(d=b[g].resolved,null!=d&&(d=d.defs[0],null!=d&&"Parameter"===d.type&&(q={missingDep:a,setter:e.callee.name,form:"inlineReducer"})));break}}e=e.parent}if(null!==q)break}});if(null!==q)switch(q.form){case "reducer":w=" You can also replace multiple useState variables with useReducer if '"+(q.setter+"' needs the current value of '")+(q.missingDep+
49
- "'.");break;case "inlineReducer":w=" If '"+q.setter+"' needs the current value of '"+(q.missingDep+"', you can also switch to useReducer instead of useState and read '")+(q.missingDep+"' in the reducer.");break;case "updater":w=" You can also do a functional update '"+q.setter+"("+q.missingDep.substring(0,1)+" => ...)' if you only need '"+q.missingDep+"' in the '"+(q.setter+"' call.");break;default:throw Error("Unknown case.");}}a.report({node:m,message:"React Hook "+a.getSource(u)+" has "+(F(H,"a",
50
- "missing","include")||F(g,"an","unnecessary","exclude")||F(J,"a","duplicate","omit"))+w,suggest:[{desc:"Update the dependencies array to be: ["+Q.join(", ")+"]",fix:function(a){return a.replaceText(m,"["+Q.join(", ")+"]")}}]})}}else{var G=null;y.forEach(function(a,e){G||a.references.forEach(function(a){if(!G&&c.has(a.identifier)){for(a=a.from;"function"!==a.type;)a=a.upper;a.block===d&&(G=e)}})});if(G){var Z=V({dependencies:y,declaredDependencies:[],optionalDependencies:E,externalDependencies:new Set,
51
- isEffect:!0}).suggestedDependencies;a.report({node:d.parent.callee,message:"React Hook "+x+" contains a call to '"+G+"'. Without a list of dependencies, this can lead to an infinite chain of updates. To fix this, pass ["+Z.join(", ")+("] as a second argument to the "+x+" Hook."),suggest:[{desc:"Add dependencies array: ["+Z.join(", ")+"]",fix:function(a){return a.insertTextAfter(d,", ["+Z.join(", ")+"]")}}]})}}}}else"useMemo"!==x&&"useCallback"!==x||a.report({node:d.parent.callee,message:"React Hook "+
52
- x+" does nothing when called with only one argument. Did you forget to pass an array of dependencies?"})}}}var h={additionalHooks:a.options&&a.options[0]&&a.options[0].additionalHooks?new RegExp(a.options[0].additionalHooks):void 0},c=new WeakMap,z=new WeakSet,l=new WeakMap,v=new WeakMap;return{FunctionExpression:d,ArrowFunctionExpression:d}}}}};
14
+ function W(a){function f(){return{isRequired:!1,isSatisfiedRecursively:!1,hasRequiredNodesBelow:!1,children:new Map}}function e(a,d){d=d.split(".");var l=!0,m=!1,e=void 0;try{for(var h=d[Symbol.iterator](),q;!(l=(q=h.next()).done);l=!0){var k=q.value,w=a.children.get(k);w||(w=f(),a.children.set(k,w));a=w}}catch(D){m=!0,e=D}finally{try{l||null==h.return||h.return()}finally{if(m)throw e;}}return a}function h(a,d,f){d=d.split(".");var l=!0,m=!1,e=void 0;try{for(var h=d[Symbol.iterator](),w;!(l=(w=h.next()).done);l=
15
+ !0){var k=a.children.get(w.value);if(!k)break;f(k);a=k}}catch(D){m=!0,e=D}finally{try{l||null==h.return||h.return()}finally{if(m)throw e;}}}function d(a,f,m,h){a.children.forEach(function(a,l){var e=h(l);a.isSatisfiedRecursively?a.hasRequiredNodesBelow&&m.add(e):a.isRequired?f.add(e):d(a,f,m,function(a){return e+"."+a})})}var w=a.dependencies,p=a.declaredDependencies,x=a.optionalDependencies,m=a.externalDependencies,E=a.isEffect,F=f();w.forEach(function(a,d){e(F,d).isRequired=!0;h(F,d,function(a){a.hasRequiredNodesBelow=
16
+ !0})});p.forEach(function(a){e(F,a.key).isSatisfiedRecursively=!0});x.forEach(function(a){e(F,a).isSatisfiedRecursively=!0});a=new Set;var k=new Set;d(F,a,k,function(a){return a});var r=[],A=new Set,B=new Set;p.forEach(function(a){a=a.key;k.has(a)?-1===r.indexOf(a)?r.push(a):B.add(a):!E||a.endsWith(".current")||m.has(a)?A.add(a):-1===r.indexOf(a)&&r.push(a)});a.forEach(function(a){r.push(a)});return{suggestedDependencies:r,unnecessaryDependencies:A,duplicateDependencies:B,missingDependencies:a}}
17
+ function ia(a){var f=a.declaredDependenciesNode,e=a.componentScope,h=a.scope;return a.declaredDependencies.map(function(a){a=e.set.get(a.key);if(null==a)return null;var d=a.defs[0];return null==d?null:"Variable"===d.type&&"VariableDeclarator"===d.node.type&&null!=d.node.init&&("ArrowFunctionExpression"===d.node.init.type||"FunctionExpression"===d.node.init.type)||"FunctionName"===d.type&&"FunctionDeclaration"===d.node.type?a:null}).filter(Boolean).map(function(a){var d=a.defs[0];a:{for(var e=!1,x=
18
+ 0;x<a.references.length;x++){var m=a.references[x];if(m.writeExpr)if(e){a=!0;break a}else{e=!0;continue}for(var E=m.from;E!==h&&null!=E;)E=E.upper;if(E!==h&&!ja(f,m.identifier)){a=!0;break a}}a=!1}return{fn:d,suggestUseCallback:a}})}function ka(a){return"MemberExpression"!==a.parent.type||a.parent.object!==a||"current"===a.parent.property.name||a.parent.computed||null!=a.parent.parent&&"CallExpression"===a.parent.parent.type&&a.parent.parent.callee===a.parent?a:ka(a.parent)}
19
+ function Y(a){if("Identifier"===a.type)return a.name;if("MemberExpression"!==a.type||a.computed)throw Error("Unsupported node type: "+a.type);var f=Y(a.object);a=Y(a.property);return f+"."+a}function ma(a){return"MemberExpression"!==a.type||"Identifier"!==a.object.type||"React"!==a.object.name||"Identifier"!==a.property.type||a.computed?a:a.property}
20
+ function na(a,f){var e=ma(a);if("Identifier"!==e.type)return null;switch(e.name){case "useEffect":case "useLayoutEffect":case "useCallback":case "useMemo":return 0;case "useImperativeHandle":return 1;default:if(e===a&&f&&f.additionalHooks){try{var h=Y(e)}catch(d){if(/Unsupported node type/.test(d.message))return 0;throw d;}return f.additionalHooks.test(h)?0:-1}return-1}}
21
+ function Z(a,f){for(var e=[a],h=null;e.length;){h=e.shift();if("Identifier"===h.type&&h.name===f.name&&h.range[0]===f.range[0]&&h.range[1]===f.range[1])return h;if(ja(h,f)){a=0;for(var d=Object.entries(h);a<d.length;a++){var w=d[a],p=w[1];"parent"!==w[0]&&(oa(p)?(p.parent=h,e.push(p)):Array.isArray(p)&&p.forEach(function(a){oa(a)&&(a.parent=h,e.push(a))}))}}}return null}
22
+ function pa(a){for(var f="",e=0;e<a.length;e++)f+=a[e],0===e&&2===a.length?f+=" and ":e===a.length-2&&2<a.length?f+=", and ":e<a.length-1&&(f+=", ");return f}function oa(a){return"object"===typeof a&&null!==a&&!Array.isArray(a)&&"string"===typeof a.type}function ja(a,f){return a.range[0]<=f.range[0]&&a.range[1]>=f.range[1]}exports.configs={recommended:{plugins:["react-hooks"],rules:{"react-hooks/rules-of-hooks":"error","react-hooks/exhaustive-deps":"warn"}}};
23
+ exports.rules={"rules-of-hooks":{create:function(a){var f=[],e=[];return{onCodePathSegmentStart:function(a){return e.push(a)},onCodePathSegmentEnd:function(){return e.pop()},onCodePathStart:function(){return f.push(new Map)},onCodePathEnd:function(e,d){function h(a,g){var b=h.cache,c=b.get(a.id);g=new Set(g);if(g.has(a.id)){b=[].concat(g);a=b.slice(b.indexOf(a.id)+1);b=!0;c=!1;var z=void 0;try{for(var d=a[Symbol.iterator](),f;!(b=(f=d.next()).done);b=!0)E.add(f.value)}catch(J){c=!0,z=J}finally{try{b||
24
+ null==d.return||d.return()}finally{if(c)throw z;}}return 0}g.add(a.id);if(void 0!==c)return c;if(e.thrownSegments.includes(a))c=0;else if(0===a.prevSegments.length)c=1;else{c=0;d=!0;f=!1;var m=void 0;try{z=a.prevSegments[Symbol.iterator]();for(var k;!(d=(k=z.next()).done);d=!0)c+=h(k.value,g)}catch(J){f=!0,m=J}finally{try{d||null==z.return||z.return()}finally{if(f)throw m;}}}a.reachable&&0===c?b.delete(a.id):b.set(a.id,c);return c}function p(a,g){var b=p.cache,c=b.get(a.id);g=new Set(g);if(g.has(a.id)){b=
25
+ Array.from(g);a=b.slice(b.indexOf(a.id)+1);b=!0;c=!1;var z=void 0;try{for(var d=a[Symbol.iterator](),f;!(b=(f=d.next()).done);b=!0)E.add(f.value)}catch(J){c=!0,z=J}finally{try{b||null==d.return||d.return()}finally{if(c)throw z;}}return 0}g.add(a.id);if(void 0!==c)return c;if(e.thrownSegments.includes(a))c=0;else if(0===a.nextSegments.length)c=1;else{c=0;d=!0;f=!1;var m=void 0;try{z=a.nextSegments[Symbol.iterator]();for(var h;!(d=(h=z.next()).done);d=!0)c+=p(h.value,g)}catch(J){f=!0,m=J}finally{try{d||
26
+ null==z.return||z.return()}finally{if(f)throw m;}}}b.set(a.id,c);return c}function x(a){var g=x.cache,b=g.get(a.id);if(null===b)return Infinity;if(void 0!==b)return b;g.set(a.id,null);if(0===a.prevSegments.length)b=1;else{b=Infinity;var c=!0,z=!1,d=void 0;try{for(var f=a.prevSegments[Symbol.iterator](),e;!(c=(e=f.next()).done);c=!0){var m=x(e.value);m<b&&(b=m)}}catch(J){z=!0,d=J}finally{try{c||null==f.return||f.return()}finally{if(z)throw d;}}b+=1}g.set(a.id,b);return b}var m=f.pop();if(0!==m.size){var E=
27
+ new Set;h.cache=new Map;p.cache=new Map;x.cache=new Map;var F=p(e.initialSegment),k=ha(d),r=fa(d),A=k?ba(k)||U(k):da(d)||ea(d),B=Infinity,l=!0,q=!1,H=void 0;try{for(var v=e.finalSegments[Symbol.iterator](),L;!(l=(L=v.next()).done);l=!0){var X=L.value;if(X.reachable){var T=x(X);T<B&&(B=T)}}}catch(I){q=!0,H=I}finally{try{l||null==v.return||v.return()}finally{if(q)throw H;}}l=!0;q=!1;H=void 0;try{for(var y=m[Symbol.iterator](),Q;!(l=(Q=y.next()).done);l=!0){var D=Q.value,C=D[0],M=D[1];if(C.reachable){var K=
28
+ 0===C.nextSegments.length?B<=x(C):B<x(C),R=h(C)*p(C),G=E.has(C.id);m=!0;v=!1;L=void 0;try{for(var u=M[Symbol.iterator](),N;!(m=(N=u.next()).done);m=!0){var n=N.value;G&&a.report({node:n,message:'React Hook "'+a.getSource(n)+'" may be executed more than once. Possibly because it is called in a loop. React Hooks must be called in the exact same order in every component render.'});if(A){if(!G&&R!==F){var V='React Hook "'+a.getSource(n)+'" is called conditionally. React Hooks must be called in the exact same order in every component render.'+
29
+ (K?" Did you accidentally call a React Hook after an early return?":"");a.report({node:n,message:V})}}else if(d.parent&&("MethodDefinition"===d.parent.type||"ClassProperty"===d.parent.type)&&d.parent.value===d){var O='React Hook "'+a.getSource(n)+'" cannot be called in a class component. React Hooks must be called in a React function component or a custom React Hook function.';a.report({node:n,message:O})}else if(k){var S='React Hook "'+a.getSource(n)+'" is called in function "'+(a.getSource(k)+'" that is neither a React function component nor a custom React Hook function.');
30
+ a.report({node:n,message:S})}else if("Program"===d.type){var t='React Hook "'+a.getSource(n)+'" cannot be called at the top level. React Hooks must be called in a React function component or a custom React Hook function.';a.report({node:n,message:t})}else if(r){var P='React Hook "'+a.getSource(n)+'" cannot be called inside a callback. React Hooks must be called in a React function component or a custom React Hook function.';a.report({node:n,message:P})}}}catch(I){v=!0,L=I}finally{try{m||null==u.return||
31
+ u.return()}finally{if(v)throw L;}}}}}catch(I){q=!0,H=I}finally{try{l||null==y.return||y.return()}finally{if(q)throw H;}}}},CallExpression:function(a){if(U(a.callee)){var d=f[f.length-1],h=e[e.length-1],p=d.get(h);p||(p=[],d.set(h,p));p.push(a.callee)}}}}},"exhaustive-deps":{meta:{schema:[{type:"object",additionalProperties:!1,properties:{additionalHooks:{type:"string"}}}]},create:function(a){function f(a,d){return function(f){if(d.has(f))return d.get(f);var e=a(f);d.set(f,e);return e}}function e(e){function m(a){var b=
32
+ !0,c=!1,g=void 0;try{for(var d=a.references[Symbol.iterator](),f;!(b=(f=d.next()).done);b=!0){var h=f.value;if(h.resolved&&H.has(h.resolved.scope)){var k=Z(e,h.identifier),l=ka(k),p=Y(l),q;if(q=B&&"Identifier"===l.type&&"MemberExpression"===l.parent.type&&!l.parent.computed&&"Identifier"===l.parent.property.type&&"current"===l.parent.property.name){for(var n=h.from,t=!1;n.block!==e;)"function"===n.type&&(t=null!=n.block.parent&&"ReturnStatement"===n.block.parent.type),n=n.upper;q=t}q&&T.set(p,{reference:h,
33
+ dependencyNode:l});var r=h.resolved.defs[0];if(null!=r&&(null==r.node||r.node.init!==e.parent)&&"TypeParameter"!==r.type)if(y.has(p))y.get(p).references.push(h);else{var v=h.resolved,w=L(v)||X(v);y.set(p,{isStatic:w,references:[h]})}}}}catch(aa){c=!0,g=aa}finally{try{b||null==d.return||d.return()}finally{if(c)throw g;}}b=!0;c=!1;g=void 0;try{for(var u=a.childScopes[Symbol.iterator](),x;!(b=(x=u.next()).done);b=!0)m(x.value)}catch(aa){c=!0,g=aa}finally{try{b||null==u.return||u.return()}finally{if(c)throw g;
34
+ }}}function F(a,b,c,d){return 0===a.size?null:(1<a.size?"":b+" ")+c+" "+(1<a.size?"dependencies":"dependency")+": "+pa(Array.from(a).sort().map(function(a){return"'"+a+"'"}))+(". Either "+d+" "+(1<a.size?"them":"it")+" or remove the dependency array.")}if(("FunctionExpression"===e.type||"ArrowFunctionExpression"===e.type)&&"CallExpression"===e.parent.type){var k=na(e.parent.callee,h);if(e.parent.arguments[k]===e){var r=e.parent.callee,A=ma(r).name,B=/Effect($|[^a-z])/g.test(A),l=e.parent.arguments[k+
35
+ 1];if(l||B){B&&e.async&&a.report({node:e,message:"Effect callbacks are synchronous to prevent race conditions. Put the async function inside:\n\nuseEffect(() => {\n async function fetchData() {\n // You can await here\n const response = await MyAPI.getData(someId);\n // ...\n }\n fetchData();\n}, [someId]); // Or [] if effect doesn't need props or state\n\nLearn more about data fetching with Hooks: https://fb.me/react-hooks-data-fetching"});var q=a.getScope(),H=new Set,v=null;for(k=q.upper;k;){H.add(k);
36
+ if("function"===k.type)break;k=k.upper}if(k){v=k;var L=f(function(a){if(!Array.isArray(a.defs))return!1;var b=a.defs[0];if(null==b||"VariableDeclarator"!==b.node.type)return!1;var c=b.node.init;if(null==c)return!1;var g=b.node.parent;if(null==g&&(Z(v.block,b.node.id),g=b.node.parent,null==g))return!1;if("const"===g.kind&&"Literal"===c.type&&("string"===typeof c.value||"number"===typeof c.value||null===c.value))return!0;if("CallExpression"!==c.type)return!1;c=c.callee;"MemberExpression"!==c.type||
37
+ "React"!==c.object.name||null==c.property||c.computed||(c=c.property);if("Identifier"!==c.type)return!1;b=b.node.id;c=c.name;if("useRef"===c&&"Identifier"===b.type)return!0;if(("useState"===c||"useReducer"===c)&&"ArrayPattern"===b.type&&2===b.elements.length&&Array.isArray(a.identifiers)){if(b.elements[1]===a.identifiers[0]){if("useState"===c)for(a=a.references,c=0;c<a.length;c++)d.set(a[c].identifier,b.elements[0]);return!0}if(b.elements[0]===a.identifiers[0]&&"useState"===c)for(a=a.references,b=
38
+ 0;b<a.length;b++)w.add(a[b].identifier)}return!1},p),X=f(function(a){if(!Array.isArray(a.defs))return!1;a=a.defs[0];if(null==a||null==a.node||null==a.node.id)return!1;var b=a.node,c=v.childScopes;a=null;var g;for(g=0;g<c.length;g++){var d=c[g],e=d.block;if("FunctionDeclaration"===b.type&&e===b||"VariableDeclarator"===b.type&&e.parent===b){a=d;break}}if(null==a)return!1;for(g=0;g<a.through.length;g++)if(b=a.through[g],null!=b.resolved&&H.has(b.resolved.scope)&&!L(b.resolved))return!1;return!0},x),
39
+ T=new Map,y=new Map;m(q);T.forEach(function(g,b){var c=g.dependencyNode;g=g.reference.resolved.references;for(var d=!1,e=0;e<g.length;e++){var f=g[e].identifier.parent;if(null!=f&&"MemberExpression"===f.type&&!f.computed&&"Identifier"===f.property.type&&"current"===f.property.name&&"AssignmentExpression"===f.parent.type&&f.parent.left===f){d=!0;break}}d||a.report({node:c.parent.property,message:"The ref value '"+b+".current' will likely have changed by the time this effect cleanup function runs. If this ref points to a node rendered by React, copy '"+
40
+ (b+".current' to a variable inside the effect, and use that variable in the cleanup function.")})});var Q=new Set,D=new Set;y.forEach(function(g,b){var c=g.references;g.isStatic&&D.add(b);c.forEach(function(c){c.writeExpr&&(c=c.writeExpr,Q.has(b)||(Q.add(b),a.report({node:c,message:"Assignments to the '"+b+"' variable from inside React Hook "+(a.getSource(r)+" will be lost after each render. To preserve the value over time, store it in a useRef Hook and keep the mutable value in the '.current' property. Otherwise, you can move this variable directly inside ")+
41
+ (a.getSource(r)+".")})))})});if(!(0<Q.size))if(l){var C=[],M=new Set;"ArrayExpression"!==l.type?a.report({node:l,message:"React Hook "+a.getSource(r)+" was passed a dependency list that is not an array literal. This means we can't statically verify whether you've passed the correct dependencies."}):l.elements.forEach(function(g){if(null!==g)if("SpreadElement"===g.type)a.report({node:g,message:"React Hook "+a.getSource(r)+" has a spread element in its dependency array. This means we can't statically verify whether you've passed the correct dependencies."});
42
+ else{try{var b=Y(g)}catch(la){if(/Unsupported node type/.test(la.message)){"Literal"===g.type?y.has(g.value)?a.report({node:g,message:"The "+g.raw+" literal is not a valid dependency because it never changes. Did you mean to include "+(g.value+" in the array instead?")}):a.report({node:g,message:"The "+g.raw+" literal is not a valid dependency because it never changes. You can safely remove it."}):a.report({node:g,message:"React Hook "+a.getSource(r)+" has a complex expression in the dependency array. Extract it to a separate variable so it can be statically checked."});
43
+ return}throw la;}for(var c=g;"MemberExpression"===c.type;)c=c.object;var d=!v.through.some(function(a){return a.identifier===c});C.push({key:b,node:g});d||M.add(b)}});var K=W({dependencies:y,declaredDependencies:C,optionalDependencies:D,externalDependencies:M,isEffect:B}),R=K.suggestedDependencies;k=K.unnecessaryDependencies;var G=K.missingDependencies;K=K.duplicateDependencies;if(0===K.size+G.size+k.size)ia({declaredDependencies:C,declaredDependenciesNode:l,componentScope:v,scope:q}).forEach(function(g){var b=
44
+ g.fn;g=g.suggestUseCallback;var c="The '"+b.name.name+"' function makes the dependencies of "+(A+" Hook (at line "+l.loc.start.line+") change on every render.");c=g?c+(" To fix this, wrap the '"+(b.name.name+"' definition into its own useCallback() Hook.")):c+(" Move it inside the "+A+" callback. Alternatively, wrap the '"+(b.name.name+"' definition into its own useCallback() Hook."));var d;g&&"Variable"===b.type&&(d=[{desc:"Wrap the '"+b.name.name+"' definition into its own useCallback() Hook.",
45
+ fix:function(a){return[a.insertTextBefore(b.node.init,"useCallback("),a.insertTextAfter(b.node.init,")")]}}]);a.report({node:b.node,message:c,suggest:d})});else{!B&&0<G.size&&(R=W({dependencies:y,declaredDependencies:[],optionalDependencies:D,externalDependencies:M,isEffect:B}).suggestedDependencies);(function(){if(0===C.length)return!0;var a=C.map(function(a){return a.key}),b=a.slice().sort();return a.join(",")===b.join(",")})()&&R.sort();var u="";if(0<k.size){var N=null;Array.from(k.keys()).forEach(function(a){null===
46
+ N&&a.endsWith(".current")&&(N=a)});if(null!==N)u=" Mutable values like '"+N+"' aren't valid dependencies because mutating them doesn't re-render the component.";else if(0<M.size){var n=Array.from(M)[0];q.set.has(n)||(u=" Outer scope values like '"+n+"' aren't valid dependencies because mutating them doesn't re-render the component.")}}if(!u&&G.has("props")){q=y.get("props");if(null==q)return;q=q.references;if(!Array.isArray(q))return;n=!0;for(var V=0;V<q.length;V++){var O=Z(v.block,q[V].identifier);
47
+ if(!O){n=!1;break}O=O.parent;if(null==O){n=!1;break}if("MemberExpression"!==O.type){n=!1;break}}n&&(u=" However, 'props' will change when *any* prop changes, so the preferred fix is to destructure the 'props' object outside of the "+(A+" call and refer to those specific props inside ")+(a.getSource(r)+"."))}if(!u&&0<G.size){var S=null;G.forEach(function(a){if(!S){var b=v.set.get(a),c=y.get(a);if(c.references[0].resolved===b&&(b=b.defs[0],null!=b&&null!=b.name&&"Parameter"===b.type)){b=!1;for(var d,
48
+ e=0;e<c.references.length;e++)if(d=c.references[e].identifier,null!=d&&null!=d.parent&&"CallExpression"===d.parent.type&&d.parent.callee===d){b=!0;break}b&&(S=a)}}});null!==S&&(u=" If '"+S+"' changes too often, find the parent component that defines it and wrap that definition in useCallback.")}if(!u&&0<G.size){var t=null;G.forEach(function(a){if(null===t)for(var b=y.get(a).references,c,e,f=0;f<b.length;f++){c=b[f].identifier;for(e=c.parent;null!=e&&e!==v.block;){if("CallExpression"===e.type){var g=
49
+ d.get(e.callee);if(null!=g){g.name===a?t={missingDep:a,setter:e.callee.name,form:"updater"}:w.has(c)?t={missingDep:a,setter:e.callee.name,form:"reducer"}:(c=b[f].resolved,null!=c&&(c=c.defs[0],null!=c&&"Parameter"===c.type&&(t={missingDep:a,setter:e.callee.name,form:"inlineReducer"})));break}}e=e.parent}if(null!==t)break}});if(null!==t)switch(t.form){case "reducer":u=" You can also replace multiple useState variables with useReducer if '"+(t.setter+"' needs the current value of '")+(t.missingDep+
50
+ "'.");break;case "inlineReducer":u=" If '"+t.setter+"' needs the current value of '"+(t.missingDep+"', you can also switch to useReducer instead of useState and read '")+(t.missingDep+"' in the reducer.");break;case "updater":u=" You can also do a functional update '"+t.setter+"("+t.missingDep.substring(0,1)+" => ...)' if you only need '"+t.missingDep+"' in the '"+(t.setter+"' call.");break;default:throw Error("Unknown case.");}}a.report({node:l,message:"React Hook "+a.getSource(r)+" has "+(F(G,"a",
51
+ "missing","include")||F(k,"an","unnecessary","exclude")||F(K,"a","duplicate","omit"))+u,suggest:[{desc:"Update the dependencies array to be: ["+R.join(", ")+"]",fix:function(a){return a.replaceText(l,"["+R.join(", ")+"]")}}]})}}else{var P=null;y.forEach(function(a,b){P||a.references.forEach(function(a){if(!P&&d.has(a.identifier)){for(a=a.from;"function"!==a.type;)a=a.upper;a.block===e&&(P=b)}})});if(P){var I=W({dependencies:y,declaredDependencies:[],optionalDependencies:D,externalDependencies:new Set,
52
+ isEffect:!0}).suggestedDependencies;a.report({node:e.parent.callee,message:"React Hook "+A+" contains a call to '"+P+"'. Without a list of dependencies, this can lead to an infinite chain of updates. To fix this, pass ["+I.join(", ")+("] as a second argument to the "+A+" Hook."),suggest:[{desc:"Add dependencies array: ["+I.join(", ")+"]",fix:function(a){return a.insertTextAfter(e,", ["+I.join(", ")+"]")}}]})}}}}else"useMemo"!==A&&"useCallback"!==A||a.report({node:e.parent.callee,message:"React Hook "+
53
+ A+" does nothing when called with only one argument. Did you forget to pass an array of dependencies?"})}}}var h={additionalHooks:a.options&&a.options[0]&&a.options[0].additionalHooks?new RegExp(a.options[0].additionalHooks):void 0},d=new WeakMap,w=new WeakSet,p=new WeakMap,x=new WeakMap;return{FunctionExpression:e,ArrowFunctionExpression:e}}}};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "eslint-plugin-react-hooks",
3
3
  "description": "ESLint rules for React Hooks",
4
- "version": "2.4.0-alpha.0",
4
+ "version": "3.0.0",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "https://github.com/facebook/react.git",