Skip to content

Commit cf68f8d

Browse files
committed
refactor: improve getCallName function and update test cases for noDirectSetStateInUseEffect rule
1 parent c01ba08 commit cf68f8d

File tree

3 files changed

+182
-34
lines changed

3 files changed

+182
-34
lines changed

packages/plugins/eslint-plugin-react-hooks-extra/src/hooks/use-no-direct-set-state-in-use-effect.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export function useNoDirectSetStateInUseEffect<Ctx extends RuleContext>(
4747
const { onViolation, useEffectKind } = options;
4848
const settings = getSettingsFromContext(context);
4949
const additionalHooks = settings.additionalHooks;
50+
const getText = (n: TSESTree.Node) => context.sourceCode.getText(n);
5051
const isUseEffectLikeCall = ER.isReactHookCallWithNameAlias(context, useEffectKind, additionalHooks[useEffectKind]);
5152
const isUseStateCall = ER.isReactHookCallWithNameAlias(context, "useState", additionalHooks.useState);
5253
const isUseMemoCall = ER.isReactHookCallWithNameAlias(context, "useMemo", additionalHooks.useMemo);
@@ -80,6 +81,13 @@ export function useNoDirectSetStateInUseEffect<Ctx extends RuleContext>(
8081
&& isUseEffectLikeCall(node.parent);
8182
}
8283

84+
function getCallName(node: TSESTree.Node) {
85+
if (node.type === T.CallExpression) {
86+
return AST.toString(node.callee, getText);
87+
}
88+
return AST.toString(node, getText);
89+
}
90+
8391
function getCallKind(node: TSESTree.CallExpression) {
8492
return match<TSESTree.CallExpression, CallKind>(node)
8593
.when(isUseStateCall, () => "useState")
@@ -221,15 +229,15 @@ export function useNoDirectSetStateInUseEffect<Ctx extends RuleContext>(
221229
const setStateCalls = getSetStateCalls(name, context.sourceCode.getScope(callee));
222230
for (const setStateCall of setStateCalls) {
223231
onViolation(context, setStateCall, {
224-
name: AST.toString(setStateCall, (n) => context.sourceCode.getText(n)),
232+
name: getCallName(setStateCall),
225233
});
226234
}
227235
}
228236
for (const id of setupFunctionIdentifiers) {
229237
const setStateCalls = getSetStateCalls(id.name, context.sourceCode.getScope(id));
230238
for (const setStateCall of setStateCalls) {
231239
onViolation(context, setStateCall, {
232-
name: AST.toString(setStateCall, (n) => context.sourceCode.getText(n)),
240+
name: getCallName(setStateCall),
233241
});
234242
}
235243
}

0 commit comments

Comments
 (0)