Skip to content

Commit 0d0b32f

Browse files
refactor: simplify logic
1 parent dd2e632 commit 0d0b32f

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

packages/plugins/eslint-plugin-react-x/src/rules/no-unnecessary-use-callback.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { isUseCallbackCall, isUseEffectLikeCall } from "@eslint-react/core";
33
import { identity } from "@eslint-react/eff";
44
import type { RuleContext, RuleFeature } from "@eslint-react/shared";
55
import { findVariable, getChildScopes, getVariableDefinitionNode } from "@eslint-react/var";
6+
import type { TSESTree } from "@typescript-eslint/types";
67
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
78
import { isIdentifier } from "@typescript-eslint/utils/ast-utils";
89
import { type RuleListener } from "@typescript-eslint/utils/ts-eslint";
@@ -118,14 +119,18 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
118119

119120
const references = context.sourceCode.getDeclaredVariables(node)[0]?.references ?? [];
120121
const usages = references.filter((ref) => !(ref.init ?? false));
122+
const effectSet = new Set<TSESTree.Node>();
121123

122-
const filteredUsageAmount = usages.reduce((set, usage) => {
124+
const isUsedOutSideOfEffect = usages.some((usage) => {
123125
const effect = AST.findParentNode(usage.identifier, (node) => isUseEffectLikeCall(node));
124-
// TODO Better check for outside of useEffect usage
125-
return set.add(effect ?? 'outside');
126-
}, new Set());
126+
if (effect == null) {
127+
return true;
128+
}
129+
effectSet.add(effect);
130+
return false;
131+
});
127132

128-
if (filteredUsageAmount.size !== 1 || filteredUsageAmount.has('outside')) {
133+
if (isUsedOutSideOfEffect || effectSet.size !== 1) {
129134
return;
130135
}
131136

0 commit comments

Comments
 (0)