|
| 1 | +import { |
| 2 | + runTransform, |
| 3 | + getParentUntilOrThrow, |
| 4 | + isJsxTagElement, |
| 5 | + getImportDeclarationByModuleSpecifier, |
| 6 | +} from '@sourcegraph/codemod-toolkit-ts' |
| 7 | + |
| 8 | +/** |
| 9 | + * Convert `<Icon as={MdiIcon} />` element to `<Icon svgPath={mdiIconPath} />` component. |
| 10 | + */ |
| 11 | +export const mdiIconToMdiPath = runTransform(context => { |
| 12 | + const { throwManualChangeError } = context |
| 13 | + |
| 14 | + const mdiIconPathsToImport = new Set<string>() |
| 15 | + |
| 16 | + const isMdiReactToken = (token: string): boolean => { |
| 17 | + const importDeclaration = getImportDeclarationByModuleSpecifier(context.sourceFile, `mdi-react/${token}`) |
| 18 | + return importDeclaration !== undefined |
| 19 | + } |
| 20 | + |
| 21 | + return { |
| 22 | + JsxAttribute(jsxAttribute) { |
| 23 | + const jsxTagElement = getParentUntilOrThrow(jsxAttribute, isJsxTagElement) |
| 24 | + if (jsxTagElement.getTagNameNode().getText() !== 'Icon') { |
| 25 | + // Not Icon component, so we exit |
| 26 | + return |
| 27 | + } |
| 28 | + |
| 29 | + const structure = jsxAttribute.getStructure() |
| 30 | + if (structure.name !== 'as' || !structure.initializer) { |
| 31 | + // Not the 'as' prop or empty, so we exit |
| 32 | + return |
| 33 | + } |
| 34 | + |
| 35 | + if (structure.initializer.includes(' ')) { |
| 36 | + // complex expression, so we exit |
| 37 | + throwManualChangeError({ |
| 38 | + node: jsxAttribute, |
| 39 | + message: 'Updating an expression is not supported. Please complete the transform manually', |
| 40 | + }) |
| 41 | + } |
| 42 | + |
| 43 | + // like `{CloseIcon}` |
| 44 | + const token = structure.initializer |
| 45 | + |
| 46 | + const iconRegex = /(\w*.)Icon/ |
| 47 | + if (!token.match(iconRegex)) { |
| 48 | + // Not an icon, so we exit |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + if (!isMdiReactToken(token.replace('{', '').replace('}', ''))) { |
| 53 | + // its a custom icon, we don't need to do anything |
| 54 | + return |
| 55 | + } |
| 56 | + |
| 57 | + const updatedValue = `mdi${token.replace(iconRegex, '$1')}`.replace('{', '').replace('}', '') |
| 58 | + |
| 59 | + // Store this value so we can import it once finished with this file. |
| 60 | + mdiIconPathsToImport.add(updatedValue) |
| 61 | + |
| 62 | + // Add `svgPath={...}` with updated value |
| 63 | + jsxTagElement.addAttribute({ |
| 64 | + name: 'svgPath', |
| 65 | + initializer: `{${updatedValue}}`, |
| 66 | + }) |
| 67 | + |
| 68 | + // Remove `as={...}` with old value |
| 69 | + jsxAttribute.remove() |
| 70 | + }, |
| 71 | + SourceFileExit(sourceFile) { |
| 72 | + if (mdiIconPathsToImport.size > 0) { |
| 73 | + // Add mdiIcon import |
| 74 | + sourceFile.addImportDeclaration({ |
| 75 | + namedImports: [...mdiIconPathsToImport], |
| 76 | + moduleSpecifier: '@mdi/js', |
| 77 | + }) |
| 78 | + } |
| 79 | + |
| 80 | + sourceFile.fixUnusedIdentifiers() |
| 81 | + }, |
| 82 | + } |
| 83 | +}) |
0 commit comments