-
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 lab Button size (#420)
- Loading branch information
1 parent
34e83f0
commit 2071e57
Showing
13 changed files
with
244 additions
and
2 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 lab Button size |
13 changes: 13 additions & 0 deletions
13
...d/transforms/web-ui/v1/__testfixtures__/rename-lab-button-size-no-button-import.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,13 @@ | ||
// this is a comment at the beginning of the file | ||
import * as React from 'react'; | ||
import { Link } from '@utilitywarehouse/web-ui/dist/lab'; | ||
import { Heading } from '@utilitywarehouse/web-ui'; | ||
|
||
const Component = () => ( | ||
<div> | ||
<Heading></Heading> | ||
<Link></Link> | ||
</div> | ||
); | ||
|
||
export default Component; |
13 changes: 13 additions & 0 deletions
13
.../transforms/web-ui/v1/__testfixtures__/rename-lab-button-size-no-button-import.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,13 @@ | ||
// this is a comment at the beginning of the file | ||
import * as React from 'react'; | ||
import { Link } from '@utilitywarehouse/web-ui/dist/lab'; | ||
import { Heading } from '@utilitywarehouse/web-ui'; | ||
|
||
const Component = () => ( | ||
<div> | ||
<Heading></Heading> | ||
<Link></Link> | ||
</div> | ||
); | ||
|
||
export default Component; |
13 changes: 13 additions & 0 deletions
13
...emod/transforms/web-ui/v1/__testfixtures__/rename-lab-button-size-no-lab-import.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,13 @@ | ||
// this is a comment at the beginning of the file | ||
import * as React from 'react'; | ||
import { Button } from '@utilitywarehouse/web-ui'; | ||
|
||
const Component = () => ( | ||
<div> | ||
<Button></Button> | ||
<Button size='small'></Button> | ||
<Button size='large'></Button> | ||
</div> | ||
); | ||
|
||
export default Component; |
13 changes: 13 additions & 0 deletions
13
...mod/transforms/web-ui/v1/__testfixtures__/rename-lab-button-size-no-lab-import.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,13 @@ | ||
// this is a comment at the beginning of the file | ||
import * as React from 'react'; | ||
import { Button } from '@utilitywarehouse/web-ui'; | ||
|
||
const Component = () => ( | ||
<div> | ||
<Button></Button> | ||
<Button size='small'></Button> | ||
<Button size='large'></Button> | ||
</div> | ||
); | ||
|
||
export default Component; |
24 changes: 24 additions & 0 deletions
24
...codemod/transforms/web-ui/v1/__testfixtures__/rename-lab-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,24 @@ | ||
// this is a comment at the beginning of the file | ||
import * as React from 'react'; | ||
import { Button } from '@utilitywarehouse/web-ui/dist/lab'; | ||
|
||
const Component = () => ( | ||
<div> | ||
<Button | ||
size={{ | ||
mobile: 'small', | ||
desktop: 'large', | ||
}} | ||
></Button> | ||
<Button | ||
size={{ | ||
mobile: 'small', | ||
tablet: 'large', | ||
desktop: 'small', | ||
wide: 'large', | ||
}} | ||
></Button> | ||
</div> | ||
); | ||
|
||
export default Component; |
24 changes: 24 additions & 0 deletions
24
...odemod/transforms/web-ui/v1/__testfixtures__/rename-lab-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,24 @@ | ||
// this is a comment at the beginning of the file | ||
import * as React from 'react'; | ||
import { Button } from '@utilitywarehouse/web-ui/dist/lab'; | ||
|
||
const Component = () => ( | ||
<div> | ||
<Button | ||
size={{ | ||
mobile: 'small', | ||
desktop: 'medium', | ||
}} | ||
></Button> | ||
<Button | ||
size={{ | ||
mobile: 'small', | ||
tablet: 'medium', | ||
desktop: 'small', | ||
wide: 'medium', | ||
}} | ||
></Button> | ||
</div> | ||
); | ||
|
||
export default Component; |
13 changes: 13 additions & 0 deletions
13
packages/codemod/transforms/web-ui/v1/__testfixtures__/rename-lab-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,13 @@ | ||
// this is a comment at the beginning of the file | ||
import * as React from 'react'; | ||
import { Button } from '@utilitywarehouse/web-ui/dist/lab'; | ||
|
||
const Component = () => ( | ||
<div> | ||
<Button></Button> | ||
<Button size='small'></Button> | ||
<Button size='large'></Button> | ||
</div> | ||
); | ||
|
||
export default Component; |
13 changes: 13 additions & 0 deletions
13
packages/codemod/transforms/web-ui/v1/__testfixtures__/rename-lab-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,13 @@ | ||
// this is a comment at the beginning of the file | ||
import * as React from 'react'; | ||
import { Button } from '@utilitywarehouse/web-ui/dist/lab'; | ||
|
||
const Component = () => ( | ||
<div> | ||
<Button></Button> | ||
<Button size='small'></Button> | ||
<Button size='medium'></Button> | ||
</div> | ||
); | ||
|
||
export default Component; |
34 changes: 34 additions & 0 deletions
34
packages/codemod/transforms/web-ui/v1/__tests__/rename-lab-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,34 @@ | ||
/* eslint-disable no-undef */ | ||
const defineTest = require('jscodeshift/dist/testUtils').defineTest; | ||
|
||
const NAME = 'rename-lab-button-size'; | ||
|
||
describe(NAME, () => { | ||
defineTest(__dirname, NAME, {}, undefined, { | ||
parser: 'tsx', | ||
}); | ||
}); | ||
|
||
const NO_LAB_IMPORT_NAME = `${NAME}-no-lab-import`; | ||
|
||
describe(NO_LAB_IMPORT_NAME, () => { | ||
defineTest(__dirname, NAME, {}, NO_LAB_IMPORT_NAME, { | ||
parser: 'tsx', | ||
}); | ||
}); | ||
|
||
const NO_BUTTON_IMPORT_NAME = `${NAME}-no-button-import`; | ||
|
||
describe(NO_BUTTON_IMPORT_NAME, () => { | ||
defineTest(__dirname, NAME, {}, NO_BUTTON_IMPORT_NAME, { | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* eslint-disable no-undef */ | ||
const transformRenameLabButtonSize = require('./rename-lab-button-size'); | ||
|
||
function transformer(file, api, options) { | ||
// run this before migrating Button out of lab submodule | ||
file.source = transformRenameLabButtonSize(file, api, options); | ||
return file.source; | ||
} | ||
|
||
module.exports = transformer; |
38 changes: 38 additions & 0 deletions
38
packages/codemod/transforms/web-ui/v1/rename-lab-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 hasWebUILabButton = | ||
root | ||
.find(j.ImportDeclaration) | ||
.filter(path => path.value.source.value === '@utilitywarehouse/web-ui/dist/lab') | ||
.find(j.ImportSpecifier) | ||
.filter(path => path.node.imported.name === 'Button').length > 0; | ||
|
||
if (hasWebUILabButton) { | ||
root | ||
.findJSXElements('Button') | ||
.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; |
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