Skip to content

Commit

Permalink
Refactor to cover extra case
Browse files Browse the repository at this point in the history
  • Loading branch information
DiegoAndai committed Sep 2, 2024
1 parent 4168d7e commit cee2d52
Showing 1 changed file with 4 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export default function transformer(file, api, options) {

const openTaggedNotHavingButtonProp = new Set();
const openTaggedHavingButtonProp = new Set();
let addedListItemButton = false;
// Rename components that have ListItem button to ListItemButton
findComponentJSX(j, { root, componentName: 'ListItem' }, (elementPath) => {
const index = elementPath.node.openingElement.attributes.findIndex(
Expand All @@ -62,6 +63,7 @@ export default function transformer(file, api, options) {
// The ListItem has a button prop
if (index !== -1) {
openTaggedHavingButtonProp.add(elementPath.node.openingElement.name.name);
addedListItemButton = true;
elementPath.node.openingElement.name.name = `ListItemButton`;
elementPath.node.openingElement.attributes.splice(index, 1);
} else {
Expand Down Expand Up @@ -111,16 +113,12 @@ export default function transformer(file, api, options) {
})
.remove();

if (importsToRemove.length === 0) {
return root.toSource(printOptions);
}

// If ListItemButton does not already exist, add it at the end
// If ListItemButton import does not already exist, add it at the end
const imports = root
.find(j.ImportDeclaration)
.filter((path) => path.node.source.value === '@mui/material/ListItemButton');

if (imports.length === 0) {
if (addedListItemButton && imports.length === 0) {
const lastImport = root.find(j.ImportDeclaration).at(-1);

// Insert the import for 'ListItemButton' after the last import declaration
Expand Down

0 comments on commit cee2d52

Please sign in to comment.