@@ -41,28 +41,30 @@ export function useNoDirectSetStateInUseEffect<Ctx extends RuleContext>(
4141 const settings = getSettingsFromContext ( context ) ;
4242 const hooks = settings . additionalHooks ;
4343 const getText = ( n : TSESTree . Node ) => context . sourceCode . getText ( n ) ;
44+
4445 const isUseEffectLikeCall = ER . isReactHookCallWithNameAlias ( context , useEffectKind , hooks [ useEffectKind ] ) ;
4546 const isUseStateCall = ER . isReactHookCallWithNameAlias ( context , "useState" , hooks . useState ) ;
4647 const isUseMemoCall = ER . isReactHookCallWithNameAlias ( context , "useMemo" , hooks . useMemo ) ;
4748 const isUseCallbackCall = ER . isReactHookCallWithNameAlias ( context , "useCallback" , hooks . useCallback ) ;
4849
4950 const functionEntries : { kind : FunctionKind ; node : AST . TSESTreeFunction } [ ] = [ ] ;
50- const setupFunctionRef : { current : AST . TSESTreeFunction | null } = { current : null } ;
51- const setupFunctionIdentifiers : TSESTree . Identifier [ ] = [ ] ;
5251
53- const indFunctionCalls : TSESTree . CallExpression [ ] = [ ] ;
54- const indSetStateCalls = new WeakMap < AST . TSESTreeFunction , TSESTree . CallExpression [ ] > ( ) ;
55- const indSetStateCallsInUseEffectArg0 = new WeakMap < TSESTree . CallExpression , TSESTree . Identifier [ ] > ( ) ;
56- const indSetStateCallsInUseEffectSetup = new Map < TSESTree . CallExpression , TSESTree . Identifier [ ] > ( ) ;
57- const indSetStateCallsInUseMemoOrCallback = new WeakMap < TSESTree . Node , TSESTree . CallExpression [ ] > ( ) ;
52+ const setupFnRef : { current : AST . TSESTreeFunction | null } = { current : null } ;
53+ const setupFnIds : TSESTree . Identifier [ ] = [ ] ;
54+
55+ const trackedFnCalls : TSESTree . CallExpression [ ] = [ ] ;
56+ const setStateCallsByFn = new WeakMap < AST . TSESTreeFunction , TSESTree . CallExpression [ ] > ( ) ;
57+ const setStateInEffectArg = new WeakMap < TSESTree . CallExpression , TSESTree . Identifier [ ] > ( ) ;
58+ const setStateInEffectSetup = new Map < TSESTree . CallExpression , TSESTree . Identifier [ ] > ( ) ;
59+ const setStateInHookCallbacks = new WeakMap < TSESTree . Node , TSESTree . CallExpression [ ] > ( ) ;
5860
5961 const onSetupFunctionEnter = ( node : AST . TSESTreeFunction ) => {
60- setupFunctionRef . current = node ;
62+ setupFnRef . current = node ;
6163 } ;
6264
6365 const onSetupFunctionExit = ( node : AST . TSESTreeFunction ) => {
64- if ( setupFunctionRef . current === node ) {
65- setupFunctionRef . current = null ;
66+ if ( setupFnRef . current === node ) {
67+ setupFnRef . current = null ;
6668 }
6769 } ;
6870
@@ -189,7 +191,7 @@ export function useNoDirectSetStateInUseEffect<Ctx extends RuleContext>(
189191 functionEntries . pop ( ) ;
190192 } ,
191193 CallExpression ( node ) {
192- const setupFunction = setupFunctionRef . current ;
194+ const setupFunction = setupFnRef . current ;
193195 const pEntry = functionEntries . at ( - 1 ) ;
194196 if ( pEntry == null || pEntry . node . async ) {
195197 return ;
@@ -211,18 +213,18 @@ export function useNoDirectSetStateInUseEffect<Ctx extends RuleContext>(
211213 }
212214 default : {
213215 const vd = AST . findParentNode ( node , isVariableDeclaratorFromHookCall ) ;
214- if ( vd == null ) getOrElseUpdate ( indSetStateCalls , pEntry . node , ( ) => [ ] ) . push ( node ) ;
215- else getOrElseUpdate ( indSetStateCallsInUseMemoOrCallback , vd . init , ( ) => [ ] ) . push ( node ) ;
216+ if ( vd == null ) getOrElseUpdate ( setStateCallsByFn , pEntry . node , ( ) => [ ] ) . push ( node ) ;
217+ else getOrElseUpdate ( setStateInHookCallbacks , vd . init , ( ) => [ ] ) . push ( node ) ;
216218 }
217219 }
218220 } )
219221 . with ( useEffectKind , ( ) => {
220222 if ( AST . isFunction ( node . arguments . at ( 0 ) ) ) return ;
221- setupFunctionIdentifiers . push ( ...AST . getNestedIdentifiers ( node ) ) ;
223+ setupFnIds . push ( ...AST . getNestedIdentifiers ( node ) ) ;
222224 } )
223225 . with ( "other" , ( ) => {
224226 if ( pEntry . node !== setupFunction ) return ;
225- indFunctionCalls . push ( node ) ;
227+ trackedFnCalls . push ( node ) ;
226228 } )
227229 . otherwise ( constVoid ) ;
228230 } ,
@@ -247,7 +249,7 @@ export function useNoDirectSetStateInUseEffect<Ctx extends RuleContext>(
247249 }
248250 const vd = AST . findParentNode ( parent , isVariableDeclaratorFromHookCall ) ;
249251 if ( vd != null ) {
250- getOrElseUpdate ( indSetStateCallsInUseEffectArg0 , vd . init , ( ) => [ ] ) . push ( node ) ;
252+ getOrElseUpdate ( setStateInEffectArg , vd . init , ( ) => [ ] ) . push ( node ) ;
251253 }
252254 break ;
253255 }
@@ -261,14 +263,14 @@ export function useNoDirectSetStateInUseEffect<Ctx extends RuleContext>(
261263 if ( isUseCallbackCall ( node . parent ) ) {
262264 const vd = AST . findParentNode ( node . parent , isVariableDeclaratorFromHookCall ) ;
263265 if ( vd != null ) {
264- getOrElseUpdate ( indSetStateCallsInUseEffectArg0 , vd . init , ( ) => [ ] ) . push ( node ) ;
266+ getOrElseUpdate ( setStateInEffectArg , vd . init , ( ) => [ ] ) . push ( node ) ;
265267 }
266268 break ;
267269 }
268270 // const [state, setState] = useState();
269271 // useEffect(setState);
270272 if ( isUseEffectLikeCall ( node . parent ) ) {
271- getOrElseUpdate ( indSetStateCallsInUseEffectSetup , node . parent , ( ) => [ ] ) . push ( node ) ;
273+ getOrElseUpdate ( setStateInEffectSetup , node . parent , ( ) => [ ] ) . push ( node ) ;
272274 }
273275 }
274276 }
@@ -283,18 +285,18 @@ export function useNoDirectSetStateInUseEffect<Ctx extends RuleContext>(
283285 case T . ArrowFunctionExpression :
284286 case T . FunctionDeclaration :
285287 case T . FunctionExpression :
286- return indSetStateCalls . get ( node ) ?? [ ] ;
288+ return setStateCallsByFn . get ( node ) ?? [ ] ;
287289 case T . CallExpression :
288- return indSetStateCallsInUseMemoOrCallback . get ( node ) ?? indSetStateCallsInUseEffectArg0 . get ( node ) ?? [ ] ;
290+ return setStateInHookCallbacks . get ( node ) ?? setStateInEffectArg . get ( node ) ?? [ ] ;
289291 }
290292 return [ ] ;
291293 } ;
292- for ( const [ , calls ] of indSetStateCallsInUseEffectSetup ) {
294+ for ( const [ , calls ] of setStateInEffectSetup ) {
293295 for ( const call of calls ) {
294296 onViolation ( context , call , { name : call . name } ) ;
295297 }
296298 }
297- for ( const { callee } of indFunctionCalls ) {
299+ for ( const { callee } of trackedFnCalls ) {
298300 if ( ! ( "name" in callee ) ) {
299301 continue ;
300302 }
@@ -306,7 +308,7 @@ export function useNoDirectSetStateInUseEffect<Ctx extends RuleContext>(
306308 } ) ;
307309 }
308310 }
309- for ( const id of setupFunctionIdentifiers ) {
311+ for ( const id of setupFnIds ) {
310312 const setStateCalls = getSetStateCalls ( id . name , context . sourceCode . getScope ( id ) ) ;
311313 for ( const setStateCall of setStateCalls ) {
312314 onViolation ( context , setStateCall , {
0 commit comments