Skip to content

Commit

Permalink
Feat(codemods): Add codemods for Modal deprecations #DS-1184 #DS-1201…
Browse files Browse the repository at this point in the history
… #DS-1181
  • Loading branch information
crishpeen committed Apr 16, 2024
1 parent 8469fc3 commit fe61e86
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from 'react';
// @ts-ignore: No declaration -- The library is not installed; we don't need to install it for fixtures.
import { ModalDialog } from '@lmc-eu/spirit-web-react';

export const MyComponent = () => (
<>
<ModalDialog />
<ModalDialog isDockedOnMobile />
<ModalDialog isDockedOnMobile={false} />
</>
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from 'react';
// @ts-ignore: No declaration -- The library is not installed; we don't need to install it for fixtures.
import { ModalDialog } from '@lmc-eu/spirit-web-react';

export const MyComponent = () => (
<>
<ModalDialog isDockedOnMobile />
<ModalDialog isDockedOnMobile />
<ModalDialog isDockedOnMobile={false} />
</>
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from 'react';
// @ts-ignore: No declaration -- The library is not installed; we don't need to install it for fixtures.
import { ModalDialog } from '@lmc-eu/spirit-web-react';

export const MyComponent = () => (
<>
<ModalDialog />
<ModalDialog isScrollable />
<ModalDialog isScrollable={false} />
</>
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from 'react';
// @ts-ignore: No declaration -- The library is not installed; we don't need to install it for fixtures.
import { ModalDialog } from '@lmc-eu/spirit-web-react';

export const MyComponent = () => (
<>
<ModalDialog isScrollable />
<ModalDialog isScrollable />
<ModalDialog isScrollable={false} />
</>
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { testTransform } from '../../../../../tests/testUtils';

testTransform(__dirname, 'modal-isdockedonmobile-prop');
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { testTransform } from '../../../../../tests/testUtils';

testTransform(__dirname, 'modal-isscrollable-prop');
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { API, FileInfo } from 'jscodeshift';

const transform = (fileInfo: FileInfo, api: API) => {
const j = api.jscodeshift;
const root = j(fileInfo.source);

// Find import statements for the specific module and ModalDialog specifier
const importStatements = root.find(j.ImportDeclaration, {
source: {
value: (value: string) => /^@lmc-eu\/spirit-web-react(\/.*)?$/.test(value),
},
});

// Check if the module is imported
if (importStatements.length > 0) {
const modalDialogSpecifier = importStatements.find(j.ImportSpecifier, {
imported: {
type: 'Identifier',
name: 'ModalDialog',
},
});

// Check if ModalDialog specifier is present
if (modalDialogSpecifier.length > 0) {
// Find ModalDialog components in the module
const modalDialogComponents = root.find(j.JSXOpeningElement, {
name: {
type: 'JSXIdentifier',
name: 'ModalDialog',
},
});

modalDialogComponents.forEach((path) => {
if (path.node && path.node.attributes) {
// Check if the component already has the isDockedOnMobile attribute
const hasIsDockedOnMobileAttribute = path.node.attributes.some(
(attr) => attr.type === 'JSXAttribute' && attr.name.name === 'isDockedOnMobile',
);

// If not, add isDockedOnMobile attribute
if (!hasIsDockedOnMobileAttribute) {
path.node.attributes.push(j.jsxAttribute(j.jsxIdentifier('isDockedOnMobile'), null));
}
}
});
}
}

return root.toSource();
};

export default transform;
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { API, FileInfo } from 'jscodeshift';

const transform = (fileInfo: FileInfo, api: API) => {
const j = api.jscodeshift;
const root = j(fileInfo.source);

// Find import statements for the specific module and ModalDialog specifier
const importStatements = root.find(j.ImportDeclaration, {
source: {
value: (value: string) => /^@lmc-eu\/spirit-web-react(\/.*)?$/.test(value),
},
});

// Check if the module is imported
if (importStatements.length > 0) {
const modalDialogSpecifier = importStatements.find(j.ImportSpecifier, {
imported: {
type: 'Identifier',
name: 'ModalDialog',
},
});

// Check if ModalDialog specifier is present
if (modalDialogSpecifier.length > 0) {
// Find ModalDialog components in the module
const modalDialogComponents = root.find(j.JSXOpeningElement, {
name: {
type: 'JSXIdentifier',
name: 'ModalDialog',
},
});

modalDialogComponents.forEach((path) => {
if (path.node && path.node.attributes) {
// Check if the component already has the isScrollable attribute
const hasIsScrollableAttribute = path.node.attributes.some(
(attr) => attr.type === 'JSXAttribute' && attr.name.name === 'isScrollable',
);

// If not, add isScrollable attribute
if (!hasIsScrollableAttribute) {
path.node.attributes.push(j.jsxAttribute(j.jsxIdentifier('isScrollable'), null));
}
}
});
}
}

return root.toSource();
};

export default transform;
38 changes: 36 additions & 2 deletions packages/web-react/DEPRECATIONS-v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,33 @@ non-scrollable by default. It will be possible to re-enable the inside scrolling

#### Migration Guide

Add `isScrollable` prop to the `ModalDialog` component.
Use codemod to automatically update your codebase.

```sh
npx @lmc-eu/spirit-codemods -p <path> -t v2/web-react/modal-iscrollable-prop
```

See [Codemods documentation][readme-codemods] for more details.

Or manually add `isScrollable` prop to the `ModalDialog` component.

### ModalDialog `isExpandedOnMobile` Prop

The `isExpandedOnMobile` prop will be set to `true` by default in the next major release and the ModalDialog will be
expanded on mobile by default. It will be possible to re-collapse the inside by setting the `isExpandedOnMobile` prop
to `false` value.

#### Migration Guide

Use codemod to automatically update your codebase.

```sh
npx @lmc-eu/spirit-codemods -p <path> -t v2/web-react/modal-isexpandedonmobile-prop
```

See [Codemods documentation][readme-codemods] for more details.

Or manually add `isExpandedOnMobile={false}` prop to the `ModalDialog` component to keep the current behavior.

### ModalDialog Uniform Appearance

Expand All @@ -201,7 +227,15 @@ remain accessible via the `isDockedOnMobile` property.

#### Migration Guide

Add `isDockedOnMobile` prop to the `ModalDialog` component.
Use codemod to automatically update your codebase.

```sh
npx @lmc-eu/spirit-codemods -p <path> -t v2/web-react/modal-isdockeonmobile-prop
```

See [Codemods documentation][readme-codemods] for more details.

Or manually add `isDockedOnMobile` prop to the `ModalDialog` component.

### Alert `danger` Icon

Expand Down

0 comments on commit fe61e86

Please sign in to comment.