Socket
Socket
Sign inDemoInstall

eslint-plugin-react-hooks

Package Overview
Dependencies
Maintainers
5
Versions
1833
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eslint-plugin-react-hooks - npm Package Compare versions

Comparing version 0.0.0-6da04b5d8 to 0.0.0-8d5403877

8

build-info.json
{
"branch": "master",
"buildNumber": "14730",
"checksum": "45d1701",
"commit": "6da04b5d8",
"buildNumber": "35115",
"checksum": "1fa61b1",
"commit": "8d5403877",
"environment": "ci",
"reactVersion": "16.8.6-canary-6da04b5d8"
"reactVersion": "16.9.0-canary-8d5403877"
}

@@ -1,2 +0,2 @@

/** @license React v0.0.0-6da04b5d8
/** @license React v0.0.0-8d5403877
* eslint-plugin-react-hooks.production.min.js

@@ -13,40 +13,40 @@ *

a.parent.key}
function V(a){function b(){return{isRequired:!1,isSatisfiedRecursively:!1,hasRequiredNodesBelow:!1,children:new Map}}function e(a,h){h=h.split(".");var l=!0,c=!1,e=void 0;try{for(var f=h[Symbol.iterator](),p;!(l=(p=f.next()).done);l=!0){var u=p.value,g=a.children.get(u);g||(g=b(),a.children.set(u,g));a=g}}catch(E){c=!0,e=E}finally{try{!l&&f.return&&f.return()}finally{if(c)throw e;}}return a}function f(a,h,c){h=h.split(".");var l=!0,b=!1,f=void 0;try{for(var e=h[Symbol.iterator](),g;!(l=(g=e.next()).done);l=
!0){var u=a.children.get(g.value);if(!u)break;c(u);a=u}}catch(E){b=!0,f=E}finally{try{!l&&e.return&&e.return()}finally{if(b)throw f;}}}function h(a,c,b,e){a.children.forEach(function(a,l){var f=e(l);a.isSatisfiedRecursively?a.hasRequiredNodesBelow&&b.add(f):a.isRequired?c.add(f):h(a,c,b,function(a){return f+"."+a})})}var u=a.dependencies,q=a.declaredDependencies,t=a.optionalDependencies,c=a.externalDependencies,n=a.isEffect,C=b();u.forEach(function(a,h){e(C,h).isRequired=!0;f(C,h,function(a){a.hasRequiredNodesBelow=
!0})});q.forEach(function(a){e(C,a.key).isSatisfiedRecursively=!0});t.forEach(function(a){e(C,a).isSatisfiedRecursively=!0});a=new Set;var g=new Set;h(C,a,g,function(a){return a});var v=[],A=new Set,B=new Set;q.forEach(function(a){a=a.key;g.has(a)?-1===v.indexOf(a)?v.push(a):B.add(a):!n||a.endsWith(".current")||c.has(a)?A.add(a):-1===v.indexOf(a)&&v.push(a)});a.forEach(function(a){v.push(a)});return{suggestedDependencies:v,unnecessaryDependencies:A,duplicateDependencies:B,missingDependencies:a}}
function da(a){var b=a.declaredDependenciesNode,e=a.componentScope,f=a.scope;return a.declaredDependencies.map(function(a){a=e.set.get(a.key);if(null==a)return null;var b=a.defs[0];return null==b?null:"Variable"===b.type&&"VariableDeclarator"===b.node.type&&null!=b.node.init&&("ArrowFunctionExpression"===b.node.init.type||"FunctionExpression"===b.node.init.type)||"FunctionName"===b.type&&"FunctionDeclaration"===b.node.type?a:null}).filter(Boolean).map(function(a){var e=a.defs[0];a:{for(var h=!1,t=
0;t<a.references.length;t++){var c=a.references[t];if(c.writeExpr)if(h){a=!0;break a}else{h=!0;continue}for(var n=c.from;n!==f&&null!=n;)n=n.upper;if(n!==f&&!ea(b,c.identifier)){a=!0;break a}}a=!1}return{fn:e,suggestUseCallback:a}})}function fa(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:fa(a.parent)}
function V(a){function b(){return{isRequired:!1,isSatisfiedRecursively:!1,hasRequiredNodesBelow:!1,children:new Map}}function e(a,d){d=d.split(".");var m=!0,k=!1,e=void 0;try{for(var h=d[Symbol.iterator](),p;!(m=(p=h.next()).done);m=!0){var z=p.value,f=a.children.get(z);f||(f=b(),a.children.set(z,f));a=f}}catch(D){k=!0,e=D}finally{try{m||null==h.return||h.return()}finally{if(k)throw e;}}return a}function h(a,d,b){d=d.split(".");var m=!0,k=!1,e=void 0;try{for(var h=d[Symbol.iterator](),f;!(m=(f=h.next()).done);m=
!0){var z=a.children.get(f.value);if(!z)break;b(z);a=z}}catch(D){k=!0,e=D}finally{try{m||null==h.return||h.return()}finally{if(k)throw e;}}}function d(a,b,k,h){a.children.forEach(function(a,m){var e=h(m);a.isSatisfiedRecursively?a.hasRequiredNodesBelow&&k.add(e):a.isRequired?b.add(e):d(a,b,k,function(a){return e+"."+a})})}var z=a.dependencies,l=a.declaredDependencies,v=a.optionalDependencies,k=a.externalDependencies,B=a.isEffect,E=b();z.forEach(function(a,d){e(E,d).isRequired=!0;h(E,d,function(a){a.hasRequiredNodesBelow=
!0})});l.forEach(function(a){e(E,a.key).isSatisfiedRecursively=!0});v.forEach(function(a){e(E,a).isSatisfiedRecursively=!0});a=new Set;var f=new Set;d(E,a,f,function(a){return a});var q=[],x=new Set,A=new Set;l.forEach(function(a){a=a.key;f.has(a)?-1===q.indexOf(a)?q.push(a):A.add(a):!B||a.endsWith(".current")||k.has(a)?x.add(a):-1===q.indexOf(a)&&q.push(a)});a.forEach(function(a){q.push(a)});return{suggestedDependencies:q,unnecessaryDependencies:x,duplicateDependencies:A,missingDependencies:a}}
function da(a){var b=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,v=
0;v<a.references.length;v++){var k=a.references[v];if(k.writeExpr)if(e){a=!0;break a}else{e=!0;continue}for(var B=k.from;B!==h&&null!=B;)B=B.upper;if(B!==h&&!ea(b,k.identifier)){a=!0;break a}}a=!1}return{fn:d,suggestUseCallback:a}})}function fa(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:fa(a.parent)}
function X(a){if("Identifier"===a.type)return a.name;if("MemberExpression"!==a.type||a.computed)throw Error("Unsupported node type: "+a.type);var b=X(a.object);a=X(a.property);return b+"."+a}function ja(a){return"MemberExpression"!==a.type||"Identifier"!==a.object.type||"React"!==a.object.name||"Identifier"!==a.property.type||a.computed?a:a.property}
function ka(a,b){var e=ja(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&&b&&b.additionalHooks){a=void 0;try{a=X(e)}catch(f){if(/Unsupported node type/.test(f.message))return 0;throw f;}return b.additionalHooks.test(a)?0:-1}return-1}}
function Y(a,b){for(var e=[a],f=null;e.length;){f=e.shift();if("Identifier"===f.type&&f.name===b.name&&f.range[0]===b.range[0]&&f.range[1]===b.range[1])return f;if(ea(f,b)){a=!0;var h=!1,u=void 0;try{for(var q=Object.entries(f)[Symbol.iterator](),t;!(a=(t=q.next()).done);a=!0){var c=t.value,n=c[1];"parent"!==c[0]&&(la(n)?(n.parent=f,e.push(n)):Array.isArray(n)&&n.forEach(function(a){la(a)&&(a.parent=f,e.push(a))}))}}catch(C){h=!0,u=C}finally{try{!a&&q.return&&q.return()}finally{if(h)throw u;}}}}return null}
function ka(a,b){var e=ja(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&&b&&b.additionalHooks){try{var h=X(e)}catch(d){if(/Unsupported node type/.test(d.message))return 0;throw d;}return b.additionalHooks.test(h)?0:-1}return-1}}
function Y(a,b){for(var e=[a],h=null;e.length;){h=e.shift();if("Identifier"===h.type&&h.name===b.name&&h.range[0]===b.range[0]&&h.range[1]===b.range[1])return h;if(ea(h,b)){a=0;for(var d=Object.entries(h);a<d.length;a++){var z=d[a],l=z[1];"parent"!==z[0]&&(la(l)?(l.parent=h,e.push(l)):Array.isArray(l)&&l.forEach(function(a){la(a)&&(a.parent=h,e.push(a))}))}}}return null}
function ma(a){for(var b="",e=0;e<a.length;e++)b+=a[e],0===e&&2===a.length?b+=" and ":e===a.length-2&&2<a.length?b+=", and ":e<a.length-1&&(b+=", ");return b}function la(a){return"object"===typeof a&&null!==a&&!Array.isArray(a)&&"string"===typeof a.type}function ea(a,b){return a.range[0]<=b.range[0]&&a.range[1]>=b.range[1]}
module.exports={rules:{"rules-of-hooks":{create:function(a){var b=[],e=[];return{onCodePathSegmentStart:function(a){return e.push(a)},onCodePathSegmentEnd:function(){return e.pop()},onCodePathStart:function(){return b.push(new Map)},onCodePathEnd:function(e,h){function f(a){var b=f.cache,c=b.get(a.id);if(null===c){if(!n.has(a.id)){n.add(a.id);b=!0;c=!1;var d=void 0;try{for(var m=a.prevSegments[Symbol.iterator](),r;!(b=(r=m.next()).done);b=!0)f(r.value)}catch(K){c=!0,d=K}finally{try{!b&&m.return&&
m.return()}finally{if(c)throw d;}}}return 0}if(void 0!==c)return c;b.set(a.id,null);if(e.thrownSegments.includes(a))c=0;else if(0===a.prevSegments.length)c=1;else{c=0;m=!0;r=!1;var H=void 0;try{d=a.prevSegments[Symbol.iterator]();for(var k;!(m=(k=d.next()).done);m=!0)c+=f(k.value)}catch(K){r=!0,H=K}finally{try{!m&&d.return&&d.return()}finally{if(r)throw H;}}}a.reachable&&0===c?b.delete(a.id):b.set(a.id,c);return c}function q(a){var b=q.cache,c=b.get(a.id);if(null===c){if(!n.has(a.id)){n.add(a.id);
b=!0;c=!1;var d=void 0;try{for(var m=a.nextSegments[Symbol.iterator](),r;!(b=(r=m.next()).done);b=!0)q(r.value)}catch(K){c=!0,d=K}finally{try{!b&&m.return&&m.return()}finally{if(c)throw d;}}}return 0}if(void 0!==c)return c;b.set(a.id,null);if(e.thrownSegments.includes(a))c=0;else if(0===a.nextSegments.length)c=1;else{c=0;m=!0;r=!1;var H=void 0;try{d=a.nextSegments[Symbol.iterator]();for(var f;!(m=(f=d.next()).done);m=!0)c+=q(f.value)}catch(K){r=!0,H=K}finally{try{!m&&d.return&&d.return()}finally{if(r)throw H;
}}}b.set(a.id,c);return c}function t(a){var c=t.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 d=!0,m=!1,r=void 0;try{for(var H=a.prevSegments[Symbol.iterator](),e;!(d=(e=H.next()).done);d=!0){var f=t(e.value);f<b&&(b=f)}}catch(na){m=!0,r=na}finally{try{!d&&H.return&&H.return()}finally{if(m)throw r;}}b+=1}c.set(a.id,b);return b}var c=b.pop();if(0!==c.size){var n=new Set;f.cache=new Map;q.cache=new Map;t.cache=
new Map;var C=q(e.initialSegment),g=ca(h),v=ba(h),A=g?aa(g)||R(g):!1,B=Infinity,l=!0,p=!1,G=void 0;try{for(var w=e.finalSegments[Symbol.iterator](),J;!(l=(J=w.next()).done);l=!0){var W=J.value;if(W.reachable){var Q=t(W);Q<B&&(B=Q)}}}catch(k){p=!0,G=k}finally{try{!l&&w.return&&w.return()}finally{if(p)throw G;}}l=!0;p=!1;G=void 0;try{for(var z=c[Symbol.iterator](),O;!(l=(O=z.next()).done);l=!0){var E=O.value,D=E[0],L=E[1];if(D.reachable){var I=0===D.nextSegments.length?B<=t(D):B<t(D),T=f(D)*q(D),F=
n.has(D.id);c=!0;w=!1;J=void 0;try{for(var x=L[Symbol.iterator](),M;!(c=(M=x.next()).done);c=!0){var y=M.value;F&&a.report({node:y,message:'React Hook "'+a.getSource(y)+'" 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(!F&&T!==C){var U='React Hook "'+a.getSource(y)+'" is called conditionally. React Hooks must be called in the exact same order in every component render.'+(I?" Did you accidentally call a React Hook after an early return?":
"");a.report({node:y,message:U})}}else if(!h.parent||"MethodDefinition"!==h.parent.type&&"ClassProperty"!==h.parent.type||h.parent.value!==h)if(g){var N='React Hook "'+a.getSource(y)+'" is called in '+('function "'+a.getSource(g)+'" ')+"which is neither a React function component or a custom React Hook function.";a.report({node:y,message:N})}else if("Program"!==h.type&&v){var P='React Hook "'+a.getSource(y)+'" 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:y,message:P})}}}catch(k){w=!0,J=k}finally{try{!c&&x.return&&x.return()}finally{if(w)throw J;}}}}}catch(k){p=!0,G=k}finally{try{!l&&z.return&&z.return()}finally{if(p)throw G;}}}},CallExpression:function(a){if(R(a.callee)){var f=b[b.length-1],u=e[e.length-1],q=f.get(u);q||(q=[],f.set(u,q));q.push(a.callee)}}}}},"exhaustive-deps":{meta:{fixable:"code",schema:[{type:"object",additionalProperties:!1,properties:{additionalHooks:{type:"string"}}}]},create:function(a){function b(a,b){return function(c){if(b.has(c))return b.get(c);
var e=a(c);b.set(c,e);return e}}function e(c){function e(a){var d=!0,r=!1,b=void 0;try{for(var f=a.references[Symbol.iterator](),h;!(d=(h=f.next()).done);d=!0){var g=h.value;if(g.resolved&&G.has(g.resolved.scope)){var l=Y(c,g.identifier),k=fa(l),p=X(k),q;if(q=B&&"Identifier"===k.type&&"MemberExpression"===k.parent.type&&!k.parent.computed&&"Identifier"===k.parent.property.type&&"current"===k.parent.property.name){for(var n=g.from,u=!1;n.block!==c;)"function"===n.type&&(u=null!=n.block.parent&&"ReturnStatement"===
n.block.parent.type),n=n.upper;q=u}q&&Q.set(p,{reference:g,dependencyNode:k});var t=g.resolved.defs[0];if(null==t||null==t.node||t.node.init!==c.parent)if(z.has(p))z.get(p).references.push(g);else{var v=g.resolved,y=J(v)||W(v);z.set(p,{isStatic:y,references:[g]})}}}}catch(Z){r=!0,b=Z}finally{try{!d&&f.return&&f.return()}finally{if(r)throw b;}}d=!0;r=!1;b=void 0;try{for(var w=a.childScopes[Symbol.iterator](),x;!(d=(x=w.next()).done);d=!0)e(x.value)}catch(Z){r=!0,b=Z}finally{try{!d&&w.return&&w.return()}finally{if(r)throw b;
}}}function C(a,b,r,c){return 0===a.size?null:(1<a.size?"":b+" ")+r+" "+(1<a.size?"dependencies":"dependency")+": "+ma(Array.from(a).sort().map(function(a){return"'"+a+"'"}))+(". Either "+c+" "+(1<a.size?"them":"it")+" or remove the dependency array.")}if(("FunctionExpression"===c.type||"ArrowFunctionExpression"===c.type)&&"CallExpression"===c.parent.type){var g=ka(c.parent.callee,f);if(c.parent.arguments[g]===c){var v=c.parent.callee,A=ja(v).name,B=A.endsWith("Effect"),l=c.parent.arguments[g+1];
if(l||B){B&&c.async&&a.report({node:c,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 p=a.getScope(),G=new Set,w=null;for(g=p.upper;g;){G.add(g);
if("function"===g.type)break;g=g.upper}if(g){w=g;var J=b(function(a){if(!Array.isArray(a.defs))return!1;var d=a.defs[0];if(null==d||"VariableDeclarator"!==d.node.type)return!1;var b=d.node.init;if(null==b)return!1;var c=d.node.parent;if(null==c&&(Y(w.block,d.node.id),c=d.node.parent,null==c))return!1;if("const"===c.kind&&"Literal"===b.type&&("string"===typeof b.value||"number"===typeof b.value||null===b.value))return!0;if("CallExpression"!==b.type)return!1;b=b.callee;"MemberExpression"!==b.type||
"React"!==b.object.name||null==b.property||b.computed||(b=b.property);if("Identifier"!==b.type)return!1;d=d.node.id;b=b.name;if("useRef"===b&&"Identifier"===d.type)return!0;if(("useState"===b||"useReducer"===b)&&"ArrayPattern"===d.type&&2===d.elements.length&&Array.isArray(a.identifiers)){if(d.elements[1]===a.identifiers[0]){if("useState"===b)for(a=a.references,b=0;b<a.length;b++)h.set(a[b].identifier,d.elements[0]);return!0}if(d.elements[0]===a.identifiers[0]&&"useState"===b)for(a=a.references,d=
0;d<a.length;d++)u.add(a[d].identifier)}return!1},q),W=b(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,d=w.childScopes;a=null;var c;for(c=0;c<d.length;c++){var e=d[c],f=e.block;if("FunctionDeclaration"===b.type&&f===b||"VariableDeclarator"===b.type&&f.parent===b){a=e;break}}if(null==a)return!1;for(c=0;c<a.through.length;c++)if(b=a.through[c],null!=b.resolved&&G.has(b.resolved.scope)&&!J(b.resolved))return!1;return!0},t),
Q=new Map,z=new Map;e(p);Q.forEach(function(b,c){var d=b.dependencyNode;b=b.reference.resolved.references;for(var e=!1,f=0;f<b.length;f++){var m=b[f].identifier.parent;if(null!=m&&"MemberExpression"===m.type&&!m.computed&&"Identifier"===m.property.type&&"current"===m.property.name&&"AssignmentExpression"===m.parent.type&&m.parent.left===m){e=!0;break}}e||a.report({node:d.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 "+
("'"+c+".current' to a variable inside the effect, and ")+"use that variable in the cleanup function."})});var O=new Set,E=new Set;z.forEach(function(b,c){var d=b.references;b.isStatic&&E.add(c);d.forEach(function(b){b.writeExpr&&(b=b.writeExpr,O.has(c)||(O.add(c),a.report({node:b,message:"Assignments to the '"+c+"' variable from inside React Hook "+(a.getSource(v)+" 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 "+
(a.getSource(v)+".")})))})});if(!(0<O.size))if(l){var D=[],L=new Set;"ArrayExpression"!==l.type?a.report({node:l,message:"React Hook "+a.getSource(v)+" 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(b){if(null!==b)if("SpreadElement"===b.type)a.report({node:b,message:"React Hook "+a.getSource(v)+" has a spread element in its dependency array. This means we can't statically verify whether you've passed the correct dependencies."});
else{var c=void 0;try{c=X(b)}catch(ia){if(/Unsupported node type/.test(ia.message)){"Literal"===b.type?z.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(v)+" has a complex expression in the dependency array. Extract it to a separate variable so it can be statically checked."});
return}throw ia;}for(var d=b;"MemberExpression"===d.type;)d=d.object;var e=!w.through.some(function(a){return a.identifier===d});D.push({key:c,node:b});e||L.add(c)}});var I=V({dependencies:z,declaredDependencies:D,optionalDependencies:E,externalDependencies:L,isEffect:B}),T=I.suggestedDependencies;g=I.unnecessaryDependencies;var F=I.missingDependencies;I=I.duplicateDependencies;if(0===I.size+F.size+g.size)da({declaredDependencies:D,declaredDependenciesNode:l,componentScope:w,scope:p}).forEach(function(b){var c=
b.fn,d=b.suggestUseCallback;b="The '"+c.name.name+"' function makes the dependencies of "+(A+" Hook (at line "+l.loc.start.line+") ")+"change on every render.";b=d?b+(" To fix this, "+("wrap the '"+c.name.name+"' definition into its own useCallback() Hook.")):b+(" Move it inside the "+A+" callback. "+("Alternatively, wrap the '"+c.name.name+"' definition into its own useCallback() Hook."));a.report({node:c.node,message:b,fix:function(a){if(d&&"Variable"===c.type)return[a.insertTextBefore(c.node.init,
"useCallback("),a.insertTextAfter(c.node.init,")")]}})});else{!B&&0<F.size&&(T=V({dependencies:z,declaredDependencies:[],optionalDependencies:E,externalDependencies:L,isEffect:B}).suggestedDependencies);(function(){if(0===D.length)return!0;var a=D.map(function(a){return a.key}),b=a.slice().sort();return a.join(",")===b.join(",")})()&&T.sort();var x="";if(0<g.size){var M=null;Array.from(g.keys()).forEach(function(a){null===M&&a.endsWith(".current")&&(M=a)});if(null!==M)x=" Mutable values like '"+M+
"' aren't valid dependencies because mutating them doesn't re-render the component.";else if(0<L.size){var y=Array.from(L)[0];p.set.has(y)||(x=" Outer scope values like '"+y+"' aren't valid dependencies because mutating them doesn't re-render the component.")}}if(!x&&F.has("props")){p=z.get("props");if(null==p)return;p=p.references;if(!Array.isArray(p))return;y=!0;for(var U=0;U<p.length;U++){var N=Y(w.block,p[U].identifier);if(!N){y=!1;break}N=N.parent;if(null==N){y=!1;break}if("MemberExpression"!==
N.type){y=!1;break}}y&&(x=" 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(v)+"."))}if(!x&&0<F.size){var P=null;F.forEach(function(a){if(!P){var b=w.set.get(a),c=z.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,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&&(P=a)}}});null!==P&&(x=" If '"+P+"' changes too often, find the parent component that defines it and wrap that definition in useCallback.")}if(!x&&0<F.size){var k=null;F.forEach(function(a){if(null===k)for(var b=z.get(a).references,c,d,e=0;e<b.length;e++){c=b[e].identifier;for(d=c.parent;null!=d&&d!==w.block;){if("CallExpression"===d.type){var f=h.get(d.callee);if(null!=f){f.name===a?k={missingDep:a,setter:d.callee.name,
form:"updater"}:u.has(c)?k={missingDep:a,setter:d.callee.name,form:"reducer"}:(c=b[e].resolved,null!=c&&(c=c.defs[0],null!=c&&"Parameter"===c.type&&(k={missingDep:a,setter:d.callee.name,form:"inlineReducer"})));break}}d=d.parent}if(null!==k)break}});if(null!==k)switch(k.form){case "reducer":x=" You can also replace multiple useState variables with useReducer "+("if '"+k.setter+"' needs the ")+("current value of '"+k.missingDep+"'.");break;case "inlineReducer":x=" If '"+k.setter+"' needs the "+("current value of '"+
k.missingDep+"', ")+"you can also switch to useReducer instead of useState and "+("read '"+k.missingDep+"' in the reducer.");break;case "updater":x=" You can also do a functional update '"+k.setter+"("+k.missingDep.substring(0,1)+" => ...)' if you only need '"+k.missingDep+"'"+(" in the '"+k.setter+"' call.");break;default:throw Error("Unknown case.");}}a.report({node:l,message:"React Hook "+a.getSource(v)+" has "+(C(F,"a","missing","include")||C(g,"an","unnecessary","exclude")||C(I,"a","duplicate",
"omit"))+x,fix:function(a){return a.replaceText(l,"["+T.join(", ")+"]")}})}}else{var S=null;z.forEach(function(a,b){S||a.references.forEach(function(a){if(!S&&h.has(a.identifier)){for(a=a.from;"function"!==a.type;)a=a.upper;a.block===c&&(S=b)}})});if(S){var ha=V({dependencies:z,declaredDependencies:[],optionalDependencies:E,externalDependencies:new Set,isEffect:!0}).suggestedDependencies;a.report({node:c.parent.callee,message:"React Hook "+A+" contains a call to '"+S+"'. Without a list of dependencies, this can lead to an infinite chain of updates. To fix this, pass ["+
ha.join(", ")+("] as a second argument to the "+A+" Hook."),fix:function(a){return a.insertTextAfter(c,", ["+ha.join(", ")+"]")}})}}}}else"useMemo"!==A&&"useCallback"!==A||a.report({node:c.parent.callee,message:"React Hook "+A+" does nothing when called with only one argument. Did you forget to pass an array of dependencies?"})}}}var f={additionalHooks:a.options&&a.options[0]&&a.options[0].additionalHooks?new RegExp(a.options[0].additionalHooks):void 0},h=new WeakMap,u=new WeakSet,q=new WeakMap,t=
module.exports={rules:{"rules-of-hooks":{create:function(a){var b=[],e=[];return{onCodePathSegmentStart:function(a){return e.push(a)},onCodePathSegmentEnd:function(){return e.pop()},onCodePathStart:function(){return b.push(new Map)},onCodePathEnd:function(e,d){function h(a){var d=h.cache,b=d.get(a.id);if(null===b){if(!B.has(a.id)){B.add(a.id);d=!0;b=!1;var c=void 0;try{for(var r=a.prevSegments[Symbol.iterator](),g;!(d=(g=r.next()).done);d=!0)h(g.value)}catch(K){b=!0,c=K}finally{try{d||null==r.return||
r.return()}finally{if(b)throw c;}}}return 0}if(void 0!==b)return b;d.set(a.id,null);if(e.thrownSegments.includes(a))b=0;else if(0===a.prevSegments.length)b=1;else{b=0;r=!0;g=!1;var H=void 0;try{c=a.prevSegments[Symbol.iterator]();for(var k;!(r=(k=c.next()).done);r=!0)b+=h(k.value)}catch(K){g=!0,H=K}finally{try{r||null==c.return||c.return()}finally{if(g)throw H;}}}a.reachable&&0===b?d.delete(a.id):d.set(a.id,b);return b}function l(a){var b=l.cache,d=b.get(a.id);if(null===d){if(!B.has(a.id)){B.add(a.id);
b=!0;d=!1;var c=void 0;try{for(var r=a.nextSegments[Symbol.iterator](),g;!(b=(g=r.next()).done);b=!0)l(g.value)}catch(K){d=!0,c=K}finally{try{b||null==r.return||r.return()}finally{if(d)throw c;}}}return 0}if(void 0!==d)return d;b.set(a.id,null);if(e.thrownSegments.includes(a))d=0;else if(0===a.nextSegments.length)d=1;else{d=0;r=!0;g=!1;var H=void 0;try{c=a.nextSegments[Symbol.iterator]();for(var k;!(r=(k=c.next()).done);r=!0)d+=l(k.value)}catch(K){g=!0,H=K}finally{try{r||null==c.return||c.return()}finally{if(g)throw H;
}}}b.set(a.id,d);return d}function v(a){var d=v.cache,b=d.get(a.id);if(null===b)return Infinity;if(void 0!==b)return b;d.set(a.id,null);if(0===a.prevSegments.length)b=1;else{b=Infinity;var c=!0,r=!1,g=void 0;try{for(var H=a.prevSegments[Symbol.iterator](),e;!(c=(e=H.next()).done);c=!0){var k=v(e.value);k<b&&(b=k)}}catch(na){r=!0,g=na}finally{try{c||null==H.return||H.return()}finally{if(r)throw g;}}b+=1}d.set(a.id,b);return b}var k=b.pop();if(0!==k.size){var B=new Set;h.cache=new Map;l.cache=new Map;
v.cache=new Map;var E=l(e.initialSegment),f=ca(d),q=ba(d),x=f?aa(f)||R(f):!1,A=Infinity,m=!0,p=!1,G=void 0;try{for(var t=e.finalSegments[Symbol.iterator](),J;!(m=(J=t.next()).done);m=!0){var W=J.value;if(W.reachable){var Q=v(W);Q<A&&(A=Q)}}}catch(n){p=!0,G=n}finally{try{m||null==t.return||t.return()}finally{if(p)throw G;}}m=!0;p=!1;G=void 0;try{for(var y=k[Symbol.iterator](),O;!(m=(O=y.next()).done);m=!0){var D=O.value,C=D[0],L=D[1];if(C.reachable){var I=0===C.nextSegments.length?A<=v(C):A<v(C),T=
h(C)*l(C),F=B.has(C.id);k=!0;t=!1;J=void 0;try{for(var w=L[Symbol.iterator](),M;!(k=(M=w.next()).done);k=!0){var u=M.value;F&&a.report({node:u,message:'React Hook "'+a.getSource(u)+'" 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(!F&&T!==E){var U='React Hook "'+a.getSource(u)+'" is called conditionally. React Hooks must be called in the exact same order in every component render.'+
(I?" Did you accidentally call a React Hook after an early return?":"");a.report({node:u,message:U})}}else if(!d.parent||"MethodDefinition"!==d.parent.type&&"ClassProperty"!==d.parent.type||d.parent.value!==d)if(f){var N='React Hook "'+a.getSource(u)+'" is called in '+('function "'+a.getSource(f)+'" ')+"which is neither a React function component or a custom React Hook function.";a.report({node:u,message:N})}else if("Program"!==d.type&&q){var P='React Hook "'+a.getSource(u)+'" 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:u,message:P})}}}catch(n){t=!0,J=n}finally{try{k||null==w.return||w.return()}finally{if(t)throw J;}}}}}catch(n){p=!0,G=n}finally{try{m||null==y.return||y.return()}finally{if(p)throw G;}}}},CallExpression:function(a){if(R(a.callee)){var d=b[b.length-1],h=e[e.length-1],l=d.get(h);l||(l=[],d.set(h,l));l.push(a.callee)}}}}},"exhaustive-deps":{meta:{fixable:"code",schema:[{type:"object",additionalProperties:!1,properties:{additionalHooks:{type:"string"}}}]},create:function(a){function b(a,
b){return function(d){if(b.has(d))return b.get(d);var e=a(d);b.set(d,e);return e}}function e(e){function k(a){var c=!0,g=!1,b=void 0;try{for(var d=a.references[Symbol.iterator](),h;!(c=(h=d.next()).done);c=!0){var f=h.value;if(f.resolved&&G.has(f.resolved.scope)){var m=Y(e,f.identifier),l=fa(m),n=X(l),p;if(p=A&&"Identifier"===l.type&&"MemberExpression"===l.parent.type&&!l.parent.computed&&"Identifier"===l.parent.property.type&&"current"===l.parent.property.name){for(var q=f.from,u=!1;q.block!==e;)"function"===
q.type&&(u=null!=q.block.parent&&"ReturnStatement"===q.block.parent.type),q=q.upper;p=u}p&&Q.set(n,{reference:f,dependencyNode:l});var t=f.resolved.defs[0];if((null==t||null==t.node||t.node.init!==e.parent)&&"TypeParameter"!==t.type)if(y.has(n))y.get(n).references.push(f);else{var w=f.resolved,z=J(w)||W(w);y.set(n,{isStatic:z,references:[f]})}}}}catch(Z){g=!0,b=Z}finally{try{c||null==d.return||d.return()}finally{if(g)throw b;}}c=!0;g=!1;b=void 0;try{for(var v=a.childScopes[Symbol.iterator](),x;!(c=
(x=v.next()).done);c=!0)k(x.value)}catch(Z){g=!0,b=Z}finally{try{c||null==v.return||v.return()}finally{if(g)throw b;}}}function E(a,r,g,b){return 0===a.size?null:(1<a.size?"":r+" ")+g+" "+(1<a.size?"dependencies":"dependency")+": "+ma(Array.from(a).sort().map(function(a){return"'"+a+"'"}))+(". Either "+b+" "+(1<a.size?"them":"it")+" or remove the dependency array.")}if(("FunctionExpression"===e.type||"ArrowFunctionExpression"===e.type)&&"CallExpression"===e.parent.type){var f=ka(e.parent.callee,h);
if(e.parent.arguments[f]===e){var q=e.parent.callee,x=ja(q).name,A=x.endsWith("Effect"),m=e.parent.arguments[f+1];if(m||A){A&&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 p=a.getScope(),G=new Set,t=null;for(f=p.upper;f;){G.add(f);if("function"===f.type)break;f=f.upper}if(f){t=f;var J=b(function(a){if(!Array.isArray(a.defs))return!1;var c=a.defs[0];if(null==c||"VariableDeclarator"!==c.node.type)return!1;var g=c.node.init;if(null==g)return!1;var b=c.node.parent;if(null==b&&(Y(t.block,c.node.id),b=c.node.parent,null==b))return!1;if("const"===b.kind&&"Literal"===g.type&&("string"===typeof g.value||"number"===typeof g.value||null===g.value))return!0;if("CallExpression"!==
g.type)return!1;g=g.callee;"MemberExpression"!==g.type||"React"!==g.object.name||null==g.property||g.computed||(g=g.property);if("Identifier"!==g.type)return!1;c=c.node.id;g=g.name;if("useRef"===g&&"Identifier"===c.type)return!0;if(("useState"===g||"useReducer"===g)&&"ArrayPattern"===c.type&&2===c.elements.length&&Array.isArray(a.identifiers)){if(c.elements[1]===a.identifiers[0]){if("useState"===g)for(a=a.references,g=0;g<a.length;g++)d.set(a[g].identifier,c.elements[0]);return!0}if(c.elements[0]===
a.identifiers[0]&&"useState"===g)for(a=a.references,c=0;c<a.length;c++)z.add(a[c].identifier)}return!1},l),W=b(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 c=a.node,b=t.childScopes;a=null;var d;for(d=0;d<b.length;d++){var e=b[d],f=e.block;if("FunctionDeclaration"===c.type&&f===c||"VariableDeclarator"===c.type&&f.parent===c){a=e;break}}if(null==a)return!1;for(d=0;d<a.through.length;d++)if(c=a.through[d],null!=c.resolved&&G.has(c.resolved.scope)&&
!J(c.resolved))return!1;return!0},v),Q=new Map,y=new Map;k(p);Q.forEach(function(c,b){var d=c.dependencyNode;c=c.reference.resolved.references;for(var e=!1,r=0;r<c.length;r++){var f=c[r].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){e=!0;break}}e||a.report({node:d.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 "+
("'"+b+".current' to a variable inside the effect, and ")+"use that variable in the cleanup function."})});var O=new Set,D=new Set;y.forEach(function(c,b){var d=c.references;c.isStatic&&D.add(b);d.forEach(function(c){c.writeExpr&&(c=c.writeExpr,O.has(b)||(O.add(b),a.report({node:c,message:"Assignments to the '"+b+"' variable from inside React Hook "+(a.getSource(q)+" 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 "+
(a.getSource(q)+".")})))})});if(!(0<O.size))if(m){var C=[],L=new Set;"ArrayExpression"!==m.type?a.report({node:m,message:"React Hook "+a.getSource(q)+" 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(c){if(null!==c)if("SpreadElement"===c.type)a.report({node:c,message:"React Hook "+a.getSource(q)+" has a spread element in its dependency array. This means we can't statically verify whether you've passed the correct dependencies."});
else{try{var b=X(c)}catch(ia){if(/Unsupported node type/.test(ia.message)){"Literal"===c.type?y.has(c.value)?a.report({node:c,message:"The "+c.raw+" literal is not a valid dependency because it never changes. "+("Did you mean to include "+c.value+" in the array instead?")}):a.report({node:c,message:"The "+c.raw+" literal is not a valid dependency because it never changes. You can safely remove it."}):a.report({node:c,message:"React Hook "+a.getSource(q)+" has a complex expression in the dependency array. Extract it to a separate variable so it can be statically checked."});
return}throw ia;}for(var d=c;"MemberExpression"===d.type;)d=d.object;var e=!t.through.some(function(a){return a.identifier===d});C.push({key:b,node:c});e||L.add(b)}});var I=V({dependencies:y,declaredDependencies:C,optionalDependencies:D,externalDependencies:L,isEffect:A}),T=I.suggestedDependencies;f=I.unnecessaryDependencies;var F=I.missingDependencies;I=I.duplicateDependencies;if(0===I.size+F.size+f.size)da({declaredDependencies:C,declaredDependenciesNode:m,componentScope:t,scope:p}).forEach(function(c){var b=
c.fn,d=c.suggestUseCallback;c="The '"+b.name.name+"' function makes the dependencies of "+(x+" Hook (at line "+m.loc.start.line+") ")+"change on every render.";c=d?c+(" To fix this, "+("wrap the '"+b.name.name+"' definition into its own useCallback() Hook.")):c+(" Move it inside the "+x+" callback. "+("Alternatively, wrap the '"+b.name.name+"' definition into its own useCallback() Hook."));a.report({node:b.node,message:c,fix:function(a){if(d&&"Variable"===b.type)return[a.insertTextBefore(b.node.init,
"useCallback("),a.insertTextAfter(b.node.init,")")]}})});else{!A&&0<F.size&&(T=V({dependencies:y,declaredDependencies:[],optionalDependencies:D,externalDependencies:L,isEffect:A}).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(",")})()&&T.sort();var w="";if(0<f.size){var M=null;Array.from(f.keys()).forEach(function(a){null===M&&a.endsWith(".current")&&(M=a)});if(null!==M)w=" Mutable values like '"+M+
"' aren't valid dependencies because mutating them doesn't re-render the component.";else if(0<L.size){var u=Array.from(L)[0];p.set.has(u)||(w=" Outer scope values like '"+u+"' aren't valid dependencies because mutating them doesn't re-render the component.")}}if(!w&&F.has("props")){p=y.get("props");if(null==p)return;p=p.references;if(!Array.isArray(p))return;u=!0;for(var U=0;U<p.length;U++){var N=Y(t.block,p[U].identifier);if(!N){u=!1;break}N=N.parent;if(null==N){u=!1;break}if("MemberExpression"!==
N.type){u=!1;break}}u&&(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(q)+"."))}if(!w&&0<F.size){var P=null;F.forEach(function(a){if(!P){var b=t.set.get(a),d=y.get(a);if(d.references[0].resolved===b&&(b=b.defs[0],null!=b&&null!=b.name&&"Parameter"===b.type)){b=!1;for(var c,e=0;e<d.references.length;e++)if(c=d.references[e].identifier,null!=
c&&null!=c.parent&&"CallExpression"===c.parent.type&&c.parent.callee===c){b=!0;break}b&&(P=a)}}});null!==P&&(w=" If '"+P+"' changes too often, find the parent component that defines it and wrap that definition in useCallback.")}if(!w&&0<F.size){var n=null;F.forEach(function(a){if(null===n)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!==t.block;){if("CallExpression"===e.type){var h=d.get(e.callee);if(null!=h){h.name===a?n={missingDep:a,setter:e.callee.name,
form:"updater"}:z.has(c)?n={missingDep:a,setter:e.callee.name,form:"reducer"}:(c=b[f].resolved,null!=c&&(c=c.defs[0],null!=c&&"Parameter"===c.type&&(n={missingDep:a,setter:e.callee.name,form:"inlineReducer"})));break}}e=e.parent}if(null!==n)break}});if(null!==n)switch(n.form){case "reducer":w=" You can also replace multiple useState variables with useReducer "+("if '"+n.setter+"' needs the ")+("current value of '"+n.missingDep+"'.");break;case "inlineReducer":w=" If '"+n.setter+"' needs the "+("current value of '"+
n.missingDep+"', ")+"you can also switch to useReducer instead of useState and "+("read '"+n.missingDep+"' in the reducer.");break;case "updater":w=" You can also do a functional update '"+n.setter+"("+n.missingDep.substring(0,1)+" => ...)' if you only need '"+n.missingDep+"'"+(" in the '"+n.setter+"' call.");break;default:throw Error("Unknown case.");}}a.report({node:m,message:"React Hook "+a.getSource(q)+" has "+(E(F,"a","missing","include")||E(f,"an","unnecessary","exclude")||E(I,"a","duplicate",
"omit"))+w,fix:function(a){return a.replaceText(m,"["+T.join(", ")+"]")}})}}else{var S=null;y.forEach(function(a,b){S||a.references.forEach(function(a){if(!S&&d.has(a.identifier)){for(a=a.from;"function"!==a.type;)a=a.upper;a.block===e&&(S=b)}})});if(S){var ha=V({dependencies:y,declaredDependencies:[],optionalDependencies:D,externalDependencies:new Set,isEffect:!0}).suggestedDependencies;a.report({node:e.parent.callee,message:"React Hook "+x+" contains a call to '"+S+"'. Without a list of dependencies, this can lead to an infinite chain of updates. To fix this, pass ["+
ha.join(", ")+("] as a second argument to the "+x+" Hook."),fix:function(a){return a.insertTextAfter(e,", ["+ha.join(", ")+"]")}})}}}}else"useMemo"!==x&&"useCallback"!==x||a.report({node:e.parent.callee,message:"React Hook "+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},d=new WeakMap,z=new WeakSet,l=new WeakMap,v=
new WeakMap;return{FunctionExpression:e,ArrowFunctionExpression:e}}}}};
{
"name": "eslint-plugin-react-hooks",
"description": "ESLint rules for React Hooks",
"version": "0.0.0-6da04b5d8",
"version": "0.0.0-8d5403877",
"repository": {

@@ -32,4 +32,4 @@ "type": "git",

"peerDependencies": {
"eslint": "^3.0.0 || ^4.0.0 || ^5.0.0"
"eslint": "^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0"
}
}

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc