Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eslint-plugin): handle ternary expressions in queryFn for exhaustive-deps rule #7747

Merged
merged 1 commit into from
Jul 17, 2024
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
34 changes: 34 additions & 0 deletions packages/eslint-plugin-query/src/__tests__/exhaustive-deps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,20 @@ ruleTester.run('exhaustive-deps', rule, {
}
`,
},
{
name: 'queryFn as a ternary expression with dep and a skipToken',
code: normalizeIndent`
import { useQuery, skipToken } from "@tanstack/react-query";
const fetch = true

function Component({ id }) {
useQuery({
queryKey: [id],
queryFn: fetch ? () => Promise.resolve(id) : skipToken
})
}
`,
},
],
invalid: [
{
Expand Down Expand Up @@ -733,5 +747,25 @@ ruleTester.run('exhaustive-deps', rule, {
},
],
},
{
name: 'should fail if queryFn is a ternary expression with missing dep and a skipToken',
code: normalizeIndent`
import { useQuery, skipToken } from "@tanstack/react-query";
const fetch = true

function Component({ id }) {
useQuery({
queryKey: [],
queryFn: fetch ? () => Promise.resolve(id) : skipToken
})
}
`,
errors: [
{
messageId: 'missingDeps',
data: { deps: 'id' },
},
],
},
],
})
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { uniqueBy } from '../../utils/unique-by'
import { detectTanstackQueryImports } from '../../utils/detect-react-query-imports'
import { ExhaustiveDepsUtils } from './exhaustive-deps.utils'
import type { TSESLint } from '@typescript-eslint/utils'
import type { TSESLint, TSESTree } from '@typescript-eslint/utils'
import type { ExtraRuleDocs } from '../../types'

const QUERY_KEY = 'queryKey'
Expand Down Expand Up @@ -59,6 +59,7 @@
!ASTUtils.isNodeOfOneOf(queryFn.value, [
AST_NODE_TYPES.ArrowFunctionExpression,
AST_NODE_TYPES.FunctionExpression,
AST_NODE_TYPES.ConditionalExpression,
])
) {
return
Expand Down Expand Up @@ -88,15 +89,15 @@
const externalRefs = ASTUtils.getExternalRefs({
scopeManager,
sourceCode: context.sourceCode,
node: queryFn.value,
node: getQueryFnRelevantNode(queryFn),
})

const relevantRefs = externalRefs.filter((reference) =>
ExhaustiveDepsUtils.isRelevantReference({
sourceCode: context.sourceCode,
reference,
scopeManager,
node: queryFn.value,
node: getQueryFnRelevantNode(queryFn),
}),
)

Expand Down Expand Up @@ -161,3 +162,18 @@
}
}),
})

function getQueryFnRelevantNode(queryFn: TSESTree.Property) {
if (queryFn.value.type !== AST_NODE_TYPES.ConditionalExpression) {
return queryFn.value
}

if (
queryFn.value.consequent.type === AST_NODE_TYPES.Identifier &&
queryFn.value.consequent.name === 'skipToken'

Check warning on line 173 in packages/eslint-plugin-query/src/rules/exhaustive-deps/exhaustive-deps.rule.ts

View check run for this annotation

Codecov / codecov/patch

packages/eslint-plugin-query/src/rules/exhaustive-deps/exhaustive-deps.rule.ts#L173

Added line #L173 was not covered by tests
) {
return queryFn.value.alternate

Check warning on line 175 in packages/eslint-plugin-query/src/rules/exhaustive-deps/exhaustive-deps.rule.ts

View check run for this annotation

Codecov / codecov/patch

packages/eslint-plugin-query/src/rules/exhaustive-deps/exhaustive-deps.rule.ts#L175

Added line #L175 was not covered by tests
}

return queryFn.value.consequent
}
Loading