Skip to content

Commit

Permalink
improve codemod again
Browse files Browse the repository at this point in the history
  • Loading branch information
hbjORbj committed Apr 20, 2023
1 parent 545b051 commit 7b1ad34
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 106 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

45 changes: 36 additions & 9 deletions packages/mui-codemod/src/v5.0.0/base-remove-unstyled-suffix.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,49 @@
export default function transformer(file, api) {
const j = api.jscodeshift;
const root = j(file.source);
const variableNameMap = new Map();
let final = file.source;
const importedNameMap = new Map();

root
let final = root
.find(j.ImportDeclaration)
.filter(({ node }) => node.source.value.startsWith('@mui/base'))
.filter(({ node }) => {
const sourceVal = node.source.value;
if (sourceVal.startsWith('@mui/base')) {
node.source.value = sourceVal.replace(/unstyled/i, '');
node.source.raw = sourceVal.replace(/unstyled/i, '');
}

return sourceVal.startsWith('@mui/base');
})
.forEach((path) => {
const specifiers = [];
path.node.specifiers.forEach((elementNode) => {
const variableName = elementNode.local.name;
if (variableName.toLowerCase().includes('unstyled') && !variableNameMap.has(variableName)) {
variableNameMap.set(variableName, variableName.replace(/unstyled/gi, ''));
if (elementNode.type === 'ImportDefaultSpecifier') {
return;
}
const importedName = elementNode.imported.name;
if (importedName.endsWith('Unstyled')) {
// component
elementNode.imported.name = importedName.replace(/unstyled/i, '');
if (elementNode.local.name === importedName) {
elementNode.local.name = importedName;
// specifier must be newly created to add "as";
// e.g., import { SwitchUnstyled } to import { Switch as SwitchUnstyled}
specifiers.push(j.importSpecifier(elementNode.imported, elementNode.local));
}
return;
}
if (importedName.toLowerCase().includes('unstyled') && !importedNameMap.has(importedName)) {
// types, utils, etc
importedNameMap.set(importedName, importedName.replace(/unstyled/gi, ''));
}
});
});
if (specifiers.length > 0) {
path.node.specifiers = specifiers;
}
})
.toSource();

variableNameMap.forEach((after, before) => {
importedNameMap.forEach((after, before) => {
final = final.replaceAll(before, after);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function read(fileName) {
describe('@mui/codemod', () => {
describe('v5.0.0', () => {
describe('base-remove-unstyled-suffix', () => {
it('removes `Unstyled` suffix from Base UI components', () => {
it('removes `Unstyled` suffix from Base UI components except default import declarations', () => {
const actual = transform(
{
source: read('./base-remove-unstyled-suffix.test/actual.js'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@ import ButtonUnstyled, {
ButtonUnstyledRootSlotProps,
buttonUnstyledClasses,
} from '@mui/base/ButtonUnstyled';
import { SwitchUnstyled } from "@mui/base";
import { InputUnstyled as BaseInput } from "@mui/base";
import BaseSlider from "@mui/base/Slider";

function App() {
return <ButtonUnstyled />;
return;
<>
<ButtonUnstyled />;
<SwitchUnstyled />;
<BaseInput />;
<BaseSlider />;
</>;
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import Button, {
import ButtonUnstyled, {
ButtonProps,
ButtonRootSlotProps,
buttonClasses,
} from '@mui/base/Button';
} from "@mui/base/Button";
import { Switch as SwitchUnstyled } from "@mui/base";
import { Input as BaseInput } from "@mui/base";
import BaseSlider from "@mui/base/Slider";

function App() {
return <Button />;
return;
<>
<ButtonUnstyled />;
<SwitchUnstyled />;
<BaseInput />;
<BaseSlider />;
</>;
}

0 comments on commit 7b1ad34

Please sign in to comment.