Skip to content

Commit da9620f

Browse files
authored
fix(eslint-plugin-query): autofix for useMutation with options (#4986)
* fix autofix on usemutation with options * format
1 parent 1a38d17 commit da9620f

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,28 @@ ruleTester.run(name, rule, {
435435
createMutation({ mutationKey: ["mutation", "key"], mutationFn: async () => await fetchUserById(userId) });
436436
`,
437437
},
438+
{
439+
code: normalizeIndent`
440+
import { useMutation } from "@tanstack/vue-query";
441+
useMutation(() => Promise.resolve(3), { onSuccess: () => {} });
442+
`,
443+
errors: [{ messageId: 'preferObjectSyntax' }],
444+
output: normalizeIndent`
445+
import { useMutation } from "@tanstack/vue-query";
446+
useMutation({ mutationFn: () => Promise.resolve(3), onSuccess: () => {} });
447+
`,
448+
},
449+
{
450+
code: normalizeIndent`
451+
import { useMutation } from "@tanstack/vue-query";
452+
useMutation(() => Promise.resolve(3), { onSuccess: () => {}, mutationKey: ["foo"] });
453+
`,
454+
errors: [{ messageId: 'preferObjectSyntax' }],
455+
output: normalizeIndent`
456+
import { useMutation } from "@tanstack/vue-query";
457+
useMutation({ mutationFn: () => Promise.resolve(3), onSuccess: () => {}, mutationKey: ["foo"] });
458+
`,
459+
},
438460
{
439461
code: normalizeIndent`
440462
import { useQuery } from '@tanstack/vue-query';

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

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ function runCheckOnNode(params: {
208208
fix(fixer) {
209209
const optionsObjectProperties: string[] = []
210210

211-
if (callProps.type === 'query' || callNode.arguments.length > 1) {
211+
if (callProps.type === 'query') {
212212
// queryKey
213213
const firstArgument = callNode.arguments[0]
214214
const queryKey = sourceCode.getText(firstArgument)
@@ -231,13 +231,28 @@ function runCheckOnNode(params: {
231231
}
232232
}
233233

234-
if (callProps.type === 'mutation' && callNode.arguments.length === 1) {
235-
const firstArgument = callNode.arguments[0]
236-
const mutationFn = sourceCode.getText(firstArgument)
234+
if (callProps.type === 'mutation') {
235+
const isMutationKeyPresent =
236+
callNode.arguments.length === 3 ||
237+
callNode.arguments[1]?.type === 'ArrowFunctionExpression'
238+
239+
if (isMutationKeyPresent) {
240+
const mutationKeyNode = callNode.arguments[0]
241+
const mutationKeyText = sourceCode.getText(mutationKeyNode)
242+
const mutationKeyProperty =
243+
mutationKeyText === callProps.key
244+
? callProps.key
245+
: `${callProps.key}: ${mutationKeyText}`
246+
247+
optionsObjectProperties.push(mutationKeyProperty)
248+
}
249+
250+
const mutationFnNode = callNode.arguments[isMutationKeyPresent ? 1 : 0]
251+
const mutationFnText = sourceCode.getText(mutationFnNode)
237252
const mutationFnProperty =
238-
mutationFn === callProps.fn
253+
mutationFnText === callProps.fn
239254
? callProps.fn
240-
: `${callProps.fn}: ${mutationFn}`
255+
: `${callProps.fn}: ${mutationFnText}`
241256

242257
optionsObjectProperties.push(mutationFnProperty)
243258
}

0 commit comments

Comments
 (0)