-
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.
- Loading branch information
1 parent
9246372
commit 4c77ee3
Showing
7 changed files
with
100 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
14 changes: 14 additions & 0 deletions
14
packages/codemod/transforms/web-ui/v1/__testfixtures__/update-lab-imports.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,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; |
14 changes: 14 additions & 0 deletions
14
packages/codemod/transforms/web-ui/v1/__testfixtures__/update-lab-imports.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,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; |
10 changes: 10 additions & 0 deletions
10
packages/codemod/transforms/web-ui/v1/__tests__/update-lab-imports.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,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', | ||
}); | ||
}); |
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
42 changes: 42 additions & 0 deletions
42
packages/codemod/transforms/web-ui/v1/update-lab-imports.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,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; |
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