-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1619 from cozy/add-codemod
feat: Add codemod to migrate ListItemText props
- Loading branch information
Showing
1 changed file
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
export default function transformer(file, api) { | ||
const j = api.jscodeshift; | ||
const root = j(file.source); | ||
|
||
const transformAttribute = (path, type) => { | ||
const oldAttr = path.node.attributes.find(attr => attr.name.name == `${type}Text`) | ||
if (oldAttr) { | ||
oldAttr.name.name = type | ||
const oldAttrClassNameIdx = path.node.attributes.findIndex(attr => attr.name.name == `${type}TextClassName`) | ||
const oldAttrClassName = path.node.attributes[oldAttrClassNameIdx] | ||
if (oldAttrClassName) { | ||
oldAttr.value = j.jsxExpressionContainer(j.jsxElement( | ||
j.jsxOpeningElement(j.jsxIdentifier('span'), [ | ||
j.jsxAttribute(j.jsxIdentifier('className'), oldAttrClassName.value) | ||
]), | ||
j.jsxClosingElement(j.jsxIdentifier('span')), | ||
[oldAttr.value] | ||
)) | ||
path.node.attributes.splice(oldAttrClassNameIdx, 1) | ||
} | ||
} | ||
} | ||
|
||
root.find(j.JSXOpeningElement, { name: { name: 'ListItemText' } } ).forEach((path) => { | ||
transformAttribute(path, 'primary'); | ||
transformAttribute(path, 'secondary'); | ||
|
||
}); | ||
|
||
return root.toSource(); | ||
} |