|
1 | | -import { getCallExpr, getUpstreamRefs } from "../util/ast.js"; |
2 | 1 | import { |
| 2 | + getCallExpr, |
| 3 | + getDownstreamRefs, |
| 4 | + getUpstreamRefs, |
3 | 5 | getEffectDepsRefs, |
4 | 6 | getEffectFnRefs, |
5 | | - isArgsAllLiterals, |
6 | 7 | isImmediateCall, |
7 | 8 | isProp, |
8 | 9 | isStateSetter, |
@@ -32,18 +33,28 @@ export default { |
32 | 33 | const depsRefs = getEffectDepsRefs(context, node); |
33 | 34 | if (!effectFnRefs || !depsRefs) return; |
34 | 35 |
|
35 | | - const isAllDepsProps = depsRefs |
| 36 | + const isSomeDepsProps = depsRefs |
36 | 37 | .flatMap((ref) => getUpstreamRefs(context, ref)) |
37 | | - .notEmptyEvery((ref) => isProp(ref)); |
| 38 | + .some((ref) => isProp(ref)); |
38 | 39 |
|
39 | 40 | effectFnRefs |
40 | 41 | .filter((ref) => isStateSetter(context, ref)) |
41 | 42 | .filter((ref) => isImmediateCall(ref.identifier)) |
42 | 43 | .forEach((ref) => { |
43 | 44 | const callExpr = getCallExpr(ref); |
44 | 45 |
|
45 | | - // TODO: Flag non-literals too? e.g. I think this is the correct warning for https://github.com/getsentry/sentry/pull/100177/files#diff-cf3aceaba5cdab4553d92644581e23d54914923199d31807fe090e0d49b786caR97 |
46 | | - if (isAllDepsProps && isArgsAllLiterals(context, callExpr)) { |
| 46 | + const argsUpstreamRefs = callExpr.arguments |
| 47 | + .flatMap((arg) => getDownstreamRefs(context, arg)) |
| 48 | + .flatMap((ref) => getUpstreamRefs(context, ref)); |
| 49 | + // Avoid overlap with no-derived-state |
| 50 | + const isSomeArgsNotProps = |
| 51 | + // TODO: literals check may be less reliable with *all* upstream refs... |
| 52 | + // What if that was `getUpstreamNodes()` instead, returning AST nodes? |
| 53 | + // Could get complicated though. Ideally we may restructure the rules to not need this at all? |
| 54 | + argsUpstreamRefs.length === 0 || // All literals |
| 55 | + argsUpstreamRefs.some((ref) => !isProp(ref)); |
| 56 | + |
| 57 | + if (isSomeDepsProps && isSomeArgsNotProps) { |
47 | 58 | context.report({ |
48 | 59 | node: callExpr, |
49 | 60 | messageId: "avoidAdjustingStateWhenAPropChanges", |
|
0 commit comments