-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat(codemod): Add
isDisposable
codemod for Collapse
- New codemod mod for changing `hideonCollapse` prop to `isDisposable` in Collapse component - Solves #832
- Loading branch information
1 parent
1ec23f2
commit 9422a58
Showing
6 changed files
with
103 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 … /> | ||
``` |
12 changes: 12 additions & 0 deletions
12
...odemods/src/transforms/v4/web-react/__testfixtures__/collapse-isDisposable-prop.input.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
</> | ||
); |
12 changes: 12 additions & 0 deletions
12
...demods/src/transforms/v4/web-react/__testfixtures__/collapse-isDisposable-prop.output.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
</> | ||
); |
3 changes: 3 additions & 0 deletions
3
packages/codemods/src/transforms/v4/web-react/__tests__/collapse-isDisposable-prop.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { testTransform } from '../../../../../tests/testUtils'; | ||
|
||
testTransform(__dirname, 'collapse-isDisposable-prop'); |
51 changes: 51 additions & 0 deletions
51
packages/codemods/src/transforms/v4/web-react/collapse-isDisposable-prop.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters