Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(codemods): upgrade to Button v3 #5360

Closed
wants to merge 33 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
e03435b
refactor(codemods): move empty check to updateKaioImports
HeartSquared Nov 25, 2024
559ace8
refactor(codemods): add utils to traverse and transform
HeartSquared Nov 25, 2024
1c3574e
feat(codemods): add failing tests to upgrade IconButton to Button
HeartSquared Nov 25, 2024
8b3c09e
feat(codemods): update import for IconButton
HeartSquared Nov 25, 2024
95a7c18
feat(codemods): add getKaioTagNamesMapByString and refactor similar u…
HeartSquared Nov 26, 2024
fac44eb
feat(upgradeIconButtonToButton): update import statements
HeartSquared Nov 26, 2024
6339c19
feat(upgradeIconButtonToButton): add IconButton to Button transformer
HeartSquared Nov 26, 2024
f7c87a4
refactor(codemods): add util createJsxElementWithChildren with failin…
HeartSquared Nov 28, 2024
3beeed0
refactor(codemods): rename upgradeIconButtonToButton to upgradeIconBu…
HeartSquared Nov 28, 2024
542bf75
docs(codemods): update README
HeartSquared Nov 28, 2024
357e2f2
feat(createJsxElementWithChildren): transform jsx expressions to chil…
HeartSquared Nov 28, 2024
c801470
test(createJsxElementWithChildren): add test for when children is mis…
HeartSquared Nov 28, 2024
3307405
test(upgradeIconButton): fix tests
HeartSquared Nov 28, 2024
0bfd81f
test(upgradeIconButton): add failing tests for variant and size
HeartSquared Nov 28, 2024
4f9573a
feat(upgradeIconButton): transform reversed to isReversed
HeartSquared Nov 28, 2024
7f37bc6
Merge branch 'main' into KZN-2846/codemod--icon-button
HeartSquared Dec 6, 2024
61b018a
refactor(codemods): update util createProp to remove "={true}"
HeartSquared Dec 6, 2024
0159868
feat(upgradeIconButton): transform classNameOverride
HeartSquared Dec 6, 2024
c80b116
feat(upgradeIconButton): transform data-automation-id
HeartSquared Dec 6, 2024
3662ae1
feat(upgradeIconButton): transform disabled
HeartSquared Dec 6, 2024
22994dd
feat(upgradeIconButton): transform newTabAndIUnderstandTheAccessibili…
HeartSquared Dec 6, 2024
fd37dd3
refactor(upgradeIconButton): rename transformer
HeartSquared Dec 6, 2024
8b51af9
feat(upgradeIconButton): only add hasHiddenLabel for IconButton
HeartSquared Dec 6, 2024
5620067
refactor(codemods): update util getKaioTagNamesMapByString to accept …
HeartSquared Dec 6, 2024
30b7387
feat(upgradeIconButton): transform Button v1
HeartSquared Dec 6, 2024
3bfc7fd
refactor(codemods): rename upgradeIconButton to upgradeV1Buttons
HeartSquared Dec 18, 2024
9f48ada
refactor(upgradeV1Buttons): rename transformV1ButtonsToV3 to transfor…
HeartSquared Dec 18, 2024
15d4f84
docs(codemods): update readme
HeartSquared Dec 18, 2024
74b974b
Merge branch 'main' into KZN-2846/codemod--icon-button
HeartSquared Dec 18, 2024
441b57a
refactor(codemods): rename getKaioTagNamesMapByRegex to getKaioTagNam…
HeartSquared Dec 18, 2024
47aeb3c
refactor(codemods): rename getKaioTagNamesMapByString to getKaioTagNa…
HeartSquared Dec 18, 2024
93b4395
refactor(codemods): rename transformComponentsAndImportsInDirByRegex …
HeartSquared Dec 18, 2024
7148b2b
refactor(codemods): update getKaioTagNamesMapByPattern to return TagI…
HeartSquared Dec 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions packages/components/codemods/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,41 @@ Released in `1.60.0`

Removes `Popover` component props `variant` and `customIcon`.

### `upgradeV1Buttons`

Released in `TBC`

Migrates `Button` and `IconButton` component to `Button` V3 or `LinkButton`.

#### Props

- `label` becomes `children`
- eg. `<IconButton label="Hello" />` becomes `<Button>Hello</Button>`
- `onClick` becomes `onPress`
- (TO DO) Variants
- (TO DO) Sizes
- `reversed` becomes `isReversed`
- `classNameOverride` becomes `className`
- `data-automation-id` becomes `data-testid`
- `disabled` becomes `isDisabled`
- (TO DO) Only when `href` exists:
- `newTabAndIUnderstandTheAccessibilityImplications` becomes `target="_blank"`
- `rel="noopener noreferrer"` is also added
- `component` will not be removed by the codemod, but will throw a TypeScript error as the prop itself no longer exists
- (TO DO) A `@todo` comment will be prepended to the prop
- For `IconButton` only:
- `hasHiddenLabel` will be added

#### Component transformation

- `Button`/`IconButton` without the `href` or `component` prop will become `Button` V3
- `Button`/`IconButton` with the `href` prop will become `LinkButton`
- `Button`/`IconButton` with the `component` prop will become `LinkButton`

#### Imports

All imports of V1 Buttons will now point to `@kaizen/components/v3/actions`.

### `upgradeIconV1`

Released in `1.67.0`; last updated in `1.68.1`
Expand Down
21 changes: 4 additions & 17 deletions packages/components/codemods/upgradeIconV1/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import fs from 'fs'
import { getKaioTagNamesByRegex, transformSource, traverseDir } from '../utils'
import { createEncodedSourceFile } from '../utils/createEncodedSourceFile'
import { transformComponentsAndImportsInDirByPattern } from '../utils'
import { upgradeIconV1 } from './upgradeIconV1'

const run = (): void => {
Expand All @@ -11,20 +9,9 @@ const run = (): void => {
process.exit(1)
}

const transformFile = (componentFilePath: string, sourceCode: string): void => {
const sourceFile = createEncodedSourceFile(componentFilePath, sourceCode)
const tagNames = getKaioTagNamesByRegex(sourceFile, 'Icon$')
if (tagNames) {
const updatedSourceFile = transformSource({
sourceFile,
transformers: [upgradeIconV1(tagNames)],
})

fs.writeFileSync(componentFilePath, updatedSourceFile, 'utf8')
}
}

traverseDir(targetDir, transformFile)
transformComponentsAndImportsInDirByPattern(targetDir, 'Icon$', (tagNames) => [
upgradeIconV1(tagNames),
])
}

run()
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 ImportModuleNameTagsMap,
type TransformSourceArgs,
} from '../utils'
import { upgradeIconV1 } from './upgradeIconV1'

const transformIcons = (
sourceFile: TransformSourceArgs['sourceFile'],
tagNames: ImportModuleNameTagsMap,
): 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))
})
})
})
Loading
Loading