From 1b82a9f9a9311b964abd2b11766ac7ea82594020 Mon Sep 17 00:00:00 2001 From: Patrick Browne Date: Fri, 23 Oct 2020 11:12:19 +0200 Subject: [PATCH] feat: Add codemod to migrate ListItemText props primaryText and secondaryText were deprecated, a codemod is here to help with the migration --- codemods/transform-list-item-attributes.js | 31 ++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 codemods/transform-list-item-attributes.js diff --git a/codemods/transform-list-item-attributes.js b/codemods/transform-list-item-attributes.js new file mode 100644 index 0000000000..e6923f4724 --- /dev/null +++ b/codemods/transform-list-item-attributes.js @@ -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(); +}