Skip to content

Commit

Permalink
fix(eslint-plugin): lookup for any function ancestor (#5448)
Browse files Browse the repository at this point in the history
  • Loading branch information
Newbie012 committed May 22, 2023
1 parent 820258b commit 3c561ba
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
Expand Up @@ -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: `
Expand Down
Expand Up @@ -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 &&
Expand Down
16 changes: 4 additions & 12 deletions packages/eslint-plugin-query/src/utils/ast-utils.ts
Expand Up @@ -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<RuleContext<string, readonly unknown[]>>,
) {
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
}

Expand All @@ -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
Expand Down

0 comments on commit 3c561ba

Please sign in to comment.