Skip to content

Commit

Permalink
fix(eslint-plugin-query): exclude instanceof right identifier in exha…
Browse files Browse the repository at this point in the history
…ustive deps eslint rule (#7419)
  • Loading branch information
Newbie012 committed May 12, 2024
1 parent 3875b3b commit 2ed5fe7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
15 changes: 15 additions & 0 deletions packages/eslint-plugin-query/src/__tests__/exhaustive-deps.test.ts
Expand Up @@ -387,6 +387,21 @@ ruleTester.run('exhaustive-deps', rule, {
}
`,
},
{
name: 'instanceof value should not be in query key',
code: `
class SomeClass {}
function Component({ value }) {
useQuery({
queryKey: ['foo'],
queryFn: () => {
return value instanceof SomeClass;
}
});
}
`,
},
],
invalid: [
{
Expand Down
Expand Up @@ -41,7 +41,7 @@ export const rule = createRule({
return
}

const scopeManager = context.getSourceCode().scopeManager
const scopeManager = context.sourceCode.scopeManager
const queryKey = ASTUtils.findPropertyWithIdentifierKey(
node.parent.properties,
QUERY_KEY,
Expand Down Expand Up @@ -83,11 +83,10 @@ export const rule = createRule({
}
}

const sourceCode = context.getSourceCode()
const queryKeyValue = queryKeyNode
const externalRefs = ASTUtils.getExternalRefs({
scopeManager,
sourceCode,
sourceCode: context.sourceCode,
node: queryFn.value,
})

Expand All @@ -100,13 +99,14 @@ export const rule = createRule({
)

const existingKeys = ASTUtils.getNestedIdentifiers(queryKeyValue).map(
(identifier) => ASTUtils.mapKeyNodeToText(identifier, sourceCode),
(identifier) =>
ASTUtils.mapKeyNodeToText(identifier, context.sourceCode),
)

const missingRefs = relevantRefs
.map((ref) => ({
ref: ref,
text: ASTUtils.mapKeyNodeToText(ref.identifier, sourceCode),
text: ASTUtils.mapKeyNodeToText(ref.identifier, context.sourceCode),
}))
.filter(({ ref, text }) => {
return (
Expand All @@ -125,10 +125,12 @@ export const rule = createRule({

if (uniqueMissingRefs.length > 0) {
const missingAsText = uniqueMissingRefs
.map((ref) => ASTUtils.mapKeyNodeToText(ref.identifier, sourceCode))
.map((ref) =>
ASTUtils.mapKeyNodeToText(ref.identifier, context.sourceCode),
)
.join(', ')

const existingWithMissing = sourceCode
const existingWithMissing = context.sourceCode
.getText(queryKeyValue)
.replace(/\]$/, `, ${missingAsText}]`)

Expand Down
@@ -1,6 +1,6 @@
import { AST_NODE_TYPES } from '@typescript-eslint/utils'
import { ASTUtils } from '../../utils/ast-utils'
import type { TSESLint } from '@typescript-eslint/utils'
import type { TSESLint, TSESTree } from '@typescript-eslint/utils'

export const ExhaustiveDepsUtils = {
isRelevantReference(params: {
Expand All @@ -25,9 +25,16 @@ export const ExhaustiveDepsUtils = {
return (
reference.identifier.name !== 'undefined' &&
reference.identifier.parent.type !== AST_NODE_TYPES.NewExpression &&
!ExhaustiveDepsUtils.isInstanceOfKind(reference.identifier.parent) &&
!ExhaustiveDepsUtils.isQueryClientReference(reference)
)
},
isInstanceOfKind(node: TSESTree.Node) {
return (
node.type === AST_NODE_TYPES.BinaryExpression &&
node.operator === 'instanceof'
)
},
isQueryClientReference(reference: TSESLint.Scope.Reference) {
const declarator = reference.resolved?.defs[0]?.node

Expand Down

0 comments on commit 2ed5fe7

Please sign in to comment.