Skip to content

Commit

Permalink
updated codemod to match only GuidanceBlock (#4922)
Browse files Browse the repository at this point in the history
* updated codemod to match only GuidanceBlock

* Delete .changeset/shy-cats-attend.md

---------

Co-authored-by: Geoffrey Chong <gyf.chong@gmail.com>
  • Loading branch information
gyfchong and Geoffrey Chong authored Aug 13, 2024
1 parent 1158d54 commit 924e462
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/thirty-shoes-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kaizen/components": patch
---

Fixed GuidanceBlock codemod to include a change to import statements
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@ import { transformSource, printAst } from "../utils"
import { transformGuidanceBlockVariantProp } from "./transformGuidanceBlockVariantProp"

describe("transformGuidanceBlockVariantProp", () => {
it("updates the import statement for GuidanceBlock to the new family version", () => {
const inputAst = parseJsx(`
import { GuidanceBlock } from "@kaizen/components"
import { Card } from "@kaizen/components"
`)
const outputAst = parseJsx(`
import { GuidanceBlock } from "@kaizen/components/v2/containers"
import { Card } from "@kaizen/components"
`)
const transformed = transformSource({
sourceFile: inputAst,
astTransformer: transformGuidanceBlockVariantProp,
tagName: "GuidanceBlock",
})
expect(transformed).toBe(printAst(outputAst))
})

it("removes all instances of `positive`, `negative`, `informative`, `cautionary`, `assertive`", () => {
const inputAst = parseJsx(`
const TestComponent = () => <GuidanceBlock variant="positive" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ export const transformGuidanceBlockVariantProp =
(context: ts.TransformationContext, tagName: string) =>
(rootNode: ts.Node): ts.Node => {
function visit(node: ts.Node): ts.Node {
if (ts.isImportDeclaration(node) && node.getText().includes(tagName)) {
const newModuleSpecifier = ts.factory.createStringLiteral(
"@kaizen/components/v2/containers"
)
return ts.factory.updateImportDeclaration(
node,
node.modifiers,
node.importClause,
newModuleSpecifier,
node.attributes
)
}

if (ts.isJsxSelfClosingElement(node)) {
if (node.tagName.getText() === tagName) {
const newAttributes = node.attributes.properties.reduce<
Expand Down

0 comments on commit 924e462

Please sign in to comment.