diff --git a/tools/codemods/README.md b/tools/codemods/README.md index 813931404b..923a4c2456 100644 --- a/tools/codemods/README.md +++ b/tools/codemods/README.md @@ -74,75 +74,26 @@ yarn lg codemod --force **_NOTE:_ These codemods are for testing purposes only** -### `consolidate-props` +### `consolidate-popover-usePortal-renderMode-props` -This codemod consolidates two props into one. +This codemod adds an explicit `usePortal` prop if left undefined and consolidates the `usePortal` and `renderMode` props into a single `renderMode` prop. ```jsx -yarn lg codemod codemode-props +yarn lg codemod consolidate-popover-usePortal-renderMode-props ``` -E.g. -In this example, the `disabled` props is merged into the `state` prop. - -**Before**: - -```jsx - -``` - -**After**: - -```jsx - -``` - -
- -### `rename-component-prop` - -This codemod renames a component prop - -```jsx -yarn lg codemod codemode-component-prop -``` - -E.g. -In this example, `prop` is renamed to `newProp`. - -**Before**: - -```jsx - -``` - -**After**: - -```jsx - -``` - -
- -### `update-component-prop-value` - -This codemod updates a prop value - -```jsx -yarn lg codemod codemode-component-prop-value -``` - -E.g. -In this example, `value` is updated to `new prop value`. - **Before**: ```jsx - + + + ``` **After**: ```jsx - + + + ``` diff --git a/tools/codemods/src/codemods/consolidate-popover-usePortal-renderMode-props/transform.ts b/tools/codemods/src/codemods/consolidate-popover-usePortal-renderMode-props/transform.ts index a9563fabf1..20be8df00d 100644 --- a/tools/codemods/src/codemods/consolidate-popover-usePortal-renderMode-props/transform.ts +++ b/tools/codemods/src/codemods/consolidate-popover-usePortal-renderMode-props/transform.ts @@ -7,11 +7,13 @@ import { import { addJSXAttributes } from '../../utils/transformations/addJSXAttributes'; type TransformerOptions = ConsolidateJSXAttributesOptions & { - componentName: string; + componentNames: Array; }; /** - * Example transformer function to consolidate props + * Transformer function that: + * 1. adds an explicit `usePortal` prop if left undefined + * 2. consolidates the `usePortal` and `renderMode` props into a single `renderMode` prop * * @param file the file to transform * @param jscodeshiftOptions an object containing at least a reference to the jscodeshift library @@ -34,29 +36,44 @@ export default function transformer( true: 'portal', }, propToRemoveType = 'boolean', - componentName = 'Popover', + componentNames = [ + 'Code', + 'Combobox', + 'DatePicker', + 'InfoSprinkle', + 'InlineDefinition', + 'Menu', + 'NumberInput', + 'Popover', + 'SearchInput', + 'Select', + 'SplitButton', + 'Tooltip', + ], } = options; - // Check if the element is on the page - const elements = source.findJSXElements(componentName); + componentNames.forEach(componentName => { + // Check if the element is on the page + const elements = source.findJSXElements(componentName); - // If there are no elements then return the original file - if (elements.length === 0) return file.source; + // If there are no elements then return the original file + if (elements.length === 0) return; - elements.forEach(element => { - addJSXAttributes({ - j, - element, - propName: propToRemove, - propValue: true, - }); - consolidateJSXAttributes({ - j, - element, - propToRemove, - propToUpdate, - propMapping, - propToRemoveType, + elements.forEach(element => { + addJSXAttributes({ + j, + element, + propName: propToRemove, + propValue: true, + }); + consolidateJSXAttributes({ + j, + element, + propToRemove, + propToUpdate, + propMapping, + propToRemoveType, + }); }); });