Skip to content

Commit

Permalink
Feat(codemods): Button and ButtonLink have isSymmetrical prop instead…
Browse files Browse the repository at this point in the history
… of isSquare #DS-1484
  • Loading branch information
curdaj committed Oct 3, 2024
1 parent 6066c8f commit 1b8c9fb
Show file tree
Hide file tree
Showing 9 changed files with 232 additions and 0 deletions.
30 changes: 30 additions & 0 deletions packages/codemods/src/transforms/v3/web-react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,33 @@ npx @lmc-eu/spirit-codemods -p <path> -t v3/web-react/link-underlined-prop
- <Link isUnderlined … />
+ <Link underlined="always" … />
```

### `v3/web-react/button-prop-name` — Button `isSquare` to `isSymmetrical` prop change

This codemod updates the `Button` component by replacing the `isSquare` prop with a `isSymmetrical` prop.

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

#### Example

```diff
- <Button isSquare … />
+ <Button isSymmetrical … />
```

### `v3/web-react/buttonLink-prop-name` — ButtonLink `isSquare` to `isSymmetrical` prop change

This codemod updates the `ButtonLink` component by replacing the `isSquare` prop with a `isSymmetrical` prop.

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

#### Example

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

export const MyComponent = () => (
<>
<Button color="primary" isSquare>
Click me
</Button>
<Button color="primary" elementType="div" isSquare>
Click me
</Button>
<Button color="primary" isDisabled isSquare>
Click me
</Button>
<Button color="primary">Click me</Button>
<Button color="primary" isDisabled isSquare={false}>
Click me
</Button>
<Button color="primary" isDisabled isSquare={true}>
Click me
</Button>
</>
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React from 'react';
// @ts-ignore: No declaration -- The library is not installed; we don't need to install it for fixtures.
import { Button } from '@lmc-eu/spirit-web-react/src';

export const MyComponent = () => (
<>
<Button color="primary" isSymmetrical>
Click me
</Button>
<Button color="primary" elementType="div" isSymmetrical>
Click me
</Button>
<Button color="primary" isDisabled isSymmetrical>
Click me
</Button>
<Button color="primary">Click me</Button>
<Button color="primary" isDisabled isSymmetrical={false}>
Click me
</Button>
<Button color="primary" isDisabled isSymmetrical={true}>
Click me
</Button>
</>
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React from 'react';
// @ts-ignore: No declaration -- The library is not installed; we don't need to install it for fixtures.
import { ButtonLink } from '@lmc-eu/spirit-web-react/src';

export const MyComponent = () => (
<>
<ButtonLink href="" color="primary" isSquare>
Click me
</ButtonLink>
<ButtonLink color="primary" elementType="div" isSquare>
Click me
</ButtonLink>
<ButtonLink color="primary" isDisabled isSquare>
Click me
</ButtonLink>
<ButtonLink color="primary">Click me</ButtonLink>
<ButtonLink color="primary" isDisabled isSquare={false}>
Click me
</ButtonLink>
<ButtonLink color="primary" isDisabled isSquare={true}>
Click me
</ButtonLink>
</>
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React from 'react';
// @ts-ignore: No declaration -- The library is not installed; we don't need to install it for fixtures.
import { ButtonLink } from '@lmc-eu/spirit-web-react/src';

export const MyComponent = () => (
<>
<ButtonLink href="" color="primary" isSymmetrical>
Click me
</ButtonLink>
<ButtonLink color="primary" elementType="div" isSymmetrical>
Click me
</ButtonLink>
<ButtonLink color="primary" isDisabled isSymmetrical>
Click me
</ButtonLink>
<ButtonLink color="primary">Click me</ButtonLink>
<ButtonLink color="primary" isDisabled isSymmetrical={false}>
Click me
</ButtonLink>
<ButtonLink color="primary" isDisabled isSymmetrical={true}>
Click me
</ButtonLink>
</>
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { testTransform } from '../../../../../tests/testUtils';

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

testTransform(__dirname, 'buttonLink-prop-name');
50 changes: 50 additions & 0 deletions packages/codemods/src/transforms/v3/web-react/button-prop-name.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
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 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: 'Button',
},
});

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

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

return root.toSource();
};

export default transform;
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
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 ButtonLink 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: 'ButtonLink',
},
});

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

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

return root.toSource();
};

export default transform;

0 comments on commit 1b8c9fb

Please sign in to comment.