From 8a10b4dc679712d0f4eaf7ca0a23e28472196059 Mon Sep 17 00:00:00 2001 From: Gundu Mahidhar Reddy Date: Mon, 2 Dec 2024 12:34:01 +0800 Subject: [PATCH] [CCUBE-1628][MAHI]Update codemod script to cater for size prop mapping --- codemods/migrate-text-list/data.ts | 16 +++++++++++++ codemods/migrate-text-list/index.ts | 24 ++++++++++++++++++++ codemods/run-codemod.ts | 3 +++ tests/codemod/migrate-text-list/test-data.ts | 2 ++ 4 files changed, 45 insertions(+) create mode 100644 codemods/migrate-text-list/data.ts diff --git a/codemods/migrate-text-list/data.ts b/codemods/migrate-text-list/data.ts new file mode 100644 index 000000000..acff08853 --- /dev/null +++ b/codemods/migrate-text-list/data.ts @@ -0,0 +1,16 @@ +export const sizePropMapping = { + D1: "header-xxl", + D2: "header-xl", + D3: "header-md", + D4: "header-sm", + H1: "header-lg", + H2: "header-md", + H3: "header-sm", + H4: "header-xs", + H5: "body-md", + H6: "body-sm", + DBody: "header-sm", + Body: "body-baseline", + BodySmall: "body-md", + XSmall: "body-xs", +}; diff --git a/codemods/migrate-text-list/index.ts b/codemods/migrate-text-list/index.ts index 1416b4ff8..71bc09dfc 100644 --- a/codemods/migrate-text-list/index.ts +++ b/codemods/migrate-text-list/index.ts @@ -1,4 +1,5 @@ import { API, FileInfo, JSCodeshift } from "jscodeshift"; +import { sizePropMapping } from "./data"; // ======= Constants ======= // @@ -72,6 +73,29 @@ export default function transformer(file: FileInfo, api: API) { path.node.object.name = JSX_IDENTIFIERS.TEXT_LIST; } }); + + source + .find(j.JSXOpeningElement, { + name: { name: JSX_IDENTIFIERS.TEXT_LIST }, + }) + .forEach((path) => { + const attributes = path.node.attributes; + + attributes?.forEach((attribute) => { + if ( + j.JSXAttribute.check(attribute) && + attribute.name.name === "size" && + j.StringLiteral.check(attribute.value) + ) { + const sizeValue = attribute.value.value; + if (sizePropMapping[sizeValue]) { + attribute.value = j.literal( + sizePropMapping[sizeValue] + ); + } + } + }); + }); } return source.toSource(); diff --git a/codemods/run-codemod.ts b/codemods/run-codemod.ts index c6af34f98..0fd735e90 100644 --- a/codemods/run-codemod.ts +++ b/codemods/run-codemod.ts @@ -14,6 +14,7 @@ enum Codemod { MigrateLayout = "migrate-layout", MigrateMediaQuery = "migrate-media-query", MigrateText = "migrate-text", + MigrateTextList = "migrate-text-list", } enum Theme { @@ -32,6 +33,8 @@ const CodemodDescriptions: { [key in Codemod]: string } = { [Codemod.MigrateMediaQuery]: "Replace V2 media queries with new Breakpoint tokens", [Codemod.MigrateText]: "Replace V2_Text with new Typography components", + [Codemod.MigrateTextList]: + "Replace V2_TextList with new Textlist components", }; const TargetDirectoryPaths = { diff --git a/tests/codemod/migrate-text-list/test-data.ts b/tests/codemod/migrate-text-list/test-data.ts index fd05f3ab6..66fba5964 100644 --- a/tests/codemod/migrate-text-list/test-data.ts +++ b/tests/codemod/migrate-text-list/test-data.ts @@ -8,6 +8,7 @@ import { V2_TextList } from "@lifesg/react-design-system/v2_text-list";
  • Second
  • Third
  • ; +; `; @@ -22,6 +23,7 @@ import { TextList } from "@lifesg/react-design-system/text-list";
  • Second
  • Third
  • ; +; `;