Skip to content

Commit

Permalink
Add lab migration codemod
Browse files Browse the repository at this point in the history
  • Loading branch information
robphoenix committed Aug 5, 2024
1 parent 9246372 commit 4c77ee3
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 2 deletions.
1 change: 0 additions & 1 deletion packages/codemod/transforms/web-ui/v0/import-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ const cwuiElements = [
'InteractiveCardProps',
];

// eslint-disable-next-line @typescript-eslint/no-unused-vars
function transformer(file, api) {
const j = api.jscodeshift;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// this is a comment at the beginning of the file
import * as React from 'react';
import { Link, TextLink } from '@utilitywarehouse/web-ui/dist/lab';

const Component = () => (
<div>
<Link>Hamburgefons</Link>
<TextLink>Hamburgefons</TextLink>
<Link size='small'>Hamburgefons</Link>
<Link size='large'>Hamburgefons</Link>
</div>
);

export default Component;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// this is a comment at the beginning of the file
import * as React from 'react';
import { Link, TextLink } from '@utilitywarehouse/web-ui';

const Component = () => (
<div>
<Link>Hamburgefons</Link>
<TextLink>Hamburgefons</TextLink>
<Link size='small'>Hamburgefons</Link>
<Link size='large'>Hamburgefons</Link>
</div>
);

export default Component;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* eslint-disable no-undef */
const defineTest = require('jscodeshift/dist/testUtils').defineTest;

const NAME = 'update-lab-imports';

describe(NAME, () => {
defineTest(__dirname, NAME, {}, undefined, {
parser: 'tsx',
});
});
2 changes: 2 additions & 0 deletions packages/codemod/transforms/web-ui/v1/migration.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/* eslint-disable no-undef */
const transformUpdateLabImports = require('./update-lab-imports');
const transformRenameLabButtonSize = require('./rename-lab-button-size');
const transformRenameIconButtonSize = require('./rename-icon-button-size');

function transformer(file, api, options) {
file.source = transformUpdateLabImports(file, api, options);
// run this before migrating Button out of lab submodule
file.source = transformRenameLabButtonSize(file, api, options);
file.source = transformRenameIconButtonSize(file, api, options);
Expand Down
42 changes: 42 additions & 0 deletions packages/codemod/transforms/web-ui/v1/update-lab-imports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
function transformer(file, api) {
const j = api.jscodeshift;

const root = j(file.source);
const getFirstNode = () => root.find(j.Program).get('body', 0).node;
// Save the comments attached to the first node
const firstNode = getFirstNode();
const { comments } = firstNode;

const components = ['Link', 'TextLink'];
const localComponents = [];

root
.find(j.ImportDeclaration)
.filter(path => path.value.source.value === '@utilitywarehouse/web-ui/dist/lab')
.forEach(path => {
j(path)
.find(j.ImportSpecifier)
.forEach(p => {
const localName = p.node.local.name;
if (components.includes(localName)) {
localComponents.push(j.importSpecifier(j.identifier(localName)));
}
});
})
.forEach(path => {
if (localComponents.length > 0) {
j(path).replaceWith(
j.importDeclaration(localComponents, j.literal('@utilitywarehouse/web-ui'))
);
}
});

// If the first node has been modified or deleted, reattach the comments
const firstNode2 = getFirstNode();
if (firstNode2 !== firstNode) {
firstNode2.comments = comments;
}
return root.toSource({ quote: 'single' });
}

module.exports = transformer;
19 changes: 18 additions & 1 deletion packages/web-ui/docs/web-ui/guides/migration/v1.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ to provide a codemod removing legacy props, so that the errors caused by having
incorrect props will highlight where manual updates and checks needed to
happen.

There is a codemod to update imports from the `lab` subdirectory, including the
`Link` component.

## TextLink component

This component has been completely redesigned. The previous implementation only
Expand All @@ -190,6 +193,9 @@ to provide a codemod removing legacy props, so that the errors caused by having
incorrect props will highlight where manual updates and checks needed to
happen.

There is a codemod to update imports from the `lab` subdirectory, including the
`TextLink` component.

## Codemods

### Migration
Expand All @@ -203,15 +209,26 @@ npx @utilitywarehouse/ds-codemod web-ui/v1/migration <path>

Included codemods:

- `update-lab-imports`
- `rename-lab-button-size`
- `rename-icon-button-size`

Please make sure you fix any linting or formatting issues before continuing to
run any further codemods.

### Update lab imports

Updates the import paths for components that are being moved out of the `lab`
subdirectory.

```console
npx @utilitywarehouse/ds-codemod web-ui/v1/update-lab-imports <path>
```

### Rename lab Button size

Updates the size value `large` to `medium` for the `Button` component from the lab submodule.
Updates the size value `large` to `medium` for the `Button` component from the
lab subdirectory.

```console
npx @utilitywarehouse/ds-codemod web-ui/v1/rename-lab-button-size <path>
Expand Down

0 comments on commit 4c77ee3

Please sign in to comment.