UNPKG

14.9 kBJavaScriptView Raw
1/** @license React v16.6.1
2 * eslint-plugin-react-hooks.production.min.js
3 *
4 * Copyright (c) Facebook, Inc. and its affiliates.
5 *
6 * This source code is licensed under the MIT license found in the
7 * LICENSE file in the root directory of this source tree.
8 */
9
10'use strict';function K(a){return"Identifier"===a.type?/^use[A-Z0-9].*$/.test(a.name):"MemberExpression"===a.type&&!a.computed&&K(a.property)?(a=a.object,"Identifier"===a.type&&"React"===a.name):!1}function N(a){return"Identifier"===a.type?!/^[a-z]/.test(a.name):!1}function Q(a){for(;a;){var b=R(a);if(b&&(N(b)||K(b)))return!0;a=a.parent}return!1}
11function R(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:a.parent.key}
12function S(a){function b(){return{isRequired:!1,isSatisfiedRecursively:!1,hasRequiredNodesBelow:!1,children:new Map}}function c(a,c){c=c.split(".");var m=!0,g=!1,d=void 0;try{for(var n=c[Symbol.iterator](),e;!(m=(e=n.next()).done);m=!0){var h=e.value,k=a.children.get(h);k||(k=b(),a.children.set(h,k));a=k}}catch(u){g=!0,d=u}finally{try{!m&&n.return&&n.return()}finally{if(g)throw d;}}return a}function g(a,c,b){c=c.split(".");var g=!0,m=!1,d=void 0;try{for(var k=c[Symbol.iterator](),e;!(g=(e=k.next()).done);g=
13!0){var h=a.children.get(e.value);if(!h)break;b(h);a=h}}catch(u){m=!0,d=u}finally{try{!g&&k.return&&k.return()}finally{if(m)throw d;}}}function h(a,c,b,g){a.children.forEach(function(a,m){var d=g(m);a.isSatisfiedRecursively?a.hasRequiredNodesBelow&&b.add(d):a.isRequired?c.add(d):h(a,c,b,function(a){return d+"."+a})})}var x=a.dependencies,d=a.declaredDependencies,l=a.optionalDependencies,r=a.externalDependencies,k=a.isEffect,y=b();x.forEach(function(a,b){c(y,b).isRequired=!0;g(y,b,function(a){a.hasRequiredNodesBelow=
14!0})});d.forEach(function(a){c(y,a.key).isSatisfiedRecursively=!0});l.forEach(function(a){c(y,a).isSatisfiedRecursively=!0});a=new Set;var p=new Set;h(y,a,p,function(a){return a});var e=[],F=new Set,v=new Set;d.forEach(function(a){a=a.key;p.has(a)?-1===e.indexOf(a)?e.push(a):v.add(a):!k||a.endsWith(".current")||r.has(a)?F.add(a):-1===e.indexOf(a)&&e.push(a)});a.forEach(function(a){e.push(a)});return{suggestedDependencies:e,unnecessaryDependencies:F,duplicateDependencies:v,missingDependencies:a}}
15function T(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:T(a.parent)}function U(a){if("Identifier"===a.type)return a.name;if("MemberExpression"!==a.type||a.computed)throw Error("Unsupported node type: "+a.type);var b=U(a.object);a=U(a.property);return b+"."+a}
16function V(a){return"MemberExpression"!==a.type||"Identifier"!==a.object.type||"React"!==a.object.name||"Identifier"!==a.property.type||a.computed?a:a.property}
17function W(a,b){var c=V(a);if("Identifier"!==c.type)return null;switch(c.name){case "useEffect":case "useLayoutEffect":case "useCallback":case "useMemo":return 0;case "useImperativeHandle":return 1;default:if(c===a&&b&&b.additionalHooks){a=void 0;try{a=U(c)}catch(g){if(/Unsupported node type/.test(g.message))return 0;throw g;}return b.additionalHooks.test(a)?0:-1}return-1}}
18function X(a){var b=a.resolved;if(null==b||!Array.isArray(b.defs))return!1;b=b.defs[0];if(null==b||"VariableDeclarator"!==b.node.type)return!1;var c=b.node.init;if(null==c)return!1;if("const"===b.node.parent.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||"React"!==c.object.name||null==c.property||c.computed||(c=c.property);if("Identifier"!==c.type)return!1;b=b.node.id;
19return"useRef"===c.name&&"Identifier"===b.type||("useState"===c.name||"useReducer"===c.name)&&"ArrayPattern"===b.type&&2===b.elements.length&&Array.isArray(a.resolved.identifiers)&&b.elements[1]===a.resolved.identifiers[0]?!0:!1}
20function Y(a,b){for(var c=[a],g=null;c.length;){g=c.shift();if("Identifier"===g.type&&g.name===b.name&&g.range[0]===b.range[0]&&g.range[1]===b.range[1])return g;if(g.range[0]<=b.range[0]&&g.range[1]>=b.range[1]){a=!0;var h=!1,x=void 0;try{for(var d=Object.entries(g)[Symbol.iterator](),l;!(a=(l=d.next()).done);a=!0){var r=l.value,k=r[1];"parent"!==r[0]&&(Z(k)?(k.parent=g,c.push(k)):Array.isArray(k)&&k.forEach(function(a){Z(a)&&(a.parent=g,c.push(a))}))}}catch(y){h=!0,x=y}finally{try{!a&&d.return&&
21d.return()}finally{if(h)throw x;}}}}return null}function aa(a){for(var b="",c=0;c<a.length;c++)b+=a[c],0===c&&2===a.length?b+=" and ":c===a.length-2&&2<a.length?b+=", and ":c<a.length-1&&(b+=", ");return b}function Z(a){return"object"===typeof a&&null!==a&&!Array.isArray(a)&&"string"===typeof a.type}
22module.exports={rules:{"rules-of-hooks":{create:function(a){var b=[],c=[];return{onCodePathSegmentStart:function(a){return c.push(a)},onCodePathSegmentEnd:function(){return c.pop()},onCodePathStart:function(){return b.push(new Map)},onCodePathEnd:function(c,h){function g(a){var b=g.cache,f=b.get(a.id);if(null===f){if(!k.has(a.id)){k.add(a.id);b=!0;f=!1;var d=void 0;try{for(var h=a.prevSegments[Symbol.iterator](),e;!(b=(e=h.next()).done);b=!0)g(e.value)}catch(G){f=!0,d=G}finally{try{!b&&h.return&&
23h.return()}finally{if(f)throw d;}}}return 0}if(void 0!==f)return f;b.set(a.id,null);if(c.thrownSegments.includes(a))f=0;else if(0===a.prevSegments.length)f=1;else{f=0;h=!0;e=!1;var D=void 0;try{d=a.prevSegments[Symbol.iterator]();for(var I;!(h=(I=d.next()).done);h=!0)f+=g(I.value)}catch(G){e=!0,D=G}finally{try{!h&&d.return&&d.return()}finally{if(e)throw D;}}}a.reachable&&0===f?b.delete(a.id):b.set(a.id,f);return f}function d(a){var b=d.cache,f=b.get(a.id);if(null===f){if(!k.has(a.id)){k.add(a.id);
24b=!0;f=!1;var g=void 0;try{for(var h=a.nextSegments[Symbol.iterator](),e;!(b=(e=h.next()).done);b=!0)d(e.value)}catch(G){f=!0,g=G}finally{try{!b&&h.return&&h.return()}finally{if(f)throw g;}}}return 0}if(void 0!==f)return f;b.set(a.id,null);if(c.thrownSegments.includes(a))f=0;else if(0===a.nextSegments.length)f=1;else{f=0;h=!0;e=!1;var I=void 0;try{g=a.nextSegments[Symbol.iterator]();for(var L;!(h=(L=g.next()).done);h=!0)f+=d(L.value)}catch(G){e=!0,I=G}finally{try{!h&&g.return&&g.return()}finally{if(e)throw I;
25}}}b.set(a.id,f);return f}function l(a){var b=l.cache,f=b.get(a.id);if(null===f)return Infinity;if(void 0!==f)return f;b.set(a.id,null);if(0===a.prevSegments.length)f=1;else{f=Infinity;var c=!0,g=!1,d=void 0;try{for(var h=a.prevSegments[Symbol.iterator](),e;!(c=(e=h.next()).done);c=!0){var k=l(e.value);k<f&&(f=k)}}catch(ba){g=!0,d=ba}finally{try{!c&&h.return&&h.return()}finally{if(g)throw d;}}f+=1}b.set(a.id,f);return f}var r=b.pop();if(0!==r.size){var k=new Set;g.cache=new Map;d.cache=new Map;l.cache=
26new Map;var y=d(c.initialSegment),p=R(h),e=Q(h),F=p?N(p)||K(p):!1,v=Infinity,m=!0,n=!1,A=void 0;try{for(var w=c.finalSegments[Symbol.iterator](),q;!(m=(q=w.next()).done);m=!0){var J=q.value;if(J.reachable){var B=l(J);B<v&&(v=B)}}}catch(D){n=!0,A=D}finally{try{!m&&w.return&&w.return()}finally{if(n)throw A;}}m=!0;n=!1;A=void 0;try{for(var C=r[Symbol.iterator](),z;!(m=(z=C.next()).done);m=!0){var u=z.value,t=u[0],H=u[1];if(t.reachable){var f=0===t.nextSegments.length?v<=l(t):v<l(t),I=g(t)*d(t),L=k.has(t.id);
27r=!0;w=!1;q=void 0;try{for(var P=H[Symbol.iterator](),M;!(r=(M=P.next()).done);r=!0){var E=M.value;L&&a.report({node:E,message:'React Hook "'+a.getSource(E)+'" 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(F){if(!L&&I!==y){var ca='React Hook "'+a.getSource(E)+'" is called conditionally. React Hooks must be called in the exact same order in every component render.'+(f?" Did you accidentally call a React Hook after an early return?":
28"");a.report({node:E,message:ca})}}else if(!h.parent||"MethodDefinition"!==h.parent.type&&"ClassProperty"!==h.parent.type||h.parent.value!==h)if(p){var da='React Hook "'+a.getSource(E)+'" is called in '+('function "'+a.getSource(p)+'" ')+"which is neither a React function component or a custom React Hook function.";a.report({node:E,message:da})}else if("Program"!==h.type&&e){var ea='React Hook "'+a.getSource(E)+'" cannot be called inside a callback. React Hooks must be called in a React function component or a custom React Hook function.';
29a.report({node:E,message:ea})}}}catch(D){w=!0,q=D}finally{try{!r&&P.return&&P.return()}finally{if(w)throw q;}}}}}catch(D){n=!0,A=D}finally{try{!m&&C.return&&C.return()}finally{if(n)throw A;}}}},CallExpression:function(a){if(K(a.callee)){var g=b[b.length-1],x=c[c.length-1],d=g.get(x);d||(d=[],g.set(x,d));d.push(a.callee)}}}}},"exhaustive-deps":{meta:{fixable:"code",schema:[{type:"object",additionalProperties:!1,properties:{additionalHooks:{type:"string"}}}]},create:function(a){function b(b){function g(a){var f=
30!0,c=!1,d=void 0;try{for(var e=a.references[Symbol.iterator](),h;!(f=(h=e.next()).done);f=!0){var k=h.value;if(k.resolved&&y.has(k.resolved.scope)){var m=Y(b,k.identifier),l=T(m),n=U(l),q;if(q=r&&"Identifier"===l.type&&"MemberExpression"===l.parent.type&&!l.parent.computed&&"Identifier"===l.parent.property.type&&"current"===l.parent.property.name){for(var p=k.from,u=!1;p.block!==b;)"function"===p.type&&(u=null!=p.block.parent&&"ReturnStatement"===p.block.parent.type),p=p.upper;q=u}q&&F.set(n,{reference:k,
31dependencyNode:l});if(!v.has(n)){var x=X(k);v.set(n,{isStatic:x,reference:k})}}}}catch(O){c=!0,d=O}finally{try{!f&&e.return&&e.return()}finally{if(c)throw d;}}f=!0;c=!1;d=void 0;try{for(var t=a.childScopes[Symbol.iterator](),w;!(f=(w=t.next()).done);f=!0)g(w.value)}catch(O){c=!0,d=O}finally{try{!f&&t.return&&t.return()}finally{if(c)throw d;}}}function x(a,b,c,d){return 0===a.size?null:(1<a.size?"":b+" ")+c+" "+(1<a.size?"dependencies":"dependency")+": "+aa(Array.from(a).sort().map(function(a){return"'"+
32a+"'"}))+(". Either "+d+" "+(1<a.size?"them":"it")+" or remove the dependency array.")}if(("FunctionExpression"===b.type||"ArrowFunctionExpression"===b.type)&&"CallExpression"===b.parent.type){var d=W(b.parent.callee,c);if(b.parent.arguments[d]===b){var l=b.parent.callee,r=V(l).name.endsWith("Effect"),k=b.parent.arguments[d+1];if(k){d=a.getScope();for(var y=new Set,p=null,e=d.upper;e;){y.add(e);if("function"===e.type)break;e=e.upper}if(e){p=e;var F=new Map,v=new Map;g(d);F.forEach(function(b,c){var f=
33b.dependencyNode;b=b.reference.resolved.references;for(var d=!1,g=0;g<b.length;g++){var e=b[g].identifier.parent;if(null!=e&&"MemberExpression"===e.type&&!e.computed&&"Identifier"===e.property.type&&"current"===e.property.name&&"AssignmentExpression"===e.parent.type&&e.parent.left===e){d=!0;break}}d||a.report({node:f.parent.property,message:"Accessing '"+c+".current' during the effect cleanup will likely read a different ref value because by this time React has already updated the ref. If this ref is managed by React, store "+
34("'"+c+".current' in a variable inside ")+"the effect itself and refer to that variable from the cleanup function."})});var m=[],n=new Set;"ArrayExpression"!==k.type?a.report({node:k,message:"React Hook "+a.getSource(l)+" has a second argument which is not an array literal. This means we can't statically verify whether you've passed the correct dependencies."}):k.elements.forEach(function(b){if(null!==b)if("SpreadElement"===b.type)a.report({node:b,message:"React Hook "+a.getSource(l)+" has a spread element in its dependency array. This means we can't statically verify whether you've passed the correct dependencies."});
35else{var c=void 0;try{c=U(b)}catch(M){if(/Unsupported node type/.test(M.message)){"Literal"===b.type?"string"===typeof b.value?a.report({node:b,message:"The "+b.raw+" string 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(l)+" has a complex expression in the dependency array. Extract it to a separate variable so it can be statically checked."});
36return}throw M;}for(var f=b;"MemberExpression"===f.type;)f=f.object;var d=!p.through.some(function(a){return a.identifier===f});m.push({key:c,node:b});d||n.add(c)}});var A=!1,w=new Set;v.forEach(function(b,c){var d=b.reference;b.isStatic&&w.add(c);d.writeExpr&&(b=d.writeExpr,A=!0,a.report({node:b,message:"Assignments to the '"+c+"' variable from inside a React "+a.getSource(l)+" Hook will not persist between re-renders. If it's only needed by this Hook, move the variable inside it. Alternatively, declare a ref with the useRef Hook, and keep the mutable value in its 'current' property."}))});
37if(!A){var q=S({dependencies:v,declaredDependencies:m,optionalDependencies:w,externalDependencies:n,isEffect:r}),J=q.suggestedDependencies;d=q.unnecessaryDependencies;e=q.missingDependencies;q=q.duplicateDependencies;if(0!==q.size+e.size+d.size){!r&&0<e.size&&(J=S({dependencies:v,declaredDependencies:[],optionalDependencies:w,externalDependencies:n,isEffect:r}).suggestedDependencies);(function(){if(0===m.length)return!0;var a=m.map(function(a){return a.key}),b=a.slice().sort();return a.join(",")===
38b.join(",")})()&&J.sort();var B="";if(0<d.size){var C=null;Array.from(d.keys()).forEach(function(a){null===C&&a.endsWith(".current")&&(C=a)});null!==C?B=" Mutable values like '"+C+"' aren't valid dependencies because their mutation doesn't re-render the component.":0<n.size&&(B=" Values like '"+Array.from(n)[0]+"' aren't valid dependencies because their mutation doesn't re-render the component.")}if(!B&&e.has("props")){var z=v.get("props");if(null==z)return;z=z.reference.resolved.references;if(!Array.isArray(z))return;
39for(var u=!0,t=0;t<z.length;t++){var H=Y(p.block,z[t].identifier);if(!H){u=!1;break}H=H.parent;if(null==H){u=!1;break}if("MemberExpression"!==H.type){u=!1;break}}u&&(B=" Alternatively, destructure the necessary props outside the callback.")}a.report({node:k,message:"React Hook "+a.getSource(l)+" has "+(x(e,"a","missing","include")||x(d,"an","unnecessary","exclude")||x(q,"a","duplicate","omit"))+B,fix:function(a){return a.replaceText(k,"["+J.join(", ")+"]")}})}}}}}}}var c={additionalHooks:a.options&&
40a.options[0]&&a.options[0].additionalHooks?new RegExp(a.options[0].additionalHooks):void 0};return{FunctionExpression:b,ArrowFunctionExpression:b}}}}};