diff --git a/packages/eslint-plugin-query/src/rules/exhaustive-deps/exhaustive-deps.test.ts b/packages/eslint-plugin-query/src/rules/exhaustive-deps/exhaustive-deps.test.ts index bb145d2a2d..32608b929d 100644 --- a/packages/eslint-plugin-query/src/rules/exhaustive-deps/exhaustive-deps.test.ts +++ b/packages/eslint-plugin-query/src/rules/exhaustive-deps/exhaustive-deps.test.ts @@ -275,7 +275,7 @@ ruleTester.run('exhaustive-deps', rule, { `, }, { - name: 'should ignore constants defined out of scope (react component)', + name: 'should ignore constants defined out of scope (react component, function declaration)', code: ` const CONST_VAL = 1 function MyComponent() { @@ -287,7 +287,31 @@ ruleTester.run('exhaustive-deps', rule, { `, }, { - name: 'should ignore constants defined out of scope (react hook)', + name: 'should ignore constants defined out of scope (react component, function expression)', + code: ` + const CONST_VAL = 1 + const MyComponent = () => { + useQuery({ + queryKey: ["foo"], + queryFn: () => CONST_VAL + }); + } + `, + }, + { + name: 'should ignore constants defined out of scope (react component, anonymous function)', + code: ` + const CONST_VAL = 1 + const MyComponent = function () { + useQuery({ + queryKey: ["foo"], + queryFn: () => CONST_VAL + }); + } + `, + }, + { + name: 'should ignore constants defined out of scope (react hook, function declaration)', code: ` const CONST_VAL = 1 function useHook() { @@ -298,6 +322,30 @@ ruleTester.run('exhaustive-deps', rule, { } `, }, + { + name: 'should ignore constants defined out of scope (react hook, function expression)', + code: ` + const CONST_VAL = 1 + const useHook = () => { + useQuery({ + queryKey: ["foo"], + queryFn: () => CONST_VAL + }); + } + `, + }, + { + name: 'should ignore constants defined out of scope (react hook, anonymous function)', + code: ` + const CONST_VAL = 1 + const useHook = function () { + useQuery({ + queryKey: ["foo"], + queryFn: () => CONST_VAL + }); + } + `, + }, { name: 'should ignore references of the queryClient', code: ` diff --git a/packages/eslint-plugin-query/src/utils/ast-utils.ts b/packages/eslint-plugin-query/src/utils/ast-utils.ts index 6b30097da7..9123551ede 100644 --- a/packages/eslint-plugin-query/src/utils/ast-utils.ts +++ b/packages/eslint-plugin-query/src/utils/ast-utils.ts @@ -203,18 +203,29 @@ export const ASTUtils = { ]), ) }, + isValidReactComponentOrHookName(identifier: TSESTree.Identifier | null) { + return identifier !== null && /^(use|[A-Z])/.test(identifier.name) + }, getReactComponentOrHookAncestor( context: Readonly>, ) { return context.getAncestors().find((x) => { + if ( + x.type === AST_NODE_TYPES.FunctionDeclaration && + ASTUtils.isValidReactComponentOrHookName(x.id) + ) { + return true + } + return ( + x.parent?.type === AST_NODE_TYPES.VariableDeclarator && + x.parent.id.type === AST_NODE_TYPES.Identifier && ASTUtils.isNodeOfOneOf(x, [ AST_NODE_TYPES.FunctionDeclaration, AST_NODE_TYPES.FunctionExpression, AST_NODE_TYPES.ArrowFunctionExpression, ]) && - x.id !== null && - /^(use|[A-Z])/.test(x.id.name) + ASTUtils.isValidReactComponentOrHookName(x.parent.id) ) }) as | TSESTree.FunctionDeclaration