@mui/x-codemod 6.0.0-beta.2 → 6.0.0-beta.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -416,7 +416,11 @@ The list includes these transformers
416
416
  - [`row-selection-props-rename`](#row-selection-props-rename)
417
417
  - [`rename-rowsPerPageOptions-prop`](#rename-rowsPerPageOptions-prop)
418
418
  - [`remove-disableExtendRowFullWidth-prop`](#remove-disableExtendRowFullWidth-prop)
419
+ - [`rename-linkOperators-logicOperators`](#rename-linkOperators-logicOperators)
420
+ - [`rename-filter-item-props`](#rename-filter-item-props)
419
421
  - [`rename-selectors-and-events`](#rename-selectors-and-events)
422
+ - [`remove-stabilized-experimentalFeatures`](#remove-stabilized-experimentalFeatures)
423
+ - [`replace-onCellFocusOut-prop`](#replace-onCellFocusOut-prop)
420
424
 
421
425
  #### `column-menu-components-rename`
422
426
 
@@ -497,13 +501,109 @@ Remove `disableExtendRowFullWidth` prop which is no longer supported.
497
501
  npx @mui/x-codemod v6.0.0/data-grid/remove-disableExtendRowFullWidth-prop <path>
498
502
  ```
499
503
 
504
+ #### `rename-linkOperators-logicOperators`
505
+
506
+ Rename `linkOperators` related props to `logicOperators` and rename classes.
507
+
508
+ ```diff
509
+ const [filterModel, setFilterModel] = React.useState<GridFilterModel>({
510
+ items: [],
511
+ - linkOperator: GridLinkOperator.Or,
512
+ - quickFilterLogicOperator: GridLinkOperator.Or,
513
+ + logicOperator: GridLogicOperator.Or,
514
+ + quickFilterLogicOperator: GridLogicOperator.Or,
515
+ });
516
+ - apiRef.current.setFilterLinkOperator('and')
517
+ - const localeText = apiRef.current.getLocaleText('filterPanelLinkOperator')
518
+ + apiRef.current.setFilterLogicOperator('and')
519
+ + const localeText = apiRef.current.getLocaleText('filterPanelLogicOperator')
520
+ <DataGrid
521
+ initialState={{
522
+ filter: {
523
+ filterModel: {
524
+ items: [],
525
+ - linkOperator: GridLinkOperator.Or,
526
+ - quickFilterLogicOperator: GridLinkOperator.Or,
527
+ + logicOperator: GridLogicOperator.Or,
528
+ + quickFilterLogicOperator: GridLogicOperator.Or,
529
+ },
530
+ },
531
+ }}
532
+ filterModel={filterModel}
533
+ componentsProps={{
534
+ filter: {
535
+ - linkOperators: [GridLinkOperator.And],
536
+ + logicOperators: [GridLogicOperator.And],
537
+ filterFormProps: {
538
+ - linkOperatorInputProps: {
539
+ + logicOperatorInputProps: {
540
+ variant: 'outlined',
541
+ size: 'small',
542
+ },
543
+ },
544
+ },
545
+ }}
546
+ sx={{
547
+ - '& .MuiDataGrid-filterFormLinkOperatorInput': { mr: 2 },
548
+ - '& .MuiDataGrid-withBorder': { borderColor: '#456' },
549
+ + '& .MuiDataGrid-filterFormLogicOperatorInput': { mr: 2 },
550
+ + '& .MuiDataGrid-withBorderColor': { borderColor: '#456' },
551
+ }}
552
+ />
553
+ ```
554
+
555
+ ```sh
556
+ npx @mui/x-codemod v6.0.0/data-grid/rename-linkOperators-logicOperators <path>
557
+ ```
558
+
559
+ #### `rename-filter-item-props`
560
+
561
+ Rename filter item props to the new values.
562
+
563
+ ```diff
564
+ <DataGrid
565
+ columns={columns}
566
+ rows={rows}
567
+ initialState={{
568
+ filter: {
569
+ filterModel: {
570
+ items: [
571
+ {
572
+ - columnField: 'column',
573
+ - operatorValue: 'contains',
574
+ + field: 'column',
575
+ + operator: 'contains',
576
+ value: 'a',
577
+ },
578
+ ],
579
+ },
580
+ },
581
+ }}
582
+ filterModel={{
583
+ items: [
584
+ {
585
+ - columnField: 'column',
586
+ - operatorValue: 'contains',
587
+ + field: 'column',
588
+ + operator: 'contains',
589
+ value: 'a',
590
+ },
591
+ ],
592
+ }}
593
+ />
594
+ ```
595
+
596
+ ```sh
597
+ npx @mui/x-codemod v6.0.0/data-grid/rename-filter-item-props <path>
598
+ ```
599
+
500
600
  #### `rename-selectors-and-events`
501
601
 
502
602
  Rename selectors and events.
503
603
 
504
604
  ```diff
505
605
  function App() {
506
- - useGridApiEventHandler('selectionChange', handleEvent);
606
+ - useGridApiEventHandler(apiRef, 'selectionChange', handleEvent);
507
607
  - apiRef.current.subscribeEvent('selectionChange', handleEvent);
508
608
  - const selection = useGridSelector(apiRef, gridSelectionStateSelector);
509
609
  - const sortedRowIds = useGridSelector(apiRef, gridVisibleSortedRowIdsSelector);
@@ -511,7 +611,12 @@ Rename selectors and events.
511
611
  - const rowCount = useGridSelector(apiRef, gridVisibleRowCountSelector);
512
612
  - const sortedTopLevelRowEntries = useGridSelector(apiRef, gridVisibleSortedTopLevelRowEntriesSelector);
513
613
  - const topLevelRowCount = useGridSelector(apiRef, gridVisibleTopLevelRowCountSelector);
514
- + useGridApiEventHandler('rowSelectionChange', handleEvent);
614
+ - const allGridColumnsFields = useGridSelector(apiRef, allGridColumnsFieldsSelector);
615
+ - const allGridColumns = useGridSelector(apiRef, allGridColumnsSelector);
616
+ - const visibleGridColumns = useGridSelector(apiRef, visibleGridColumnsSelector);
617
+ - const filterableGridColumns = useGridSelector(apiRef, filterableGridColumnsSelector);
618
+ - const getGridNumericColumn = useGridSelector(apiRef, getGridNumericColumnOperators);
619
+ + useGridApiEventHandler(apiRef, 'rowSelectionChange', handleEvent);
515
620
  + apiRef.current.subscribeEvent('rowSelectionChange', handleEvent);
516
621
  + const selection = useGridSelector(apiRef, gridRowSelectionStateSelector);
517
622
  + const sortedRowIds = useGridSelector(apiRef, gridExpandedSortedRowIdsSelector);
@@ -519,6 +624,11 @@ Rename selectors and events.
519
624
  + const rowCount = useGridSelector(apiRef, gridExpandedRowCountSelector);
520
625
  + const sortedTopLevelRowEntries = useGridSelector(apiRef, gridFilteredSortedTopLevelRowEntriesSelector);
521
626
  + const topLevelRowCount = useGridSelector(apiRef, gridFilteredTopLevelRowCountSelector);
627
+ + const allGridColumnsFields = useGridSelector(apiRef, gridColumnFieldsSelector);
628
+ + const allGridColumns = useGridSelector(apiRef, gridColumnDefinitionsSelector);
629
+ + const visibleGridColumns = useGridSelector(apiRef, gridVisibleColumnDefinitionsSelector);
630
+ + const filterableGridColumns = useGridSelector(apiRef, gridFilterableColumnDefinitionsSelector);
631
+ + const getGridNumericColumn = useGridSelector(apiRef, getGridNumericOperators);
522
632
  }
523
633
  ```
524
634
 
@@ -526,4 +636,48 @@ Rename selectors and events.
526
636
  npx @mui/x-codemod v6.0.0/data-grid/rename-selectors-and-events <path>
527
637
  ```
528
638
 
639
+ #### `remove-stabilized-experimentalFeatures`
640
+
641
+ Remove feature flags for stabilized `experimentalFeatures`.
642
+
643
+ ```diff
644
+ <DataGrid
645
+ - experimentalFeatures={{
646
+ - newEditingApi: true,
647
+ - }}
648
+ />
649
+ ```
650
+
651
+ ```diff
652
+ <DataGrid
653
+ experimentalFeatures={{
654
+ - newEditingApi: true,
655
+ columnGrouping: true,
656
+ }}
657
+ />
658
+ ```
659
+
660
+ ```sh
661
+ npx @mui/x-codemod v6.0.0/data-grid/remove-stabilized-experimentalFeatures <path>
662
+ ```
663
+
664
+ #### `replace-onCellFocusOut-prop`
665
+
666
+ Replace `onCellFocusOut` prop with `componentsProps.cell.onBlur`.
667
+
668
+ ```diff
669
+ <DataGrid
670
+ - onCellFocusOut={handleBlur}
671
+ + componentsProps={{
672
+ + cell: {
673
+ + onBlur: handleBlur,
674
+ + },
675
+ + }}
676
+ />
677
+ ```
678
+
679
+ ```sh
680
+ npx @mui/x-codemod v6.0.0/data-grid/replace-onCellFocusOut-prop <path>
681
+ ```
682
+
529
683
  You can find more details about Data Grid breaking change in [the migration guide](https://next.mui.com/x/migration/migration-data-grid-v5/).
package/codemod.js CHANGED
@@ -34,6 +34,17 @@ async function runTransform(transform, files, flags, codemodFlags) {
34
34
 
35
35
  // eslint-disable-next-line no-console -- debug information
36
36
  console.log(`Executing command: jscodeshift ${args.join(' ')}`);
37
+ console.warn(`
38
+ ====================================
39
+ IMPORTANT NOTICE ABOUT CODEMOD USAGE
40
+ ====================================
41
+ Not all use cases are covered by codemods. In some scenarios, like props spreading, cross-file dependencies and etc., the changes are not properly identified and therefore must be handled manually.
42
+
43
+ For example, if a codemod tries to rename a prop, but this prop is hidden with the spread operator, it won't be transformed as expected.
44
+ <DatePicker {...pickerProps} />
45
+
46
+ After running the codemods, make sure to test your application and that you don't have any console errors.
47
+ `);
37
48
  const jscodeshiftProcess = _child_process.default.spawnSync('node', args, {
38
49
  stdio: 'inherit'
39
50
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mui/x-codemod",
3
- "version": "6.0.0-beta.2",
3
+ "version": "6.0.0-beta.4",
4
4
  "bin": "./codemod.js",
5
5
  "private": false,
6
6
  "author": "MUI Team",
@@ -32,8 +32,8 @@
32
32
  },
33
33
  "dependencies": {
34
34
  "@babel/core": "^7.20.12",
35
- "@babel/runtime": "^7.20.7",
36
- "@babel/traverse": "^7.20.12",
35
+ "@babel/runtime": "^7.20.13",
36
+ "@babel/traverse": "^7.20.13",
37
37
  "jscodeshift": "0.13.1",
38
38
  "jscodeshift-add-imports": "^1.0.10",
39
39
  "yargs": "^17.6.2"
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = removeObjectProperty;
7
+ function removeObjectProperty({
8
+ root,
9
+ propName,
10
+ componentsNames,
11
+ propKey,
12
+ j
13
+ }) {
14
+ root.find(j.JSXElement).filter(path => {
15
+ return componentsNames.includes(path.value.openingElement.name.name);
16
+ }).forEach(element => {
17
+ const targetAttribute = element.value.openingElement.attributes.find(attribute => attribute.name.name === propName);
18
+ if (!targetAttribute) {
19
+ return;
20
+ }
21
+ const definedKeys = [];
22
+ const objectProperties = j(targetAttribute).find(j.Property);
23
+ objectProperties.forEach(path => {
24
+ const objectKey = path.value.key.name;
25
+ definedKeys.push(objectKey);
26
+ });
27
+ if (definedKeys.length === 1 && definedKeys[0] === propKey) {
28
+ // only that property is defined, remove the whole prop
29
+ j(element).find(j.JSXAttribute).filter(a => a.value.name.name === propName).forEach(path => {
30
+ j(path).remove();
31
+ });
32
+ } else {
33
+ objectProperties.forEach(path => {
34
+ if (path.value.key.name === propKey) {
35
+ j(path).remove();
36
+ }
37
+ });
38
+ }
39
+ });
40
+ }
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = renameCSSClasses;
7
+ function renameCSSClasses({
8
+ root,
9
+ j,
10
+ renamedClasses
11
+ }) {
12
+ root.find(j.Literal).filter(path => !!Object.keys(renamedClasses).find(className => {
13
+ const literal = path.node.value;
14
+ return typeof literal === 'string' && literal.includes(className) && !literal.includes(renamedClasses[className]);
15
+ })).replaceWith(path => {
16
+ const literal = path.node.value;
17
+ const targetClassKey = Object.keys(renamedClasses).find(className => literal.includes(className));
18
+ return j.literal(literal.replace(targetClassKey, renamedClasses[targetClassKey]));
19
+ });
20
+ }
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.checkPreRequisitesSatisfied = void 0;
7
+ exports.default = renameIdentifiers;
8
+ exports.matchImport = void 0;
9
+ /* eslint-disable no-underscore-dangle */
10
+
11
+ const matchImport = (path, regex) => (path.node.source.value.toString() || '').match(regex);
12
+ exports.matchImport = matchImport;
13
+ const findDeepASTPath = (j, currentPath, currentIdentifiers) => {
14
+ if (!currentIdentifiers.length) {
15
+ return true;
16
+ }
17
+ const target = j(currentPath).find(j.Identifier).filter(path => path.value.name === currentIdentifiers[0]);
18
+ if (target.__paths.length === 0) {
19
+ return false;
20
+ }
21
+ return findDeepASTPath(j, currentPath, currentIdentifiers.slice(1));
22
+ };
23
+
24
+ /**
25
+ * Checks if all pre-requisites are satisfied for the given path `root`.
26
+ * For it to be satisfied, at least one condition of all the pre-requisites must be fulfilled.
27
+ *
28
+ * @param {*} j
29
+ * @param {*} root
30
+ * @param {PreRequisiteUsage} preReqs
31
+ * @returns {boolean}
32
+ */
33
+ const checkPreRequisitesSatisfied = (j, root, preReqs) => {
34
+ if (preReqs.packageRegex || preReqs.components) {
35
+ // check if any of the components is imported from a package which satisfies `preReqs.packageRegex`
36
+ const imports = root.find(j.ImportDeclaration);
37
+ const matchingImports = preReqs.packageRegex ? imports.filter(path => !!matchImport(path, preReqs.packageRegex)) : imports;
38
+ if (!matchingImports.__paths.length) {
39
+ return false;
40
+ }
41
+ // check if any of the `preReqs.components` is imported from the matching imports
42
+ const satisfyingImports = preReqs.components?.length ? matchingImports.find(j.Identifier).filter(path => preReqs.components.includes(path.node.name)) : matchingImports;
43
+ if (!satisfyingImports.__paths.length) {
44
+ return false;
45
+ }
46
+ }
47
+ if (!preReqs.possiblePaths?.length) {
48
+ return true;
49
+ }
50
+ const allComponents = root.find(j.JSXElement);
51
+ const matchingComponents = preReqs.components?.length ? allComponents.filter(path => preReqs.components.includes(path.node.openingElement.name.name)) : allComponents;
52
+ const matchingAttributes = matchingComponents.find(j.JSXAttribute)
53
+ // filter by props first
54
+ .filter(attribute => preReqs.possiblePaths.map(path => path.split('.')[0]).includes(attribute.node.name.name))
55
+ // filter by nested levels
56
+ .filter(attribute => findDeepASTPath(j, attribute, preReqs.possiblePaths.find(path => path.startsWith(attribute.node.name.name)).split('.').slice(1)));
57
+ return matchingAttributes.__paths.length > 0;
58
+ };
59
+
60
+ /**
61
+ * Renames identifiers based on the `identifiers` object passed plus an optional `preRequisiteUsages` object
62
+ * to control renaming based on certain conditions.
63
+ *
64
+ * @export renameIdentifiers
65
+ * @param {RenameIdentifiersArgs} {
66
+ * root, // jscodeshift root
67
+ * identifiers, // object of identifiers to rename
68
+ * preRequisiteUsages, // an optional list of pre-requisite to control renaming based on certain conditions
69
+ * // if any of the pre-requisites is satisfied then the identifier will be renamed
70
+ * // for more details, see the type definition of `PreRequisiteUsage`
71
+ * j, // jscodeshift
72
+ * }
73
+ */
74
+ exports.checkPreRequisitesSatisfied = checkPreRequisitesSatisfied;
75
+ function renameIdentifiers({
76
+ root,
77
+ identifiers,
78
+ preRequisiteUsages,
79
+ j
80
+ }) {
81
+ root.find(j.Identifier).filter(path => !!identifiers[path.node.name]).replaceWith(path => {
82
+ if (!preRequisiteUsages || !preRequisiteUsages[path.node.name]) {
83
+ return j.importSpecifier(j.identifier(identifiers[path.node.name]));
84
+ }
85
+ const shouldReplace = checkPreRequisitesSatisfied(j, root, preRequisiteUsages[path.node.name]);
86
+ if (shouldReplace) {
87
+ return j.importSpecifier(j.identifier(identifiers[path.node.name]));
88
+ }
89
+ return j.importSpecifier(j.identifier(path.node.name));
90
+ });
91
+ }
@@ -9,12 +9,20 @@ var _columnMenuComponentsRename = _interopRequireDefault(require("../column-menu
9
9
  var _rowSelectionPropsRename = _interopRequireDefault(require("../row-selection-props-rename"));
10
10
  var _renameRowsPerPageOptionsProp = _interopRequireDefault(require("../rename-rowsPerPageOptions-prop"));
11
11
  var _removeDisableExtendRowFullWidthProp = _interopRequireDefault(require("../remove-disableExtendRowFullWidth-prop"));
12
+ var _renameLinkOperatorsLogicOperators = _interopRequireDefault(require("../rename-linkOperators-logicOperators"));
13
+ var _renameFilterItemProps = _interopRequireDefault(require("../rename-filter-item-props"));
12
14
  var _renameSelectorsAndEvents = _interopRequireDefault(require("../rename-selectors-and-events"));
15
+ var _removeStabilizedExperimentalFeatures = _interopRequireDefault(require("../remove-stabilized-experimentalFeatures"));
16
+ var _replaceOnCellFocusOutProp = _interopRequireDefault(require("../replace-onCellFocusOut-prop"));
13
17
  function transformer(file, api, options) {
14
18
  file.source = (0, _columnMenuComponentsRename.default)(file, api, options);
15
19
  file.source = (0, _rowSelectionPropsRename.default)(file, api, options);
16
20
  file.source = (0, _renameRowsPerPageOptionsProp.default)(file, api, options);
17
21
  file.source = (0, _removeDisableExtendRowFullWidthProp.default)(file, api, options);
22
+ file.source = (0, _renameLinkOperatorsLogicOperators.default)(file, api, options);
23
+ file.source = (0, _renameFilterItemProps.default)(file, api, options);
18
24
  file.source = (0, _renameSelectorsAndEvents.default)(file, api, options);
25
+ file.source = (0, _removeStabilizedExperimentalFeatures.default)(file, api, options);
26
+ file.source = (0, _replaceOnCellFocusOutProp.default)(file, api, options);
19
27
  return file.source;
20
28
  }
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.default = transformer;
8
+ var _removeObjectProperty = _interopRequireDefault(require("../../../util/removeObjectProperty"));
9
+ const componentsNames = ['DataGrid', 'DataGridPro', 'DataGridPremium'];
10
+ const propName = 'experimentalFeatures';
11
+ const propKey = 'newEditingApi';
12
+ function transformer(file, api, options) {
13
+ const j = api.jscodeshift;
14
+ const root = j(file.source);
15
+ const printOptions = options.printOptions || {
16
+ quote: 'single',
17
+ trailingComma: true
18
+ };
19
+ (0, _removeObjectProperty.default)({
20
+ root,
21
+ j,
22
+ propName,
23
+ componentsNames,
24
+ propKey
25
+ });
26
+ return root.toSource(printOptions);
27
+ }
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.default = transformer;
8
+ var _renameIdentifiers = _interopRequireDefault(require("../../../util/renameIdentifiers"));
9
+ const renamedIdentifiers = {
10
+ columnField: 'field',
11
+ operatorValue: 'operator'
12
+ };
13
+ const PACKAGE_REGEXP = /@mui\/x-data-grid(-pro|-premium)?/;
14
+ const GridComponents = ['DataGrid', 'DataGridPro', 'DataGridPremium'];
15
+ const preRequisiteUsages = {
16
+ columnField: {
17
+ possiblePaths: ['initialState', 'filterModel', 'onFilterModelChange'],
18
+ components: GridComponents,
19
+ packageRegex: PACKAGE_REGEXP
20
+ },
21
+ operatorValue: {
22
+ possiblePaths: ['initialState', 'filterModel', 'onFilterModelChange'],
23
+ components: GridComponents,
24
+ packageRegex: PACKAGE_REGEXP
25
+ }
26
+ };
27
+ function transformer(file, api, options) {
28
+ const j = api.jscodeshift;
29
+ const root = j(file.source);
30
+ const printOptions = options.printOptions || {
31
+ quote: 'single',
32
+ trailingComma: true
33
+ };
34
+
35
+ // <DataGrid(Pro|Premium)
36
+ // filterModel={{
37
+ // items: [{
38
+ // - columnField: 'name',
39
+ // + field: 'name',
40
+ // - operatorValue: 'contains',
41
+ // + operator: 'contains',
42
+ // value: 'a'
43
+ // }]
44
+ // }}
45
+ // />
46
+ (0, _renameIdentifiers.default)({
47
+ root,
48
+ j,
49
+ preRequisiteUsages,
50
+ identifiers: renamedIdentifiers
51
+ });
52
+ return root.toSource(printOptions);
53
+ }
@@ -0,0 +1,97 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.default = transform;
8
+ var _renameCSSClasses = _interopRequireDefault(require("../../../util/renameCSSClasses"));
9
+ var _renameIdentifiers = _interopRequireWildcard(require("../../../util/renameIdentifiers"));
10
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
11
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
12
+ const renamedIdentifiers = {
13
+ GridLinkOperator: 'GridLogicOperator',
14
+ linkOperatorInputProps: 'logicOperatorInputProps',
15
+ linkOperator: 'logicOperator',
16
+ linkOperators: 'logicOperators',
17
+ setFilterLinkOperator: 'setFilterLogicOperator'
18
+ };
19
+ const PACKAGE_REGEXP = /@mui\/x-data-grid(-pro|-premium)?/;
20
+ const GridComponents = ['DataGrid', 'DataGridPro', 'DataGridPremium'];
21
+ const preRequisiteUsages = {
22
+ GridLinkOperator: {
23
+ possiblePaths: ['initialState.filter', 'filterModel', 'componentsProps.filter'],
24
+ components: GridComponents,
25
+ packageRegex: PACKAGE_REGEXP
26
+ },
27
+ linkOperatorInputProps: {
28
+ possiblePaths: ['componentsProps.filter'],
29
+ components: GridComponents,
30
+ packageRegex: PACKAGE_REGEXP
31
+ },
32
+ linkOperator: {
33
+ possiblePaths: ['initialState.filter', 'filterModel', 'componentsProps.filter'],
34
+ components: GridComponents,
35
+ packageRegex: PACKAGE_REGEXP
36
+ },
37
+ linkOperators: {
38
+ possiblePaths: ['componentsProps.filter'],
39
+ components: GridComponents,
40
+ packageRegex: PACKAGE_REGEXP
41
+ }
42
+ };
43
+ const renamedLiterals = {
44
+ filterPanelLinkOperator: 'filterPanelLogicOperator'
45
+ };
46
+ const renamedClasses = {
47
+ 'MuiDataGrid-filterFormLinkOperatorInput': 'MuiDataGrid-filterFormLogicOperatorInput',
48
+ 'MuiDataGrid-withBorder': 'MuiDataGrid-withBorderColor'
49
+ };
50
+ function transform(file, api, options) {
51
+ const j = api.jscodeshift;
52
+ const root = j(file.source);
53
+ const printOptions = options.printOptions || {
54
+ quote: 'single',
55
+ trailingComma: true
56
+ };
57
+ const matchingImports = root.find(j.ImportDeclaration).filter(path => !!(0, _renameIdentifiers.matchImport)(path, PACKAGE_REGEXP));
58
+ if (matchingImports.length > 0) {
59
+ // Rename the identifiers
60
+ // <DataGrid
61
+ // componentsProps={{
62
+ // filter: {
63
+ // - linkOperators: [GridLinkOperator.And],
64
+ // + logicOperators: [GridLogicOperator.And],
65
+ // filterFormProps: {
66
+ // - linkOperatorInputProps: {
67
+ // + logicOperatorInputProps: {
68
+ // variant: 'outlined',
69
+ // size: 'small',
70
+ // },
71
+ // },
72
+ // },
73
+ // }}
74
+ // />
75
+ (0, _renameIdentifiers.default)({
76
+ j,
77
+ root,
78
+ identifiers: renamedIdentifiers,
79
+ preRequisiteUsages
80
+ });
81
+
82
+ // Rename the literals
83
+ // - apiRef.current.getLocaleText('filterPanelLinkOperator')
84
+ // + apiRef.current.getLocaleText('filterPanelLogicOperator')
85
+ root.find(j.Literal).filter(path => !!renamedLiterals[path.node.value]).replaceWith(path => j.literal(renamedLiterals[path.node.value]));
86
+
87
+ // Rename the classes
88
+ // - 'MuiDataGrid-filterFormLinkOperatorInput'
89
+ // + 'MuiDataGrid-filterFormLogicOperatorInput'
90
+ (0, _renameCSSClasses.default)({
91
+ j,
92
+ root,
93
+ renamedClasses
94
+ });
95
+ }
96
+ return root.toSource(printOptions);
97
+ }
@@ -4,18 +4,28 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = transformer;
7
+ var _renameIdentifiers = require("../../../util/renameIdentifiers");
7
8
  const renamedSelectors = {
8
9
  gridSelectionStateSelector: 'gridRowSelectionStateSelector',
9
10
  gridVisibleSortedRowIdsSelector: 'gridExpandedSortedRowIdsSelector',
10
11
  gridVisibleSortedRowEntriesSelector: 'gridExpandedSortedRowEntriesSelector',
11
12
  gridVisibleRowCountSelector: 'gridExpandedRowCountSelector',
12
13
  gridVisibleSortedTopLevelRowEntriesSelector: 'gridFilteredSortedTopLevelRowEntriesSelector',
13
- gridVisibleTopLevelRowCountSelector: 'gridFilteredTopLevelRowCountSelector'
14
+ gridVisibleTopLevelRowCountSelector: 'gridFilteredTopLevelRowCountSelector',
15
+ allGridColumnsFieldsSelector: 'gridColumnFieldsSelector',
16
+ allGridColumnsSelector: 'gridColumnDefinitionsSelector',
17
+ visibleGridColumnsSelector: 'gridVisibleColumnDefinitionsSelector',
18
+ filterableGridColumnsSelector: 'gridFilterableColumnDefinitionsSelector',
19
+ getGridNumericColumnOperators: 'getGridNumericOperators'
14
20
  };
15
21
  const renamedEvents = {
16
22
  selectionChange: 'rowSelectionChange',
17
23
  rowsScroll: 'scrollPositionChange'
18
24
  };
25
+ const preRequisites = {
26
+ components: ['DataGrid', 'DataGridPro', 'DataGridPremium'],
27
+ packageRegex: /@mui\/x-data-grid(-pro|-premium)?/
28
+ };
19
29
  function transformer(file, api, options) {
20
30
  const j = api.jscodeshift;
21
31
  const root = j(file.source);
@@ -23,17 +33,19 @@ function transformer(file, api, options) {
23
33
  quote: 'single',
24
34
  trailingComma: true
25
35
  };
36
+ const isGridUsed = (0, _renameIdentifiers.checkPreRequisitesSatisfied)(j, root, preRequisites);
37
+ if (isGridUsed) {
38
+ // Rename the import and usage of renamed selectors
39
+ // - import { gridSelectionStateSelector } from '@mui/x-data-grid'
40
+ // + import { gridRowSelectionStateSelector } from '@mui/x-data-grid'
41
+ root.find(j.Identifier).filter(path => !!renamedSelectors[path.node.name]).replaceWith(path => j.identifier(renamedSelectors[path.node.name]));
26
42
 
27
- // Rename the import and usage of renamed selectors
28
- // - import { gridSelectionStateSelector } from '@mui/x-data-grid'
29
- // + import { gridRowSelectionStateSelector } from '@mui/x-data-grid'
30
- root.find(j.Identifier).filter(path => !!renamedSelectors[path.node.name]).replaceWith(path => j.identifier(renamedSelectors[path.node.name]));
31
-
32
- // Rename the usage of renamed event literals
33
- // - useGridApiEventHandler('selectionChange', handleEvent);
34
- // - apiRef.current.subscribeEvent('selectionChange', handleEvent);
35
- // + useGridApiEventHandler('rowSelectionChange', handleEvent);
36
- // + apiRef.current.subscribeEvent('rowSelectionChange', handleEvent);
37
- root.find(j.CallExpression).find(j.Literal).filter(path => !!renamedEvents[path.node.value]).replaceWith(path => j.literal(renamedEvents[path.node.value]));
43
+ // Rename the usage of renamed event literals
44
+ // - useGridApiEventHandler(apiRef, 'selectionChange', handleEvent);
45
+ // - apiRef.current.subscribeEvent('selectionChange', handleEvent);
46
+ // + useGridApiEventHandler(apiRef, 'rowSelectionChange', handleEvent);
47
+ // + apiRef.current.subscribeEvent('rowSelectionChange', handleEvent);
48
+ root.find(j.CallExpression).find(j.Literal).filter(path => !!renamedEvents[path.node.value]).replaceWith(path => j.literal(renamedEvents[path.node.value]));
49
+ }
38
50
  return root.toSource(printOptions);
39
51
  }
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.default = transformer;
8
+ var _removeProps = _interopRequireDefault(require("../../../util/removeProps"));
9
+ var _addComponentsSlots = require("../../../util/addComponentsSlots");
10
+ const componentNames = ['DataGrid', 'DataGridPro', 'DataGridPremium'];
11
+ const propsToRename = {
12
+ onCellFocusOut: {
13
+ prop: 'componentsProps',
14
+ path: 'cell.onBlur'
15
+ }
16
+ };
17
+ function transformer(file, api, options) {
18
+ const j = api.jscodeshift;
19
+ const root = j(file.source);
20
+ const printOptions = options.printOptions || {
21
+ quote: 'single',
22
+ trailingComma: true
23
+ };
24
+ root.find(j.JSXElement).filter(path => {
25
+ return componentNames.includes(path.value.openingElement.name.name);
26
+ }).forEach(path => {
27
+ const attributesToTransform = j(path).find(j.JSXAttribute).filter(attribute => Object.keys(propsToRename).includes(attribute.value.name.name));
28
+ attributesToTransform.forEach(attribute => {
29
+ const attributeName = attribute.value.name.name;
30
+ const value = attribute.value.value?.type === 'JSXExpressionContainer' ? attribute.value.value.expression : attribute.value.value;
31
+ (0, _addComponentsSlots.transformNestedProp)(path, propsToRename[attributeName].prop, propsToRename[attributeName].path, value, j);
32
+ });
33
+ });
34
+ return (0, _removeProps.default)({
35
+ root,
36
+ j,
37
+ props: Object.keys(propsToRename),
38
+ componentNames
39
+ }).toSource(printOptions);
40
+ }
@@ -1,29 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = renameClassKey;
7
- function renameClassKey({
8
- root,
9
- componentName,
10
- classes,
11
- printOptions
12
- }) {
13
- const source = root.findJSXElements(componentName).forEach(path => {
14
- path.node.openingElement.attributes?.forEach(node => {
15
- if (node.type === 'JSXAttribute' && node.name.name === 'classes') {
16
- // @ts-expect-error
17
- node.value?.expression?.properties?.forEach(subNode => {
18
- if (Object.keys(classes).includes(subNode.key.name)) {
19
- subNode.key.name = classes[subNode.key.name];
20
- }
21
- });
22
- }
23
- });
24
- }).toSource(printOptions);
25
- return Object.entries(classes).reduce((result, [currentKey, newKey]) => {
26
- const regex = new RegExp(`.Mui${componentName}-${currentKey}`, 'gm');
27
- return result.replace(regex, `.Mui${componentName}-${newKey}`);
28
- }, source);
29
- }