|
| 1 | +const parseSync = require("./utils/parseSync"); |
| 2 | + |
| 3 | +/** |
| 4 | + * @type {import('jscodeshift').Transform} |
| 5 | + */ |
| 6 | +const deprecatedReactFragmentTransform = (file, api) => { |
| 7 | + const j = api.jscodeshift; |
| 8 | + const ast = parseSync(file); |
| 9 | + |
| 10 | + const hasReactNodeImport = ast.find(j.ImportSpecifier, (node) => { |
| 11 | + const { imported, local } = node; |
| 12 | + return ( |
| 13 | + imported.type === "Identifier" && |
| 14 | + imported.name === "ReactNode" && |
| 15 | + // We don't support renames generally, so we don't handle them here |
| 16 | + (local == null || local.name === "ReactNode") |
| 17 | + ); |
| 18 | + }); |
| 19 | + const reactFragmentImports = ast.find(j.ImportSpecifier, (node) => { |
| 20 | + const { imported, local } = node; |
| 21 | + return ( |
| 22 | + imported.type === "Identifier" && |
| 23 | + imported.name === "ReactFragment" && |
| 24 | + // We don't support renames generally, so we don't handle them here |
| 25 | + (local == null || local.name === "ReactFragment") |
| 26 | + ); |
| 27 | + }); |
| 28 | + |
| 29 | + if (hasReactNodeImport.length > 0) { |
| 30 | + reactFragmentImports.remove(); |
| 31 | + } else { |
| 32 | + reactFragmentImports.replaceWith(() => { |
| 33 | + return j.importSpecifier(j.identifier("ReactNode")); |
| 34 | + }); |
| 35 | + } |
| 36 | + |
| 37 | + const changedIdentifiers = ast |
| 38 | + .find(j.TSTypeReference, (node) => { |
| 39 | + const { typeName } = node; |
| 40 | + |
| 41 | + return ( |
| 42 | + typeName.type === "Identifier" && typeName.name === "ReactFragment" |
| 43 | + ); |
| 44 | + }) |
| 45 | + .replaceWith(() => { |
| 46 | + // `Iterable<ReactNode>` |
| 47 | + return j.tsTypeReference( |
| 48 | + j.identifier("Iterable"), |
| 49 | + j.tsTypeParameterInstantiation([ |
| 50 | + j.tsTypeReference(j.identifier("ReactNode")), |
| 51 | + ]), |
| 52 | + ); |
| 53 | + }); |
| 54 | + |
| 55 | + const changedQualifiedNames = ast |
| 56 | + .find(j.TSTypeReference, (node) => { |
| 57 | + const { typeName } = node; |
| 58 | + |
| 59 | + return ( |
| 60 | + typeName.type === "TSQualifiedName" && |
| 61 | + typeName.right.type === "Identifier" && |
| 62 | + typeName.right.name === "ReactFragment" |
| 63 | + ); |
| 64 | + }) |
| 65 | + .replaceWith((path) => { |
| 66 | + const { node } = path; |
| 67 | + const typeName = /** @type {import('jscodeshift').TSQualifiedName} */ ( |
| 68 | + node.typeName |
| 69 | + ); |
| 70 | + // `Iterable<*.ReactNode>` |
| 71 | + return j.tsTypeReference( |
| 72 | + j.identifier("Iterable"), |
| 73 | + j.tsTypeParameterInstantiation([ |
| 74 | + j.tsTypeReference( |
| 75 | + j.tsQualifiedName(typeName.left, j.identifier("ReactNode")), |
| 76 | + ), |
| 77 | + ]), |
| 78 | + ); |
| 79 | + }); |
| 80 | + |
| 81 | + // Otherwise some files will be marked as "modified" because formatting changed |
| 82 | + if ( |
| 83 | + changedIdentifiers.length > 0 || |
| 84 | + changedQualifiedNames.length > 0 || |
| 85 | + reactFragmentImports.length > 0 |
| 86 | + ) { |
| 87 | + return ast.toSource(); |
| 88 | + } |
| 89 | + return file.source; |
| 90 | +}; |
| 91 | + |
| 92 | +module.exports = deprecatedReactFragmentTransform; |
0 commit comments