-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add codemod to update icon Button size
- Loading branch information
1 parent
4d7877a
commit 218d3ba
Showing
8 changed files
with
165 additions
and
0 deletions.
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,5 @@ | ||
--- | ||
'@utilitywarehouse/ds-codemod': major | ||
--- | ||
|
||
Add codemod to update IconButton size |
31 changes: 31 additions & 0 deletions
31
...odemod/transforms/web-ui/v1/__testfixtures__/rename-icon-button-size-responsive.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,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; |
31 changes: 31 additions & 0 deletions
31
...demod/transforms/web-ui/v1/__testfixtures__/rename-icon-button-size-responsive.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,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; |
20 changes: 20 additions & 0 deletions
20
packages/codemod/transforms/web-ui/v1/__testfixtures__/rename-icon-button-size.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,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; |
20 changes: 20 additions & 0 deletions
20
packages/codemod/transforms/web-ui/v1/__testfixtures__/rename-icon-button-size.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,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; |
18 changes: 18 additions & 0 deletions
18
packages/codemod/transforms/web-ui/v1/__tests__/rename-icon-button-size.test.js
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,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', | ||
}); | ||
}); |
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
38 changes: 38 additions & 0 deletions
38
packages/codemod/transforms/web-ui/v1/rename-icon-button-size.js
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,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; |