@@ -28,6 +28,16 @@ export const isReactFunctionalHOC = (node) =>
2828 node . id . type === "Identifier" &&
2929 node . id . name [ 0 ] . toUpperCase ( ) === node . id . name [ 0 ] ;
3030
31+ export const isCustomHook = ( node ) =>
32+ ( node . type === "FunctionDeclaration" ||
33+ ( node . type === "VariableDeclarator" &&
34+ node . init &&
35+ ( node . init . type === "ArrowFunctionExpression" ||
36+ node . init . type === "FunctionExpression" ) ) ) &&
37+ node . id . type === "Identifier" &&
38+ node . id . name . startsWith ( "use" ) &&
39+ node . id . name [ 3 ] === node . id . name [ 3 ] . toUpperCase ( ) ;
40+
3141export const isUseState = ( node ) =>
3242 node . type === "VariableDeclarator" &&
3343 node . init &&
@@ -114,29 +124,22 @@ export const isProp = (variable) =>
114124 variable . defs . some (
115125 ( def ) =>
116126 def . type === "Parameter" &&
117- isReactFunctionalComponent (
118- // TODO: Simplify
119- def . node . type === "ArrowFunctionExpression"
120- ? def . node . parent . type === "CallExpression"
121- ? def . node . parent . parent
122- : def . node . parent
123- : def . node ,
124- ) ,
127+ ( isReactFunctionalComponent ( getDeclNode ( def . node ) ) ||
128+ isCustomHook ( getDeclNode ( def . node ) ) ) ,
125129 ) ;
126130export const isHOCProp = ( variable ) =>
127131 variable . defs . some (
128132 ( def ) =>
129- def . type === "Parameter" &&
130- isReactFunctionalHOC (
131- // TODO: Simplify
132- def . node . type === "ArrowFunctionExpression"
133- ? def . node . parent . type === "CallExpression"
134- ? def . node . parent . parent
135- : def . node . parent
136- : def . node ,
137- ) ,
133+ def . type === "Parameter" && isReactFunctionalHOC ( getDeclNode ( def . node ) ) ,
138134 ) ;
139135
136+ const getDeclNode = ( node ) =>
137+ node . type === "ArrowFunctionExpression"
138+ ? node . parent . type === "CallExpression"
139+ ? node . parent . parent
140+ : node . parent
141+ : node ;
142+
140143export const getUseStateNode = ( context , ref ) => {
141144 return getUpstreamReactVariables ( context , ref . identifier )
142145 . find ( ( variable ) => isState ( variable ) )
0 commit comments