@@ -2,8 +2,14 @@ import type { TSESLint, TSESTree } from '@typescript-eslint/utils'
22import { AST_NODE_TYPES } from '@typescript-eslint/utils'
33import { createRule } from '../../utils/create-rule'
44import { ASTUtils } from '../../utils/ast-utils'
5+ import { objectKeys } from '../../utils/object-utils'
56
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+ }
713
814const messages = {
915 preferObjectSyntax : `Objects syntax for query is preferred` ,
@@ -32,11 +38,13 @@ export const rule: TSESLint.RuleModule<MessageKey, readonly unknown[]> =
3238 create ( context , _ , helpers ) {
3339 return {
3440 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+ ) {
4048 return
4149 }
4250
@@ -101,6 +109,7 @@ export const rule: TSESLint.RuleModule<MessageKey, readonly unknown[]> =
101109 runCheckOnNode ( {
102110 context : context ,
103111 callNode : node ,
112+ callString : node . callee . name ,
104113 expression : stmt . argument ,
105114 messageId : 'returnTypeAreNotObjectSyntax' ,
106115 } )
@@ -121,6 +130,7 @@ export const rule: TSESLint.RuleModule<MessageKey, readonly unknown[]> =
121130 return runCheckOnNode ( {
122131 context : context ,
123132 callNode : node ,
133+ callString : node . callee . name ,
124134 expression : referencedNode ,
125135 messageId : 'preferObjectSyntax' ,
126136 } )
@@ -130,6 +140,7 @@ export const rule: TSESLint.RuleModule<MessageKey, readonly unknown[]> =
130140 runCheckOnNode ( {
131141 context : context ,
132142 callNode : node ,
143+ callString : node . callee . name ,
133144 expression : firstArgument ,
134145 messageId : 'preferObjectSyntax' ,
135146 } )
@@ -141,10 +152,11 @@ export const rule: TSESLint.RuleModule<MessageKey, readonly unknown[]> =
141152function runCheckOnNode ( params : {
142153 context : Readonly < TSESLint . RuleContext < MessageKey , readonly unknown [ ] > >
143154 callNode : TSESTree . CallExpression
155+ callString : keyof typeof QUERY_CALLS
144156 expression : TSESTree . Node
145157 messageId : MessageKey
146158} ) {
147- const { context, expression, messageId, callNode } = params
159+ const { context, expression, messageId, callNode, callString } = params
148160 const sourceCode = context . getSourceCode ( )
149161
150162 if ( expression . type === AST_NODE_TYPES . ObjectExpression ) {
@@ -184,6 +196,8 @@ function runCheckOnNode(params: {
184196 return
185197 }
186198
199+ const callProps = QUERY_CALLS [ callString ]
200+
187201 context . report ( {
188202 node : callNode ,
189203 messageId : 'preferObjectSyntax' ,
@@ -194,15 +208,19 @@ function runCheckOnNode(params: {
194208 const firstArgument = callNode . arguments [ 0 ]
195209 const queryKey = sourceCode . getText ( firstArgument )
196210 const queryKeyProperty =
197- queryKey === 'queryKey' ? 'queryKey' : `queryKey: ${ queryKey } `
211+ queryKey === callProps . key
212+ ? callProps . key
213+ : `${ callProps . key } : ${ queryKey } `
198214
199215 optionsObjectProperties . push ( queryKeyProperty )
200216
201217 // queryFn
202218 if ( secondArgument && secondArgument !== optionsObject ) {
203219 const queryFn = sourceCode . getText ( secondArgument )
204220 const queryFnProperty =
205- queryFn === 'queryFn' ? 'queryFn' : `queryFn: ${ queryFn } `
221+ queryFn === callProps . fn
222+ ? callProps . fn
223+ : `${ callProps . fn } : ${ queryFn } `
206224
207225 optionsObjectProperties . push ( queryFnProperty )
208226 }
0 commit comments