Skip to content

Commit

Permalink
Feat(codemod): Add isDisposable codemod for Collapse
Browse files Browse the repository at this point in the history
- New codemod mod for changing `hideonCollapse` prop to `isDisposable` in Collapse component

- Solves #832
  • Loading branch information
pavelklibani committed Dec 2, 2024
1 parent 1ec23f2 commit 9422a58
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 1 deletion.
24 changes: 24 additions & 0 deletions packages/codemods/src/transforms/v4/web-react/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Web-React v4 Codemods

This is a collection of codemods for updating Web-React v4 components.

You can find instructions on how to run these codemods in the main package [README](https://github.com/lmc-eu/spirit-design-system/blob/main/packages/codemods/README.md).

## Included Scripts

### `v4/web-react/collapse-isDisposable-prop` — UncontrolledCollapse `hideOnCollapse` to `isDisposable` prop change

This codemod updates the `UncontrolledCollapse` component by replacing the `hideOnCollapse` prop with a new `isDisposable` prop.

#### Usage

```sh
npx @lmc-eu/spirit-codemods -p <path> -t v4/web-react/collapse-isDisposable-prop
```

#### Example

```diff
- <UncontrolledCollapse hideOnCollapse … />
+ <UncontrolledCollapse isDisposable … />
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React from 'react';
// @ts-ignore: No declaration -- The library is not installed; we don't need to install it for fixtures.
import { UncontrolledCollapse } from '@lmc-eu/spirit-web-react';

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

export const MyComponent = () => (
<>
<UncontrolledCollapse id="collapse-1">collapse</UncontrolledCollapse>
<UncontrolledCollapse id="collapse-2" isDisposable>
collapse
</UncontrolledCollapse>
</>
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { testTransform } from '../../../../../tests/testUtils';

testTransform(__dirname, 'collapse-isDisposable-prop');
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { API, FileInfo } from 'jscodeshift';
import { removeParentheses } from '../../../helpers';

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

// Find import statements for the specific module and Button 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 componentSpecifier = importStatements.find(j.ImportSpecifier, {
imported: {
type: 'Identifier',
name: 'UncontrolledCollapse',
},
});

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

// Rename 'hideOnCollapse' attribute to 'isDisposable' in UncontrolledCollapse components
components
.find(j.JSXAttribute, {
name: {
type: 'JSXIdentifier',
name: 'hideOnCollapse',
},
})
.forEach((attributePath) => {
attributePath.node.name.name = 'isDisposable';
});
}
}

return removeParentheses(root.toSource());
};

export default transform;
2 changes: 1 addition & 1 deletion packages/codemods/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
"module": "es2020",
"target": "es2020"
},
"include": ["./", "./.eslintrc.js"],
"include": ["./*", "./.eslintrc.js"],
"exclude": ["./node_modules", "**/__testfixtures__/**"]
}

0 comments on commit 9422a58

Please sign in to comment.