Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[codemod] Skip ListItemButton import for unrelated files #43532

Merged
merged 2 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -110,12 +112,13 @@ export default function transformer(file, api, options) {
return false;
})
.remove();
// 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
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,16 @@ describe('@mui/codemod', () => {
expect(actual).to.not.equal(expected);
});
});

it('should skip files that do not import ListItem from @mui/material', () => {
const actual = transform(
{ source: read('./test-cases/not-related.actual.js') },
{ jscodeshift },
{},
);

const expected = read('./test-cases/not-related.expected.js');
expect(actual).to.equal(expected, 'The transformed version should be correct');
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import ListItem from '@component/ListItem';

// The codemod should skip this file because it does not import ListItem from `@mui/material`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import ListItem from '@component/ListItem';

// The codemod should skip this file because it does not import ListItem from `@mui/material`.