Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as React from 'react'
import {
useIsFetching,
useIsMutating,
useQuery,
useQueryClient,
} from '@tanstack/react-query'
import { queryKeysFromAnotherModule } from '../another/module'
Expand Down Expand Up @@ -197,3 +198,13 @@ export const WithIdentifiers = () => {
queryClient.fetchQuery(queryKeysFromAnotherModule, fetchOptions)
queryClient.fetchQuery(queryKeysFromAnotherModule, queryFn, fetchOptions)
}

export const SecondArgumentIsAFunctionExample = () => {
useQuery(ordersCacheKeys.groupOrders(ouuid), () => api.getPatientGroupOrders(ouuid).then((r) => r.data))

const rest = 'rest'
const of = 1
const functionArguments = { foo: 'bar' }

useQuery(ordersCacheKeys.groupOrders(ouuid), () => api.getPatientGroupOrders(ouuid).then((r) => r.data), rest, of, functionArguments)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as React from 'react'
import {
useIsFetching,
useIsMutating,
useQuery,
useQueryClient,
} from '@tanstack/react-query'
import { queryKeysFromAnotherModule } from '../another/module'
Expand Down Expand Up @@ -482,3 +483,19 @@ export const WithIdentifiers = () => {
queryClient.fetchQuery(queryKeysFromAnotherModule, fetchOptions)
queryClient.fetchQuery(queryKeysFromAnotherModule, queryFn, fetchOptions)
}

export const SecondArgumentIsAFunctionExample = () => {
useQuery({
queryKey: ordersCacheKeys.groupOrders(ouuid),
queryFn: () => api.getPatientGroupOrders(ouuid).then((r) => r.data)
})

const rest = 'rest'
const of = 1
const functionArguments = { foo: 'bar' }

useQuery({
queryKey: ordersCacheKeys.groupOrders(ouuid),
queryFn: () => api.getPatientGroupOrders(ouuid).then((r) => r.data)
}, rest, of, functionArguments)
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module.exports = (file, api) => {
'resetQueries',
// 'setQueriesData',
],
hooks: ['useIsFetching'],
hooks: ['useIsFetching', 'useQuery'],
},
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,40 @@ const transformFilterAwareUsages = ({
* it will notify the consumers that they need to rewrite this usage manually.
*/
if (!keyProperty) {
throw new UnknownUsageError(node, filePath)
const secondArgument =
node.arguments.length > 1 ? node.arguments[1] : null

if (!secondArgument) {
throw new UnknownUsageError(node, filePath)
}

if (utils.isFunctionDefinition(secondArgument)) {
const originalArguments = node.arguments
const firstArgument = jscodeshift.objectExpression([
jscodeshift.property(
'init',
jscodeshift.identifier('queryKey'),
originalArguments[0],
),
jscodeshift.property(
'init',
jscodeshift.identifier('queryFn'),
secondArgument,
),
])

return jscodeshift.callExpression(node.original.callee, [
firstArgument,
...originalArguments.slice(2),
])
}
}

const parameters = [jscodeshift.objectExpression([keyProperty])]
const functionArguments = [jscodeshift.objectExpression([keyProperty])]
const secondParameter = node.arguments[1]

if (secondParameter) {
const createdObjectExpression = parameters[0]
const createdObjectExpression = functionArguments[0]

/**
* If it has a second argument, and it's an object expression, then we get the properties from it
Expand All @@ -122,10 +148,10 @@ const transformFilterAwareUsages = ({
}
}

// The rest of the parameters can be simply pushed to the parameters object so all will be kept.
parameters.push(...node.arguments.slice(2))
// The rest of the function arguments can be simply pushed to the function arguments object so all will be kept.
functionArguments.push(...node.arguments.slice(2))

return jscodeshift.callExpression(node.original.callee, parameters)
return jscodeshift.callExpression(node.original.callee, functionArguments)
} catch (error) {
utils.warn(
error.name === UnknownUsageError.name
Expand Down