-
Notifications
You must be signed in to change notification settings - Fork 47
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
yangwooseong
merged 11 commits into
channel-io:alpha
from
yangwooseong:feat/codemod-for-text
Dec 22, 2023
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
047a3ee
feat(bezier-codemod): add transform util to rename component props
yangwooseong c0582d3
feat(bezier-codemod): add codemod for `Text` component
yangwooseong 6dd61c2
feat(bezier-codemod): update README
yangwooseong ef4797c
chore(bezier-codemod): add changeset
yangwooseong 452bdb2
feat(bezier-codemod): include value transform logic in util function
yangwooseong e2097a2
chore(bezier-codemod): update README
yangwooseong 55fdcf4
feat(bezier-codemod): add test case for using Typography enum as inte…
yangwooseong 88612d0
refactor(bezier-codemod): rename variable
yangwooseong bcf4880
chore(bezier-codemod): add test case
yangwooseong 4a70aab
refactor(bezier-codemod): add bezier context to enum transform function
yangwooseong 356134e
chore(bezier-codemod): typo fix
yangwooseong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ | ||
--- | ||
"@channel.io/bezier-codemod": minor | ||
--- | ||
|
||
Add codemod to transform interface of `Text` component |
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
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
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
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
12 changes: 12 additions & 0 deletions
12
...odemod/src/transforms/v2-text-component-interface/fixtures/text-component-attrs.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,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, | ||
})`` |
12 changes: 12 additions & 0 deletions
12
...demod/src/transforms/v2-text-component-interface/fixtures/text-component-attrs.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,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, | ||
})`` |
18 changes: 18 additions & 0 deletions
18
...odemod/src/transforms/v2-text-component-interface/fixtures/text-component-props.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,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> | ||
) | ||
} |
18 changes: 18 additions & 0 deletions
18
...demod/src/transforms/v2-text-component-interface/fixtures/text-component-props.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,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> | ||
) | ||
} |
16 changes: 16 additions & 0 deletions
16
...ansforms/v2-text-component-interface/fixtures/text-component-with-interpolation.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,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}; | ||
` |
16 changes: 16 additions & 0 deletions
16
...nsforms/v2-text-component-interface/fixtures/text-component-with-interpolation.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,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}; | ||
` |
17 changes: 17 additions & 0 deletions
17
packages/bezier-codemod/src/transforms/v2-text-component-interface/transform.test.ts
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,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) | ||
}) | ||
}) |
76 changes: 76 additions & 0 deletions
76
packages/bezier-codemod/src/transforms/v2-text-component-interface/transform.ts
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,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 |
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 | ||||
---|---|---|---|---|---|---|
@@ -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);', | ||||||
|
@@ -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) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
마이너: typo. 지금 봤네요 |
||||||
|
||||||
const isChanged = sourceFile.getText() !== oldSourceFileText | ||||||
if (isChanged) { | ||||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
뒤 문구는 왜 제거된건지 궁금합니다!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
transformEnumMemberToStringLiteral
를 사용하게 되면,Typography
enum 을 모두 바꿔버립니다.그렇기 때문에
changeAttrProperty
함수로 바꾸었고, 이제 이런 케이스를 잘 핸들링할 수 있게 되어서 위 문구를 제거했습니다. ba57985 에서 관련 테스트 케이스를 추가했습니다.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
아 바로 이해됐습니다ㅎㅎ 감사합니다