Skip to content

Commit aa4d3a5

Browse files
committed
Update note regarding CallExpressions on state
1 parent 64102d3 commit aa4d3a5

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

src/util/react.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,11 @@ export function getDependenciesRefs(context, node) {
102102
}
103103

104104
export const isFnRef = (ref) => getCallExpr(ref) !== undefined;
105-
// TODO: Technically this includes state with call expressions, like countryCode.toUpperCase().
106-
// A true `isStateSetter` would check that the identifier name matches the useState's second element.
107-
// Maybe we sometimes prefer this behavior though, like when state is mutated (even though that is not recommended).
105+
106+
// NOTE: These return true for state with CallExpressions, like `list.concat()`.
107+
// Arguably preferable, as mutating the state is functionally the same as calling the setter.
108+
// (Even though that is not recommended and should be prevented by a different rule).
109+
// And in the case of a prop, we can't differentiate state mutations from callbacks anyway.
108110
export const isStateSetter = (context, ref) =>
109111
isFnRef(ref) &&
110112
getUpstreamReactVariables(context, ref.identifier).notEmptyEvery((variable) =>

test/deriving-state.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ new MyRuleTester().run("/deriving-state", {
391391
data: { state: "doubleList" },
392392
},
393393
{
394-
// NOTE: We consider `list.concat` to essentially be a prop callback
394+
// We consider `list.concat` to essentially be a prop callback
395395
messageId: messageIds.avoidParentChildCoupling,
396396
},
397397
],
@@ -414,7 +414,7 @@ new MyRuleTester().run("/deriving-state", {
414414
data: { state: "doubleList" },
415415
},
416416
{
417-
// NOTE: We consider `list.concat` to essentially be a state setter call
417+
// We consider `list.concat` to essentially be a state setter call
418418
messageId: messageIds.avoidDerivedState,
419419
data: { state: "list" },
420420
},
@@ -434,7 +434,7 @@ new MyRuleTester().run("/deriving-state", {
434434
`,
435435
errors: [
436436
{
437-
// NOTE: We consider `doubleList.push` to essentially be a state setter call
437+
// We consider `doubleList.push` to essentially be a state setter call
438438
messageId: messageIds.avoidDerivedState,
439439
data: { state: "doubleList" },
440440
},

0 commit comments

Comments
 (0)