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

Implement codemod transform which replaces enum usage with string literal #1639

Merged
merged 13 commits into from
Sep 27, 2023

Conversation

Dogdriip
Copy link
Contributor

@Dogdriip Dogdriip commented Sep 18, 2023

Self Checklist

  • I wrote a PR title in English and added an appropriate label to the PR.
  • I wrote the commit message in English and to follow the Conventional Commits specification.
  • I added the changeset about the changes that needed to be released. (or didn't have to)
  • I wrote or updated documentation related to the changes. (or didn't have to)
  • I wrote or updated tests related to the changes. (or didn't have to)
  • I tested the changes in various browsers. (or didn't have to)
    • Windows: Chrome, Edge, (Optional) Firefox
    • macOS: Chrome, Edge, Safari, (Optional) Firefox

Related Issue

Summary

Details

transforms/progress-bar-string-literal-variants.ts

  • 타겟으로 삼을 enum과 치환할 enum value 값을 내부에서 결정해서, enum 사용을 string literal로 replace합니다.
    • EnumTransforms: Record<string, Record<string, string>> 형식으로 작성합니다.
  • 로직 설명은 PR 코멘트로 갈음합니다.
  • 초기 버전에는 아래 두 개 enum에 대해 수행합니다.
    • ProgressBarVariant
    • ProgressBarSize

Breaking change? (Yes/No)

  • No

References

@Dogdriip Dogdriip self-assigned this Sep 18, 2023
@changeset-bot
Copy link

changeset-bot bot commented Sep 18, 2023

🦋 Changeset detected

Latest commit: a67d809

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@channel.io/bezier-codemod Minor

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

@Dogdriip Dogdriip changed the title Implement codemod transform which replaces enum usage with string literal [WIP] Implement codemod transform which replaces enum usage with string literal Sep 18, 2023
@Dogdriip Dogdriip marked this pull request as draft September 18, 2023 10:24
@codecov
Copy link

codecov bot commented Sep 19, 2023

Codecov Report

All modified lines are covered by tests ✅

Comparison is base (cca0db2) 87.23% compared to head (a67d809) 87.23%.

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.
📢 Have feedback on the report? Share it here.

@Dogdriip Dogdriip changed the title [WIP] Implement codemod transform which replaces enum usage with string literal Implement codemod transform which replaces enum usage with string literal Sep 19, 2023
@Dogdriip Dogdriip marked this pull request as ready for review September 19, 2023 09:58
@Dogdriip Dogdriip added the feat Issue or PR related to a new feature label Sep 19, 2023
Copy link
Contributor

@sungik-choi sungik-choi left a comment

Choose a reason for hiding this comment

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

오프라인으로 싱크했지만, 타겟으로 삼은 enum만 string literal로 변환되도록 변경되면 좋겠습니다~!

Comment on lines 10 to 36
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)
}
}
}
}
})
Copy link
Contributor Author

Choose a reason for hiding this comment

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

  1. PropertyAccessExpression을 찾아 첫 identifier와 마지막 identifier를 찾습니다.
  • ex) Enum1.A.B.C -> Enum1, C
  1. 첫 identifier의 symbol과 마지막 identifier의 valueDeclaration을 봅니다.
  • 각각 enum 이름과 enum member 이름이 됩니다.
  1. enumMemberValue.slice(1, -1)으로 값을 대체합니다.
  • enum member 이름에 single quote가 있어, 앞뒤 글자를 자릅니다 ('green-alt' 식으로 되어있음)
  • JSX에 사용되는 경우 중괄호까지 replace해야 하므로 ancestor를 건드립니다.

@Dogdriip
Copy link
Contributor Author

오프라인으로 싱크했지만, 타겟으로 삼은 enum만 string literal로 변환되도록 변경되면 좋겠습니다~!

기존에 작성했던 enum-member-to-string-literal을 common 아래에 넣고, ProgressBarVariant, ProgressBarSize만 변환하는 progress-bar-string-literal-variants transform을 새로 만들었습니다!

Copy link
Contributor

@sungik-choi sungik-choi left a 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)

Copy link
Contributor

@sungik-choi sungik-choi left a comment

Choose a reason for hiding this comment

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

👍 수고하셨습니다! 마이너 코멘트만 하나 확인 부탁드려요

packages/bezier-codemod/README.md Outdated Show resolved Hide resolved
@Dogdriip Dogdriip merged commit 28fa9cc into channel-io:main Sep 27, 2023
5 checks passed
@Dogdriip Dogdriip deleted the feat/enum-to-literal-codemod branch September 27, 2023 05:31
sungik-choi pushed a commit that referenced this pull request Sep 27, 2023
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>
@yangwooseong yangwooseong added the bezier-codemod Issue or PR related to bezier-codemod label Dec 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bezier-codemod Issue or PR related to bezier-codemod feat Issue or PR related to a new feature
Projects
No open projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants