packages/codemods/documentation/renamePropTransforms.md
For developers who want more freedom when changing the value of props, transforms come to the rescue!
Check out ../src/codemods/utilities/transforms.ts to see local documentation and more transform examples!
renameProp() utility). Use the second parameter if you know your domain of prop values and want old values to be mapped to new ones.
const file = project.getSourceFileOrThrow(DropdownPropsFile);
const tags = findJsxTag(file, 'Dropdown');
const dropdownMap: ValueMap<string> = { false: 'true', true: 'false' };
const transform = boolTransform(dropdownMap);
renameProp(tags, 'isDisabled', 'disabled', undefined, transform);
PropTransform, which is a closure that actually modifies the codebase. This is what aPropTransform function takes in:export type PropTransform = (
node: JsxExpression | JsxOpeningElement | JsxSelfClosingElement,
toRename: string,
replacementName: string,
) => Result<string, NoOp>;
And here's what the body of a transform might look like:
export function boolTransform(newValue?: boolean, map?: ValueMap<string>): PropTransform {
return (
element: JsxExpression | JsxOpeningElement | JsxSelfClosingElement,
toRename: string,
replacementName: string,
) => {
if (elementNotInSpread(element)) {
const toChange = getValueToChange(element as JsxExpression);
if (toChange) {
const oldText = toChange.getText();
toChange.replaceWithText(map ? map[oldText] : newValue !== undefined ? newValue.toString() : toRename);
return Ok('Prop value transformed successfully');
}
return Err({ reason: 'Could not access prop value to transform.' });
} else {
return renamePropInSpread(
element as JsxOpeningElement | JsxSelfClosingElement,
toRename,
replacementName,
map,
newValue?.toString(),
);
}
};
}
renameProp().renamePropInSpread(). Because PropTransform returns the type Result<string, NoOp>, the return statements are necessary for logging purposes.
Ok('success message'), and in an unsuccessful case you should return Err('failure message'). renamePropInspread() returns a result.