-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Optional codemod for migrating enums (#2543)
- Loading branch information
1 parent
601b44d
commit 3bf7563
Showing
6 changed files
with
845 additions
and
33 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
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
49 changes: 49 additions & 0 deletions
49
packages/codemod/transformations/core/v2-to-v3/enums/Enums-migration.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 { TransformationContext } from "../../../../types"; | ||
import { getCoreImportsForFile, getPropValue, setPropValue, wrap } from "../../../../src/utils"; | ||
import enumToStringMapping from "./enumMappings.json"; | ||
import { NEW_CORE_IMPORT_PATH } from "../../../../src/consts"; | ||
|
||
const enumToString: Record<string, string> = enumToStringMapping; | ||
|
||
/** | ||
* Replace enums with string equivalent | ||
*/ | ||
function transform({ j, root }: TransformationContext) { | ||
// Since it runs after the imports are updated need to get the new import path | ||
const coreImports = getCoreImportsForFile(root, NEW_CORE_IMPORT_PATH); | ||
|
||
const importedComponents = coreImports | ||
.find(j.ImportSpecifier) | ||
.nodes() | ||
.map(importSpecifier => { | ||
if (importSpecifier.local && importSpecifier.local.name) { | ||
return importSpecifier.local.name; | ||
} | ||
return null; | ||
}) | ||
.filter(Boolean); | ||
|
||
const allElements = root.find(j.JSXElement).nodes(); | ||
|
||
const elements = allElements.filter(path => { | ||
const openingElement = path.openingElement; | ||
const elementName = openingElement.name.type === "JSXIdentifier" ? openingElement.name.name : null; | ||
return elementName && importedComponents.includes(elementName); | ||
}); | ||
|
||
if (!elements.length) return; | ||
elements.forEach(elementPath => { | ||
const props = j(elementPath).find(j.JSXOpeningElement).find(j.JSXAttribute); | ||
props.forEach(prop => { | ||
const propValue = getPropValue(j, prop.node) as string; | ||
if (enumToString[propValue]) { | ||
setPropValue(j, prop, { | ||
value: enumToString[propValue], | ||
type: "Literal" | ||
}); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
export default wrap(transform); |
41 changes: 41 additions & 0 deletions
41
packages/codemod/transformations/core/v2-to-v3/enums/__tests__/Enums-migration.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,41 @@ | ||
import transform from "../Enums-migration"; | ||
import { defineInlineTest } from "jscodeshift/src/testUtils"; | ||
|
||
function prependImport(source: string): string { | ||
return ` | ||
import { Button } from "@vibe/core"; | ||
${source} | ||
`; | ||
} | ||
|
||
describe("Enums migration", () => { | ||
defineInlineTest( | ||
transform, | ||
{}, | ||
prependImport(`<Button kind={Button.kinds.PRIMARY}/>`), | ||
prependImport(`<Button kind="primary"/>`), | ||
"should update enum" | ||
); | ||
|
||
defineInlineTest( | ||
transform, | ||
{}, | ||
prependImport(`<Button kind={Button.kinds.SECONDARY} size={Button.sizes.SMALL} />`), | ||
prependImport(`<Button kind="secondary" size="small" />`), | ||
"should update enum" | ||
); | ||
|
||
defineInlineTest( | ||
transform, | ||
{}, | ||
` | ||
import { Button } from "another-library"; | ||
<Button size={Button.sizes.SMALL} /> | ||
`, | ||
` | ||
import { Button } from "another-library"; | ||
<Button size={Button.sizes.SMALL} /> | ||
`, | ||
"should not change component is not imported from vibe" | ||
); | ||
}); |
Oops, something went wrong.