Open History to see the last commit
1import { AST_NODE_TYPES, ESLintUtils } from '@typescript-eslint/utils'
2import { ASTUtils } from '../../utils/ast-utils'
3import { detectTanstackQueryImports } from '../../utils/detect-react-query-imports'
4import { getDocsUrl } from '../../utils/get-docs-url'
5import { NoRestDestructuringUtils } from './no-rest-destructuring.utils'
6import type { ExtraRuleDocs } from '../../types'
7β
8export const name = 'no-rest-destructuring'
9β
10const queryHooks = [
11 'useQuery',
12 'useQueries',
13 'useInfiniteQuery',
14 'useSuspenseQuery',
15 'useSuspenseQueries',
16 'useSuspenseInfiniteQuery',
17]
18β
19const createRule = ESLintUtils.RuleCreator<ExtraRuleDocs>(getDocsUrl)
20β
21export const rule = createRule({
22 name,
23 meta: {
24 type: 'problem',
25 docs: {
26 description: 'Disallows rest destructuring in queries',
27 recommended: 'warn',
28 },
29 messages: {
30 objectRestDestructure: `Object rest destructuring on a query will observe all changes to the query, leading to excessive re-renders.`,
31 },
32 schema: [],
33 },
34 defaultOptions: [],
35β
36 create: detectTanstackQueryImports((context, _, helpers) => {
37 const queryResultVariables = new Set<string>()
38β
39 return {
40 CallExpression: (node) => {
41 if (node.parent.type !== AST_NODE_TYPES.VariableDeclarator) {
42 return
43 }
44β
45 const returnValue = node.parent.id
46β
47 const isDirectHook =
48 ASTUtils.isIdentifierWithOneOfNames(node.callee, queryHooks) &&
49 helpers.isTanstackQueryImport(node.callee)
50β
51 if (!isDirectHook) {
52 // The type-aware path can only report when the result is rest
53 // destructured or assigned to an identifier that may later be
54 // spread. Skip the expensive type lookup for any other binding.
55 const canReportQueryResult =
56 returnValue.type === AST_NODE_TYPES.Identifier ||
57 NoRestDestructuringUtils.isObjectRestDestructuring(returnValue)
58β
59 if (
60 !canReportQueryResult ||
61 !NoRestDestructuringUtils.isQueryResultCall(
62 node,
63 context.sourceCode.parserServices,
64 )
65 ) {
66 return
67 }
68 }
69β
70 const calleeName = ASTUtils.isIdentifier(node.callee)
71 ? node.callee.name
72 : null
73β
74 if (
75 calleeName !== 'useQueries' &&
76 calleeName !== 'useSuspenseQueries'
77 ) {
78 if (NoRestDestructuringUtils.isObjectRestDestructuring(returnValue)) {
79 return context.report({
80 node: node.parent,
81 messageId: 'objectRestDestructure',
82 })
83 }
84β
85 if (returnValue.type === AST_NODE_TYPES.Identifier) {
86 queryResultVariables.add(returnValue.name)
87 }
88β
89 return
90 }
91β
92 if (returnValue.type !== AST_NODE_TYPES.ArrayPattern) {
93 if (returnValue.type === AST_NODE_TYPES.Identifier) {
94 queryResultVariables.add(returnValue.name)
95 }
96 return
97 }
98β
99 returnValue.elements.forEach((queryResult) => {
100 if (queryResult === null) {
101 return
102 }
103 if (NoRestDestructuringUtils.isObjectRestDestructuring(queryResult)) {
104 context.report({
105 node: queryResult,
106 messageId: 'objectRestDestructure',
107 })
108 }
109 })
110 },
111β
112 VariableDeclarator: (node) => {
113 if (
114 node.init?.type === AST_NODE_TYPES.Identifier &&
115 queryResultVariables.has(node.init.name) &&
116 NoRestDestructuringUtils.isObjectRestDestructuring(node.id)
117 ) {
118 context.report({
119 node,
120 messageId: 'objectRestDestructure',
121 })
122 }
123 },
124β
125 SpreadElement: (node) => {
126 if (
127 node.argument.type === AST_NODE_TYPES.Identifier &&
128 queryResultVariables.has(node.argument.name)
129 ) {
130 context.report({
131 node,
132 messageId: 'objectRestDestructure',
133 })
134 }
135 },
136 }
137 }),
138})