Skip to content

Commit da144f5

Browse files
committed
refactor: receive variable in upstream functions
1 parent 2495a70 commit da144f5

File tree

7 files changed

+24
-35
lines changed

7 files changed

+24
-35
lines changed

src/no-chain-state-updates.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export const rule = {
3737
if (!effectFnRefs || !depsRefs) return;
3838

3939
const isAllDepsInternal = depsRefs
40-
.flatMap((ref) => getUpstreamReactVariables(context, ref.identifier))
40+
.flatMap((ref) => getUpstreamReactVariables(context, ref.resolved))
4141
.notEmptyEvery(
4242
(variable) =>
4343
isState(variable) || (isProp(variable) && !isHOCProp(variable)),
@@ -56,9 +56,7 @@ export const rule = {
5656

5757
const argsUpstreamVariables = callExpr.arguments
5858
.flatMap((arg) => getDownstreamRefs(context, arg))
59-
.flatMap((ref) =>
60-
getUpstreamReactVariables(context, ref.identifier),
61-
);
59+
.flatMap((ref) => getUpstreamReactVariables(context, ref.resolved));
6260
const isAllArgsInternal = argsUpstreamVariables.notEmptyEvery(
6361
(variable) =>
6462
isState(variable) || (isProp(variable) && !isHOCProp(variable)),

src/no-derived-state.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export const rule = {
5252
getDownstreamRefs(context, arg),
5353
);
5454
const argsUpstreamVariables = argsRefs.flatMap((ref) =>
55-
getUpstreamReactVariables(context, ref.identifier),
55+
getUpstreamReactVariables(context, ref.resolved),
5656
);
5757
const isAllArgsInternal = argsUpstreamVariables.notEmptyEvery(
5858
(variable) =>
@@ -65,8 +65,8 @@ export const rule = {
6565
.notEmptyEvery((argRef) =>
6666
depsRefs.some((depRef) =>
6767
arraysEqual(
68-
getUpstreamReactVariables(context, argRef.identifier),
69-
getUpstreamReactVariables(context, depRef.identifier),
68+
getUpstreamReactVariables(context, argRef.resolved),
69+
getUpstreamReactVariables(context, depRef.resolved),
7070
),
7171
),
7272
);

src/no-event-handler.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ export const rule = {
3636
.filter((ifNode) => !ifNode.alternate)
3737
.filter((ifNode) =>
3838
getDownstreamRefs(context, ifNode.test)
39-
.flatMap((ref) =>
40-
getUpstreamReactVariables(context, ref.identifier),
41-
)
39+
.flatMap((ref) => getUpstreamReactVariables(context, ref.resolved))
4240
// TODO: Should flag props too, but maybe with a different message?
4341
.notEmptyEvery((variable) => isState(variable)),
4442
)

src/no-pass-data-to-parent.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@ export const rule = {
4646
const callExpr = getCallExpr(ref);
4747
const argsUpstreamVariables = callExpr.arguments
4848
.flatMap((arg) => getDownstreamRefs(context, arg))
49-
.flatMap((ref) =>
50-
getUpstreamReactVariables(context, ref.identifier),
51-
);
49+
.flatMap((ref) => getUpstreamReactVariables(context, ref.resolved));
5250

5351
if (
5452
callExpr.arguments.some((arg) => arg.type === "Literal") ||

src/no-pass-live-state-to-parent.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@ export const rule = {
4646
const callExpr = getCallExpr(ref);
4747
const argsUpstreamVariables = callExpr.arguments
4848
.flatMap((arg) => getDownstreamRefs(context, arg))
49-
.flatMap((ref) =>
50-
getUpstreamReactVariables(context, ref.identifier),
51-
);
49+
.flatMap((ref) => getUpstreamReactVariables(context, ref.resolved));
5250

5351
if (argsUpstreamVariables.some((variable) => isState(variable))) {
5452
context.report({

src/util/ast.js

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,31 +33,28 @@ export const findDownstreamNodes = (context, topNode, type) => {
3333

3434
export const getUpstreamVariables = (
3535
context,
36-
node,
36+
variable,
3737
filter,
3838
visited = new Set(),
3939
) => {
40-
if (visited.has(node)) {
40+
if (visited.has(variable)) {
4141
return [];
4242
}
4343

44-
visited.add(node);
44+
visited.add(variable);
4545

46-
const variable = findVariable(context.sourceCode.getScope(node), node);
47-
if (!variable) {
46+
const upstreamVariables = variable.defs
47+
.filter((def) => !!def.node.init)
48+
.filter((def) => filter(def.node))
49+
.flatMap((def) => getDownstreamRefs(context, def.node.init))
50+
.map((ref) => ref.resolved)
4851
// I think this only happens when:
4952
// 1. There's genuinely no variable, i.e. `node` is a literal
5053
// 2. Import statement is missing
5154
// 3. ESLint globals are misconfigured
52-
return [];
53-
}
54-
55-
const upstreamVariables = variable.defs
56-
.filter((def) => !!def.node.init)
57-
.filter((def) => filter(def.node))
58-
.flatMap((def) => findDownstreamNodes(context, def.node.init, "Identifier"))
59-
.flatMap((identifier) =>
60-
getUpstreamVariables(context, identifier, filter, visited),
55+
.filter(Boolean)
56+
.flatMap((variable) =>
57+
getUpstreamVariables(context, variable, filter, visited),
6158
);
6259

6360
// Ultimately return only leaf variables

src/util/react.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,12 @@ export const isFnRef = (ref) => getCallExpr(ref) !== undefined;
117117
// And in the case of a prop, we can't differentiate state mutations from callbacks anyway.
118118
export const isStateSetter = (context, ref) =>
119119
isFnRef(ref) &&
120-
getUpstreamReactVariables(context, ref.identifier).notEmptyEvery((variable) =>
120+
getUpstreamReactVariables(context, ref.resolved).notEmptyEvery((variable) =>
121121
isState(variable),
122122
);
123123
export const isPropCallback = (context, ref) =>
124124
isFnRef(ref) &&
125-
getUpstreamReactVariables(context, ref.identifier).notEmptyEvery((variable) =>
125+
getUpstreamReactVariables(context, ref.resolved).notEmptyEvery((variable) =>
126126
isProp(variable),
127127
);
128128

@@ -153,7 +153,7 @@ const getDeclNode = (node) =>
153153
: node;
154154

155155
export const getUseStateNode = (context, ref) => {
156-
return getUpstreamReactVariables(context, ref.identifier)
156+
return getUpstreamReactVariables(context, ref.resolved)
157157
.find((variable) => isState(variable))
158158
?.defs.find((def) => isUseState(def.node))?.node;
159159
};
@@ -260,10 +260,10 @@ const findContainingNode = (node) => {
260260
}
261261
};
262262

263-
export const getUpstreamReactVariables = (context, node) =>
263+
export const getUpstreamReactVariables = (context, variable) =>
264264
getUpstreamVariables(
265265
context,
266-
node,
266+
variable,
267267
// Stop at the *usage* of `useState` - don't go up to the `useState` variable.
268268
// Not needed for props - they don't go "too far".
269269
// We could remove this and check for the `useState` variable instead,

0 commit comments

Comments
 (0)