Skip to content

Commit

Permalink
fix(eslint-plugin-query): add Suspense query hooks to no-rest-destruc…
Browse files Browse the repository at this point in the history
…turing rule (#8260)

* fix(eslint-plugin-query): add missing hooks to no-rest-destructuring rule

* test(no-rest-destructuring): add test cases for Suspense query hooks
  • Loading branch information
gyumong authored Nov 8, 2024
1 parent 3ea6c0c commit 2f6c028
Show file tree
Hide file tree
Showing 2 changed files with 189 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,144 @@ ruleTester.run('no-rest-destructuring', rule, {
}
`,
},
{
name: 'useSuspenseQuery is not captured',
code: normalizeIndent`
import { useSuspenseQuery } from '@tanstack/react-query'
function Component() {
useSuspenseQuery()
return
}
`,
},
{
name: 'useSuspenseQuery is not destructured',
code: normalizeIndent`
import { useSuspenseQuery } from '@tanstack/react-query'
function Component() {
const query = useSuspenseQuery()
return
}
`,
},
{
name: 'useSuspenseQuery is destructured without rest',
code: normalizeIndent`
import { useSuspenseQuery } from '@tanstack/react-query'
function Component() {
const { data, isLoading, isError } = useSuspenseQuery()
return
}
`,
},
{
name: 'useSuspenseInfiniteQuery is not captured',
code: normalizeIndent`
import { useSuspenseInfiniteQuery } from '@tanstack/react-query'
function Component() {
useSuspenseInfiniteQuery()
return
}
`,
},
{
name: 'useSuspenseInfiniteQuery is not destructured',
code: normalizeIndent`
import { useSuspenseInfiniteQuery } from '@tanstack/react-query'
function Component() {
const query = useSuspenseInfiniteQuery()
return
}
`,
},
{
name: 'useSuspenseInfiniteQuery is destructured without rest',
code: normalizeIndent`
import { useSuspenseInfiniteQuery } from '@tanstack/react-query'
function Component() {
const { data, isLoading, isError } = useSuspenseInfiniteQuery()
return
}
`,
},
{
name: 'useSuspenseQueries is not captured',
code: normalizeIndent`
import { useSuspenseQueries } from '@tanstack/react-query'
function Component() {
useSuspenseQueries([])
return
}
`,
},
{
name: 'useSuspenseQueries is not destructured',
code: normalizeIndent`
import { useSuspenseQueries } from '@tanstack/react-query'
function Component() {
const queries = useSuspenseQueries([])
return
}
`,
},
{
name: 'useSuspenseQueries array has no rest destructured element',
code: normalizeIndent`
import { useSuspenseQueries } from '@tanstack/react-query'
function Component() {
const [query1, { data, isLoading }] = useSuspenseQueries([
{ queryKey: ['key1'], queryFn: () => {} },
{ queryKey: ['key2'], queryFn: () => {} },
])
return
}
`,
},
{
name: 'useSuspenseQuery is destructured with rest but not from tanstack query',
code: normalizeIndent`
import { useSuspenseQuery } from 'other-package'
function Component() {
const { data, ...rest } = useSuspenseQuery()
return
}
`,
},
{
name: 'useSuspenseInfiniteQuery is destructured with rest but not from tanstack query',
code: normalizeIndent`
import { useSuspenseInfiniteQuery } from 'other-package'
function Component() {
const { data, ...rest } = useSuspenseInfiniteQuery()
return
}
`,
},
{
name: 'useSuspenseQueries array has rest destructured element but not from tanstack query',
code: normalizeIndent`
import { useSuspenseQueries } from 'other-package'
function Component() {
const [query1, { data, ...rest }] = useSuspenseQueries([
{ queryKey: ['key1'], queryFn: () => {} },
{ queryKey: ['key2'], queryFn: () => {} },
])
return
}
`,
},
],
invalid: [
{
Expand Down Expand Up @@ -188,5 +326,44 @@ ruleTester.run('no-rest-destructuring', rule, {
`,
errors: [{ messageId: 'objectRestDestructure' }],
},
{
name: 'useSuspenseQuery is destructured with rest',
code: normalizeIndent`
import { useSuspenseQuery } from '@tanstack/react-query'
function Component() {
const { data, ...rest } = useSuspenseQuery()
return
}
`,
errors: [{ messageId: 'objectRestDestructure' }],
},
{
name: 'useSuspenseInfiniteQuery is destructured with rest',
code: normalizeIndent`
import { useSuspenseInfiniteQuery } from '@tanstack/react-query'
function Component() {
const { data, ...rest } = useSuspenseInfiniteQuery()
return
}
`,
errors: [{ messageId: 'objectRestDestructure' }],
},
{
name: 'useSuspenseQueries is destructured with rest',
code: normalizeIndent`
import { useSuspenseQueries } from '@tanstack/react-query'
function Component() {
const [query1, { data, ...rest }] = useSuspenseQueries([
{ queryKey: ['key1'], queryFn: () => {} },
{ queryKey: ['key2'], queryFn: () => {} },
])
return
}
`,
errors: [{ messageId: 'objectRestDestructure' }],
},
],
})
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ import type { ExtraRuleDocs } from '../../types'

export const name = 'no-rest-destructuring'

const queryHooks = ['useQuery', 'useQueries', 'useInfiniteQuery']
const queryHooks = [
'useQuery',
'useQueries',
'useInfiniteQuery',
'useSuspenseQuery',
'useSuspenseQueries',
'useSuspenseInfiniteQuery',
]

const createRule = ESLintUtils.RuleCreator<ExtraRuleDocs>(getDocsUrl)

Expand Down Expand Up @@ -38,7 +45,10 @@ export const rule = createRule({
}

const returnValue = node.parent.id
if (node.callee.name !== 'useQueries') {
if (
node.callee.name !== 'useQueries' &&
node.callee.name !== 'useSuspenseQueries'
) {
if (NoRestDestructuringUtils.isObjectRestDestructuring(returnValue)) {
context.report({
node: node.parent,
Expand Down

0 comments on commit 2f6c028

Please sign in to comment.