-
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
Implement codemod transform which replaces enum usage with string literal #1639
Implement codemod transform which replaces enum usage with string literal #1639
Conversation
🦋 Changeset detectedLatest commit: a67d809 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Codecov ReportAll modified lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #1639 +/- ##
=======================================
Coverage 87.23% 87.23%
=======================================
Files 281 281
Lines 3900 3900
Branches 820 820
=======================================
Hits 3402 3402
Misses 424 424
Partials 74 74 ☔ View full report in Codecov by Sentry. |
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.
오프라인으로 싱크했지만, 타겟으로 삼은 enum만 string literal로 변환되도록 변경되면 좋겠습니다~!
sourceFile.forEachDescendant((node) => { | ||
if (node.isKind(SyntaxKind.PropertyAccessExpression)) { | ||
const firstIdentifier = node.getFirstChildByKind(SyntaxKind.Identifier) | ||
const lastIdentifier = node.getLastChildByKind(SyntaxKind.Identifier) | ||
|
||
if (firstIdentifier && lastIdentifier) { | ||
const declarationSymbol = firstIdentifier.getSymbol() | ||
const memberValueDeclaration = lastIdentifier.getSymbol()?.getValueDeclaration() | ||
|
||
if (Node.isEnumMember(memberValueDeclaration)) { | ||
const enumName = declarationSymbol?.getName() | ||
const enumMemberValue = memberValueDeclaration.getInitializer()?.getText() | ||
|
||
if (enumName && enumMemberValue && targetEnums.includes(enumName)) { | ||
const ancestor = node.getFirstAncestor() | ||
if (ancestor?.isKind(SyntaxKind.JsxExpression)) { | ||
ancestor.replaceWithText(`'${enumMemberValue.slice(1, -1)}'`) | ||
} else { | ||
node.replaceWithText(`'${enumMemberValue.slice(1, -1)}'`) | ||
} | ||
|
||
enumNames.push(enumName) | ||
} | ||
} | ||
} | ||
} | ||
}) |
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.
PropertyAccessExpression
을 찾아 첫 identifier와 마지막 identifier를 찾습니다.
- ex)
Enum1.A.B.C
->Enum1
,C
- 첫 identifier의 symbol과 마지막 identifier의 valueDeclaration을 봅니다.
- 각각 enum 이름과 enum member 이름이 됩니다.
enumMemberValue.slice(1, -1)
으로 값을 대체합니다.
- enum member 이름에 single quote가 있어, 앞뒤 글자를 자릅니다 (
'green-alt'
식으로 되어있음) - JSX에 사용되는 경우 중괄호까지 replace해야 하므로 ancestor를 건드립니다.
기존에 작성했던 |
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.
👍 README도 업데이트해주시면 감사하겠습니다~!
(Ref: https://nextjs.org/docs/app/building-your-application/upgrading/codemods)
packages/bezier-codemod/src/transforms/common/enum-member-to-string-literal.ts
Outdated
Show resolved
Hide resolved
packages/bezier-codemod/src/transforms/progress-bar-string-literal-variants.ts
Outdated
Show resolved
Hide resolved
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.
👍 수고하셨습니다! 마이너 코멘트만 하나 확인 부탁드려요
This PR was opened by the [Changesets release](https://github.com/changesets/action) GitHub action. When you're ready to do a release, you can merge this and the packages will be published to npm automatically. If you're not ready to do a release yet, that's fine, whenever you add more changesets to main, this PR will be updated. # Releases ## @channel.io/[email protected] ### Minor Changes - Implement codemod transform which replaces enum usage with string literal ([#1639](#1639)) by @Dogdriip Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Self Checklist
Related Issue
ProgressBarSize
,ProgressBarVariant
string literal type and deprecate enum #1595Summary
ProgressBarSize
,ProgressBarVariant
string literal type and deprecate enum #1595 에서ProgressBar
의 Size, Variant에 대해 string literal을 사용하고 enum을 deprecate함에 따라, enum 사용을 이에 대응하는 string literal로 변경하는 codemod를 작성합니다.Details
transforms/progress-bar-string-literal-variants.ts
EnumTransforms: Record<string, Record<string, string>>
형식으로 작성합니다.ProgressBarVariant
ProgressBarSize
Breaking change? (Yes/No)
References