Skip to content

Commit c5dcd47

Browse files
committed
Update direct calls comments
1 parent 1b1afe8 commit c5dcd47

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

src/rule.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ export const rule = {
7373
// Don't analyze HOC prop callbacks -- we don't have control over them to lift state or logic
7474
!isHOCProp(ref.resolved)),
7575
)
76+
// Non-direct calls are likely inside a callback passed to an external system like `window.addEventListener`,
77+
// or a Promise chain that (probably) retrieves external data.
78+
// Note we'll still analyze derived setters because isStateSetter considers that.
79+
// Heuristic inspired by https://eslint-react.xyz/docs/rules/hooks-extra-no-direct-set-state-in-use-effect
7680
.filter((ref) => isDirectCall(ref.identifier))
7781
.forEach((ref) => {
7882
const callExpr = getCallExpr(ref);

src/util/react.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,14 @@ export const getUseStateNode = (context, ref) => {
149149
?.defs.find((def) => isUseState(def.node))?.node;
150150
};
151151

152-
// When false, it's likely inside a callback, e.g. a listener, or Promise chain that retrieves external data.
153-
// Note we'll still analyze derived setters because isStateSetter considers that.
154-
// Heuristic inspired by https://eslint-react.xyz/docs/rules/hooks-extra-no-direct-set-state-in-use-effect
152+
// Returns true if the node is called directly inside a `useEffect`.
153+
// Note IIFEs do not break the "direct" chain because they're invoked immediately, as opposed to being a callback.
155154
export const isDirectCall = (node) => {
156155
if (!node) {
157156
return false;
158157
} else if (
159158
(node.type === "ArrowFunctionExpression" ||
160159
node.type === "FunctionExpression") &&
161-
// Keep walking up IIFEs -- we consider them direct calls because they're invoked immediately, as opposed to being a callback
162160
!isIIFE(node.parent)
163161
) {
164162
return isUseEffect(node.parent);

0 commit comments

Comments
 (0)