in codemods/deprecateUtilityComponents.js [78:110]
function rewriteImport(from, to, attributes) {
imports.forEach(decl => {
j(decl)
.find(j.ImportSpecifier, {imported: {name: from}})
.forEach(spec => {
if (importsByName[to]) {
// if the destination import already exists and there are members
// in this identifier, then this one is a dupe
j(spec).remove()
} else {
// otherwise, we can safely rename this one to the new identifier
spec.node.imported.name = to
importsByName[to] = spec
}
})
})
ast.find(j.JSXOpeningElement, {name: {name: from}}).forEach(nodePath => {
for (const [attr, value] of Object.entries(attributes || {})) {
const expression = typeof value === 'string' ? j.literal(value) : j.jsxExpressionContainer(j.literal(value))
const attrExists = nodePath.value.attributes.find(a => a?.name?.name === attr)
if (!attrExists) {
nodePath.value.attributes.push(j.jsxAttribute(j.jsxIdentifier(attr), expression))
}
}
})
// replace all of the rewritten identifiers with member expressions
ast
.find(j.Identifier, {name: from})
.filter(id => id.parent.node.type !== 'ImportSpecifier')
.replaceWith(j.jsxIdentifier(to))
}