Skip to content

Commit

Permalink
Address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
stephl3 committed Oct 2, 2024
1 parent c133311 commit 54664af
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 79 deletions.
67 changes: 9 additions & 58 deletions tools/codemods/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,75 +74,26 @@ yarn lg codemod <codemode> <path> --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 <path>
yarn lg codemod consolidate-popover-usePortal-renderMode-props <path>
```

E.g.
In this example, the `disabled` props is merged into the `state` prop.

**Before**:

```jsx
<MyComponent disabled={true} state="valid" />
```

**After**:

```jsx
<MyComponent state="disabled" />
```

<hr>

### `rename-component-prop`

This codemod renames a component prop

```jsx
yarn lg codemod codemode-component-prop <path>
```

E.g.
In this example, `prop` is renamed to `newProp`.

**Before**:

```jsx
<MyComponent prop="hey" />
```

**After**:

```jsx
<MyComponent newProp="hey" />
```

<hr>

### `update-component-prop-value`

This codemod updates a prop value

```jsx
yarn lg codemod codemode-component-prop-value <path>
```

E.g.
In this example, `value` is updated to `new prop value`.

**Before**:

```jsx
<MyComponent prop="value" />
<Popover />
<Popover usePortal={true} />
<Popover usePortal={false} />
```

**After**:

```jsx
<MyComponent prop="new prop value" />
<Popover renderMode="portal" />
<Popover renderMode="portal" />
<Popover renderMode="inline" />
```
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import {
import { addJSXAttributes } from '../../utils/transformations/addJSXAttributes';

type TransformerOptions = ConsolidateJSXAttributesOptions & {
componentName: string;
componentNames: Array<string>;
};

/**
* 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
Expand All @@ -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,
});
});
});

Expand Down

0 comments on commit 54664af

Please sign in to comment.