diff --git a/packages/eslint-plugin-query/src/__tests__/exhaustive-deps.test.ts b/packages/eslint-plugin-query/src/__tests__/exhaustive-deps.test.ts index 63172c83f5..482fc9dd5c 100644 --- a/packages/eslint-plugin-query/src/__tests__/exhaustive-deps.test.ts +++ b/packages/eslint-plugin-query/src/__tests__/exhaustive-deps.test.ts @@ -422,6 +422,44 @@ ruleTester.run('exhaustive-deps', rule, { }); `, }, + { + name: 'should not fail when queryKey uses arrow function to produce a key', + code: normalizeIndent` + const obj = reactive<{ boo?: string }>({}); + + const query = useQuery({ + queryKey: ['foo', () => obj.boo], + queryFn: () => fetch(\`/mock/getSomething/\${obj.boo}\`), + enable: () => !!obj.boo, + }); + `, + }, + { + name: 'should not fail when queryKey uses arrow function to produce a key as the body return', + code: normalizeIndent` + const obj = reactive<{ boo?: string }>({}); + + const query = useQuery({ + queryKey: ['foo', () => { return obj.boo }], + queryFn: () => fetch(\`/mock/getSomething/\${obj.boo}\`), + enable: () => !!obj.boo, + }); + `, + }, + { + name: 'should not fail when queryKey uses function expression to produce a key as the body return', + code: normalizeIndent` + const obj = reactive<{ boo?: string }>({}); + + const query = useQuery({ + queryKey: ['foo', function() { + return obj.boo + }], + queryFn: () => fetch(\`/mock/getSomething/\${obj.boo}\`), + enable: () => !!obj.boo, + }); + `, + }, ], invalid: [ { diff --git a/packages/eslint-plugin-query/src/utils/ast-utils.ts b/packages/eslint-plugin-query/src/utils/ast-utils.ts index 392c582534..b5e5dcf0d4 100644 --- a/packages/eslint-plugin-query/src/utils/ast-utils.ts +++ b/packages/eslint-plugin-query/src/utils/ast-utils.ts @@ -111,6 +111,24 @@ export const ASTUtils = { identifiers.push(...ASTUtils.getNestedIdentifiers(node.expression)) } + if (node.type === AST_NODE_TYPES.ArrowFunctionExpression) { + identifiers.push(...ASTUtils.getNestedIdentifiers(node.body)) + } + + if (node.type === AST_NODE_TYPES.FunctionExpression) { + identifiers.push(...ASTUtils.getNestedIdentifiers(node.body)) + } + + if (node.type === AST_NODE_TYPES.BlockStatement) { + identifiers.push( + ...node.body.map((body) => ASTUtils.getNestedIdentifiers(body)).flat(), + ) + } + + if (node.type === AST_NODE_TYPES.ReturnStatement && node.argument) { + identifiers.push(...ASTUtils.getNestedIdentifiers(node.argument)) + } + return identifiers }, isAncestorIsCallee(identifier: TSESTree.Node) {