Skip to content

Commit e06faca

Browse files
[codemod] Document and Clean the codemod utils (mui#21014)
1 parent 2cf8e64 commit e06faca

File tree

9 files changed

+112
-108
lines changed

9 files changed

+112
-108
lines changed

packages/x-codemod/src/util/propsToObject.ts

Lines changed: 0 additions & 68 deletions
This file was deleted.

packages/x-codemod/src/util/removeObjectProperty.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ const getAttributeName = (attribute: JSXAttribute): string =>
55

66
interface RemoveObjectPropertyArgs {
77
root: Collection<any>;
8+
/**
9+
* Names of the components to target
10+
* @example ["DataGrid", "DataGridPro"]
11+
*/
812
componentsNames: string[];
913
/**
1014
* Prop which contains the object whose property will be removed
@@ -21,6 +25,10 @@ interface RemoveObjectPropertyArgs {
2125
j: JSCodeshift;
2226
}
2327

28+
/**
29+
* Removes a property from an object prop in specified components.
30+
* If the object only contains that property, the whole prop is removed.
31+
*/
2432
export default function removeObjectProperty({
2533
root,
2634
propName,

packages/x-codemod/src/util/removeProps.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,22 @@ import type { Collection, JSCodeshift } from 'jscodeshift';
22

33
interface RemovePropsArgs {
44
root: Collection<any>;
5+
/**
6+
* Names of the components to target
7+
* @example ["DataGrid", "DataGridPro"]
8+
*/
59
componentNames: string[];
10+
/**
11+
* The list of props to remove
12+
* @example ["disableSelectionOnClick", "hideFooterSelectedRowCount"]
13+
*/
614
props: string[];
715
j: JSCodeshift;
816
}
917

18+
/**
19+
* Removes specified props from given components.
20+
*/
1021
export default function removeProps({ root, componentNames, props, j }: RemovePropsArgs) {
1122
return root
1223
.find(j.JSXElement)

packages/x-codemod/src/util/renameCSSClasses.ts

Lines changed: 0 additions & 30 deletions
This file was deleted.

packages/x-codemod/src/util/renameComponentsSlots.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@ import type { Collection, JSCodeshift } from 'jscodeshift';
22

33
interface RenamePropsArgs {
44
root: Collection<any>;
5+
/**
6+
* Names of the components to target
7+
* @example ["DataGrid", "DataGridPro"]
8+
*/
59
componentNames: string[];
10+
/**
11+
* Translation mapping from component names to slot names
12+
* @example { Root: "root", Input: "input" }
13+
*/
614
translation: Record<string, string>;
715
j: JSCodeshift;
816
}
@@ -18,6 +26,10 @@ const getSlotsTranslation = (translations: Record<string, string>) => {
1826
return lowercasedTranslation;
1927
};
2028

29+
/**
30+
* Replace the components / componentsProps by their equivalent slots / slotProps.
31+
* Only used for v6 -> v7 migration.
32+
*/
2133
export default function renameComponentsSlots({
2234
root,
2335
componentNames,

packages/x-codemod/src/util/renameImports.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,34 @@ import type {
77
} from 'jscodeshift';
88

99
interface ImportConfig {
10+
/**
11+
* The old endpoint relative to the package name.
12+
* @example 'TreeView' in '@mui/x-tree-view/TreeView'
13+
*/
1014
oldEndpoint?: string;
15+
/**
16+
* The new endpoint relative to the package name.
17+
* @example 'SimpleTreeView' in '@mui/x-tree-view/SimpleTreeView'
18+
*/
1119
newEndpoint?: string;
12-
skipRoot?: boolean;
20+
/**
21+
* The mapping of old identifier names to new identifier names.
22+
* @example { TreeView: 'SimpleTreeView', TreeItem: 'SimpleTreeItem' }
23+
*/
1324
importsMapping: Record<string, string>;
1425
}
1526

1627
interface RenameImportsParameters {
1728
j: JSCodeshift;
1829
root: Collection<any>;
30+
/**
31+
* The list of packages impacted by the renaming.
32+
* @example ['@mui/x-date-pickers', '@mui/x-date-pickers-pro']
33+
*/
1934
packageNames: string[];
35+
/**
36+
* The list of renaming configurations to apply.
37+
*/
2038
imports: ImportConfig[];
2139
}
2240

@@ -49,12 +67,13 @@ const getMatchingRootImport = (
4967
parameters: RenameImportsParameters,
5068
) => {
5169
return parameters.imports.find((importConfig) => {
52-
return (
53-
!importConfig.skipRoot && importConfig.importsMapping.hasOwnProperty(path.node.imported.name)
54-
);
70+
return importConfig.importsMapping.hasOwnProperty(path.node.imported.name);
5571
});
5672
};
5773

74+
/**
75+
* Rename import paths, identifiers and usages based on a renaming configuration.
76+
*/
5877
export function renameImports(parameters: RenameImportsParameters) {
5978
const { j, root } = parameters;
6079

packages/x-codemod/src/util/renameNestedProps.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,24 @@ import type { Collection, JSCodeshift } from 'jscodeshift';
22

33
interface RenamePropsArgs {
44
root: Collection<any>;
5+
/**
6+
* Names of the components to target
7+
* @example ["DataGrid", "DataGridPro"]
8+
*/
59
componentNames: string[];
10+
/**
11+
* Object that maps prop names to their nested properties renaming mappings.
12+
*
13+
* In the following example we rename properties inside `componentsProps` prop:
14+
* @example { componentsProps: { root: "slotRoot", input: "slotInput" } }
15+
*/
616
nestedProps: Record<string, Record<string, any>>;
717
j: JSCodeshift;
818
}
919

20+
/**
21+
* Allow to rename object properties inside props of specified components.
22+
*/
1023
export default function renameNestedProps({
1124
root,
1225
componentNames,

packages/x-codemod/src/util/renameProps.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@ import type { Collection, JSCodeshift } from 'jscodeshift';
22

33
interface RenamePropsArgs {
44
root: Collection<any>;
5+
/**
6+
* Names of the components to target
7+
* @example ["DataGrid", "DataGridPro"]
8+
*/
59
componentNames: string[];
10+
/**
11+
* Props renaming mapping
12+
* @example { disableSelectionOnClick: "disableRowSelectionOnClick" }
13+
*/
614
props: Record<string, any>;
715
j: JSCodeshift;
816
}

packages/x-codemod/src/v6.0.0/data-grid/rename-linkOperators-logicOperators/index.ts

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import renameCSSClasses from '../../../util/renameCSSClasses';
1+
import type { Collection, JSCodeshift } from 'jscodeshift';
22
import { JsCodeShiftAPI, JsCodeShiftFileInfo } from '../../../types';
33
import renameIdentifiers, { PreRequisiteUsage, matchImport } from '../../../util/renameIdentifiers';
44

@@ -50,10 +50,41 @@ const renamedLiterals = {
5050
filterPanelLinkOperator: 'filterPanelLogicOperator',
5151
};
5252

53-
const renamedClasses = {
54-
'MuiDataGrid-filterFormLinkOperatorInput': 'MuiDataGrid-filterFormLogicOperatorInput',
55-
'MuiDataGrid-withBorder': 'MuiDataGrid-withBorderColor',
56-
};
53+
interface RenameCSSClassesArgs {
54+
root: Collection<any>;
55+
j: JSCodeshift;
56+
}
57+
58+
/**
59+
* Renames CSS classes when they are literals based on the provided mapping.
60+
*/
61+
function renameCSSClasses({ root, j }: RenameCSSClassesArgs) {
62+
const renamedClasses = {
63+
'MuiDataGrid-filterFormLinkOperatorInput': 'MuiDataGrid-filterFormLogicOperatorInput',
64+
'MuiDataGrid-withBorder': 'MuiDataGrid-withBorderColor',
65+
};
66+
67+
root
68+
.find(j.Literal)
69+
.filter(
70+
(path) =>
71+
!!Object.keys(renamedClasses).find((className) => {
72+
const literal = path.node.value as any;
73+
return (
74+
typeof literal === 'string' &&
75+
literal.includes(className) &&
76+
!literal.includes(renamedClasses[className])
77+
);
78+
}),
79+
)
80+
.replaceWith((path) => {
81+
const literal = path.node.value as any;
82+
const targetClassKey = Object.keys(renamedClasses).find((className) =>
83+
literal.includes(className),
84+
)!;
85+
return j.literal(literal.replace(targetClassKey, renamedClasses[targetClassKey]));
86+
});
87+
}
5788

5889
export default function transform(file: JsCodeShiftFileInfo, api: JsCodeShiftAPI, options: any) {
5990
const j = api.jscodeshift;
@@ -98,7 +129,7 @@ export default function transform(file: JsCodeShiftFileInfo, api: JsCodeShiftAPI
98129
// Rename the classes
99130
// - 'MuiDataGrid-filterFormLinkOperatorInput'
100131
// + 'MuiDataGrid-filterFormLogicOperatorInput'
101-
renameCSSClasses({ j, root, renamedClasses });
132+
renameCSSClasses({ j, root });
102133
}
103134

104135
return root.toSource(printOptions);

0 commit comments

Comments
 (0)