Skip to content

Commit

Permalink
Merge pull request #1619 from cozy/add-codemod
Browse files Browse the repository at this point in the history
feat: Add codemod to migrate ListItemText props
  • Loading branch information
ptbrowne authored Oct 25, 2020
2 parents 5f65fd7 + 1b82a9f commit a25d6fc
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions codemods/transform-list-item-attributes.js
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();
}

0 comments on commit a25d6fc

Please sign in to comment.