Skip to content

Commit 7933834

Browse files
fix(codemod): remove the function overloads when the second argument is a function expression (#6191)
* fix(codemod): remove the function overloads when the second argument is a function expression After a quick sync with @TkDodo, it turned out in this case it's safe to do the transformation, because the second argument is a function expression, so it means the first parameter must be the query key. * fix(codemod): add the `useQuery` hook to the transformation list, because it was missing * chore(codemod): rename the `parameters` constant to `functionArguments` The naming was wrong from the beginning. The codemod does transformations with the arguments of the functions, not with their parameters. :) * chore(codemod): fix the import order The CI was failing because of it. --------- Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>
1 parent c03e159 commit 7933834

File tree

4 files changed

+61
-7
lines changed

4 files changed

+61
-7
lines changed

packages/codemods/src/v5/remove-overloads/__testfixtures__/default-import.input.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as React from 'react'
22
import {
33
useIsFetching,
44
useIsMutating,
5+
useQuery,
56
useQueryClient,
67
} from '@tanstack/react-query'
78
import { queryKeysFromAnotherModule } from '../another/module'
@@ -197,3 +198,13 @@ export const WithIdentifiers = () => {
197198
queryClient.fetchQuery(queryKeysFromAnotherModule, fetchOptions)
198199
queryClient.fetchQuery(queryKeysFromAnotherModule, queryFn, fetchOptions)
199200
}
201+
202+
export const SecondArgumentIsAFunctionExample = () => {
203+
useQuery(ordersCacheKeys.groupOrders(ouuid), () => api.getPatientGroupOrders(ouuid).then((r) => r.data))
204+
205+
const rest = 'rest'
206+
const of = 1
207+
const functionArguments = { foo: 'bar' }
208+
209+
useQuery(ordersCacheKeys.groupOrders(ouuid), () => api.getPatientGroupOrders(ouuid).then((r) => r.data), rest, of, functionArguments)
210+
}

packages/codemods/src/v5/remove-overloads/__testfixtures__/default-import.output.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as React from 'react'
22
import {
33
useIsFetching,
44
useIsMutating,
5+
useQuery,
56
useQueryClient,
67
} from '@tanstack/react-query'
78
import { queryKeysFromAnotherModule } from '../another/module'
@@ -482,3 +483,19 @@ export const WithIdentifiers = () => {
482483
queryClient.fetchQuery(queryKeysFromAnotherModule, fetchOptions)
483484
queryClient.fetchQuery(queryKeysFromAnotherModule, queryFn, fetchOptions)
484485
}
486+
487+
export const SecondArgumentIsAFunctionExample = () => {
488+
useQuery({
489+
queryKey: ordersCacheKeys.groupOrders(ouuid),
490+
queryFn: () => api.getPatientGroupOrders(ouuid).then((r) => r.data)
491+
})
492+
493+
const rest = 'rest'
494+
const of = 1
495+
const functionArguments = { foo: 'bar' }
496+
497+
useQuery({
498+
queryKey: ordersCacheKeys.groupOrders(ouuid),
499+
queryFn: () => api.getPatientGroupOrders(ouuid).then((r) => r.data)
500+
}, rest, of, functionArguments)
501+
}

packages/codemods/src/v5/remove-overloads/remove-overloads.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ module.exports = (file, api) => {
2727
'resetQueries',
2828
// 'setQueriesData',
2929
],
30-
hooks: ['useIsFetching'],
30+
hooks: ['useIsFetching', 'useQuery'],
3131
},
3232
})
3333

packages/codemods/src/v5/remove-overloads/transformers/filter-aware-usage-transformer.js

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,40 @@ const transformFilterAwareUsages = ({
9494
* it will notify the consumers that they need to rewrite this usage manually.
9595
*/
9696
if (!keyProperty) {
97-
throw new UnknownUsageError(node, filePath)
97+
const secondArgument =
98+
node.arguments.length > 1 ? node.arguments[1] : null
99+
100+
if (!secondArgument) {
101+
throw new UnknownUsageError(node, filePath)
102+
}
103+
104+
if (utils.isFunctionDefinition(secondArgument)) {
105+
const originalArguments = node.arguments
106+
const firstArgument = jscodeshift.objectExpression([
107+
jscodeshift.property(
108+
'init',
109+
jscodeshift.identifier('queryKey'),
110+
originalArguments[0],
111+
),
112+
jscodeshift.property(
113+
'init',
114+
jscodeshift.identifier('queryFn'),
115+
secondArgument,
116+
),
117+
])
118+
119+
return jscodeshift.callExpression(node.original.callee, [
120+
firstArgument,
121+
...originalArguments.slice(2),
122+
])
123+
}
98124
}
99125

100-
const parameters = [jscodeshift.objectExpression([keyProperty])]
126+
const functionArguments = [jscodeshift.objectExpression([keyProperty])]
101127
const secondParameter = node.arguments[1]
102128

103129
if (secondParameter) {
104-
const createdObjectExpression = parameters[0]
130+
const createdObjectExpression = functionArguments[0]
105131

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

125-
// The rest of the parameters can be simply pushed to the parameters object so all will be kept.
126-
parameters.push(...node.arguments.slice(2))
151+
// The rest of the function arguments can be simply pushed to the function arguments object so all will be kept.
152+
functionArguments.push(...node.arguments.slice(2))
127153

128-
return jscodeshift.callExpression(node.original.callee, parameters)
154+
return jscodeshift.callExpression(node.original.callee, functionArguments)
129155
} catch (error) {
130156
utils.warn(
131157
error.name === UnknownUsageError.name

0 commit comments

Comments
 (0)