| | const createUtilsObject = require('../../utils/index.cjs') |
| | const createUseQueryLikeTransformer = require('../../utils/transformers/use-query-like-transformer.cjs') |
| | const createQueryClientTransformer = require('../../utils/transformers/query-client-transformer.cjs') |
| |
|
| | const originalName = 'isLoading' |
| | const newName = 'isPending' |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | const transformUsages = ({ jscodeshift, utils, root, filePath, config }) => { |
| | |
| | |
| | |
| | |
| | const getNodeLocation = (node) => { |
| | const location = utils.isCallExpression(node) ? node.callee.loc : node.loc |
| | const start = location.start.line |
| | const end = location.end.line |
| |
|
| | return { start, end } |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | const isObjectExpression = (node) => { |
| | return jscodeshift.match(node, { |
| | type: jscodeshift.ObjectExpression.name, |
| | }) |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | const isObjectPattern = (node) => { |
| | return jscodeshift.match(node, { |
| | type: jscodeshift.ObjectPattern.name, |
| | }) |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | const isVariableDeclarator = (node) => { |
| | return jscodeshift.match(node, { |
| | type: jscodeshift.VariableDeclarator.name, |
| | }) |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | const findIsLoadingPropertiesOfIdentifier = (node, identifier) => { |
| | return jscodeshift(node).find(jscodeshift.MemberExpression, { |
| | object: { |
| | type: jscodeshift.Identifier.name, |
| | name: identifier.name, |
| | }, |
| | property: { |
| | type: jscodeshift.Identifier.name, |
| | name: originalName, |
| | }, |
| | }) |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | const findIsLoadingObjectPropertyInObjectPattern = (node) => { |
| | return ( |
| | node.properties.find((property) => |
| | jscodeshift.match(property, { |
| | key: { |
| | type: jscodeshift.Identifier.name, |
| | name: originalName, |
| | }, |
| | }), |
| | ) ?? null |
| | ) |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | const findRestElementInObjectPattern = (node) => { |
| | return ( |
| | node.properties.find((property) => |
| | jscodeshift.match(property, { |
| | type: jscodeshift.RestElement.name, |
| | }), |
| | ) ?? null |
| | ) |
| | } |
| |
|
| | const replacer = (path, transformNode) => { |
| | const node = path.node |
| | const parentNode = path.parentPath.value |
| | const { start, end } = getNodeLocation(node) |
| |
|
| | try { |
| | if (!isVariableDeclarator(parentNode)) { |
| | |
| | return node |
| | } |
| |
|
| | const lookupNode = path.scope.node |
| | const variableDeclaratorId = parentNode.id |
| |
|
| | if (isObjectPattern(variableDeclaratorId)) { |
| | const isLoadingObjectProperty = |
| | findIsLoadingObjectPropertyInObjectPattern(variableDeclaratorId) |
| |
|
| | if (isLoadingObjectProperty) { |
| | jscodeshift(lookupNode) |
| | .find(jscodeshift.ObjectProperty, { |
| | key: { |
| | type: jscodeshift.Identifier.name, |
| | name: originalName, |
| | }, |
| | }) |
| | .replaceWith((mutablePath) => { |
| | if (isObjectPattern(mutablePath.parent)) { |
| | const affectedProperty = mutablePath.value.value.shorthand |
| | ? 'value' |
| | : 'key' |
| |
|
| | mutablePath.value[affectedProperty].name = newName |
| |
|
| | return mutablePath.value |
| | } |
| |
|
| | if (isObjectExpression(mutablePath.parent)) { |
| | const affectedProperty = mutablePath.value.value.shorthand |
| | ? 'key' |
| | : 'value' |
| |
|
| | mutablePath.value[affectedProperty].name = newName |
| |
|
| | return mutablePath.value |
| | } |
| |
|
| | return mutablePath.value |
| | }) |
| |
|
| | |
| | jscodeshift(lookupNode) |
| | .find(jscodeshift.Identifier, { name: originalName }) |
| | .replaceWith((mutablePath) => { |
| | if ( |
| | !jscodeshift.match(mutablePath.parent, { |
| | type: jscodeshift.ObjectProperty.name, |
| | }) |
| | ) { |
| | mutablePath.value.name = newName |
| | } |
| |
|
| | return mutablePath.value |
| | }) |
| | } |
| |
|
| | const restElement = findRestElementInObjectPattern(variableDeclaratorId) |
| |
|
| | if (restElement) { |
| | findIsLoadingPropertiesOfIdentifier( |
| | lookupNode, |
| | restElement.argument, |
| | ).replaceWith(({ node: mutableNode }) => { |
| | mutableNode.property.name = newName |
| |
|
| | return mutableNode |
| | }) |
| | } |
| |
|
| | return node |
| | } |
| |
|
| | if (utils.isIdentifier(variableDeclaratorId)) { |
| | findIsLoadingPropertiesOfIdentifier( |
| | lookupNode, |
| | variableDeclaratorId, |
| | ).replaceWith(({ node: mutableNode }) => { |
| | mutableNode.property.name = newName |
| |
|
| | return mutableNode |
| | }) |
| |
|
| | return node |
| | } |
| |
|
| | utils.warn( |
| | `The usage in file "${filePath}" at line ${start}:${end} could not be transformed. Please migrate this usage manually.`, |
| | ) |
| |
|
| | return node |
| | } catch (error) { |
| | utils.warn( |
| | `An unknown error occurred while processing the "${filePath}" file. Please review this file, because the codemod couldn't be applied.`, |
| | ) |
| |
|
| | return node |
| | } |
| | } |
| |
|
| | createUseQueryLikeTransformer({ jscodeshift, utils, root }).execute( |
| | config.hooks, |
| | replacer, |
| | ) |
| |
|
| | createQueryClientTransformer({ jscodeshift, utils, root }).execute( |
| | config.queryClientMethods, |
| | replacer, |
| | ) |
| | } |
| |
|
| | module.exports = (file, api) => { |
| | const jscodeshift = api.jscodeshift |
| | const root = jscodeshift(file.source) |
| | const utils = createUtilsObject({ root, jscodeshift }) |
| | const filePath = file.path |
| |
|
| | const dependencies = { jscodeshift, utils, root, filePath } |
| |
|
| | transformUsages({ |
| | ...dependencies, |
| | config: { |
| | hooks: ['useQuery', 'useMutation'], |
| | queryClientMethods: [], |
| | }, |
| | }) |
| |
|
| | return root.toSource({ quote: 'single', lineTerminator: '\n' }) |
| | } |
| |
|