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

Add codemod for Text component interface #1849

Merged
merged 11 commits into from
Dec 22, 2023
Merged
5 changes: 5 additions & 0 deletions .changeset/early-tables-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@channel.io/bezier-codemod": minor
---

Add codemod to transform interface of `Text` component
46 changes: 45 additions & 1 deletion packages/bezier-codemod/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ export const Wrapper = styled(Button)``;

### Remove Alpha prefix from `AlphaStack` and add Legacy prefix to `Stack`

`remove-alpha-from-alpha-stack`
`v2-remove-alpha-from-alpha-stack`

Deprecates current `Stack`, `HStack`, `VStack`, `StackItem`, `Spacer` components and supports `AlphaStack` instead, removing "Alpha" prefix.

Expand Down Expand Up @@ -317,3 +317,47 @@ function Bar() {
);
}
```

### Change interface of `Text`

`v2-text-component-interface`

Replace `Typography` enum with string literal and change properties related to margin to be shorthand.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

뒤 문구는 왜 제거된건지 궁금합니다!

Copy link
Collaborator Author

@yangwooseong yangwooseong Dec 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

transformEnumMemberToStringLiteral 를 사용하게 되면, Typography enum 을 모두 바꿔버립니다.

// As-is
import { styled, Text, Typogarphy } from '@channel.io/bezier-react'

const Title = styled(Text).attrs({
  typo: Typography.Size11,
})``

const Subtitle = styled(Text)`
  ${Typography.Size11}
`  

// To-be
import { styled, Text } from '@channel.io/bezier-react'

const Title = styled(Text).attrs({
  typo: '11',
})``

const Subtitle = styled(Text)`
  '11',
`  

그렇기 때문에 changeAttrProperty 함수로 바꾸었고, 이제 이런 케이스를 잘 핸들링할 수 있게 되어서 위 문구를 제거했습니다. ba57985 에서 관련 테스트 케이스를 추가했습니다.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아 바로 이해됐습니다ㅎㅎ 감사합니다


For example:

```tsx
import { Text, styled, Typography } from "@channel.io/bezier-react";

function Foo() {
return (
<Text typo={Typography.Size13} marginLeft={4}>
title
</Text>
);
}

const Title = styled(Text).attrs({
typo: Typography.Size13,
marginLeft: 4,
})``;
```

Transforms into:

```tsx
import { Text, styled } from "@channel.io/bezier-react";

function Foo() {
return (
<Text typo="13" ml={4}>
title
</Text>
);
}

const Title = styled(Text).attrs({
typo: "13",
ml: 4,
})``;
```
3 changes: 3 additions & 0 deletions packages/bezier-codemod/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import foundationToCssVariableTransition from './transforms/v2-foundation-to-css
import styledToStyledComponents from './transforms/v2-import-styled-from-styled-components/transform.js'
import inputInterpolationToCssVariable from './transforms/v2-input-interpolation-to-css-variable/transform.js'
import removeAlphaFromAlphaStack from './transforms/v2-remove-alpha-from-alpha-stack/transform.js'
import textComponentInterface from './transforms/v2-text-component-interface/transform.js'
import typographyInterpolationToCssVariable from './transforms/v2-typography-interpolation-to-css-variable/transform.js'
import zIndexInterpolationToCssVariable from './transforms/v2-z-index-interpolation-to-css-variable/transform.js'

Expand All @@ -55,6 +56,7 @@ enum Option {
V2StyledToStyledComponents = 'v2-styled-to-styled-components',
V2RemoveAlphaFromAlphaStack = 'v2-remove-alpha-from-alpha-stack',
V2ZIndexInterpolationToCssVariable = 'v2-z-index-interpolation-to-css-variable',
V2TextComponentInterface = 'v2-text-component-interface',
Exit = 'Exit',
}

Expand All @@ -75,6 +77,7 @@ const transformMap = {
[Option.V2StyledToStyledComponents]: styledToStyledComponents,
[Option.V2RemoveAlphaFromAlphaStack]: removeAlphaFromAlphaStack,
[Option.V2ZIndexInterpolationToCssVariable]: zIndexInterpolationToCssVariable,
[Option.V2TextComponentInterface]: textComponentInterface,
}

const options = (Object.keys(transformMap) as Option[]).map((transformName) => ({
Expand Down
2 changes: 1 addition & 1 deletion packages/bezier-codemod/src/shared/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type Member = string
type Value = string
export type EnumTransformMap = Record<Name, Record<Member, Value>>

export const transformEnumMemberToStringLiteral = (sourceFile: SourceFile, enumTransforms: EnumTransformMap) => {
export const transformEnumToStringLiteralInBezier = (sourceFile: SourceFile, enumTransforms: EnumTransformMap) => {
const transformedEnumNames: string[] = []

Object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { type SourceFile } from 'ts-morph'

import {
type EnumTransformMap,
transformEnumMemberToStringLiteral,
transformEnumToStringLiteralInBezier,
} from '../../shared/enum.js'

const ENUM_TRANSFORM_MAP: EnumTransformMap = {
Expand All @@ -18,7 +18,7 @@ const ENUM_TRANSFORM_MAP: EnumTransformMap = {
}

function enumMemberToStringLiteral(sourceFile: SourceFile): true | void {
return transformEnumMemberToStringLiteral(sourceFile, ENUM_TRANSFORM_MAP)
return transformEnumToStringLiteralInBezier(sourceFile, ENUM_TRANSFORM_MAP)
}

export default enumMemberToStringLiteral
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Text, Typography, styled } from '@channel.io/bezier-react'

export const Title = styled(Text).attrs({
typo: Typography.Size11,
marginAll: 1,
marginTop: 2,
marginRight: 3,
marginBottom: 4,
marginLeft: 5,
marginHorizontal: 6,
marginVertical: 7,
})``
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Text, styled } from '@channel.io/bezier-react'

export const Title = styled(Text).attrs({
typo: "11",
m: 1,
mt: 2,
mr: 3,
mb: 4,
ml: 5,
mx: 6,
my: 7,
})``
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Text, Typography } from '@channel.io/bezier-react'

export function Component () {
return (
<Text
typo={Typography.Size14}
marginAll={1}
marginTop={3}
marginRight={3}
marginBottom={3}
marginLeft={2}
marginHorizontal={3}
marginVertical={3}
>
text
</Text>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Text } from '@channel.io/bezier-react'

export function Component () {
return (
<Text
typo="14"
m={1}
mt={3}
mr={3}
mb={3}
ml={2}
mx={3}
my={3}
>
text
</Text>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Text, Typography, styled } from '@channel.io/bezier-react'

export const Title = styled(Text).attrs({
typo: Typography.Size11,
marginAll: 1,
marginTop: 2,
marginRight: 3,
marginBottom: 4,
marginLeft: 5,
marginHorizontal: 6,
marginVertical: 7,
})``

export const Subtitle = styled(Text)`
${Typography.Size11};
`
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Text, Typography, styled } from '@channel.io/bezier-react'

export const Title = styled(Text).attrs({
typo: "11",
m: 1,
mt: 2,
mr: 3,
mb: 4,
ml: 5,
mx: 6,
my: 7,
})``

export const Subtitle = styled(Text)`
${Typography.Size11};
`
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { testTransformFunction } from '../../utils/test.js'

import textTransform from './transform.js'

describe('Text component transform', () => {
it('should transform typography enum to string literal and margin properties to be shorthand', () => {
testTransformFunction(__dirname, 'text-component-props', textTransform)
})

it('should transform properties in attrs object of styled component', () => {
testTransformFunction(__dirname, 'text-component-attrs', textTransform)
})

it('should transform properties only in attrs object of styled component', () => {
testTransformFunction(__dirname, 'text-component-with-interpolation', textTransform)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { type SourceFile } from 'ts-morph'

import {
type ComponentTransformMap,
changeAttrProperty,
changeComponentProp,
} from '../../utils/component.js'

const STYLED_ATTRS_TRANSFORM_MAP: ComponentTransformMap = {
Text: {
keyMapper: {
marginAll: 'm',
marginTop: 'mt',
marginRight: 'mr',
marginBottom: 'mb',
marginLeft: 'ml',
marginHorizontal: 'mx',
marginVertical: 'my',
},
valueMapper: {
'Typography.Size11': '"11"',
'Typography.Size12': '"12"',
'Typography.Size13': '"13"',
'Typography.Size14': '"14"',
'Typography.Size15': '"15"',
'Typography.Size16': '"16"',
'Typography.Size17': '"17"',
'Typography.Size18': '"18"',
'Typography.Size22': '"22"',
'Typography.Size24': '"24"',
'Typography.Size30': '"30"',
'Typography.Size36': '"36"',
},
},
}

const JSX_PROP_TRANSFORM_MAP: ComponentTransformMap = {
Text: {
keyMapper: {
marginAll: 'm',
marginTop: 'mt',
marginRight: 'mr',
marginBottom: 'mb',
marginLeft: 'ml',
marginHorizontal: 'mx',
marginVertical: 'my',
},
valueMapper: {
'{Typography.Size11}': '"11"',
'{Typography.Size12}': '"12"',
'{Typography.Size13}': '"13"',
'{Typography.Size14}': '"14"',
'{Typography.Size15}': '"15"',
'{Typography.Size16}': '"16"',
'{Typography.Size17}': '"17"',
'{Typography.Size18}': '"18"',
'{Typography.Size22}': '"22"',
'{Typography.Size24}': '"24"',
'{Typography.Size30}': '"30"',
'{Typography.Size36}': '"36"',
},
},
}

const transformTextComponentProps = (sourceFile: SourceFile) => {
const oldSourceFileText = sourceFile.getText()

changeComponentProp(sourceFile, JSX_PROP_TRANSFORM_MAP)
changeAttrProperty(sourceFile, STYLED_ATTRS_TRANSFORM_MAP)

sourceFile.fixUnusedIdentifiers()

return oldSourceFileText !== sourceFile.getText()
}

export default transformTextComponentProps
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/* eslint-disable no-template-curly-in-string */
import { type SourceFile } from 'ts-morph'

import { transformEnumMemberToStringLiteral } from '../../shared/enum.js'
import { transformEnumToStringLiteralInBezier } from '../../shared/enum.js'
import { interpolationTransform } from '../../shared/interpolation.js'
import { removeUnusedNamedImport } from '../../utils/import.js'

const cssVariableByInterpolation = {
const CSS_VARIABLE_TRANSFORM_MAP = {
'ZIndex.Hide': 'var(--z-index-hidden);',
'ZIndex.Auto': 'var(--z-index-auto);',
'ZIndex.Base': 'var(--z-index-base);',
Expand All @@ -17,22 +17,24 @@ const cssVariableByInterpolation = {
'ZIndex.Important': 'var(--z-index-important);',
}

const ENUM_TRANSFORM_MAP = {
ZIndex: {
Hide: 'var(--z-index-hidden)',
Base: 'var(--z-index-base)',
Float: 'var(--z-index-float)',
Overlay: 'var(--z-index-overlay)',
Modal: 'var(--z-index-modal)',
Toast: 'var(--z-index-toast)',
Tooltip: 'var(--z-index-tooltip)',
Important: 'var(--z-index-important)',
},
}

const replaceZIndexInterpolation = (sourceFile: SourceFile) => {
const oldSourceFileText = sourceFile.getText()

interpolationTransform(sourceFile, cssVariableByInterpolation)
transformEnumMemberToStringLiteral(sourceFile, {
ZIndex: {
Hide: 'var(--z-index-hidden)',
Base: 'var(--z-index-base)',
Float: 'var(--z-index-float)',
Overlay: 'var(--z-index-overlay)',
Modal: 'var(--z-index-modal)',
Toast: 'var(--z-index-toast)',
Tooltip: 'var(--z-index-tooltip)',
Important: 'var(--z-index-important)',
},
})
interpolationTransform(sourceFile, CSS_VARIABLE_TRANSFORM_MAP)
transformEnumToStringLiteralInBezier(sourceFile, ENUM_TRANSFORM_MAP)

const isChanged = sourceFile.getText() !== oldSourceFileText
if (isChanged) {
Expand Down
Loading