Skip to content

Commit

Permalink
add codemod to update icon Button size
Browse files Browse the repository at this point in the history
  • Loading branch information
robphoenix committed Jul 11, 2024
1 parent 4d7877a commit 218d3ba
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/four-sheep-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@utilitywarehouse/ds-codemod': major
---

Add codemod to update IconButton size
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// this is a comment at the beginning of the file
import * as React from 'react';
import { IconButton } from '@utilitywarehouse/web-ui';
import { ChevronRightMediumIcon } from '@utilitywarehouse/react-icons';

const Component = () => (
<div>
<IconButton
label="continue"
size={{
mobile: 'small',
desktop: 'large',
}}
>
<ChevronRightMediumIcon />
</IconButton>
<IconButton
label="continue"
size={{
mobile: 'small',
tablet: 'large',
desktop: 'small',
wide: 'large',
}}
>
<ChevronRightMediumIcon />
</IconButton>
</div>
);

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

const Component = () => (
<div>
<IconButton
label="continue"
size={{
mobile: 'small',
desktop: 'medium',
}}
>
<ChevronRightMediumIcon />
</IconButton>
<IconButton
label="continue"
size={{
mobile: 'small',
tablet: 'medium',
desktop: 'small',
wide: 'medium',
}}
>
<ChevronRightMediumIcon />
</IconButton>
</div>
);

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

const Component = () => (
<div>
<IconButton label="continue">
<ChevronRightMediumIcon/>
</IconButton>
<IconButton size='small' label="continue">
<ChevronRightMediumIcon/>
</IconButton>
<IconButton size='large' label="continue">
<ChevronRightMediumIcon/>
</IconButton>
</div>
);

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

const Component = () => (
<div>
<IconButton label="continue">
<ChevronRightMediumIcon/>
</IconButton>
<IconButton size='small' label="continue">
<ChevronRightMediumIcon/>
</IconButton>
<IconButton size='medium' label="continue">
<ChevronRightMediumIcon/>
</IconButton>
</div>
);

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

const NAME = 'rename-icon-button-size';

describe(NAME, () => {
defineTest(__dirname, NAME, {}, undefined, {
parser: 'tsx',
});
});

const RESPONSIVE_NAME = `${NAME}-responsive`;

describe(RESPONSIVE_NAME, () => {
defineTest(__dirname, NAME, {}, RESPONSIVE_NAME, {
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,9 +1,11 @@
/* eslint-disable no-undef */
const transformRenameLabButtonSize = require('./rename-lab-button-size');
const transformRenameIconButtonSize = require('./rename-icon-button-size');

function transformer(file, api, options) {
// run this before migrating Button out of lab submodule
file.source = transformRenameLabButtonSize(file, api, options);
file.source = transformRenameIconButtonSize(file, api, options);
return file.source;
}

Expand Down
38 changes: 38 additions & 0 deletions packages/codemod/transforms/web-ui/v1/rename-icon-button-size.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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 hasWebUIIconButton =
root
.find(j.ImportDeclaration)
.filter(path => path.value.source.value === '@utilitywarehouse/web-ui')
.find(j.ImportSpecifier)
.filter(path => path.node.imported.name === 'IconButton').length > 0;

if (hasWebUIIconButton) {
root
.findJSXElements('IconButton')
.find(j.JSXAttribute, { name: { type: 'JSXIdentifier', name: 'size' } })
.find(j.Literal)
.forEach(path => {
const value = path.node.value;
if (value === 'large') {
path.node.value = 'medium';
}
});
}

// 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;

0 comments on commit 218d3ba

Please sign in to comment.