Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,20 @@ export const rule = createRule({

const sourceCode = context.getSourceCode()
const queryKeyValue = queryKeyNode
const reactComponent = ASTUtils.getReactComponentOrHookAncestor(context)
const refs = ASTUtils.getExternalRefs({
scopeManager,
sourceCode,
node: queryFn.value,
}).filter((ref) => {
return (
reactComponent === undefined ||
ASTUtils.isDeclaredInNode({
scopeManager,
functionNode: reactComponent,
reference: ref,
})
)
})

const relevantRefs = refs.filter((ref) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,30 @@ ruleTester.run('exhaustive-deps', rule, {
});
`,
},
{
name: 'should ignore constants defined out of scope (react component)',
code: `
const CONST_VAL = 1
function MyComponent() {
useQuery({
queryKey: ["foo"],
queryFn: () => CONST_VAL
});
}
`,
},
{
name: 'should ignore constants defined out of scope (react hook)',
code: `
const CONST_VAL = 1
function useHook() {
useQuery({
queryKey: ["foo"],
queryFn: () => CONST_VAL
});
}
`,
},
],
invalid: [
{
Expand Down
34 changes: 34 additions & 0 deletions packages/eslint-plugin-query/src/utils/ast-utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { TSESLint, TSESTree } from '@typescript-eslint/utils'
import type TSESLintScopeManager from '@typescript-eslint/scope-manager'
import { AST_NODE_TYPES } from '@typescript-eslint/utils'
import type { RuleContext } from '@typescript-eslint/utils/dist/ts-eslint'
import { uniqueBy } from './unique-by'
Expand Down Expand Up @@ -140,6 +141,20 @@ export const ASTUtils = {

return identifier
},
isDeclaredInNode(params: {
functionNode: TSESTree.Node
reference: TSESLintScopeManager.Reference
scopeManager: TSESLint.Scope.ScopeManager
}) {
const { functionNode, reference, scopeManager } = params
const scope = scopeManager.acquire(functionNode)

if (scope === null) {
return false
}

return scope.set.has(reference.identifier.name)
},
getExternalRefs(params: {
scopeManager: TSESLint.Scope.ScopeManager
sourceCode: Readonly<TSESLint.SourceCode>
Expand Down Expand Up @@ -188,6 +203,25 @@ export const ASTUtils = {
]),
)
},
getReactComponentOrHookAncestor(
context: Readonly<RuleContext<string, readonly unknown[]>>,
) {
return context.getAncestors().find((x) => {
return (
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)
)
}) as
| TSESTree.FunctionDeclaration
| TSESTree.FunctionExpression
| TSESTree.ArrowFunctionExpression
| undefined
},
getReferencedExpressionByIdentifier(params: {
node: TSESTree.Node
context: Readonly<RuleContext<string, readonly unknown[]>>
Expand Down