Skip to content

Commit

Permalink
fix(eslint-plugin-query): mutation function is turned into mutation k…
Browse files Browse the repository at this point in the history
…ey (#4973)
  • Loading branch information
Newbie012 authored Feb 12, 2023
1 parent 5a5fd6b commit 48e806b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,17 @@ ruleTester.run(name, rule, {
useMutation({ mutationKey: ["mutation", "key"], mutationFn: async () => await fetchUserById(userId) });
`,
},
{
code: normalizeIndent`
import { useMutation } from "@tanstack/react-query";
useMutation(async () => await fetchUserById(userId));
`,
errors: [{ messageId: 'preferObjectSyntax' }],
output: normalizeIndent`
import { useMutation } from "@tanstack/react-query";
useMutation({ mutationFn: async () => await fetchUserById(userId) });
`,
},
{
code: normalizeIndent`
import { createMutation } from "@tanstack/solid-query";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { ASTUtils } from '../../utils/ast-utils'
import { objectKeys } from '../../utils/object-utils'

const QUERY_CALLS = {
useQuery: { key: 'queryKey', fn: 'queryFn' },
createQuery: { key: 'queryKey', fn: 'queryFn' },
useMutation: { key: 'mutationKey', fn: 'mutationFn' },
createMutation: { key: 'mutationKey', fn: 'mutationFn' },
useQuery: { key: 'queryKey', fn: 'queryFn', type: 'query' },
createQuery: { key: 'queryKey', fn: 'queryFn', type: 'query' },
useMutation: { key: 'mutationKey', fn: 'mutationFn', type: 'mutation' },
createMutation: { key: 'mutationKey', fn: 'mutationFn', type: 'mutation' },
}

const messages = {
Expand Down Expand Up @@ -208,25 +208,38 @@ function runCheckOnNode(params: {
fix(fixer) {
const optionsObjectProperties: string[] = []

// queryKey
const firstArgument = callNode.arguments[0]
const queryKey = sourceCode.getText(firstArgument)
const queryKeyProperty =
queryKey === callProps.key
? callProps.key
: `${callProps.key}: ${queryKey}`

optionsObjectProperties.push(queryKeyProperty)

// queryFn
if (secondArgument && secondArgument !== optionsObject) {
const queryFn = sourceCode.getText(secondArgument)
const queryFnProperty =
queryFn === callProps.fn
if (callProps.type === 'query' || callNode.arguments.length > 1) {
// queryKey
const firstArgument = callNode.arguments[0]
const queryKey = sourceCode.getText(firstArgument)
const queryKeyProperty =
queryKey === callProps.key
? callProps.key
: `${callProps.key}: ${queryKey}`

optionsObjectProperties.push(queryKeyProperty)

// queryFn
if (secondArgument && secondArgument !== optionsObject) {
const queryFn = sourceCode.getText(secondArgument)
const queryFnProperty =
queryFn === callProps.fn
? callProps.fn
: `${callProps.fn}: ${queryFn}`

optionsObjectProperties.push(queryFnProperty)
}
}

if (callProps.type === 'mutation' && callNode.arguments.length === 1) {
const firstArgument = callNode.arguments[0]
const mutationFn = sourceCode.getText(firstArgument)
const mutationFnProperty =
mutationFn === callProps.fn
? callProps.fn
: `${callProps.fn}: ${queryFn}`
: `${callProps.fn}: ${mutationFn}`

optionsObjectProperties.push(queryFnProperty)
optionsObjectProperties.push(mutationFnProperty)
}

// options
Expand Down

0 comments on commit 48e806b

Please sign in to comment.