@@ -34,33 +34,46 @@ export const rule = createRule({
34
34
defaultOptions : [ ] ,
35
35
36
36
create : detectTanstackQueryImports ( ( context , _ , helpers ) => {
37
+ // Track variables that hold query results
38
+ const queryResultVariables = new Set < string > ( )
39
+
37
40
return {
38
41
CallExpression : ( node ) => {
39
42
if (
40
43
! ASTUtils . isIdentifierWithOneOfNames ( node . callee , queryHooks ) ||
41
- ! helpers . isTanstackQueryImport ( node . callee ) ||
42
- node . parent . type !== AST_NODE_TYPES . VariableDeclarator
44
+ node . parent . type !== AST_NODE_TYPES . VariableDeclarator ||
45
+ ! helpers . isTanstackQueryImport ( node . callee )
43
46
) {
44
47
return
45
48
}
46
49
47
50
const returnValue = node . parent . id
51
+
48
52
if (
49
53
node . callee . name !== 'useQueries' &&
50
54
node . callee . name !== 'useSuspenseQueries'
51
55
) {
52
56
if ( NoRestDestructuringUtils . isObjectRestDestructuring ( returnValue ) ) {
53
- context . report ( {
57
+ return context . report ( {
54
58
node : node . parent ,
55
59
messageId : 'objectRestDestructure' ,
56
60
} )
57
61
}
62
+
63
+ if ( returnValue . type === AST_NODE_TYPES . Identifier ) {
64
+ queryResultVariables . add ( returnValue . name )
65
+ }
66
+
58
67
return
59
68
}
60
69
61
70
if ( returnValue . type !== AST_NODE_TYPES . ArrayPattern ) {
71
+ if ( returnValue . type === AST_NODE_TYPES . Identifier ) {
72
+ queryResultVariables . add ( returnValue . name )
73
+ }
62
74
return
63
75
}
76
+
64
77
returnValue . elements . forEach ( ( queryResult ) => {
65
78
if ( queryResult === null ) {
66
79
return
@@ -73,6 +86,33 @@ export const rule = createRule({
73
86
}
74
87
} )
75
88
} ,
89
+
90
+ // Check for later destructuring of tracked variables
91
+ VariableDeclarator : ( node ) => {
92
+ if (
93
+ node . init ?. type === AST_NODE_TYPES . Identifier &&
94
+ queryResultVariables . has ( node . init . name ) &&
95
+ NoRestDestructuringUtils . isObjectRestDestructuring ( node . id )
96
+ ) {
97
+ context . report ( {
98
+ node,
99
+ messageId : 'objectRestDestructure' ,
100
+ } )
101
+ }
102
+ } ,
103
+
104
+ // Check for spread operations in object expressions
105
+ SpreadElement : ( node ) => {
106
+ if (
107
+ node . argument . type === AST_NODE_TYPES . Identifier &&
108
+ queryResultVariables . has ( node . argument . name )
109
+ ) {
110
+ context . report ( {
111
+ node,
112
+ messageId : 'objectRestDestructure' ,
113
+ } )
114
+ }
115
+ } ,
76
116
}
77
117
} ) ,
78
118
} )
0 commit comments