Skip to content

Commit

Permalink
refactor(codemods): update getKaioTagNamesMapByPattern to return TagI…
Browse files Browse the repository at this point in the history
…mportAttributesMap
  • Loading branch information
HeartSquared committed Dec 18, 2024
1 parent 93b4395 commit 7148b2b
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 215 deletions.
138 changes: 18 additions & 120 deletions packages/components/codemods/upgradeIconV1/upgradeIconV1.spec.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import { parseJsx } from '../__tests__/utils'
import {
getKaioTagNamesMapByPattern,
printAst,
transformSource,
type ImportModuleRegexTagNamesMap,
type TransformSourceArgs,
} from '../utils'
import { upgradeIconV1 } from './upgradeIconV1'

const transformIcons = (
sourceFile: TransformSourceArgs['sourceFile'],
tagNames: ImportModuleRegexTagNamesMap,
): string =>
transformSource({
const transformIcons = (sourceFile: TransformSourceArgs['sourceFile']): string => {
const kaioTagNamesMap = getKaioTagNamesMapByPattern(sourceFile, 'Icon$')
return transformSource({
sourceFile,
transformers: [upgradeIconV1(tagNames)],
transformers: [upgradeIconV1(kaioTagNamesMap!)],
})
}

describe('upgradeIconV1()', () => {
describe('CaMonogramIcon to Brand', () => {
Expand All @@ -29,12 +28,7 @@ describe('upgradeIconV1()', () => {
import { Brand } from "@kaizen/components"
export const TestComponent = () => <Brand ${transformedBrandProps} />
`)
expect(
transformIcons(
inputAst,
new Map([['@kaizen/components', new Map([['CaMonogramIcon', 'CaMonogramIcon']])]]),
),
).toEqual(printAst(outputAst))
expect(transformIcons(inputAst)).toEqual(printAst(outputAst))
})

it('updates import from CaMonogramIcon using alias to Brand', () => {
Expand All @@ -46,12 +40,7 @@ describe('upgradeIconV1()', () => {
import { Brand } from "@kaizen/components"
export const TestComponent = () => <Brand ${transformedBrandProps} />
`)
expect(
transformIcons(
inputAst,
new Map([['@kaizen/components', new Map([['LogoAlias', 'CaMonogramIcon']])]]),
),
).toEqual(printAst(outputAst))
expect(transformIcons(inputAst)).toEqual(printAst(outputAst))
})

it('does not add another Brand import if it is already imported', () => {
Expand All @@ -63,20 +52,7 @@ describe('upgradeIconV1()', () => {
import { Brand } from "@kaizen/components"
export const TestComponent = () => <Brand ${transformedBrandProps} />
`)
expect(
transformIcons(
inputAst,
new Map([
[
'@kaizen/components',
new Map([
['Brand', 'Brand'],
['CaMonogramIcon', 'CaMonogramIcon'],
]),
],
]),
),
).toEqual(printAst(outputAst))
expect(transformIcons(inputAst)).toEqual(printAst(outputAst))
})

it('uses Brand alias for an existing import', () => {
Expand All @@ -88,20 +64,7 @@ describe('upgradeIconV1()', () => {
import { Brand as KzBrand } from "@kaizen/components"
export const TestComponent = () => <KzBrand ${transformedBrandProps} />
`)
expect(
transformIcons(
inputAst,
new Map([
[
'@kaizen/components',
new Map([
['KzBrand', 'Brand'],
['CaMonogramIcon', 'CaMonogramIcon'],
]),
],
]),
),
).toEqual(printAst(outputAst))
expect(transformIcons(inputAst)).toEqual(printAst(outputAst))
})
})

Expand All @@ -117,12 +80,7 @@ describe('upgradeIconV1()', () => {
import { LoadingSpinner } from "@kaizen/components"
export const TestComponent = () => <LoadingSpinner ${transformedLoadingSpinnerProps} />
`)
expect(
transformIcons(
inputAst,
new Map([['@kaizen/components', new Map([['SpinnerIcon', 'SpinnerIcon']])]]),
),
).toEqual(printAst(outputAst))
expect(transformIcons(inputAst)).toEqual(printAst(outputAst))
})

it('updates import from SpinnerIcon using alias to LoadingSpinner', () => {
Expand All @@ -134,12 +92,7 @@ describe('upgradeIconV1()', () => {
import { LoadingSpinner } from "@kaizen/components"
export const TestComponent = () => <LoadingSpinner ${transformedLoadingSpinnerProps} />
`)
expect(
transformIcons(
inputAst,
new Map([['@kaizen/components', new Map([['LogoAlias', 'SpinnerIcon']])]]),
),
).toEqual(printAst(outputAst))
expect(transformIcons(inputAst)).toEqual(printAst(outputAst))
})

it('does not add another LoadingSpinner import if it is already imported', () => {
Expand All @@ -151,20 +104,7 @@ describe('upgradeIconV1()', () => {
import { LoadingSpinner } from "@kaizen/components"
export const TestComponent = () => <LoadingSpinner ${transformedLoadingSpinnerProps} />
`)
expect(
transformIcons(
inputAst,
new Map([
[
'@kaizen/components',
new Map([
['LoadingSpinner', 'LoadingSpinner'],
['SpinnerIcon', 'SpinnerIcon'],
]),
],
]),
),
).toEqual(printAst(outputAst))
expect(transformIcons(inputAst)).toEqual(printAst(outputAst))
})

it('uses LoadingSpinner alias for an existing import', () => {
Expand All @@ -176,20 +116,7 @@ describe('upgradeIconV1()', () => {
import { LoadingSpinner as KzLoadingSpinner } from "@kaizen/components"
export const TestComponent = () => <KzLoadingSpinner ${transformedLoadingSpinnerProps} />
`)
expect(
transformIcons(
inputAst,
new Map([
[
'@kaizen/components',
new Map([
['KzLoadingSpinner', 'LoadingSpinner'],
['SpinnerIcon', 'SpinnerIcon'],
]),
],
]),
),
).toEqual(printAst(outputAst))
expect(transformIcons(inputAst)).toEqual(printAst(outputAst))
})
})

Expand All @@ -202,12 +129,7 @@ describe('upgradeIconV1()', () => {
import { Icon } from "@kaizen/components/future"
export const TestComponent = () => <Icon name="flag" isFilled />
`)
expect(
transformIcons(
inputAst,
new Map([['@kaizen/components', new Map([['FlagOnIcon', 'FlagOnIcon']])]]),
),
).toEqual(printAst(outputAst))
expect(transformIcons(inputAst)).toEqual(printAst(outputAst))
})

it('transforms aliased old Icon', () => {
Expand All @@ -219,12 +141,7 @@ describe('upgradeIconV1()', () => {
import { Icon } from "@kaizen/components/future"
export const TestComponent = () => <Icon name="menu" />
`)
expect(
transformIcons(
inputAst,
new Map([['@kaizen/components', new Map([['IconAlias', 'HamburgerIcon']])]]),
),
).toEqual(printAst(outputAst))
expect(transformIcons(inputAst)).toEqual(printAst(outputAst))
})

describe('import statements', () => {
Expand All @@ -238,12 +155,7 @@ describe('upgradeIconV1()', () => {
import { Icon } from "@kaizen/components/future"
export const TestComponent = () => <Card><Icon name="add" /></Card>
`)
expect(
transformIcons(
inputAst,
new Map([['@kaizen/components', new Map([['AddIcon', 'AddIcon']])]]),
),
).toEqual(printAst(outputAst))
expect(transformIcons(inputAst)).toEqual(printAst(outputAst))
})

it('does not update import of components which are not from KAIO', () => {
Expand All @@ -269,21 +181,7 @@ describe('upgradeIconV1()', () => {
</>
)
`)
expect(
transformIcons(
inputAst,
new Map([
[
'@kaizen/components',
new Map([
['AddIcon', 'AddIcon'],
['IconAlias', 'HamburgerIcon'],
]),
],
['somewhere-else', new Map([['HamHam', 'HamburgerIcon']])],
]),
),
).toEqual(printAst(outputAst))
expect(transformIcons(inputAst)).toEqual(printAst(outputAst))
})
})
})
44 changes: 20 additions & 24 deletions packages/components/codemods/upgradeIconV1/upgradeIconV1.ts
Original file line number Diff line number Diff line change
@@ -1,66 +1,62 @@
import ts from 'typescript'
import {
getKaioTagName,
setImportToAdd,
setImportToRemove,
updateKaioImports,
type ImportModuleRegexTagNamesMap,
type TagImportAttributesMap,
type UpdateKaioImportsArgs,
} from '../utils'
import { getNewIconPropsFromOldIconName } from './getNewIconPropsFromOldIconName'
import { transformCaMonogramIconToBrand } from './transformCaMonogramIconToBrand'
import { transformIcon } from './transformIcon'
import { transformSpinnerIconToLoadingSpinner } from './transformSpinnerIconToLoadingSpinner'

const reverseStringMap = <Key extends string, Value extends string>(
map: Map<Key, Value>,
): Map<Value, Key> => {
const reverseMap = new Map<Value, Key>()
map.forEach((value, key) => reverseMap.set(value, key))
return reverseMap
}

export const upgradeIconV1 =
(tagNames: ImportModuleRegexTagNamesMap): ts.TransformerFactory<ts.SourceFile> =>
(tagsMap: TagImportAttributesMap): ts.TransformerFactory<ts.SourceFile> =>
(context) =>
(rootNode) => {
const oldImportSource = '@kaizen/components'

const kaioTagNames = tagNames.get(oldImportSource)
if (!kaioTagNames) return rootNode
const importedBrandTagName = getKaioTagName(rootNode, 'Brand')
const importedLoadingSpinnerTagName = getKaioTagName(rootNode, 'LoadingSpinner')

const componentToAliasMap = reverseStringMap(kaioTagNames)
const importsToRemove = new Map() satisfies UpdateKaioImportsArgs['importsToRemove']
const importsToAdd = new Map() satisfies UpdateKaioImportsArgs['importsToAdd']

const visit = (node: ts.Node): ts.Node => {
if (ts.isJsxOpeningElement(node) || ts.isJsxSelfClosingElement(node)) {
const tagName = node.tagName.getText()
const kaioComponentName = kaioTagNames.get(tagName)
const tagImportAttributes = tagsMap.get(tagName)

if (!tagImportAttributes) return node

const kaioComponentName = tagImportAttributes.originalName
const oldImportSource = tagImportAttributes.importModuleName

if (kaioComponentName === 'CaMonogramIcon') {
setImportToRemove(importsToRemove, oldImportSource, kaioComponentName)
const alias = componentToAliasMap.get('Brand')!

if (!kaioTagNames.has(alias)) {
if (!importedBrandTagName) {
setImportToAdd(importsToAdd, '@kaizen/components', {
componentName: 'Brand',
alias: alias !== 'Brand' ? alias : undefined,
alias: importedBrandTagName !== 'Brand' ? importedBrandTagName : undefined,
})
}
return transformCaMonogramIconToBrand(node, alias)
return transformCaMonogramIconToBrand(node, importedBrandTagName)
}

if (kaioComponentName === 'SpinnerIcon') {
setImportToRemove(importsToRemove, oldImportSource, kaioComponentName)
const alias = componentToAliasMap.get('LoadingSpinner')!

if (!kaioTagNames.has(alias)) {
if (!importedLoadingSpinnerTagName) {
setImportToAdd(importsToAdd, '@kaizen/components', {
componentName: 'LoadingSpinner',
alias: alias !== 'LoadingSpinner' ? alias : undefined,
alias:
importedLoadingSpinnerTagName !== 'LoadingSpinner'
? importedLoadingSpinnerTagName
: undefined,
})
}
return transformSpinnerIconToLoadingSpinner(node, alias)
return transformSpinnerIconToLoadingSpinner(node, importedLoadingSpinnerTagName)
}

if (kaioComponentName) {
Expand Down
56 changes: 44 additions & 12 deletions packages/components/codemods/utils/getKaioTagName.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,20 @@ describe('getKaioTagNamesMapByPattern()', () => {
expect(tagNames).toEqual(
new Map([
[
'@kaizen/components',
new Map([
['AddIcon', 'AddIcon'],
['ArrowDownIcon', 'ArrowDownIcon'],
]),
'AddIcon',
{
importModuleName: '@kaizen/components',
tagName: 'AddIcon',
originalName: 'AddIcon',
},
],
[
'ArrowDownIcon',
{
importModuleName: '@kaizen/components',
tagName: 'ArrowDownIcon',
originalName: 'ArrowDownIcon',
},
],
]),
)
Expand All @@ -158,11 +167,20 @@ describe('getKaioTagNamesMapByPattern()', () => {
expect(tagNames).toEqual(
new Map([
[
'@kaizen/components',
new Map([
['KzAddIcon', 'AddIcon'],
['ArrowDownIcon', 'ArrowDownIcon'],
]),
'KzAddIcon',
{
importModuleName: '@kaizen/components',
tagName: 'KzAddIcon',
originalName: 'AddIcon',
},
],
[
'ArrowDownIcon',
{
importModuleName: '@kaizen/components',
tagName: 'ArrowDownIcon',
originalName: 'ArrowDownIcon',
},
],
]),
)
Expand All @@ -176,8 +194,22 @@ describe('getKaioTagNamesMapByPattern()', () => {
const tagNames = getKaioTagNamesMapByPattern(input, 'Icon$')
expect(tagNames).toEqual(
new Map([
['@kaizen/components', new Map([['AddIcon', 'AddIcon']])],
['@kaizen/components/future', new Map([['Icon', 'Icon']])],
[
'AddIcon',
{
importModuleName: '@kaizen/components',
tagName: 'AddIcon',
originalName: 'AddIcon',
},
],
[
'Icon',
{
importModuleName: '@kaizen/components/future',
tagName: 'Icon',
originalName: 'Icon',
},
],
]),
)
})
Expand Down
Loading

0 comments on commit 7148b2b

Please sign in to comment.