Skip to content

Commit fe87fc1

Browse files
fix(codemod): extend the remove overloads codemod logic (#6268)
* fix(codemod): extend codemod logic to transform usages when the second parameter is a function in the scope * fix(codemod): handle the case when the second argument is a function definition * chore(codemod): fix the `eslint` issue in the newly added example --------- Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>
1 parent 6b79e10 commit fe87fc1

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// From: https://github.com/TanStack/query/issues/6204
2+
export function useDetails({ groupId }: { groupId?: string }): any {
3+
const cacheKey = ["details", groupId];
4+
const query = () => fetch(`/group/${groupId}`);
5+
const queryObject = useQuery(cacheKey, query, {
6+
enabled: !!groupId,
7+
});
8+
return queryObject;
9+
}
10+
11+
// Based on: https://github.com/TanStack/query/issues/6204
12+
export function useDetailsContainsIdentifierAsThirdArgument({ groupId }: { groupId?: string }): any {
13+
const cacheKey = ["details", groupId];
14+
const query = () => fetch(`/group/${groupId}`);
15+
const opts = { enabled: !!groupId }
16+
const queryObject = useQuery(cacheKey, query, opts);
17+
return queryObject;
18+
}
19+
20+
// From: https://twitter.com/samcook_/status/1715063150184132902
21+
export function useWhatever({ thing }: { thing: string }) {
22+
return useQuery(
23+
['some-string', someVar],
24+
async () => 'foo',
25+
{ enabled: Boolean(thing) },
26+
);
27+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// From: https://github.com/TanStack/query/issues/6204
2+
export function useDetails({ groupId }: { groupId?: string }): any {
3+
const cacheKey = ["details", groupId];
4+
const query = () => fetch(`/group/${groupId}`);
5+
const queryObject = useQuery({
6+
queryKey: cacheKey,
7+
queryFn: query,
8+
enabled: !!groupId
9+
});
10+
return queryObject;
11+
}
12+
13+
// Based on: https://github.com/TanStack/query/issues/6204
14+
export function useDetailsContainsIdentifierAsThirdArgument({ groupId }: { groupId?: string }): any {
15+
const cacheKey = ["details", groupId];
16+
const query = () => fetch(`/group/${groupId}`);
17+
const opts = { enabled: !!groupId }
18+
const queryObject = useQuery({
19+
queryKey: cacheKey,
20+
queryFn: query,
21+
...opts
22+
});
23+
return queryObject;
24+
}
25+
26+
// From: https://twitter.com/samcook_/status/1715063150184132902
27+
export function useWhatever({ thing }: { thing: string }) {
28+
return useQuery({
29+
queryKey: ['some-string', someVar],
30+
queryFn: async () => 'foo',
31+
enabled: Boolean(thing)
32+
});
33+
}
34+

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ const defineTest = require('jscodeshift/dist/testUtils').defineTest
44
defineTest(__dirname, 'remove-overloads', null, 'default-import', {
55
parser: 'tsx',
66
})
7+
8+
defineTest(__dirname, 'remove-overloads', null, 'bug-reports', {
9+
parser: 'tsx',
10+
})

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,26 @@ const transformFilterAwareUsages = ({
7070
const replacer = (path) => {
7171
const node = path.node
7272

73+
const isFunctionDefinition = (functionArgument) => {
74+
if (utils.isFunctionDefinition(functionArgument)) {
75+
return true
76+
}
77+
78+
if (utils.isIdentifier(functionArgument)) {
79+
const binding = v5Utils.getBindingFromScope(
80+
path,
81+
functionArgument.name,
82+
filePath,
83+
)
84+
85+
const isVariableDeclarator = jscodeshift.match(binding, {
86+
type: jscodeshift.VariableDeclarator.name,
87+
})
88+
89+
return isVariableDeclarator && utils.isFunctionDefinition(binding.init)
90+
}
91+
}
92+
7393
try {
7494
// If the given method/function call matches certain criteria, the node doesn't need to be replaced, this step can be skipped.
7595
if (canSkipReplacement(node, config.keyName)) {
@@ -129,6 +149,43 @@ const transformFilterAwareUsages = ({
129149
if (secondParameter) {
130150
const createdObjectExpression = functionArguments[0]
131151

152+
if (isFunctionDefinition(secondParameter)) {
153+
const objectExpression = jscodeshift.objectExpression([
154+
jscodeshift.property(
155+
'init',
156+
jscodeshift.identifier('queryKey'),
157+
node.arguments[0],
158+
),
159+
jscodeshift.property(
160+
'init',
161+
jscodeshift.identifier('queryFn'),
162+
secondParameter,
163+
),
164+
])
165+
166+
const thirdArgument = node.arguments[2]
167+
168+
if (thirdArgument) {
169+
// If it's an object expression, we can copy the properties from it to the newly created object expression.
170+
if (utils.isObjectExpression(thirdArgument)) {
171+
v5Utils.copyPropertiesFromSource(
172+
thirdArgument,
173+
objectExpression,
174+
predicate,
175+
)
176+
} else {
177+
// Otherwise, we simply spread the third argument in the newly created object expression.
178+
objectExpression.properties.push(
179+
jscodeshift.spreadElement(thirdArgument),
180+
)
181+
}
182+
}
183+
184+
return jscodeshift.callExpression(node.original.callee, [
185+
objectExpression,
186+
])
187+
}
188+
132189
/**
133190
* If it has a second argument, and it's an object expression, then we get the properties from it
134191
* (except the "queryKey" or "mutationKey" properties), because these arguments will also be moved to the

0 commit comments

Comments
 (0)