@@ -2,7 +2,7 @@ import type { TSESTreeFunction } from "@eslint-react/ast";
2
2
import { getNestedIdentifiers , is , isFunction , isIIFE , NodeType } from "@eslint-react/ast" ;
3
3
import { isReactHookCallWithNameLoose , isUseEffectCall , isUseStateCall } from "@eslint-react/core" ;
4
4
import { parseESLintSettings } from "@eslint-react/shared" ;
5
- import { Chunk , F , MutList , MutRef , O } from "@eslint-react/tools" ;
5
+ import { F , MutList , MutRef , O } from "@eslint-react/tools" ;
6
6
import { findVariable , getVariableNode } from "@eslint-react/var" ;
7
7
import type { ESLintUtils , TSESTree } from "@typescript-eslint/utils" ;
8
8
import { getStaticValue } from "@typescript-eslint/utils/ast-utils" ;
@@ -32,10 +32,11 @@ export default createRule<[], MessageID>({
32
32
} ,
33
33
name : RULE_NAME ,
34
34
create ( context ) {
35
- const settings = parseESLintSettings ( context . settings ) [ "react-x" ] ;
36
- const { useEffect : useEffectAlias = [ ] , useState : useStateAlias = [ ] } = settings ? .additionalHooks ?? { } ;
35
+ const settings = parseESLintSettings ( context . settings ) [ "react-x" ] ?? { } ;
36
+ const { useEffect : useEffectAlias = [ ] , useState : useStateAlias = [ ] } = settings . additionalHooks ?? { } ;
37
37
function isUseEffectCallWithAlias ( node : TSESTree . CallExpression ) {
38
- return isUseEffectCall ( node , context ) || useEffectAlias . some ( F . flip ( isReactHookCallWithNameLoose ) ( node ) ) ;
38
+ return isUseEffectCall ( node , context )
39
+ || useEffectAlias . some ( F . flip ( isReactHookCallWithNameLoose ) ( node ) ) ;
39
40
}
40
41
function isUseStateCallWithAlias ( node : TSESTree . CallExpression ) {
41
42
return isUseStateCall ( node , context ) || useStateAlias . some ( F . flip ( isReactHookCallWithNameLoose ) ( node ) ) ;
@@ -124,9 +125,9 @@ export default createRule<[], MessageID>({
124
125
}
125
126
const functionStack = MutList . make < [ node : TSESTreeFunction , kind : FunctionKind ] > ( ) ;
126
127
const effectFunctionRef = MutRef . make < TSESTreeFunction | null > ( null ) ;
127
- const effectFunctionIdentifiers = MutRef . make ( Chunk . empty < TSESTree . Identifier > ( ) ) ;
128
- const indirectFunctionCalls = MutRef . make ( Chunk . empty < TSESTree . CallExpression > ( ) ) ;
129
- const indirectSetStateCalls = new WeakMap < TSESTreeFunction , Chunk . Chunk < TSESTree . CallExpression > > ( ) ;
128
+ const effectFunctionIdentifiers : TSESTree . Identifier [ ] = [ ] ;
129
+ const indirectFunctionCalls : TSESTree . CallExpression [ ] = [ ] ;
130
+ const indirectSetStateCalls = new WeakMap < TSESTreeFunction , TSESTree . CallExpression [ ] > ( ) ;
130
131
const onEffectFunctionEnter = ( node : TSESTreeFunction ) => {
131
132
MutRef . set ( effectFunctionRef , node ) ;
132
133
} ;
@@ -156,8 +157,8 @@ export default createRule<[], MessageID>({
156
157
. with ( "setState" , ( ) => {
157
158
if ( ! parentFn ) return ;
158
159
if ( parentFn !== effectFn && parentFnKind !== "immediate" ) {
159
- const calls = indirectSetStateCalls . get ( parentFn ) ?? Chunk . empty < TSESTree . CallExpression > ( ) ;
160
- indirectSetStateCalls . set ( parentFn , Chunk . append ( calls , node ) ) ;
160
+ const calls = indirectSetStateCalls . get ( parentFn ) ?? [ ] ;
161
+ indirectSetStateCalls . set ( parentFn , [ ... calls , node ] ) ;
161
162
return ;
162
163
}
163
164
context . report ( {
@@ -168,12 +169,12 @@ export default createRule<[], MessageID>({
168
169
. with ( "useEffect" , ( ) => {
169
170
if ( node . arguments . every ( isFunction ) ) return ;
170
171
const identifiers = getNestedIdentifiers ( node ) ;
171
- MutRef . update ( effectFunctionIdentifiers , Chunk . appendAll ( Chunk . unsafeFromArray ( identifiers ) ) ) ;
172
+ effectFunctionIdentifiers . push ( ... identifiers ) ;
172
173
} )
173
174
. with ( "other" , ( ) => {
174
175
const isInEffectFunction = effectFn === parentFn ;
175
176
if ( ! isInEffectFunction ) return ;
176
- MutRef . update ( indirectFunctionCalls , Chunk . append ( node ) ) ;
177
+ indirectFunctionCalls . push ( node ) ;
177
178
} )
178
179
. otherwise ( F . constVoid ) ;
179
180
} ,
@@ -183,12 +184,11 @@ export default createRule<[], MessageID>({
183
184
findVariable ( id , initialScope ) ,
184
185
O . flatMap ( getVariableNode ( 0 ) ) ,
185
186
O . filter ( isFunction ) ,
186
- O . flatMapNullable ( ( fn ) => indirectSetStateCalls . get ( fn as TSESTreeFunction ) ) ,
187
- O . map ( Chunk . toReadonlyArray ) ,
187
+ O . flatMapNullable ( ( fn ) => indirectSetStateCalls . get ( fn ) ) ,
188
188
O . getOrElse ( ( ) => [ ] ) ,
189
189
) ;
190
190
} ;
191
- for ( const { callee } of Chunk . toReadonlyArray ( MutRef . get ( indirectFunctionCalls ) ) ) {
191
+ for ( const { callee } of indirectFunctionCalls ) {
192
192
if ( ! ( "name" in callee ) ) continue ;
193
193
const { name } = callee ;
194
194
const setStateCalls = getSetStateCalls ( name , context . sourceCode . getScope ( callee ) ) ;
@@ -200,7 +200,7 @@ export default createRule<[], MessageID>({
200
200
} ) ;
201
201
}
202
202
}
203
- for ( const id of Chunk . toReadonlyArray ( MutRef . get ( effectFunctionIdentifiers ) ) ) {
203
+ for ( const id of effectFunctionIdentifiers ) {
204
204
const setStateCalls = getSetStateCalls ( id . name , context . sourceCode . getScope ( id ) ) ;
205
205
for ( const setStateCall of setStateCalls ) {
206
206
context . report ( {
0 commit comments