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 @@ -365,5 +365,30 @@ ruleTester.run('no-rest-destructuring', rule, {
`,
errors: [{ messageId: 'objectRestDestructure' }],
},
{
name: 'useQuery result is spread in return statement',
code: normalizeIndent`
import { useQuery } from '@tanstack/react-query'

function Component() {
const query = useQuery()
return { ...query, data: query.data[0] }
}
`,
errors: [{ messageId: 'objectRestDestructure' }],
},
{
name: 'useQuery result is spread in object expression',
code: normalizeIndent`
import { useQuery } from '@tanstack/react-query'

function Component() {
const query = useQuery()
const result = { ...query, data: query.data[0] }
return result
}
`,
errors: [{ messageId: 'objectRestDestructure' }],
},
],
})
Original file line number Diff line number Diff line change
Expand Up @@ -34,33 +34,45 @@
defaultOptions: [],

create: detectTanstackQueryImports((context, _, helpers) => {
const queryResultVariables = new Set<string>()

return {
CallExpression: (node) => {
if (
!ASTUtils.isIdentifierWithOneOfNames(node.callee, queryHooks) ||
!helpers.isTanstackQueryImport(node.callee) ||
node.parent.type !== AST_NODE_TYPES.VariableDeclarator
node.parent.type !== AST_NODE_TYPES.VariableDeclarator ||
!helpers.isTanstackQueryImport(node.callee)
) {
return
}

const returnValue = node.parent.id

if (
node.callee.name !== 'useQueries' &&
node.callee.name !== 'useSuspenseQueries'
) {
if (NoRestDestructuringUtils.isObjectRestDestructuring(returnValue)) {
context.report({
return context.report({
node: node.parent,
messageId: 'objectRestDestructure',
})
}

if (returnValue.type === AST_NODE_TYPES.Identifier) {
queryResultVariables.add(returnValue.name)
}

return
}

if (returnValue.type !== AST_NODE_TYPES.ArrayPattern) {
if (returnValue.type === AST_NODE_TYPES.Identifier) {
queryResultVariables.add(returnValue.name)
}
return
}

returnValue.elements.forEach((queryResult) => {
if (queryResult === null) {
return
Expand All @@ -73,6 +85,31 @@
}
})
},

VariableDeclarator: (node) => {
if (
node.init?.type === AST_NODE_TYPES.Identifier &&
queryResultVariables.has(node.init.name) &&
NoRestDestructuringUtils.isObjectRestDestructuring(node.id)

Check warning on line 93 in packages/eslint-plugin-query/src/rules/no-rest-destructuring/no-rest-destructuring.rule.ts

View check run for this annotation

Codecov / codecov/patch

packages/eslint-plugin-query/src/rules/no-rest-destructuring/no-rest-destructuring.rule.ts#L92-L93

Added lines #L92 - L93 were not covered by tests
) {
context.report({

Check warning on line 95 in packages/eslint-plugin-query/src/rules/no-rest-destructuring/no-rest-destructuring.rule.ts

View check run for this annotation

Codecov / codecov/patch

packages/eslint-plugin-query/src/rules/no-rest-destructuring/no-rest-destructuring.rule.ts#L95

Added line #L95 was not covered by tests
node,
messageId: 'objectRestDestructure',
})
}
},

SpreadElement: (node) => {
if (
node.argument.type === AST_NODE_TYPES.Identifier &&
queryResultVariables.has(node.argument.name)
) {
context.report({
node,
messageId: 'objectRestDestructure',
})
}
},
}
}),
})