Skip to content

Commit

Permalink
[codemod] Add codemod to use referenceDate instead of `defaultCalen…
Browse files Browse the repository at this point in the history
…darMonth` (mui#11139)
  • Loading branch information
flaviendelangle authored Nov 22, 2023
1 parent 037d1ef commit a022c67
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ You can read more about the deprecation of this prop in [v6 migration guide](/x/
/>
```

### Replace `defaultCalendarMonth` with `referenceDate`
### Replace `defaultCalendarMonth` with `referenceDate`

The deprecated `defaultCalendarMonth` prop has been removed in favor of the more flexible `referenceDate` prop.

Expand Down
17 changes: 13 additions & 4 deletions packages/x-codemod/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,26 @@ The corresponding sub-sections are listed below

### Pickers codemods

#### `preset-safe` for pickers v6.0.0
#### `preset-safe` for pickers v7.0.0

The `preset-safe` codemods for pickers.

```bash
npx @mui/x-codemod v6.0.0/pickers/preset-safe <path|folder>
npx @mui/x-codemod v7.0.0/pickers/preset-safe <path|folder>
```

The list includes these transformers

NO CODEMOD ADDED YET
- [`rename-default-calendar-month-to-reference-date`](#rename-default-calendar-month-to-reference-date)

#### `rename-default-calendar-month-to-reference-date`

Replace the `defaultCalendarMonth` prop with the `referenceDate` prop.

```diff
- <DateCalendar defaultCalendarMonth={dayjs('2022-04-01')};
+ <DateCalendar referenceDate{dayjs('2022-04-01')} />
```

### Data grid codemods

Expand All @@ -95,7 +104,7 @@ NO CODEMOD ADDED YET
The `preset-safe` codemods for data grid.

```bash
npx @mui/x-codemod v6.0.0/data-grid/preset-safe <path|folder>
npx @mui/x-codemod v7.0.0/data-grid/preset-safe <path|folder>
```

The list includes these transformers
Expand Down
2 changes: 2 additions & 0 deletions packages/x-codemod/src/v7.0.0/pickers/preset-safe/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import transformRenameComponentsToSlots from '../../../v6.0.0/pickers/rename-components-to-slots';
import transformRenameDefaultCalendarMonthToReferenceDate from '../rename-default-calendar-month-to-reference-date';

import { JsCodeShiftAPI, JsCodeShiftFileInfo } from '../../../types';

export default function transformer(file: JsCodeShiftFileInfo, api: JsCodeShiftAPI, options: any) {
file.source = transformRenameComponentsToSlots(file, api, options);
file.source = transformRenameDefaultCalendarMonthToReferenceDate(file, api, options);

return file.source;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<>
<DateCalendar defaultCalendarMonth={dayjs('2022-04-01')} />
<DatePicker defaultCalendarMonth={dayjs('2022-04-01')} />
<DateRangeCalendar defaultCalendarMonth={dayjs('2022-04-01')} />
<DateRangePicker defaultCalendarMonth={dayjs('2022-04-01')} />
<DateTimePicker defaultCalendarMonth={dayjs('2022-04-01')} />
<DesktopDatePicker defaultCalendarMonth={dayjs('2022-04-01')} />
<DesktopDateRangePicker defaultCalendarMonth={dayjs('2022-04-01')} />
<DesktopDateTimePicker defaultCalendarMonth={dayjs('2022-04-01')} />
<MobileDatePicker defaultCalendarMonth={dayjs('2022-04-01')} />
<MobileDateRangePicker defaultCalendarMonth={dayjs('2022-04-01')} />
<MobileDateTimePicker defaultCalendarMonth={dayjs('2022-04-01')} />
<StaticDatePicker defaultCalendarMonth={dayjs('2022-04-01')} />
<StaticDateRangePicker defaultCalendarMonth={dayjs('2022-04-01')} />
<StaticDateTimePicker defaultCalendarMonth={dayjs('2022-04-01')} />
</>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<>
<DateCalendar referenceDate={dayjs('2022-04-01')} />
<DatePicker referenceDate={dayjs('2022-04-01')} />
<DateRangeCalendar referenceDate={dayjs('2022-04-01')} />
<DateRangePicker referenceDate={dayjs('2022-04-01')} />
<DateTimePicker referenceDate={dayjs('2022-04-01')} />
<DesktopDatePicker referenceDate={dayjs('2022-04-01')} />
<DesktopDateRangePicker referenceDate={dayjs('2022-04-01')} />
<DesktopDateTimePicker referenceDate={dayjs('2022-04-01')} />
<MobileDatePicker referenceDate={dayjs('2022-04-01')} />
<MobileDateRangePicker referenceDate={dayjs('2022-04-01')} />
<MobileDateTimePicker referenceDate={dayjs('2022-04-01')} />
<StaticDatePicker referenceDate={dayjs('2022-04-01')} />
<StaticDateRangePicker referenceDate={dayjs('2022-04-01')} />
<StaticDateTimePicker referenceDate={dayjs('2022-04-01')} />
</>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import renameProps from '../../../util/renameProps';
import type { JsCodeShiftAPI, JsCodeShiftFileInfo } from '../../../types';

export default function transformer(file: JsCodeShiftFileInfo, api: JsCodeShiftAPI, options: any) {
const j = api.jscodeshift;
const root = j(file.source);

const printOptions = options.printOptions;

return renameProps({
root,
componentNames: [
'DateCalendar',
'DatePicker',
'DateRangeCalendar',
'DateRangePicker',
'DateTimePicker',
'DesktopDatePicker',
'DesktopDateRangePicker',
'DesktopDateTimePicker',
'MobileDatePicker',
'MobileDateRangePicker',
'MobileDateTimePicker',
'StaticDatePicker',
'StaticDateRangePicker',
'StaticDateTimePicker',
],
props: { defaultCalendarMonth: 'referenceDate' },
j,
}).toSource(printOptions);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import path from 'path';
import { expect } from 'chai';
import jscodeshift from 'jscodeshift';
import transform from '.';
import readFile from '../../../util/readFile';

function read(fileName) {
return readFile(path.join(__dirname, fileName));
}

describe('v7.0.0/pickers', () => {
describe('rename-default-calendar-month-to-reference-date', () => {
it('transforms props as needed', () => {
const actual = transform({ source: read('./actual.spec.js') }, { jscodeshift }, {});

const expected = read('./expected.spec.js');
expect(actual).to.equal(expected, 'The transformed version should be correct');
});

it('should be idempotent', () => {
const actual = transform({ source: read('./expected.spec.js') }, { jscodeshift }, {});

const expected = read('./expected.spec.js');
expect(actual).to.equal(expected, 'The transformed version should be correct');
});
});
});

0 comments on commit a022c67

Please sign in to comment.