Skip to content

Commit

Permalink
fix(codemod): extend the remove overloads codemod logic (#6268)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
balazsmatepetro and TkDodo authored Nov 28, 2023
1 parent 6b79e10 commit fe87fc1
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// From: https://github.com/TanStack/query/issues/6204
export function useDetails({ groupId }: { groupId?: string }): any {
const cacheKey = ["details", groupId];
const query = () => fetch(`/group/${groupId}`);
const queryObject = useQuery(cacheKey, query, {
enabled: !!groupId,
});
return queryObject;
}

// Based on: https://github.com/TanStack/query/issues/6204
export function useDetailsContainsIdentifierAsThirdArgument({ groupId }: { groupId?: string }): any {
const cacheKey = ["details", groupId];
const query = () => fetch(`/group/${groupId}`);
const opts = { enabled: !!groupId }
const queryObject = useQuery(cacheKey, query, opts);
return queryObject;
}

// From: https://twitter.com/samcook_/status/1715063150184132902
export function useWhatever({ thing }: { thing: string }) {
return useQuery(
['some-string', someVar],
async () => 'foo',
{ enabled: Boolean(thing) },
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// From: https://github.com/TanStack/query/issues/6204
export function useDetails({ groupId }: { groupId?: string }): any {
const cacheKey = ["details", groupId];
const query = () => fetch(`/group/${groupId}`);
const queryObject = useQuery({
queryKey: cacheKey,
queryFn: query,
enabled: !!groupId
});
return queryObject;
}

// Based on: https://github.com/TanStack/query/issues/6204
export function useDetailsContainsIdentifierAsThirdArgument({ groupId }: { groupId?: string }): any {
const cacheKey = ["details", groupId];
const query = () => fetch(`/group/${groupId}`);
const opts = { enabled: !!groupId }
const queryObject = useQuery({
queryKey: cacheKey,
queryFn: query,
...opts
});
return queryObject;
}

// From: https://twitter.com/samcook_/status/1715063150184132902
export function useWhatever({ thing }: { thing: string }) {
return useQuery({
queryKey: ['some-string', someVar],
queryFn: async () => 'foo',
enabled: Boolean(thing)
});
}

Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ const defineTest = require('jscodeshift/dist/testUtils').defineTest
defineTest(__dirname, 'remove-overloads', null, 'default-import', {
parser: 'tsx',
})

defineTest(__dirname, 'remove-overloads', null, 'bug-reports', {
parser: 'tsx',
})
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,26 @@ const transformFilterAwareUsages = ({
const replacer = (path) => {
const node = path.node

const isFunctionDefinition = (functionArgument) => {
if (utils.isFunctionDefinition(functionArgument)) {
return true
}

if (utils.isIdentifier(functionArgument)) {
const binding = v5Utils.getBindingFromScope(
path,
functionArgument.name,
filePath,
)

const isVariableDeclarator = jscodeshift.match(binding, {
type: jscodeshift.VariableDeclarator.name,
})

return isVariableDeclarator && utils.isFunctionDefinition(binding.init)
}
}

try {
// If the given method/function call matches certain criteria, the node doesn't need to be replaced, this step can be skipped.
if (canSkipReplacement(node, config.keyName)) {
Expand Down Expand Up @@ -129,6 +149,43 @@ const transformFilterAwareUsages = ({
if (secondParameter) {
const createdObjectExpression = functionArguments[0]

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

const thirdArgument = node.arguments[2]

if (thirdArgument) {
// If it's an object expression, we can copy the properties from it to the newly created object expression.
if (utils.isObjectExpression(thirdArgument)) {
v5Utils.copyPropertiesFromSource(
thirdArgument,
objectExpression,
predicate,
)
} else {
// Otherwise, we simply spread the third argument in the newly created object expression.
objectExpression.properties.push(
jscodeshift.spreadElement(thirdArgument),
)
}
}

return jscodeshift.callExpression(node.original.callee, [
objectExpression,
])
}

/**
* If it has a second argument, and it's an object expression, then we get the properties from it
* (except the "queryKey" or "mutationKey" properties), because these arguments will also be moved to the
Expand Down

0 comments on commit fe87fc1

Please sign in to comment.