Skip to content

Commit 76c8a43

Browse files
committed
combine findDownstreamIfs and getDownstreamIdentifiers
1 parent c9605f0 commit 76c8a43

File tree

2 files changed

+11
-22
lines changed

2 files changed

+11
-22
lines changed

src/rule.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { messageIds, messages } from "./messages.js";
22
import {
3-
findDownstreamIfs,
3+
findDownstreamNodes,
44
getCallExpr,
55
getDownstreamRefs,
66
} from "./util/ast.js";
@@ -79,10 +79,9 @@ export const rule = {
7979
isState(variable) || (isProp(variable) && !isHOCProp(variable)),
8080
);
8181

82-
findDownstreamIfs(context, node)
82+
findDownstreamNodes(context, node, "IfStatement")
8383
// An event-handling effect (invalid) and a synchronizing effect (valid)
84-
// look quite similar. But the latter should act on *all* possible states,
85-
// whereas the former waits for a specific state (from the event).
84+
// look quite similar. But the latter should act on *all* possible states, // whereas the former waits for a specific state (from the event).
8685
// Technically synchronizing effects can be inlined too.
8786
// But an effect is arguably more readable (for once), and recommended by the React docs.
8887
.filter((ifNode) => !ifNode.alternate)

src/util/ast.js

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,14 @@ export const traverse = (context, node, visit, visited = new Set()) => {
2121
.forEach((child) => traverse(context, child, visit, visited));
2222
};
2323

24-
const getDownstreamIdentifiers = (context, rootNode) => {
25-
const identifiers = [];
26-
traverse(context, rootNode, (node) => {
27-
if (node.type === "Identifier") {
28-
identifiers.push(node);
24+
export const findDownstreamNodes = (context, topNode, type) => {
25+
const nodes = [];
26+
traverse(context, topNode, (node) => {
27+
if (node.type === type) {
28+
nodes.push(node);
2929
}
3030
});
31-
return identifiers;
32-
};
33-
34-
export const findDownstreamIfs = (context, node) => {
35-
const ifs = [];
36-
traverse(context, node, (n) => {
37-
if (n.type === "IfStatement") {
38-
ifs.push(n);
39-
}
40-
});
41-
return ifs;
31+
return nodes;
4232
};
4333

4434
export const getUpstreamVariables = (
@@ -65,7 +55,7 @@ export const getUpstreamVariables = (
6555
const upstreamVariables = variable.defs
6656
.filter((def) => !!def.node.init)
6757
.filter((def) => filter(def.node))
68-
.flatMap((def) => getDownstreamIdentifiers(context, def.node.init))
58+
.flatMap((def) => findDownstreamNodes(context, def.node.init, "Identifier"))
6959
.flatMap((identifier) =>
7060
getUpstreamVariables(context, identifier, filter, visited),
7161
);
@@ -75,7 +65,7 @@ export const getUpstreamVariables = (
7565
};
7666

7767
export const getDownstreamRefs = (context, node) =>
78-
getDownstreamIdentifiers(context, node)
68+
findDownstreamNodes(context, node, "Identifier")
7969
.map((identifier) => getRef(context, identifier))
8070
.filter(Boolean);
8171

0 commit comments

Comments
 (0)