|
| 1 | +const parseSync = require("./utils/parseSync"); |
| 2 | +const { |
| 3 | + findTSTypeReferenceCollections, |
| 4 | +} = require("./utils/jscodeshift-bugfixes"); |
| 5 | + |
| 6 | +/** |
| 7 | + * @type {import('jscodeshift').Transform} |
| 8 | + */ |
| 9 | +const deprecatedLegacyRefTransform = (file, api) => { |
| 10 | + const j = api.jscodeshift; |
| 11 | + const ast = parseSync(file); |
| 12 | + |
| 13 | + let hasChanges = false; |
| 14 | + |
| 15 | + const targetIdentifierImports = ast.find(j.ImportSpecifier, (node) => { |
| 16 | + const { imported, local } = node; |
| 17 | + return ( |
| 18 | + imported.type === "Identifier" && |
| 19 | + imported.name === "Ref" && |
| 20 | + // We don't support renames generally, so we don't handle them here |
| 21 | + (local == null || local.name === "Ref") |
| 22 | + ); |
| 23 | + }); |
| 24 | + const sourceIdentifierImports = ast.find(j.ImportSpecifier, (node) => { |
| 25 | + const { imported, local } = node; |
| 26 | + return ( |
| 27 | + imported.type === "Identifier" && |
| 28 | + imported.name === "LegacyRef" && |
| 29 | + // We don't support renames generally, so we don't handle them here |
| 30 | + (local == null || local.name === "LegacyRef") |
| 31 | + ); |
| 32 | + }); |
| 33 | + if (targetIdentifierImports.length > 0) { |
| 34 | + hasChanges = true; |
| 35 | + sourceIdentifierImports.remove(); |
| 36 | + } else if (sourceIdentifierImports.length > 0) { |
| 37 | + hasChanges = true; |
| 38 | + sourceIdentifierImports.replaceWith((path) => { |
| 39 | + const importSpecifier = j.importSpecifier(j.identifier("Ref")); |
| 40 | + if ("importKind" in path.node) { |
| 41 | + // @ts-expect-error -- Missing types in jscodeshift. Babel uses `importKind`: https://astexplorer.net/#/gist/a76bd35f28483a467fef29d3c63aac9b/0e7ba6688fc09bd11b92197349b2384bb4c94574 |
| 42 | + importSpecifier.importKind = path.node.importKind; |
| 43 | + } |
| 44 | + |
| 45 | + return importSpecifier; |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + const sourceIdentifierTypeReferences = findTSTypeReferenceCollections( |
| 50 | + j, |
| 51 | + ast, |
| 52 | + (node) => { |
| 53 | + const { typeName } = node; |
| 54 | + |
| 55 | + return typeName.type === "Identifier" && typeName.name === "LegacyRef"; |
| 56 | + }, |
| 57 | + ); |
| 58 | + for (const typeReferences of sourceIdentifierTypeReferences) { |
| 59 | + const changedIdentifiers = typeReferences.replaceWith((path) => { |
| 60 | + // `Ref<T>` |
| 61 | + return j.tsTypeReference( |
| 62 | + j.identifier("Ref"), |
| 63 | + path.get("typeParameters").value, |
| 64 | + ); |
| 65 | + }); |
| 66 | + if (changedIdentifiers.length > 0) { |
| 67 | + hasChanges = true; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + const sourceIdentifierQualifiedNamesReferences = |
| 72 | + findTSTypeReferenceCollections(j, ast, (node) => { |
| 73 | + const { typeName } = node; |
| 74 | + |
| 75 | + return ( |
| 76 | + typeName.type === "TSQualifiedName" && |
| 77 | + typeName.right.type === "Identifier" && |
| 78 | + typeName.right.name === "LegacyRef" |
| 79 | + ); |
| 80 | + }); |
| 81 | + for (const typeReferences of sourceIdentifierQualifiedNamesReferences) { |
| 82 | + const changedQualifiedNames = typeReferences.replaceWith((path) => { |
| 83 | + const { node } = path; |
| 84 | + const typeName = /** @type {import('jscodeshift').TSQualifiedName} */ ( |
| 85 | + node.typeName |
| 86 | + ); |
| 87 | + // `*.Ref<T>` |
| 88 | + return j.tsTypeReference( |
| 89 | + j.tsQualifiedName(typeName.left, j.identifier("Ref")), |
| 90 | + path.get("typeParameters").value, |
| 91 | + ); |
| 92 | + }); |
| 93 | + if (changedQualifiedNames.length > 0) { |
| 94 | + hasChanges = true; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + // Otherwise some files will be marked as "modified" because formatting changed |
| 99 | + if (hasChanges) { |
| 100 | + return ast.toSource(); |
| 101 | + } |
| 102 | + return file.source; |
| 103 | +}; |
| 104 | + |
| 105 | +module.exports = deprecatedLegacyRefTransform; |
0 commit comments