Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pickers] Rename AdapterDateFns into AdapterDateFnsV2 and AdapterDateFnsV3 into AdapterDateFns #16082

Merged
merged 19 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,25 +100,23 @@ module.exports = function getBabelConfig(api) {

if (process.env.NODE_ENV === 'test') {
plugins.push(['@babel/plugin-transform-export-namespace-from']);
// We replace `date-fns` imports with an aliased `date-fns@v4` version installed as `date-fns-v4` for tests.
// The plugin is patched to only run on `AdapterDateFnsV3.ts`.
// TODO: remove when we upgrade to date-fns v4 by default.
// We replace `date-fns` imports with an aliased `date-fns@v2` version installed as `date-fns-v2` for tests.
plugins.push([
'babel-plugin-replace-imports',
{
test: /date-fns/i,
replacer: 'date-fns-v4',
replacer: 'date-fns-v2',
// This option is provided by the `patches/[email protected]` patch
filenameIncludes: 'src/AdapterDateFnsV3/',
filenameIncludes: 'src/AdapterDateFnsV2/',
},
]);
plugins.push([
'babel-plugin-replace-imports',
{
test: /date-fns-jalali/i,
replacer: 'date-fns-jalali-v4',
replacer: 'date-fns-jalali-v2',
// This option is provided by the `patches/[email protected]` patch
filenameIncludes: 'src/AdapterDateFnsJalaliV3/',
filenameIncludes: 'src/AdapterDateFnsJalaliV2/',
},
'replace-date-fns-jalali-imports',
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { DatePicker } from '@mui/x-date-pickers/DatePicker';
import { DateTimePicker } from '@mui/x-date-pickers/DateTimePicker';
import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns';
import { enUS as locale } from 'date-fns/locale';
import format from 'date-fns/format';
import { format } from 'date-fns/format';
import { unstable_useEnhancedEffect as useEnhancedEffect } from '@mui/utils';
/**
* `date` column
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { DatePicker } from '@mui/x-date-pickers/DatePicker';
import { DateTimePicker } from '@mui/x-date-pickers/DateTimePicker';
import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns';
import { enUS as locale } from 'date-fns/locale';
import format from 'date-fns/format';
import { format } from 'date-fns/format';
import { unstable_useEnhancedEffect as useEnhancedEffect } from '@mui/utils';
/**
* `date` column
Expand Down
14 changes: 7 additions & 7 deletions docs/data/date-pickers/adapters-locale/adapters-locale.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,18 @@ We support `date-fns` package v2.x, v3.x, and v4.x major versions.

A single adapter cannot work for all `date-fns` versions, because the way functions are exported has been changed in v3.x.

To use `date-fns` v3.x or v4.x, you need to import the adapter from `@mui/x-date-pickers/AdapterDateFnsV3` instead of `@mui/x-date-pickers/AdapterDateFns`.
To use `date-fns` v2.x, you need to import the adapter from `@mui/x-date-pickers/AdapterDateFnsV2` instead of `@mui/x-date-pickers/AdapterDateFns`.
:::

```tsx
// with date-fns v2.x
import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns';
// with date-fns v3.x or v4.x
import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFnsV3';
import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns';
// with date-fns v2.x
import de from 'date-fns/locale/de';
import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFnsV2';
// with date-fns v3.x or v4.x
import { de } from 'date-fns/locale/de';
// with date-fns v2.x
import de from 'date-fns/locale/de';

<LocalizationProvider dateAdapter={AdapterDateFns} adapterLocale={de}>
{children}
Expand Down Expand Up @@ -303,10 +303,10 @@ For `date-fns`, override the `options.weekStartsOn` of the used locale:

```ts
import { Locale } from 'date-fns';
// with date-fns v3.x or v4.x
import { enUS } from 'date-fns/locale/en-US';
// with date-fns v2.x
import enUS from 'date-fns/locale/en-US';
// with date-fns v3.x
import { enUS } from 'date-fns/locale/en-US';

const customEnLocale: Locale = {
...enUS,
Expand Down
8 changes: 4 additions & 4 deletions docs/data/date-pickers/calendar-systems/calendar-systems.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ We support `date-fns-jalali` package v2.x, v3.x, and v4.x major versions.

A single adapter cannot work for all `date-fns-jalali` versions, because the way functions are exported has been changed in v3.x.

To use `date-fns-jalali` v3.x or v4.x, you will have to import the adapter from `@mui/x-date-pickers/AdapterDateFnsJalaliV3` instead of `@mui/x-date-pickers/AdapterDateFnsJalali`.
To use `date-fns-jalali` v2.x, you need to import the adapter from `@mui/x-date-pickers/AdapterDateFnsJalaliV2` instead of `@mui/x-date-pickers/AdapterDateFnsJalali`.

```tsx
// with date-fns-jalali v2.x
import { AdapterDateFnsJalali } from '@mui/x-date-pickers/AdapterDateFnsJalali';
// with date-fns-jalali v3.x or v4.x
import { AdapterDateFnsJalali } from '@mui/x-date-pickers/AdapterDateFnsJalaliV3';
import { AdapterDateFnsJalali } from '@mui/x-date-pickers/AdapterDateFnsJalali';
// with date-fns-jalali v2.x
import { AdapterDateFnsJalali } from '@mui/x-date-pickers/AdapterDateFnsJalaliV2';
```

:::
Expand Down
26 changes: 25 additions & 1 deletion docs/data/migration/migration-pickers-v7/migration-pickers-v7.md
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ The following variables and types have been renamed to have a coherent `Picker`
+const pickerContext = usePickerContext();
```

- `FieldValueType`
- `FieldValueType`

```diff
-import { FieldValueType } from '@mui/x-date-pickers/models';
Expand Down Expand Up @@ -1129,6 +1129,30 @@ If you were using them, you need to replace them with the following code:
+ extends BaseMultiInputPickersTextFieldProps<true> {}
```

## ✅ Rename `date-fns` adapter imports

- The `AdapterDateFns` and `AdapterDateFnsJalali` adapters have been renamed to `AdapterDateFnsV2` and `AdapterDateFnsJalaliV2` respectively.
If you were using the old imports, you need to update them:

```diff
-import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns';
-import { AdapterDateFnsJalali } from '@mui/x-date-pickers/AdapterDateFnsJalali';
+import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFnsV2';
+import { AdapterDateFnsJalali } from '@mui/x-date-pickers/AdapterDateFnsJalaliV2';
```

Or consider updating the `date-fns` or `date-fns-jalali` package to the latest version and use the updated adapters.

- The `AdapterDateFnsV3` and `AdapterDateFnsJalaliV3` adapters have been renamed to `AdapterDateFns` and `AdapterDateFnsJalali` respectively.
If you were using the old imports, you need to update them:

```diff
-import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFnsV3';
-import { AdapterDateFnsJalali } from '@mui/x-date-pickers/AdapterDateFnsJalaliV3';
+import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns';
+import { AdapterDateFnsJalali } from '@mui/x-date-pickers/AdapterDateFnsJalali';
```

## Stop using `LicenseInfo` from `@mui/x-date-pickers-pro`

The `LicenseInfo` object is no longer exported from the `@mui/x-date-pickers-pro` package.
Expand Down
4 changes: 2 additions & 2 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@
"core-js": "^2.6.12",
"cross-env": "^7.0.3",
"d3-scale-chromatic": "^3.1.0",
"date-fns": "^2.30.0",
"date-fns-jalali": "^2.30.0-0",
"date-fns": "^4.1.0",
"date-fns-jalali": "^4.1.0-0",
"dayjs": "^1.11.13",
"doctrine": "^3.0.0",
"exceljs": "^4.4.0",
Expand Down
8 changes: 4 additions & 4 deletions docs/src/modules/components/PickersRenderingInstructions.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ export default function PickersRenderingInstructions() {
const commandLines = [
`import { LocalizationProvider } from '${componentPackage}';`,
...(libraryUsed === 'date-fns'
? ['// If you are using date-fns v2.x, please import `AdapterDateFns`']
? ['// If you are using date-fns v3.x or v4.x, please import the `AdapterDateFns`']
LukasTy marked this conversation as resolved.
Show resolved Hide resolved
: []),
`import { ${adapterName} } from '${componentPackage}/${adapterName}'`,
`import { ${adapterName} } from '${componentPackage}/${adapterName}';`,
...(libraryUsed === 'date-fns'
? [
'// If you are using date-fns v3.x or v4.x, please import the v3 adapter',
`import { ${adapterName} } from '${componentPackage}/AdapterDateFnsV3'`,
'// If you are using date-fns v2.x, please import the v2 adapter',
`import { ${adapterName} } from '${componentPackage}/AdapterDateFnsV2';`,
]
: []),
'',
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@
"cpy-cli": "^5.0.0",
"cross-env": "^7.0.3",
"danger": "^12.3.3",
"date-fns-jalali-v4": "npm:date-fns-jalali@4.1.0-0",
"date-fns-v4": "npm:date-fns@4.1.0",
"date-fns-jalali-v2": "npm:date-fns-jalali@2.30.0-0",
"date-fns-v2": "npm:date-fns@2.30.0",
"eslint": "^8.57.1",
"eslint-config-airbnb": "^19.0.4",
"eslint-config-airbnb-typescript": "^18.0.0",
Expand Down
31 changes: 31 additions & 0 deletions packages/x-codemod/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,39 @@

The list includes these transformers

- [`rename-adapter-date-fns-imports`](#rename-adapter-date-fns-imports)
- [`rename-and-move-field-value-type`](#rename-and-move-field-value-type)

#### `rename-adapter-date-fns-imports`

:::warning
This codemod is not idempotent. Running it multiple times will rename the imports back and forth.

Check warning on line 258 in packages/x-codemod/README.md

View workflow job for this annotation

GitHub Actions / runner / vale

[vale] reported by reviewdog 🐶 [Google.Will] Avoid using 'will'. Raw Output: {"message": "[Google.Will] Avoid using 'will'.", "location": {"path": "packages/x-codemod/README.md", "range": {"start": {"line": 258, "column": 59}}}, "severity": "WARNING"}
:::

- Renames `AdapterDateFns` and `AdapterDateFnsJalali` imports to `AdapterDateFnsV2` and `AdapterDateFnsJalaliV2` respectfully.

```diff
-import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns';
-import { AdapterDateFnsJalali } from '@mui/x-date-pickers/AdapterDateFnsJalali';
+import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFnsV2';
+import { AdapterDateFnsJalali } from '@mui/x-date-pickers/AdapterDateFnsJalaliV2';
```

- Renames `AdapterDateFnsV3` and `AdapterDateFnsJalaliV3` imports to `AdapterDateFns` and `AdapterDateFnsJalali` respectfully.

```diff
-import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFnsV3';
-import { AdapterDateFnsJalali } from '@mui/x-date-pickers/AdapterDateFnsJalaliV3';
+import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns';
+import { AdapterDateFnsJalali } from '@mui/x-date-pickers/AdapterDateFnsJalali';
```

<!-- #default-branch-switch -->

```bash
npx @mui/x-codemod@next v8.0.0/pickers/rename-adapter-date-fns-imports <path>
```

#### `rename-and-move-field-value-type`

Renames `FieldValueType` to `PickerValueType`.
Expand Down
2 changes: 2 additions & 0 deletions packages/x-codemod/src/v8.0.0/pickers/preset-safe/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import transformAdapterDateFnsImports from '../rename-adapter-date-fns-imports';
import transformFieldValue from '../rename-and-move-field-value-type';

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

export default function transformer(file: JsCodeShiftFileInfo, api: JsCodeShiftAPI, options: any) {
file.source = transformAdapterDateFnsImports(file, api, options);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDYT, should we include it in the safe preset if it is NOT idempotent?
Running it repeatedly will change the imports back and forth. 🙈

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDYT, should we include it in the safe preset if it is NOT idempotent?

Oh crap, that's a good question.
Could it be work adding a small comment at the end of the line that we can check to make it idempotent? 😬

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't imagine how we could do it cleanly (without adding some marker comment)... 🤷
It's the nature of the rename, that we are essentially swapping the adapter names, with the exception that AdapterV3 becomes Adapter and then after a subsequent run would become AdapterV2 at which point the codemod would no longer rename it back to Adapter, but it's still:

`AdapterV3` -> run codemod -> `Adapter` -> run codemod -> `AdapterV2`

Copy link
Member

@flaviendelangle flaviendelangle Jan 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't imagine how we could do it cleanly (without adding some marker comment)... 🤷

Me neither
The only solution I see is to transform:

import { AdapterDateFnsV3 } from '@mui/x-date-pickers/AdapterDateFnsV3';

into

// x-codemod/v8.0.0/pickers/adapter-date-fns-imports makes sure this codemod is idempotent. Don't remove unless you are sure not to run the v8.0.0 preset safe again.
import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns'; 

And check for the existence of the comment before transforming.

Which is far from great 😬

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, exactly... 🙈
Let's go the slightly risky route and wait for user feedback if this seems problematic. 🤔

file.source = transformFieldValue(file, api, options);

return file.source;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// @ts-nocheck
import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFnsV3';
import { AdapterDateFns as DateFns } from '@mui/x-date-pickers/AdapterDateFns';
import { AdapterDateFnsJalali } from '@mui/x-date-pickers/AdapterDateFnsJalaliV3';
import { AdapterDateFnsJalali as DateFnsJalali } from '@mui/x-date-pickers/AdapterDateFnsJalali';
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// @ts-nocheck
import { AdapterDateFns } from '@mui/x-date-pickers-pro/AdapterDateFnsV3';
import { AdapterDateFns as DateFns } from '@mui/x-date-pickers-pro/AdapterDateFns';
import { AdapterDateFnsJalali } from '@mui/x-date-pickers-pro/AdapterDateFnsJalaliV3';
import { AdapterDateFnsJalali as DateFnsJalali } from '@mui/x-date-pickers-pro/AdapterDateFnsJalali';
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns';
import { AdapterDateFns as DateFns } from '@mui/x-date-pickers/AdapterDateFnsV2';
import { AdapterDateFnsJalali } from '@mui/x-date-pickers/AdapterDateFnsJalali';
import { AdapterDateFnsJalali as DateFnsJalali } from '@mui/x-date-pickers/AdapterDateFnsJalaliV2';
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { AdapterDateFns } from '@mui/x-date-pickers-pro/AdapterDateFns';
import { AdapterDateFns as DateFns } from '@mui/x-date-pickers-pro/AdapterDateFnsV2';
import { AdapterDateFnsJalali } from '@mui/x-date-pickers-pro/AdapterDateFnsJalali';
import { AdapterDateFnsJalali as DateFnsJalali } from '@mui/x-date-pickers-pro/AdapterDateFnsJalaliV2';
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import type { JsCodeShiftAPI, JsCodeShiftFileInfo } from '../../../types';
import { renameImports } from '../../../util/renameImports';

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

const printOptions = options.printOptions || {
quote: 'single',
trailingComma: true,
};

renameImports({
j,
root,
packageNames: ['@mui/x-date-pickers', '@mui/x-date-pickers-pro'],
imports: [
{
oldEndpoint: 'AdapterDateFns',
newEndpoint: 'AdapterDateFnsV2',
importsMapping: {
AdapterDateFns: 'AdapterDateFns',
},
},
{
oldEndpoint: 'AdapterDateFnsV3',
newEndpoint: 'AdapterDateFns',
importsMapping: {
AdapterDateFns: 'AdapterDateFns',
},
},
{
oldEndpoint: 'AdapterDateFnsJalali',
newEndpoint: 'AdapterDateFnsJalaliV2',
importsMapping: {
AdapterDateFnsJalali: 'AdapterDateFnsJalali',
},
},
{
oldEndpoint: 'AdapterDateFnsJalaliV3',
newEndpoint: 'AdapterDateFnsJalali',
importsMapping: {
AdapterDateFnsJalali: 'AdapterDateFnsJalali',
},
},
],
});
return root.toSource(printOptions);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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));
}

const TEST_FILES = ['community-import', 'pro-import'];

describe('v8.0.0/pickers', () => {
describe('rename-adapter-date-fns-imports', () => {
TEST_FILES.forEach((testFile) => {
const actualPath = `./actual-${testFile}.spec.tsx`;
const expectedPath = `./expected-${testFile}.spec.tsx`;

describe(`${testFile.replace(/-/g, ' ')}`, () => {
it('transforms imports as needed', () => {
const actual = transform(
{ source: read(actualPath) },
{ jscodeshift: jscodeshift.withParser('tsx') },
{},
);

const expected = read(expectedPath);
expect(actual).to.equal(expected, 'The transformed version should be correct');
});
});
});
});
});
2 changes: 1 addition & 1 deletion packages/x-data-grid-premium/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
"@mui/material": "^5.16.13",
"@mui/system": "^5.16.13",
"@types/prop-types": "^15.7.14",
"date-fns": "^2.30.0",
"date-fns": "^4.1.0",
"rimraf": "^6.0.1"
},
"engines": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import addYears from 'date-fns/addYears';
import { addYears } from 'date-fns/addYears';
import { expect } from 'chai';
import { createRenderer, screen, waitFor } from '@mui/internal-test-utils';
import { DataGridPremium } from '@mui/x-data-grid-premium';
Expand Down
4 changes: 2 additions & 2 deletions packages/x-date-pickers-pro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@
"@mui/system": "^5.16.13",
"@types/luxon": "^3.4.2",
"@types/prop-types": "^15.7.14",
"date-fns": "^2.30.0",
"date-fns-jalali": "^2.30.0-0",
"date-fns": "^4.1.0",
"date-fns-jalali": "^4.1.0-0",
"dayjs": "^1.11.13",
"luxon": "^3.5.0",
"moment": "^2.30.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { AdapterDateFnsJalali } from '@mui/x-date-pickers/AdapterDateFnsJalaliV3';
export { AdapterDateFnsJalali } from '@mui/x-date-pickers/AdapterDateFnsJalaliV2';
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFnsV3';
export { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFnsV2';
4 changes: 2 additions & 2 deletions packages/x-date-pickers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@
"@types/moment-hijri": "^2.1.4",
"@types/moment-jalaali": "^0.7.9",
"@types/prop-types": "^15.7.14",
"date-fns": "^2.30.0",
"date-fns-jalali": "^2.30.0-0",
"date-fns": "^4.1.0",
"date-fns-jalali": "^4.1.0-0",
"dayjs": "^1.11.13",
"luxon": "^3.5.0",
"moment": "^2.30.1",
Expand Down
Loading
Loading