Skip to content

Commit 01c62b9

Browse files
committed
Convert isDirectCall to recursive for consistency
1 parent 08ca488 commit 01c62b9

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

src/rule.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export const rule = {
7575
// Don't analyze HOC prop callbacks -- we don't have control over them to lift state or logic
7676
!isHOCProp(ref.resolved)),
7777
)
78-
.filter((ref) => isDirectCall(ref))
78+
.filter((ref) => isDirectCall(ref.identifier))
7979
.forEach((ref) => {
8080
const callExpr = getCallExpr(ref);
8181

src/util/react.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -151,21 +151,20 @@ export const getUseStateNode = (context, ref) => {
151151
// When false, it's likely inside a callback, e.g. a listener, or Promise chain that retrieves external data.
152152
// Note we'll still analyze derived setters because isStateSetter considers that.
153153
// Heuristic inspired by https://eslint-react.xyz/docs/rules/hooks-extra-no-direct-set-state-in-use-effect
154-
// Also returns false for IIFEs, which technically could cause a false negative.
154+
// TODO: Also returns false for IIFEs, which could cause a false negative.
155155
// But IIFEs in effects are typically used to call async functions, implying it retrieves external state.
156156
// So, not a big deal.
157-
export const isDirectCall = (ref) => {
158-
let node = ref.identifier;
159-
160-
while (
161-
node &&
162-
node.type !== "ArrowFunctionExpression" &&
163-
node.type !== "FunctionExpression"
157+
export const isDirectCall = (node) => {
158+
if (!node) {
159+
return false;
160+
} else if (
161+
node.type === "ArrowFunctionExpression" ||
162+
node.type === "FunctionExpression"
164163
) {
165-
node = node.parent;
164+
return isUseEffect(node.parent);
165+
} else {
166+
return isDirectCall(node.parent);
166167
}
167-
168-
return node && isUseEffect(node.parent);
169168
};
170169

171170
export const findPropUsedToResetAllState = (

0 commit comments

Comments
 (0)