Skip to content

Commit 7382e2a

Browse files
committed
address false positives
1 parent a1d03cb commit 7382e2a

2 files changed

Lines changed: 34 additions & 9 deletions

File tree

packages/eslint-plugin-query/src/__tests__/exhaustive-deps.test.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,17 @@ ruleTester.run('exhaustive-deps', rule, {
103103
}
104104
`,
105105
},
106+
{
107+
name: 'should ignore type parameters used only in queryFn return type',
108+
code: normalizeIndent`
109+
function useThing<TData>() {
110+
return useQuery({
111+
queryKey: ['thing'],
112+
queryFn: (): Promise<TData> => Promise.reject(new Error('nope')),
113+
})
114+
}
115+
`,
116+
},
106117
{
107118
name: 'should add "...args" to deps',
108119
code: `
@@ -442,7 +453,7 @@ ruleTester.run('exhaustive-deps', rule, {
442453
{
443454
name: 'should pass when queryKey uses a direct conditional expression',
444455
code: normalizeIndent`
445-
function Component(cond, a, b) {
456+
function Component({ cond, a, b }) {
446457
useQuery({
447458
queryKey: ['thing', cond ? a : b],
448459
queryFn: () => (cond ? a : b),
@@ -453,7 +464,7 @@ ruleTester.run('exhaustive-deps', rule, {
453464
{
454465
name: 'should pass when queryKey uses a direct binary expression',
455466
code: normalizeIndent`
456-
function Component(a, b) {
467+
function Component({ a, b }) {
457468
useQuery({
458469
queryKey: ['thing', a + b],
459470
queryFn: () => a + b,
@@ -810,6 +821,19 @@ ruleTester.run('exhaustive-deps', rule, {
810821
}
811822
`,
812823
},
824+
{
825+
name: 'should pass when queryKey has call expression with identifier callee',
826+
code: normalizeIndent`
827+
function useThing(dep) {
828+
const makeKeyPart = (value) => value
829+
830+
return useQuery({
831+
queryKey: ['thing', makeKeyPart(dep)],
832+
queryFn: () => makeKeyPart(dep)
833+
})
834+
}
835+
`,
836+
},
813837
{
814838
name: 'should pass when queryKey has call expression with nested member callee',
815839
code: normalizeIndent`

packages/eslint-plugin-query/src/rules/exhaustive-deps/exhaustive-deps.utils.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const ExhaustiveDepsUtils = {
1414
const component = ASTUtils.getFunctionAncestor(sourceCode, node)
1515
const queryFnScope = scopeManager.acquire(node)
1616

17-
if (queryFnScope === null) {
17+
if (queryFnScope === null || reference.isValueReference === false) {
1818
return false
1919
}
2020

@@ -215,12 +215,13 @@ export const ExhaustiveDepsUtils = {
215215
return
216216
case AST_NODE_TYPES.CallExpression:
217217
node.arguments.forEach((argument) => visit(argument))
218-
if (
219-
node.callee.type === AST_NODE_TYPES.MemberExpression ||
220-
node.callee.type === AST_NODE_TYPES.ChainExpression ||
221-
node.callee.type === AST_NODE_TYPES.TSNonNullExpression
222-
) {
223-
visit(node.callee)
218+
switch (node.callee.type) {
219+
case AST_NODE_TYPES.Identifier:
220+
case AST_NODE_TYPES.MemberExpression:
221+
case AST_NODE_TYPES.ChainExpression:
222+
case AST_NODE_TYPES.TSNonNullExpression:
223+
visit(node.callee)
224+
break
224225
}
225226
return
226227
}

0 commit comments

Comments
 (0)