|
| 1 | +import { Node, printNode } from 'ts-morph' |
| 2 | + |
| 3 | +import { |
| 4 | + removeClassNameAndUpdateJsxElement, |
| 5 | + addOrUpdateSourcegraphWildcardImportIfNeeded, |
| 6 | +} from '@sourcegraph/codemod-toolkit-packages' |
| 7 | +import { |
| 8 | + runTransform, |
| 9 | + getParentUntilOrThrow, |
| 10 | + isJsxTagElement, |
| 11 | + getTagName, |
| 12 | + JsxTagElement, |
| 13 | + setOnJsxTagElement, |
| 14 | + getJsxAttributeStringValue, |
| 15 | + removeJsxAttribute, |
| 16 | +} from '@sourcegraph/codemod-toolkit-ts' |
| 17 | + |
| 18 | +import { validateCodemodTarget, validateCodemodTargetOrThrow } from './validateCodemodTarget' |
| 19 | + |
| 20 | +/** |
| 21 | + * Convert `<input class="form-control" />` element to the `<Input />` component. |
| 22 | + */ |
| 23 | +export const inputToComponent = runTransform(context => { |
| 24 | + const { throwManualChangeError, addManualChangeLog } = context |
| 25 | + |
| 26 | + const jsxTagElementsToUpdate = new Set<JsxTagElement>() |
| 27 | + |
| 28 | + return { |
| 29 | + JsxSelfClosingElement(jsxTagElement) { |
| 30 | + if (validateCodemodTarget.JsxTagElement(jsxTagElement)) { |
| 31 | + jsxTagElementsToUpdate.add(jsxTagElement) |
| 32 | + } |
| 33 | + }, |
| 34 | + StringLiteral(stringLiteral) { |
| 35 | + const { classNameMappings } = validateCodemodTargetOrThrow.StringLiteral(stringLiteral) |
| 36 | + const jsxAttribute = getParentUntilOrThrow(stringLiteral, Node.isJsxAttribute) |
| 37 | + |
| 38 | + if (!/classname/i.test(jsxAttribute.getName())) { |
| 39 | + return |
| 40 | + } |
| 41 | + |
| 42 | + const jsxTagElement = getParentUntilOrThrow(jsxAttribute, isJsxTagElement) |
| 43 | + |
| 44 | + if (!validateCodemodTarget.JsxTagElement(jsxTagElement)) { |
| 45 | + throwManualChangeError({ |
| 46 | + node: jsxTagElement, |
| 47 | + message: `Class '${stringLiteral.getLiteralText()}' is used on the '${getTagName( |
| 48 | + jsxTagElement |
| 49 | + )}' element. Please update it manually.`, |
| 50 | + }) |
| 51 | + } |
| 52 | + |
| 53 | + for (const { className, props } of classNameMappings) { |
| 54 | + const { isRemoved, manualChangeLog } = removeClassNameAndUpdateJsxElement(stringLiteral, className) |
| 55 | + |
| 56 | + if (manualChangeLog) { |
| 57 | + addManualChangeLog(manualChangeLog) |
| 58 | + } |
| 59 | + |
| 60 | + if (isRemoved) { |
| 61 | + for (const { name, value } of props) { |
| 62 | + jsxTagElement.addAttribute({ |
| 63 | + name, |
| 64 | + initializer: printNode(value), |
| 65 | + }) |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + jsxTagElementsToUpdate.add(jsxTagElement) |
| 71 | + }, |
| 72 | + SourceFileExit(sourceFile) { |
| 73 | + if (jsxTagElementsToUpdate.size === 0) { |
| 74 | + return |
| 75 | + } |
| 76 | + |
| 77 | + for (const jsxTagElement of jsxTagElementsToUpdate) { |
| 78 | + if (getJsxAttributeStringValue(jsxTagElement, 'type') === 'text') { |
| 79 | + removeJsxAttribute(jsxTagElement, 'type') |
| 80 | + } |
| 81 | + |
| 82 | + setOnJsxTagElement(jsxTagElement, { name: 'Input' }) |
| 83 | + } |
| 84 | + |
| 85 | + addOrUpdateSourcegraphWildcardImportIfNeeded({ |
| 86 | + sourceFile, |
| 87 | + importStructure: { |
| 88 | + namedImports: ['Input'], |
| 89 | + }, |
| 90 | + }) |
| 91 | + |
| 92 | + sourceFile.fixUnusedIdentifiers() |
| 93 | + }, |
| 94 | + } |
| 95 | +}) |
0 commit comments