|
| 1 | +import { API, Collection, FileInfo, Options } from 'jscodeshift'; |
| 2 | + |
| 3 | +const KNEX_SPECIFIC_METHODS = [ |
| 4 | + 'loadFirstByFieldEqualityConjunctionAsync', |
| 5 | + 'loadManyByFieldEqualityConjunctionAsync', |
| 6 | + 'loadManyByRawWhereClauseAsync', |
| 7 | +]; |
| 8 | + |
| 9 | +function isKnexSpecificMethodUsed(j: API['jscodeshift'], node: any): boolean { |
| 10 | + // Check if this loader call is followed by a knex-specific method |
| 11 | + // We need to traverse the AST to find usages of the loader result |
| 12 | + const parent = node.parent; |
| 13 | + |
| 14 | + // Check if the loader call is directly chained with a knex method |
| 15 | + if (parent && parent.value.type === 'MemberExpression' && parent.value.object === node.value) { |
| 16 | + const grandParent = parent.parent; |
| 17 | + if ( |
| 18 | + grandParent && |
| 19 | + grandParent.value.type === 'CallExpression' && |
| 20 | + grandParent.value.callee === parent.value |
| 21 | + ) { |
| 22 | + if ( |
| 23 | + parent.value.property.type === 'Identifier' && |
| 24 | + KNEX_SPECIFIC_METHODS.includes(parent.value.property.name) |
| 25 | + ) { |
| 26 | + return true; |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + // Check if the loader is assigned to a variable and then used with knex methods |
| 32 | + if (parent && parent.value.type === 'VariableDeclarator' && parent.value.init === node.value) { |
| 33 | + const variableName = parent.value.id.name; |
| 34 | + const scope = parent.scope; |
| 35 | + |
| 36 | + // Find all references to this variable in the same scope |
| 37 | + const references = j(scope.path) |
| 38 | + .find(j.Identifier, { name: variableName }) |
| 39 | + .filter((path) => { |
| 40 | + // Check if this identifier is used as object in member expression |
| 41 | + const parentNode = path.parent.value; |
| 42 | + if (parentNode.type === 'MemberExpression' && parentNode.object === path.value) { |
| 43 | + const prop = parentNode.property; |
| 44 | + if (prop.type === 'Identifier' && KNEX_SPECIFIC_METHODS.includes(prop.name)) { |
| 45 | + return true; |
| 46 | + } |
| 47 | + } |
| 48 | + return false; |
| 49 | + }); |
| 50 | + |
| 51 | + if (references.size() > 0) { |
| 52 | + return true; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // Check await expressions |
| 57 | + if (parent && parent.value.type === 'AwaitExpression' && parent.value.argument === node.value) { |
| 58 | + const awaitParent = parent.parent; |
| 59 | + if (awaitParent && awaitParent.value.type === 'VariableDeclarator') { |
| 60 | + const variableName = awaitParent.value.id.name; |
| 61 | + const scope = awaitParent.scope; |
| 62 | + |
| 63 | + // Find all references to this variable in the same scope |
| 64 | + const references = j(scope.path) |
| 65 | + .find(j.Identifier, { name: variableName }) |
| 66 | + .filter((path) => { |
| 67 | + const parentNode = path.parent.value; |
| 68 | + if (parentNode.type === 'MemberExpression' && parentNode.object === path.value) { |
| 69 | + const prop = parentNode.property; |
| 70 | + if (prop.type === 'Identifier' && KNEX_SPECIFIC_METHODS.includes(prop.name)) { |
| 71 | + return true; |
| 72 | + } |
| 73 | + } |
| 74 | + return false; |
| 75 | + }); |
| 76 | + |
| 77 | + if (references.size() > 0) { |
| 78 | + return true; |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return false; |
| 84 | +} |
| 85 | + |
| 86 | +function transformLoaderToKnexLoader(j: API['jscodeshift'], root: Collection<any>): void { |
| 87 | + // Find all entity expressions of the form `Entity.loader(viewerContext)` |
| 88 | + root |
| 89 | + .find(j.CallExpression, { |
| 90 | + callee: { |
| 91 | + type: 'MemberExpression', |
| 92 | + property: { |
| 93 | + name: 'loader', |
| 94 | + }, |
| 95 | + }, |
| 96 | + }) |
| 97 | + .forEach((path) => { |
| 98 | + const loaderCallExpression = path.node; // Entity.loader(viewerContext) |
| 99 | + const loaderCallee = loaderCallExpression.callee; // Entity.loader |
| 100 | + |
| 101 | + if (loaderCallee.type !== 'MemberExpression') { |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + // Make sure this is a static method call on an entity (not on an instance) |
| 106 | + // Typically entity names start with uppercase letter |
| 107 | + if (loaderCallee.object.type === 'Identifier') { |
| 108 | + const firstChar = loaderCallee.object.name[0]; |
| 109 | + if (firstChar && firstChar === firstChar.toUpperCase()) { |
| 110 | + // Check if this loader uses knex-specific methods |
| 111 | + if (isKnexSpecificMethodUsed(j, path)) { |
| 112 | + // Rename loader to knexLoader |
| 113 | + if (loaderCallee.property.type === 'Identifier') { |
| 114 | + loaderCallee.property.name = 'knexLoader'; |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + }); |
| 120 | +} |
| 121 | + |
| 122 | +function transformLoaderWithAuthorizationResultsToKnexLoaderWithAuthorizationResults( |
| 123 | + j: API['jscodeshift'], |
| 124 | + root: Collection<any>, |
| 125 | +): void { |
| 126 | + // Find all entity expressions of the form `Entity.loaderWithAuthorizationResults(viewerContext)` |
| 127 | + root |
| 128 | + .find(j.CallExpression, { |
| 129 | + callee: { |
| 130 | + type: 'MemberExpression', |
| 131 | + property: { |
| 132 | + name: 'loaderWithAuthorizationResults', |
| 133 | + }, |
| 134 | + }, |
| 135 | + }) |
| 136 | + .forEach((path) => { |
| 137 | + const loaderCallExpression = path.node; // Entity.loaderWithAuthorizationResults(viewerContext) |
| 138 | + const loaderCallee = loaderCallExpression.callee; // Entity.loaderWithAuthorizationResults |
| 139 | + |
| 140 | + if (loaderCallee.type !== 'MemberExpression') { |
| 141 | + return; |
| 142 | + } |
| 143 | + |
| 144 | + // Make sure this is a static method call on an entity (not on an instance) |
| 145 | + // Typically entity names start with uppercase letter |
| 146 | + if (loaderCallee.object.type === 'Identifier') { |
| 147 | + const firstChar = loaderCallee.object.name[0]; |
| 148 | + if (firstChar && firstChar === firstChar.toUpperCase()) { |
| 149 | + // Check if this loader uses knex-specific methods |
| 150 | + if (isKnexSpecificMethodUsed(j, path)) { |
| 151 | + // Rename loaderWithAuthorizationResults to knexLoaderWithAuthorizationResults |
| 152 | + if (loaderCallee.property.type === 'Identifier') { |
| 153 | + loaderCallee.property.name = 'knexLoaderWithAuthorizationResults'; |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + }); |
| 159 | +} |
| 160 | + |
| 161 | +export default function transformer(file: FileInfo, api: API, _options: Options): string { |
| 162 | + const j = api.jscodeshift; |
| 163 | + const root = j.withParser('ts')(file.source); |
| 164 | + |
| 165 | + transformLoaderToKnexLoader(j, root); |
| 166 | + transformLoaderWithAuthorizationResultsToKnexLoaderWithAuthorizationResults(j, root); |
| 167 | + |
| 168 | + return root.toSource(); |
| 169 | +} |
0 commit comments