@@ -2,8 +2,14 @@ import type { TSESLint, TSESTree } from '@typescript-eslint/utils'
2
2
import { AST_NODE_TYPES } from '@typescript-eslint/utils'
3
3
import { createRule } from '../../utils/create-rule'
4
4
import { ASTUtils } from '../../utils/ast-utils'
5
+ import { objectKeys } from '../../utils/object-utils'
5
6
6
- const QUERY_CALLS = [ 'useQuery' , 'createQuery' ]
7
+ const QUERY_CALLS = {
8
+ useQuery : { key : 'queryKey' , fn : 'queryFn' } ,
9
+ createQuery : { key : 'queryKey' , fn : 'queryFn' } ,
10
+ useMutation : { key : 'mutationKey' , fn : 'mutationFn' } ,
11
+ createMutation : { key : 'mutationKey' , fn : 'mutationFn' } ,
12
+ }
7
13
8
14
const messages = {
9
15
preferObjectSyntax : `Objects syntax for query is preferred` ,
@@ -32,11 +38,13 @@ export const rule: TSESLint.RuleModule<MessageKey, readonly unknown[]> =
32
38
create ( context , _ , helpers ) {
33
39
return {
34
40
CallExpression ( node ) {
35
- const isTanstackQueryCall =
36
- ASTUtils . isIdentifierWithOneOfNames ( node . callee , QUERY_CALLS ) &&
37
- helpers . isTanstackQueryImport ( node . callee )
38
-
39
- if ( ! isTanstackQueryCall ) {
41
+ if (
42
+ ! ASTUtils . isIdentifierWithOneOfNames (
43
+ node . callee ,
44
+ objectKeys ( QUERY_CALLS ) ,
45
+ ) ||
46
+ ! helpers . isTanstackQueryImport ( node . callee )
47
+ ) {
40
48
return
41
49
}
42
50
@@ -101,6 +109,7 @@ export const rule: TSESLint.RuleModule<MessageKey, readonly unknown[]> =
101
109
runCheckOnNode ( {
102
110
context : context ,
103
111
callNode : node ,
112
+ callString : node . callee . name ,
104
113
expression : stmt . argument ,
105
114
messageId : 'returnTypeAreNotObjectSyntax' ,
106
115
} )
@@ -121,6 +130,7 @@ export const rule: TSESLint.RuleModule<MessageKey, readonly unknown[]> =
121
130
return runCheckOnNode ( {
122
131
context : context ,
123
132
callNode : node ,
133
+ callString : node . callee . name ,
124
134
expression : referencedNode ,
125
135
messageId : 'preferObjectSyntax' ,
126
136
} )
@@ -130,6 +140,7 @@ export const rule: TSESLint.RuleModule<MessageKey, readonly unknown[]> =
130
140
runCheckOnNode ( {
131
141
context : context ,
132
142
callNode : node ,
143
+ callString : node . callee . name ,
133
144
expression : firstArgument ,
134
145
messageId : 'preferObjectSyntax' ,
135
146
} )
@@ -141,10 +152,11 @@ export const rule: TSESLint.RuleModule<MessageKey, readonly unknown[]> =
141
152
function runCheckOnNode ( params : {
142
153
context : Readonly < TSESLint . RuleContext < MessageKey , readonly unknown [ ] > >
143
154
callNode : TSESTree . CallExpression
155
+ callString : keyof typeof QUERY_CALLS
144
156
expression : TSESTree . Node
145
157
messageId : MessageKey
146
158
} ) {
147
- const { context, expression, messageId, callNode } = params
159
+ const { context, expression, messageId, callNode, callString } = params
148
160
const sourceCode = context . getSourceCode ( )
149
161
150
162
if ( expression . type === AST_NODE_TYPES . ObjectExpression ) {
@@ -184,6 +196,8 @@ function runCheckOnNode(params: {
184
196
return
185
197
}
186
198
199
+ const callProps = QUERY_CALLS [ callString ]
200
+
187
201
context . report ( {
188
202
node : callNode ,
189
203
messageId : 'preferObjectSyntax' ,
@@ -194,15 +208,19 @@ function runCheckOnNode(params: {
194
208
const firstArgument = callNode . arguments [ 0 ]
195
209
const queryKey = sourceCode . getText ( firstArgument )
196
210
const queryKeyProperty =
197
- queryKey === 'queryKey' ? 'queryKey' : `queryKey: ${ queryKey } `
211
+ queryKey === callProps . key
212
+ ? callProps . key
213
+ : `${ callProps . key } : ${ queryKey } `
198
214
199
215
optionsObjectProperties . push ( queryKeyProperty )
200
216
201
217
// queryFn
202
218
if ( secondArgument && secondArgument !== optionsObject ) {
203
219
const queryFn = sourceCode . getText ( secondArgument )
204
220
const queryFnProperty =
205
- queryFn === 'queryFn' ? 'queryFn' : `queryFn: ${ queryFn } `
221
+ queryFn === callProps . fn
222
+ ? callProps . fn
223
+ : `${ callProps . fn } : ${ queryFn } `
206
224
207
225
optionsObjectProperties . push ( queryFnProperty )
208
226
}
0 commit comments