Skip to content

Commit 446e29b

Browse files
committed
refactor: minor improvements
1 parent b9a39a1 commit 446e29b

File tree

4 files changed

+34
-33
lines changed

4 files changed

+34
-33
lines changed

packages/plugins/eslint-plugin-react-hooks-extra/src/rules-hooks/use-no-direct-set-state-in-use-effect.ts

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -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, {

packages/utilities/ast/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ export * from "./array";
22
export * from "./class-id";
33
export * from "./expression";
44
export * from "./function";
5-
export * from "./line";
65
export * from "./literal";
6+
export * from "./misc";
77
export * from "./node";
88
export * from "./promise-then";
99
export * from "./property-name";

packages/utilities/ast/src/literal.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
import type { TSESTree } from "@typescript-eslint/types";
22
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
33

4+
type LiteralType =
5+
| "boolean"
6+
| "null"
7+
| "number"
8+
| "regexp"
9+
| "string";
10+
411
export function isLiteral(node: TSESTree.Node): node is TSESTree.Literal;
512
export function isLiteral(node: TSESTree.Node, type: "boolean"): node is TSESTree.BooleanLiteral;
613
export function isLiteral(node: TSESTree.Node, type: "null"): node is TSESTree.NullLiteral;
714
export function isLiteral(node: TSESTree.Node, type: "number"): node is TSESTree.NumberLiteral;
815
export function isLiteral(node: TSESTree.Node, type: "regexp"): node is TSESTree.RegExpLiteral;
916
export function isLiteral(node: TSESTree.Node, type: "string"): node is TSESTree.StringLiteral;
10-
export function isLiteral(
11-
node: TSESTree.Node,
12-
type?:
13-
| "boolean"
14-
| "null"
15-
| "number"
16-
| "regexp"
17-
| "string",
18-
) {
17+
export function isLiteral(node: TSESTree.Node, type?: LiteralType) {
1918
if (node.type !== T.Literal) return false;
2019
if (type == null) return true;
2120
switch (type) {
File renamed without changes.

0 commit comments

Comments
 (0)