Skip to content

Commit 03635c6

Browse files
committed
Simplify isDirectCall and fix possible crash in isUseEffect
1 parent aa4d3a5 commit 03635c6

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
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(getEffectFn(node), ref))
78+
.filter((ref) => isDirectCall(ref))
7979
.forEach((ref) => {
8080
const callExpr = getCallExpr(ref);
8181

src/util/react.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ export const isUseState = (node) =>
5353
});
5454

5555
export const isUseEffect = (node) =>
56-
(node.type === "CallExpression" &&
57-
node.callee.type === "Identifier" &&
56+
node.type === "CallExpression" &&
57+
((node.callee.type === "Identifier" &&
5858
(node.callee.name === "useEffect" ||
5959
node.callee.name === "useLayoutEffect")) ||
60-
(node.callee.type === "MemberExpression" &&
61-
node.callee.object.name === "React" &&
62-
(node.callee.property.name === "useEffect" ||
63-
node.callee.property.name === "useLayoutEffect"));
60+
(node.callee.type === "MemberExpression" &&
61+
node.callee.object.name === "React" &&
62+
(node.callee.property.name === "useEffect" ||
63+
node.callee.property.name === "useLayoutEffect")));
6464

6565
export const getEffectFn = (node) => {
6666
if (!isUseEffect(node) || node.arguments.length < 1) {
@@ -151,10 +151,9 @@ 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-
export const isDirectCall = (effectFn, ref) => {
154+
export const isDirectCall = (ref) => {
155155
let node = ref.identifier;
156156

157-
// Walk up the parent chain to find the enclosing function
158157
while (
159158
node &&
160159
node.type !== "ArrowFunctionExpression" &&
@@ -163,7 +162,7 @@ export const isDirectCall = (effectFn, ref) => {
163162
node = node.parent;
164163
}
165164

166-
return node && node === effectFn;
165+
return node && isUseEffect(node.parent);
167166
};
168167

169168
export const findPropUsedToResetAllState = (

0 commit comments

Comments
 (0)