Skip to content

Commit 9f4df51

Browse files
authored
[codemod] Add package name option (#45977)
1 parent 4c64b72 commit 9f4df51

File tree

235 files changed

+5189
-230
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

235 files changed

+5189
-230
lines changed

packages/mui-codemod/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ Examples:
4040
npx @mui/codemod@latest v5.0.0/preset-safe src --parser=flow
4141
```
4242

43+
### package name
44+
45+
Use this flag if you have a custom package name that reexports Material UI components. For example:
46+
47+
```bash
48+
npx @mui/codemod@latest --packageName="@org/ui"
49+
```
50+
51+
The snippet above will look for `@org/ui` instead of `@mui/material` in the codemod.
52+
4353
### jscodeshift options
4454

4555
To pass more options directly to jscodeshift, use `--jscodeshift="..."`. For example:

packages/mui-codemod/codemod.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ async function runJscodeshiftTransform(transform, files, flags, codemodFlags) {
7373
if (flags.jscodeshift) {
7474
args.push(flags.jscodeshift);
7575
}
76+
if (flags.packageName) {
77+
args.push(`--packageName=${flags.packageName}`);
78+
}
7679

7780
args.push(...files);
7881

@@ -196,6 +199,11 @@ yargs
196199
description: '(Advanced) Pass options directly to jscodeshift',
197200
default: false,
198201
type: 'string',
202+
})
203+
.option('packageName', {
204+
description: 'The package name to look for in the import statements',
205+
default: '@mui/material',
206+
type: 'string',
199207
});
200208
},
201209
handler: run,

packages/mui-codemod/src/deprecations/accordion-props/accordion-props.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ export default function transformer(file, api, options) {
1212

1313
movePropIntoSlots(j, {
1414
root,
15+
packageName: options.packageName,
1516
componentName: 'Accordion',
1617
propName: 'TransitionComponent',
1718
slotName: 'transition',
1819
});
1920

2021
movePropIntoSlotProps(j, {
2122
root,
23+
packageName: options.packageName,
2224
componentName: 'Accordion',
2325
propName: 'TransitionProps',
2426
slotName: 'transition',

packages/mui-codemod/src/deprecations/accordion-props/accordion-props.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,29 @@ describe('@mui/codemod', () => {
4949
expect(actual).to.equal(expected, 'The transformed version should be correct');
5050
});
5151
});
52+
53+
describe('[custom package] accordion-props', () => {
54+
it('transforms props as needed', () => {
55+
const actual = transform(
56+
{ source: read('./test-cases/package.actual.js') },
57+
{ jscodeshift },
58+
{ packageName: '@org/ui/material' },
59+
);
60+
61+
const expected = read('./test-cases/package.expected.js');
62+
expect(actual).to.equal(expected, 'The transformed version should be correct');
63+
});
64+
65+
it('should be idempotent', () => {
66+
const actual = transform(
67+
{ source: read('./test-cases/package.expected.js') },
68+
{ jscodeshift },
69+
{ packageName: '@org/ui/material' },
70+
);
71+
72+
const expected = read('./test-cases/package.expected.js');
73+
expect(actual).to.equal(expected, 'The transformed version should be correct');
74+
});
75+
});
5276
});
5377
});
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import Accordion from '@org/ui/material/Accordion';
2+
import { Accordion as MyAccordion } from '@org/ui/material';
3+
4+
<Accordion TransitionProps={{ unmountOnExit: true }} slots={{
5+
transition: CustomTransition
6+
}} />;
7+
<MyAccordion TransitionProps={transitionVars} slots={{
8+
transition: CustomTransition
9+
}} />;
10+
<Accordion
11+
TransitionProps={{ unmountOnExit: true }}
12+
slots={{
13+
root: 'div',
14+
transition: CustomTransition
15+
}}
16+
slotProps={{
17+
root: { className: 'foo' },
18+
}} />;
19+
<MyAccordion
20+
TransitionProps={{ unmountOnExit: true }}
21+
slots={{
22+
...outerSlots,
23+
transition: CustomTransition
24+
}}
25+
slotProps={{
26+
...outerSlotProps,
27+
}} />;
28+
<Accordion slots={{ transition: SlotTransition }} />;
29+
<Accordion TransitionProps={{ unmountOnExit: true }} slotProps={{ transition: { id: 'test' } }} />;
30+
// should skip non MUI components
31+
<NonMuiAccordion
32+
TransitionComponent={CustomTransition}
33+
TransitionProps={{ unmountOnExit: true }}
34+
/>;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import Accordion from '@org/ui/material/Accordion';
2+
import { Accordion as MyAccordion } from '@org/ui/material';
3+
4+
<Accordion slots={{
5+
transition: CustomTransition
6+
}} slotProps={{
7+
transition: { unmountOnExit: true }
8+
}} />;
9+
<MyAccordion slots={{
10+
transition: CustomTransition
11+
}} slotProps={{
12+
transition: transitionVars
13+
}} />;
14+
<Accordion
15+
slots={{
16+
root: 'div',
17+
transition: CustomTransition
18+
}}
19+
slotProps={{
20+
root: { className: 'foo' },
21+
transition: { unmountOnExit: true }
22+
}} />;
23+
<MyAccordion
24+
slots={{
25+
...outerSlots,
26+
transition: CustomTransition
27+
}}
28+
slotProps={{
29+
...outerSlotProps,
30+
transition: { unmountOnExit: true }
31+
}} />;
32+
<Accordion slots={{ transition: SlotTransition }} />;
33+
<Accordion
34+
slotProps={{ transition: {
35+
...{ unmountOnExit: true },
36+
...{ id: 'test' }
37+
} }} />;
38+
// should skip non MUI components
39+
<NonMuiAccordion
40+
TransitionComponent={CustomTransition}
41+
TransitionProps={{ unmountOnExit: true }}
42+
/>;

packages/mui-codemod/src/deprecations/accordion-summary-classes/accordion-summary-classes.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ export default function transformer(file, api, options) {
1414

1515
root
1616
.find(j.ImportDeclaration)
17-
.filter((path) => path.node.source.value.match(/^@mui\/material\/AccordionSummary$/))
17+
.filter((path) =>
18+
path.node.source.value.match(
19+
new RegExp(`^${options.packageName || '@mui/material'}(/AccordionSummary)?$`),
20+
),
21+
)
1822
.forEach((path) => {
1923
path.node.specifiers.forEach((specifier) => {
2024
if (

packages/mui-codemod/src/deprecations/accordion-summary-classes/accordion-summary-classes.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,33 @@ describe('@mui/codemod', () => {
3939
});
4040
});
4141

42+
describe('[package] js-transform', () => {
43+
it('transforms props as needed', () => {
44+
const actual = jsTransform(
45+
{ source: read('./test-cases/package.actual.js') },
46+
{ jscodeshift },
47+
{
48+
printOptions: { quote: 'single', trailingComma: true },
49+
packageName: '@org/ui/material',
50+
},
51+
);
52+
53+
const expected = read('./test-cases/package.expected.js');
54+
expect(actual).to.equal(expected, 'The transformed version should be correct');
55+
});
56+
57+
it('should be idempotent', () => {
58+
const actual = jsTransform(
59+
{ source: read('./test-cases/package.expected.js') },
60+
{ jscodeshift },
61+
{ packageName: '@org/ui/material' },
62+
);
63+
64+
const expected = read('./test-cases/package.expected.js');
65+
expect(actual).to.equal(expected, 'The transformed version should be correct');
66+
});
67+
});
68+
4269
describe('css-transform', () => {
4370
it('transforms classes as needed', async () => {
4471
const actual = await postcssProcessor.process(read('./test-cases/actual.css'), {
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { accordionSummaryClasses } from '@org/ui/material/AccordionSummary';
2+
3+
fn({
4+
MuiAccordionSummary: {
5+
styleOverrides: {
6+
root: {
7+
'& .MuiAccordionSummary-contentGutters': {
8+
color: 'red',
9+
},
10+
},
11+
},
12+
},
13+
});
14+
15+
fn({
16+
MuiAccordionSummary: {
17+
styleOverrides: {
18+
root: {
19+
[`& .${accordionSummaryClasses.contentGutters}`]: {
20+
color: 'red',
21+
},
22+
},
23+
},
24+
},
25+
});
26+
27+
styled(Component)(() => {
28+
return {
29+
'& .MuiAccordionSummary-contentGutters': {
30+
color: 'red',
31+
},
32+
};
33+
});
34+
35+
styled(Component)(() => {
36+
return {
37+
[`& .${accordionSummaryClasses.contentGutters}`]: {
38+
color: 'red',
39+
},
40+
};
41+
});
42+
43+
<AccordionSummary
44+
sx={{
45+
'& .MuiAccordionSummary-contentGutters': {
46+
color: 'red',
47+
},
48+
}}
49+
/>;
50+
51+
<AccordionSummary
52+
sx={{
53+
[`& .${accordionSummaryClasses.contentGutters}`]: {
54+
color: 'red',
55+
},
56+
}}
57+
/>;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { accordionSummaryClasses } from '@org/ui/material/AccordionSummary';
2+
3+
fn({
4+
MuiAccordionSummary: {
5+
styleOverrides: {
6+
root: {
7+
'&.MuiAccordionSummary-gutters .MuiAccordionSummary-content': {
8+
color: 'red',
9+
},
10+
},
11+
},
12+
},
13+
});
14+
15+
fn({
16+
MuiAccordionSummary: {
17+
styleOverrides: {
18+
root: {
19+
[`&.${accordionSummaryClasses.gutters} .${accordionSummaryClasses.content}`]: {
20+
color: 'red',
21+
},
22+
},
23+
},
24+
},
25+
});
26+
27+
styled(Component)(() => {
28+
return {
29+
'&.MuiAccordionSummary-gutters .MuiAccordionSummary-content': {
30+
color: 'red',
31+
},
32+
};
33+
});
34+
35+
styled(Component)(() => {
36+
return {
37+
[`&.${accordionSummaryClasses.gutters} .${accordionSummaryClasses.content}`]: {
38+
color: 'red',
39+
},
40+
};
41+
});
42+
43+
<AccordionSummary
44+
sx={{
45+
'&.MuiAccordionSummary-gutters .MuiAccordionSummary-content': {
46+
color: 'red',
47+
},
48+
}}
49+
/>;
50+
51+
<AccordionSummary
52+
sx={{
53+
[`&.${accordionSummaryClasses.gutters} .${accordionSummaryClasses.content}`]: {
54+
color: 'red',
55+
},
56+
}}
57+
/>;

packages/mui-codemod/src/deprecations/alert-classes/alert-classes.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ export default function transformer(file, api, options) {
1111
classes.forEach(({ deprecatedClass, replacementSelector }) => {
1212
root
1313
.find(j.ImportDeclaration)
14-
.filter((path) => path.node.source.value.match(/^@mui\/material\/Alert$/))
14+
.filter((path) =>
15+
path.node.source.value.match(
16+
new RegExp(`^${options.packageName || '@mui/material'}(/Alert)?$`),
17+
),
18+
)
1519
.forEach((path) => {
1620
path.node.specifiers.forEach((specifier) => {
1721
if (specifier.type === 'ImportSpecifier' && specifier.imported.name === 'alertClasses') {

packages/mui-codemod/src/deprecations/alert-classes/alert-classes.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,33 @@ describe('@mui/codemod', () => {
3939
});
4040
});
4141

42+
describe('[package] js-transform', () => {
43+
it('transforms props as needed', () => {
44+
const actual = jsTransform(
45+
{ source: read('./test-cases/package.actual.js') },
46+
{ jscodeshift },
47+
{
48+
printOptions: { quote: 'single', trailingComma: true },
49+
packageName: '@org/ui/material',
50+
},
51+
);
52+
53+
const expected = read('./test-cases/package.expected.js');
54+
expect(actual).to.equal(expected, 'The transformed version should be correct');
55+
});
56+
57+
it('should be idempotent', () => {
58+
const actual = jsTransform(
59+
{ source: read('./test-cases/package.expected.js') },
60+
{ jscodeshift },
61+
{ packageName: '@org/ui/material' },
62+
);
63+
64+
const expected = read('./test-cases/package.expected.js');
65+
expect(actual).to.equal(expected, 'The transformed version should be correct');
66+
});
67+
});
68+
4269
describe('css-transform', () => {
4370
it('transforms classes as needed', async () => {
4471
const actual = await postcssProcessor.process(read('./test-cases/actual.css'), {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { alertClasses } from '@org/ui/material/Alert';
2+
3+
('&.MuiAlert-standardSuccess');
4+
('&.MuiAlert-standardInfo');
5+
('&.MuiAlert-standardWarning');
6+
('&.MuiAlert-standardError');
7+
('&.MuiAlert-outlinedSuccess');
8+
('&.MuiAlert-outlinedInfo');
9+
('&.MuiAlert-outlinedWarning');
10+
('&.MuiAlert-outlinedError');
11+
('&.MuiAlert-filledSuccess');
12+
('&.MuiAlert-filledInfo');
13+
('&.MuiAlert-filledWarning');
14+
('&.MuiAlert-filledError');
15+
`&.${alertClasses.standardSuccess}`;
16+
`&.${alertClasses.standardInfo}`;
17+
`&.${alertClasses.standardWarning}`;
18+
`&.${alertClasses.standardError}`;
19+
`&.${alertClasses.outlinedSuccess}`;
20+
`&.${alertClasses.outlinedInfo}`;
21+
`&.${alertClasses.outlinedWarning}`;
22+
`&.${alertClasses.outlinedError}`;
23+
`&.${alertClasses.filledSuccess}`;
24+
`&.${alertClasses.filledInfo}`;
25+
`&.${alertClasses.filledWarning}`;
26+
`&.${alertClasses.filledError}`;

0 commit comments

Comments
 (0)