Skip to content

Commit 48e806b

Browse files
authored
fix(eslint-plugin-query): mutation function is turned into mutation key (#4973)
1 parent 5a5fd6b commit 48e806b

File tree

2 files changed

+45
-21
lines changed

2 files changed

+45
-21
lines changed

packages/eslint-plugin-query/src/rules/prefer-query-object-syntax/prefer-query-object-syntax.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,17 @@ ruleTester.run(name, rule, {
413413
useMutation({ mutationKey: ["mutation", "key"], mutationFn: async () => await fetchUserById(userId) });
414414
`,
415415
},
416+
{
417+
code: normalizeIndent`
418+
import { useMutation } from "@tanstack/react-query";
419+
useMutation(async () => await fetchUserById(userId));
420+
`,
421+
errors: [{ messageId: 'preferObjectSyntax' }],
422+
output: normalizeIndent`
423+
import { useMutation } from "@tanstack/react-query";
424+
useMutation({ mutationFn: async () => await fetchUserById(userId) });
425+
`,
426+
},
416427
{
417428
code: normalizeIndent`
418429
import { createMutation } from "@tanstack/solid-query";

packages/eslint-plugin-query/src/rules/prefer-query-object-syntax/prefer-query-object-syntax.ts

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import { ASTUtils } from '../../utils/ast-utils'
55
import { objectKeys } from '../../utils/object-utils'
66

77
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' },
8+
useQuery: { key: 'queryKey', fn: 'queryFn', type: 'query' },
9+
createQuery: { key: 'queryKey', fn: 'queryFn', type: 'query' },
10+
useMutation: { key: 'mutationKey', fn: 'mutationFn', type: 'mutation' },
11+
createMutation: { key: 'mutationKey', fn: 'mutationFn', type: 'mutation' },
1212
}
1313

1414
const messages = {
@@ -208,25 +208,38 @@ function runCheckOnNode(params: {
208208
fix(fixer) {
209209
const optionsObjectProperties: string[] = []
210210

211-
// queryKey
212-
const firstArgument = callNode.arguments[0]
213-
const queryKey = sourceCode.getText(firstArgument)
214-
const queryKeyProperty =
215-
queryKey === callProps.key
216-
? callProps.key
217-
: `${callProps.key}: ${queryKey}`
218-
219-
optionsObjectProperties.push(queryKeyProperty)
220-
221-
// queryFn
222-
if (secondArgument && secondArgument !== optionsObject) {
223-
const queryFn = sourceCode.getText(secondArgument)
224-
const queryFnProperty =
225-
queryFn === callProps.fn
211+
if (callProps.type === 'query' || callNode.arguments.length > 1) {
212+
// queryKey
213+
const firstArgument = callNode.arguments[0]
214+
const queryKey = sourceCode.getText(firstArgument)
215+
const queryKeyProperty =
216+
queryKey === callProps.key
217+
? callProps.key
218+
: `${callProps.key}: ${queryKey}`
219+
220+
optionsObjectProperties.push(queryKeyProperty)
221+
222+
// queryFn
223+
if (secondArgument && secondArgument !== optionsObject) {
224+
const queryFn = sourceCode.getText(secondArgument)
225+
const queryFnProperty =
226+
queryFn === callProps.fn
227+
? callProps.fn
228+
: `${callProps.fn}: ${queryFn}`
229+
230+
optionsObjectProperties.push(queryFnProperty)
231+
}
232+
}
233+
234+
if (callProps.type === 'mutation' && callNode.arguments.length === 1) {
235+
const firstArgument = callNode.arguments[0]
236+
const mutationFn = sourceCode.getText(firstArgument)
237+
const mutationFnProperty =
238+
mutationFn === callProps.fn
226239
? callProps.fn
227-
: `${callProps.fn}: ${queryFn}`
240+
: `${callProps.fn}: ${mutationFn}`
228241

229-
optionsObjectProperties.push(queryFnProperty)
242+
optionsObjectProperties.push(mutationFnProperty)
230243
}
231244

232245
// options

0 commit comments

Comments
 (0)