Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | // eslint-disable-next-line @typescript-eslint/no-var-requires const createUtilsObject = require('../../utils') // eslint-disable-next-line @typescript-eslint/no-var-requires const createUseQueryLikeTransformer = require('../../utils/transformers/use-query-like-transformer') // eslint-disable-next-line @typescript-eslint/no-var-requires const createQueryClientTransformer = require('../../utils/transformers/query-client-transformer') // eslint-disable-next-line @typescript-eslint/no-var-requires const AlreadyHasPlaceholderDataProperty = require('./utils/already-has-placeholder-data-property') /** * @param {import('jscodeshift')} jscodeshift * @param {Object} utils * @param {import('jscodeshift').Collection} root * @param {string} filePath * @param {{keyName: "mutationKey"|"queryKey", queryClientMethods: ReadonlyArray<string>, hooks: ReadonlyArray<string>}} config */ const transformUsages = ({ jscodeshift, utils, root, filePath, config }) => { /** * @param {import('jscodeshift').CallExpression | import('jscodeshift').ExpressionStatement} node * @returns {{start: number, end: number}} */ 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 } } /** * @param {import('jscodeshift').ObjectProperty} objectProperty * @returns {boolean} */ const isKeepPreviousDataObjectProperty = (objectProperty) => { return jscodeshift.match(objectProperty.key, { type: jscodeshift.Identifier.name, name: 'keepPreviousData', }) } /** * @param {import('jscodeshift').ObjectProperty} objectProperty * @returns {boolean} */ const isObjectPropertyHasTrueBooleanLiteralValue = (objectProperty) => { return jscodeshift.match(objectProperty.value, { type: jscodeshift.BooleanLiteral.name, value: true, }) } /** * @param {import('jscodeshift').ObjectExpression} objectExpression * @returns {Array<import('jscodeshift').ObjectProperty>} */ const filterKeepPreviousDataProperty = (objectExpression) => { return objectExpression.properties.filter((objectProperty) => { return !isKeepPreviousDataObjectProperty(objectProperty) }) } const createPlaceholderDataObjectProperty = () => { return jscodeshift.objectProperty( jscodeshift.identifier('placeholderData'), jscodeshift.identifier('keepPreviousData'), ) } /** * @param {import('jscodeshift').ObjectExpression} objectExpression * @returns {boolean} */ const hasPlaceholderDataProperty = (objectExpression) => { return ( objectExpression.properties.findIndex((objectProperty) => { return jscodeshift.match(objectProperty.key, { type: jscodeshift.Identifier.name, name: 'placeholderData', }) }) !== -1 ) } /** * @param {import('jscodeshift').ObjectExpression} objectExpression * @returns {import('jscodeshift').ObjectProperty | undefined} */ const getKeepPreviousDataProperty = (objectExpression) => { return objectExpression.properties.find(isKeepPreviousDataObjectProperty) } let shouldAddKeepPreviousDataImport = false const replacer = (path, resolveTargetArgument, transformNode) => { const node = path.node const { start, end } = getNodeLocation(node) try { const targetArgument = resolveTargetArgument(node) if (targetArgument && utils.isObjectExpression(targetArgument)) { const isPlaceholderDataPropertyPresent = hasPlaceholderDataProperty(targetArgument) if (hasPlaceholderDataProperty(targetArgument)) { throw new AlreadyHasPlaceholderDataProperty(node, filePath) } const keepPreviousDataProperty = getKeepPreviousDataProperty(targetArgument) const keepPreviousDataPropertyHasTrueValue = isObjectPropertyHasTrueBooleanLiteralValue(keepPreviousDataProperty) if (!keepPreviousDataPropertyHasTrueValue) { utils.warn( `The usage in file "${filePath}" at line ${start}:${end} already contains a "keepPreviousData" property but its value is not "true". Please migrate this usage manually.`, ) return node } if (keepPreviousDataPropertyHasTrueValue) { // Removing the `keepPreviousData` property from the object. const mutableObjectExpressionProperties = filterKeepPreviousDataProperty(targetArgument) if (!isPlaceholderDataPropertyPresent) { shouldAddKeepPreviousDataImport = true // When the `placeholderData` property is not present, the `placeholderData: keepPreviousData` property will be added. mutableObjectExpressionProperties.push( createPlaceholderDataObjectProperty(), ) } return transformNode( node, jscodeshift.objectExpression(mutableObjectExpressionProperties), ) } } utils.warn( `The usage in file "${filePath}" at line ${start}:${end} could not be transformed, because the first parameter is not an object expression. Please migrate this usage manually.`, ) return node } catch (error) { utils.warn( error.name === AlreadyHasPlaceholderDataProperty.name ? error.message : `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, (path) => { const resolveTargetArgument = (node) => node.arguments[0] ?? null const transformNode = (node, transformedArgument) => jscodeshift.callExpression(node.original.callee, [transformedArgument]) return replacer(path, resolveTargetArgument, transformNode) }, ) createQueryClientTransformer({ jscodeshift, utils, root }).execute( config.queryClientMethods, (path) => { const resolveTargetArgument = (node) => node.arguments[1] ?? null const transformNode = (node, transformedArgument) => { return jscodeshift.callExpression(node.original.callee, [ node.arguments[0], transformedArgument, ...node.arguments.slice(2, 0), ]) } return replacer(path, resolveTargetArgument, transformNode) }, ) const importIdentifierOfQueryClient = utils.getSelectorByImports( utils.locateImports(['QueryClient']), 'QueryClient', ) root .find(jscodeshift.ExpressionStatement, { expression: { type: jscodeshift.NewExpression.name, callee: { type: jscodeshift.Identifier.name, name: importIdentifierOfQueryClient, }, }, }) .filter((path) => path.node.expression) .replaceWith((path) => { const resolveTargetArgument = (node) => { const paths = jscodeshift(node) .find(jscodeshift.ObjectProperty, { key: { type: jscodeshift.Identifier.name, name: 'keepPreviousData', }, }) .paths() return paths.length > 0 ? paths[0].parent.node : null } const transformNode = (node, transformedArgument) => { jscodeshift(node.expression) .find(jscodeshift.ObjectProperty, { key: { type: jscodeshift.Identifier.name, name: 'queries', }, }) .replaceWith(({ node: mutableNode }) => { mutableNode.value.properties = transformedArgument.properties return mutableNode }) return node } return replacer(path, resolveTargetArgument, transformNode) }) return { shouldAddKeepPreviousDataImport } } 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 } const { shouldAddKeepPreviousDataImport } = transformUsages({ ...dependencies, config: { hooks: ['useInfiniteQuery', 'useQueries', 'useQuery'], queryClientMethods: ['setQueryDefaults'], }, }) if (shouldAddKeepPreviousDataImport) { root .find(jscodeshift.ImportDeclaration, { source: { value: '@tanstack/react-query', }, }) .replaceWith(({ node: mutableNode }) => { mutableNode.specifiers = [ jscodeshift.importSpecifier( jscodeshift.identifier('keepPreviousData'), ), ...mutableNode.specifiers, ] return mutableNode }) } return root.toSource({ quote: 'single', lineTerminator: '\n' }) } |