From eb6792deb1e020ae6d5b569fddd7c946caaf0166 Mon Sep 17 00:00:00 2001 From: Newbie012 Date: Mon, 22 May 2023 22:45:31 +0300 Subject: [PATCH] fix(eslint-plugin): lookup for any function ancestor --- .../exhaustive-deps/exhaustive-deps.test.ts | 12 ++++++++++++ .../exhaustive-deps/exhaustive-deps.utils.ts | 2 +- .../eslint-plugin-query/src/utils/ast-utils.ts | 16 ++++------------ 3 files changed, 17 insertions(+), 13 deletions(-) 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 32608b929d..7c927768cf 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 @@ -310,6 +310,18 @@ ruleTester.run('exhaustive-deps', rule, { } `, }, + { + name: 'should ignore constants defined out of scope (non react component/hook function)', + code: ` + const CONST_VAL = 1 + function fn() { + return { + queryKey: ["foo"], + queryFn: () => CONST_VAL + } + } + `, + }, { name: 'should ignore constants defined out of scope (react hook, function declaration)', code: ` diff --git a/packages/eslint-plugin-query/src/rules/exhaustive-deps/exhaustive-deps.utils.ts b/packages/eslint-plugin-query/src/rules/exhaustive-deps/exhaustive-deps.utils.ts index e9768ec5dc..f60bf20cab 100644 --- a/packages/eslint-plugin-query/src/rules/exhaustive-deps/exhaustive-deps.utils.ts +++ b/packages/eslint-plugin-query/src/rules/exhaustive-deps/exhaustive-deps.utils.ts @@ -9,7 +9,7 @@ export const ExhaustiveDepsUtils = { scopeManager: TSESLint.Scope.ScopeManager }) { const { reference, scopeManager, context } = params - const component = ASTUtils.getReactComponentOrHookAncestor(context) + const component = ASTUtils.getFunctionAncestor(context) if ( component !== undefined && diff --git a/packages/eslint-plugin-query/src/utils/ast-utils.ts b/packages/eslint-plugin-query/src/utils/ast-utils.ts index 9123551ede..540063325b 100644 --- a/packages/eslint-plugin-query/src/utils/ast-utils.ts +++ b/packages/eslint-plugin-query/src/utils/ast-utils.ts @@ -206,14 +206,11 @@ export const ASTUtils = { isValidReactComponentOrHookName(identifier: TSESTree.Identifier | null) { return identifier !== null && /^(use|[A-Z])/.test(identifier.name) }, - getReactComponentOrHookAncestor( + getFunctionAncestor( context: Readonly>, ) { return context.getAncestors().find((x) => { - if ( - x.type === AST_NODE_TYPES.FunctionDeclaration && - ASTUtils.isValidReactComponentOrHookName(x.id) - ) { + if (x.type === AST_NODE_TYPES.FunctionDeclaration) { return true } @@ -224,14 +221,9 @@ export const ASTUtils = { AST_NODE_TYPES.FunctionDeclaration, AST_NODE_TYPES.FunctionExpression, AST_NODE_TYPES.ArrowFunctionExpression, - ]) && - ASTUtils.isValidReactComponentOrHookName(x.parent.id) + ]) ) - }) as - | TSESTree.FunctionDeclaration - | TSESTree.FunctionExpression - | TSESTree.ArrowFunctionExpression - | undefined + }) }, getReferencedExpressionByIdentifier(params: { node: TSESTree.Node