Skip to content

Commit

Permalink
Feat(codemod): Add codemod for Link underline prop
Browse files Browse the repository at this point in the history
- replacing the `isUnderlined` prop with a new `underline` prop
and set it to "always" if true
  • Loading branch information
pavelklibani committed Aug 7, 2024
1 parent 0f6cd05 commit 8e2ffb6
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 0 deletions.
24 changes: 24 additions & 0 deletions packages/codemods/src/transforms/v3/web-react/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Web-React v3 Codemods

This is a collection of codemods for updating Web-React v3 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

### `v3/web-react/link-underline-prop` — Link `isUnderlined` to `underline` prop change

This codemod updates the `Link` component by replacing the `isUnderlined` prop with a new `underline` prop, setting it to "always".

#### Usage

```sh
npx @lmc-eu/spirit-codemods -p <path> -t v3/web-react/link-underline-prop
```

#### Example

```diff
- <Link isUnderlined … />
+ <Link underline="always" … />
```
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 { Link } from '@lmc-eu/spirit-web-react';

export const MyComponent = () => (
<>
<Link />
<Link isUnderlined />
<Link isUnderlined={true} />
<Link isUnderlined={false} />
</>
);
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 { Link } from '@lmc-eu/spirit-web-react';

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

testTransform(__dirname, 'link-underline-prop');
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import {
API,
FileInfo,
Collection,
JSCodeshift,
JSXElement,
JSXAttribute,
ImportDeclaration,
JSXOpeningElement,
} from 'jscodeshift';

const transform = (fileInfo: FileInfo, api: API): string => {
const j: JSCodeshift = api.jscodeshift;
const root: Collection<JSXElement> = j(fileInfo.source);

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

// Check if the module is imported
if (importStatements.length > 0) {
// Find Link components in the code
const linkComponents: Collection<JSXOpeningElement> = root.find(j.JSXOpeningElement, {
name: {
type: 'JSXIdentifier',
name: 'Link',
},
});

linkComponents.forEach((path) => {
if (path.node && path.node.attributes) {
// Find the isUnderlined attribute
path.node.attributes.forEach((attr, index) => {
if (attr.type === 'JSXAttribute' && (attr as JSXAttribute).name.name === 'isUnderlined') {
const jsxAttr = attr as JSXAttribute;
// Check if the attribute value is true
if (
jsxAttr.value === null ||
(jsxAttr.value?.type === 'JSXExpressionContainer' &&
jsxAttr.value.expression.type === 'BooleanLiteral' &&
jsxAttr.value.expression.value === true) ||
(jsxAttr.value?.type === 'Literal' && jsxAttr.value.value === true)
) {
// Change isUnderlined to underline="always"
jsxAttr.name.name = 'underline';
jsxAttr.value = j.literal('always');
} else if (
jsxAttr.value?.type === 'JSXExpressionContainer' &&
jsxAttr.value.expression.type === 'BooleanLiteral' &&
jsxAttr.value.expression.value === false
) {
// If isUnderlined is set to false, remove the attribute
path.node.attributes?.splice(index, 1);
}
}
});
}
});
}

return root.toSource();
};

export default transform;

0 comments on commit 8e2ffb6

Please sign in to comment.