|
| 1 | +/** |
| 2 | + * This renames: |
| 3 | + * - <Async.Pending> to <Async.Initial> |
| 4 | + * - <Async.Loading> to <Async.Pending> |
| 5 | + * - <Async.Resolved> to <Async.Fulfilled> |
| 6 | + * |
| 7 | + * This includes any custom instances created with createInstance(). |
| 8 | + */ |
| 9 | + |
| 10 | +export default function transform({ path, source }, api) { |
| 11 | + if (path.includes("/node_modules/")) return |
| 12 | + |
| 13 | + const j = api.jscodeshift |
| 14 | + const root = j(source) |
| 15 | + |
| 16 | + const renameJsxMembers = parentName => { |
| 17 | + root |
| 18 | + .find(j.JSXMemberExpression, { object: { name: parentName }, property: { name: "Pending" } }) |
| 19 | + .forEach(node => (node.value.property.name = "Initial")) |
| 20 | + root |
| 21 | + .find(j.JSXMemberExpression, { object: { name: parentName }, property: { name: "Loading" } }) |
| 22 | + .forEach(node => (node.value.property.name = "Pending")) |
| 23 | + root |
| 24 | + .find(j.JSXMemberExpression, { object: { name: parentName }, property: { name: "Resolved" } }) |
| 25 | + .forEach(node => (node.value.property.name = "Fulfilled")) |
| 26 | + } |
| 27 | + |
| 28 | + // Rename instances using default import |
| 29 | + root |
| 30 | + .find(j.ImportDeclaration, { source: { value: "react-async" } }) |
| 31 | + .find(j.ImportDefaultSpecifier) |
| 32 | + .forEach(node => renameJsxMembers(node.value.local.name)) |
| 33 | + |
| 34 | + // Rename instances using named `Async` import |
| 35 | + root |
| 36 | + .find(j.ImportDeclaration, { source: { value: "react-async" } }) |
| 37 | + .find(j.ImportSpecifier, { imported: { name: "Async" } }) |
| 38 | + .forEach(node => renameJsxMembers(node.value.local.name)) |
| 39 | + |
| 40 | + // Rename instances created with `createInstance` |
| 41 | + root |
| 42 | + .find(j.ImportDeclaration, { source: { value: "react-async" } }) |
| 43 | + .find(j.ImportSpecifier, { imported: { name: "createInstance" } }) |
| 44 | + .forEach(node => { |
| 45 | + const createInstance = node.value.local.name |
| 46 | + root |
| 47 | + .find(j.VariableDeclarator) |
| 48 | + .filter(node => node.value.init.type === "CallExpression") |
| 49 | + .filter(node => node.value.init.callee.name === createInstance) |
| 50 | + .forEach(node => renameJsxMembers(node.value.id.name)) |
| 51 | + }) |
| 52 | + |
| 53 | + return root.toSource() |
| 54 | +} |
0 commit comments