-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement codemod transform which replaces enum usage with string lit…
…eral (#1639)
- Loading branch information
Showing
11 changed files
with
302 additions
and
0 deletions.
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 | ||
--- | ||
|
||
Implement codemod transform which replaces enum usage with string literal |
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
65 changes: 65 additions & 0 deletions
65
packages/bezier-codemod/src/transforms/enum-member-to-string-literal.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,65 @@ | ||
import { | ||
Node, | ||
type SourceFile, | ||
SyntaxKind, | ||
} from 'ts-morph' | ||
|
||
type EnumTransforms = Record<string, Record<string, string>> | ||
|
||
function transformEnumMemberToStringLiteral(sourceFile: SourceFile, enumTransforms: EnumTransforms) { | ||
const transformedEnumNames: string[] = [] | ||
|
||
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 memberSymbol = lastIdentifier.getSymbol() | ||
const memberValueDeclaration = memberSymbol?.getValueDeclaration() | ||
|
||
if (Node.isEnumMember(memberValueDeclaration)) { | ||
const enumName = declarationSymbol?.getName() | ||
const enumMember = memberSymbol?.getName() | ||
|
||
if (enumName && enumMember) { | ||
const newEnumMemberValue = enumTransforms[enumName][enumMember] | ||
const ancestor = node.getFirstAncestor() | ||
if (ancestor?.isKind(SyntaxKind.JsxExpression)) { | ||
ancestor.replaceWithText(`'${newEnumMemberValue}'`) | ||
} else { | ||
node.replaceWithText(`'${newEnumMemberValue}'`) | ||
} | ||
|
||
transformedEnumNames.push(enumName) | ||
} | ||
} | ||
} | ||
} | ||
}) | ||
|
||
if (transformedEnumNames.length > 0) { | ||
sourceFile.fixUnusedIdentifiers() | ||
return true | ||
} | ||
|
||
return undefined | ||
} | ||
|
||
function enumMemberToStringLiteral(sourceFile: SourceFile): true | void { | ||
const enumTransforms: EnumTransforms = { | ||
ProgressBarSize: { | ||
M: 'm', | ||
S: 's', | ||
}, | ||
ProgressBarVariant: { | ||
Green: 'green', | ||
GreenAlt: 'green-alt', | ||
Monochrome: 'monochrome', | ||
}, | ||
} | ||
return transformEnumMemberToStringLiteral(sourceFile, enumTransforms) | ||
} | ||
|
||
export default enumMemberToStringLiteral |
49 changes: 49 additions & 0 deletions
49
.../src/transforms/tests/enum-member-to-string-literal/enum-member-to-string-literal.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,49 @@ | ||
import fs from 'fs' | ||
import path from 'path' | ||
|
||
import project from '../../../project.js' | ||
import enumMemberToStringLiteral from '../../enum-member-to-string-literal.js' | ||
|
||
describe('enumMemberToStringLiteral', () => { | ||
it('should transform enum members to string literals correctly', () => { | ||
const inputPath = path.join(__dirname, 'fixtures', 'input1.tsx') | ||
const outputPath = path.join(__dirname, 'fixtures', 'output1.tsx') | ||
|
||
const inputCode = fs.readFileSync(inputPath, 'utf-8') | ||
const outputCode = fs.readFileSync(outputPath, 'utf-8') | ||
const sourceFile = project.createSourceFile('test.tsx', inputCode, { overwrite: true }) | ||
|
||
const isMigrated = enumMemberToStringLiteral(sourceFile) | ||
|
||
expect(sourceFile.getFullText()).toBe(outputCode) | ||
expect(isMigrated).toBe(true) | ||
}) | ||
|
||
it('should transform enum members to string literals correctly with type imports', () => { | ||
const inputPath = path.join(__dirname, 'fixtures', 'input2.tsx') | ||
const outputPath = path.join(__dirname, 'fixtures', 'output2.tsx') | ||
|
||
const inputCode = fs.readFileSync(inputPath, 'utf-8') | ||
const outputCode = fs.readFileSync(outputPath, 'utf-8') | ||
const sourceFile = project.createSourceFile('test.tsx', inputCode, { overwrite: true }) | ||
|
||
const isMigrated = enumMemberToStringLiteral(sourceFile) | ||
|
||
expect(sourceFile.getFullText()).toBe(outputCode) | ||
expect(isMigrated).toBe(true) | ||
}) | ||
|
||
it('should transform enum members to string literals correctly with mixed imports', () => { | ||
const inputPath = path.join(__dirname, 'fixtures', 'input3.tsx') | ||
const outputPath = path.join(__dirname, 'fixtures', 'output3.tsx') | ||
|
||
const inputCode = fs.readFileSync(inputPath, 'utf-8') | ||
const outputCode = fs.readFileSync(outputPath, 'utf-8') | ||
const sourceFile = project.createSourceFile('test.tsx', inputCode, { overwrite: true }) | ||
|
||
const isMigrated = enumMemberToStringLiteral(sourceFile) | ||
|
||
expect(sourceFile.getFullText()).toBe(outputCode) | ||
expect(isMigrated).toBe(true) | ||
}) | ||
}) |
17 changes: 17 additions & 0 deletions
17
...ges/bezier-codemod/src/transforms/tests/enum-member-to-string-literal/fixtures/input1.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,17 @@ | ||
import React from 'react' | ||
import { ProgressBar, ProgressBarSize, ProgressBarVariant } from '@channel.io/bezier-react' | ||
|
||
export default function UploadProgress({ | ||
uploadProgressPercentage, | ||
}: { | ||
uploadProgressPercentage: number | ||
}) { | ||
return ( | ||
<ProgressBar | ||
width='100%' | ||
size={ProgressBarSize.M} | ||
variant={ProgressBarVariant.GreenAlt} | ||
value={uploadProgressPercentage / 100} | ||
/> | ||
) | ||
} |
19 changes: 19 additions & 0 deletions
19
...ges/bezier-codemod/src/transforms/tests/enum-member-to-string-literal/fixtures/input2.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,19 @@ | ||
import React from 'react' | ||
import { ProgressBar, type ProgressBarSize, ProgressBarVariant } from '@channel.io/bezier-react' | ||
|
||
export default function UploadProgress({ | ||
uploadProgressPercentage, | ||
size, | ||
}: { | ||
uploadProgressPercentage: number | ||
size: ProgressBarSize | ||
}) { | ||
return ( | ||
<ProgressBar | ||
width='100%' | ||
size={size} | ||
variant={ProgressBarVariant.GreenAlt} | ||
value={uploadProgressPercentage / 100} | ||
/> | ||
) | ||
} |
36 changes: 36 additions & 0 deletions
36
...ges/bezier-codemod/src/transforms/tests/enum-member-to-string-literal/fixtures/input3.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,36 @@ | ||
import React from 'react' | ||
import { ProgressBar, ProgressBarSize, ProgressBarVariant } from '@channel.io/bezier-react' | ||
|
||
export default function UploadProgress({ | ||
uploadProgressPercentage, | ||
variant, | ||
}: { | ||
uploadProgressPercentage: number | ||
variant: ProgressBarVariant | ||
}) { | ||
if (variant === ProgressBarVariant.Monochrome) { | ||
return ( | ||
<> | ||
<p>Monochrome ProgressBar</p> | ||
<ProgressBar | ||
width='100%' | ||
size={ProgressBarSize.M} | ||
variant={ProgressBarVariant.Monochrome} | ||
value={uploadProgressPercentage / 100} | ||
/> | ||
</> | ||
) | ||
} | ||
|
||
return ( | ||
<> | ||
<p>Colored ProgressBar</p> | ||
<ProgressBar | ||
width='100%' | ||
size={ProgressBarSize.M} | ||
variant={variant} | ||
value={uploadProgressPercentage / 100} | ||
/> | ||
</> | ||
) | ||
} |
17 changes: 17 additions & 0 deletions
17
...es/bezier-codemod/src/transforms/tests/enum-member-to-string-literal/fixtures/output1.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,17 @@ | ||
import React from 'react' | ||
import { ProgressBar } from '@channel.io/bezier-react' | ||
|
||
export default function UploadProgress({ | ||
uploadProgressPercentage, | ||
}: { | ||
uploadProgressPercentage: number | ||
}) { | ||
return ( | ||
<ProgressBar | ||
width='100%' | ||
size='m' | ||
variant='green-alt' | ||
value={uploadProgressPercentage / 100} | ||
/> | ||
) | ||
} |
19 changes: 19 additions & 0 deletions
19
...es/bezier-codemod/src/transforms/tests/enum-member-to-string-literal/fixtures/output2.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,19 @@ | ||
import React from 'react' | ||
import { ProgressBar, type ProgressBarSize } from '@channel.io/bezier-react' | ||
|
||
export default function UploadProgress({ | ||
uploadProgressPercentage, | ||
size, | ||
}: { | ||
uploadProgressPercentage: number | ||
size: ProgressBarSize | ||
}) { | ||
return ( | ||
<ProgressBar | ||
width='100%' | ||
size={size} | ||
variant='green-alt' | ||
value={uploadProgressPercentage / 100} | ||
/> | ||
) | ||
} |
36 changes: 36 additions & 0 deletions
36
...es/bezier-codemod/src/transforms/tests/enum-member-to-string-literal/fixtures/output3.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,36 @@ | ||
import React from 'react' | ||
import { ProgressBar, ProgressBarVariant } from '@channel.io/bezier-react' | ||
|
||
export default function UploadProgress({ | ||
uploadProgressPercentage, | ||
variant, | ||
}: { | ||
uploadProgressPercentage: number | ||
variant: ProgressBarVariant | ||
}) { | ||
if (variant === 'monochrome') { | ||
return ( | ||
<> | ||
<p>Monochrome ProgressBar</p> | ||
<ProgressBar | ||
width='100%' | ||
size='m' | ||
variant='monochrome' | ||
value={uploadProgressPercentage / 100} | ||
/> | ||
</> | ||
) | ||
} | ||
|
||
return ( | ||
<> | ||
<p>Colored ProgressBar</p> | ||
<ProgressBar | ||
width='100%' | ||
size='m' | ||
variant={variant} | ||
value={uploadProgressPercentage / 100} | ||
/> | ||
</> | ||
) | ||
} |