From e0f8e9800524257859571da95c1488802e44b0f6 Mon Sep 17 00:00:00 2001 From: Julian Nymark Date: Mon, 17 Mar 2025 16:47:06 +0100 Subject: [PATCH] :construction: box migration scaffolding --- @navikt/aksel/src/codemod/migrations.ts | 8 +++++ .../transforms/box-to-boxnew/box-to-boxnew.ts | 32 +++++++++++++++++++ .../box-to-boxnew/tests/box.input.js | 10 ++++++ .../box-to-boxnew/tests/box.output.js | 10 ++++++ 4 files changed, 60 insertions(+) create mode 100644 @navikt/aksel/src/codemod/transforms/box-to-boxnew/box-to-boxnew.ts create mode 100644 @navikt/aksel/src/codemod/transforms/box-to-boxnew/tests/box.input.js create mode 100644 @navikt/aksel/src/codemod/transforms/box-to-boxnew/tests/box.output.js diff --git a/@navikt/aksel/src/codemod/migrations.ts b/@navikt/aksel/src/codemod/migrations.ts index 98f25fe1f2..605395838f 100644 --- a/@navikt/aksel/src/codemod/migrations.ts +++ b/@navikt/aksel/src/codemod/migrations.ts @@ -117,6 +117,14 @@ export const migrations: { path: "spacing/token-spacing-js/spacing", }, ], + "box-to-boxnew": [ + { + description: + "Update Box to to BoxNew (future Box) using the new token system", + value: "box-to-boxnew", + path: "box-to-boxnew/box-to-boxnew", + }, + ], }; export function getMigrationPath(str: string) { diff --git a/@navikt/aksel/src/codemod/transforms/box-to-boxnew/box-to-boxnew.ts b/@navikt/aksel/src/codemod/transforms/box-to-boxnew/box-to-boxnew.ts new file mode 100644 index 0000000000..a77386464f --- /dev/null +++ b/@navikt/aksel/src/codemod/transforms/box-to-boxnew/box-to-boxnew.ts @@ -0,0 +1,32 @@ +import type { API, FileInfo } from "jscodeshift"; +import { getLineTerminator } from "../../utils/lineterminator"; +import renameProps from "../../utils/rename-props"; +import { findComponentImport, findJSXElement } from "../spacing/spacing.utils"; + +// to look up in replacements +const deprecationMap = ["background", "borderColor", "shadow"]; + +export default function transformer(file: FileInfo, api: API) { + const j = api.jscodeshift; + const root = j(file.source); + + const toSourceOptions = getLineTerminator(file.source); + + const sourceName = findComponentImport({ + file, + j, + name: "Box", + packageType: "react", + }); + + if (!sourceName) { + return; + } + + for (const prop of deprecationMap) { + const jsx = findJSXElement({ root, j, name: sourceName }); + // conditional renameProps() + } + + return root.toSource(toSourceOptions); +} diff --git a/@navikt/aksel/src/codemod/transforms/box-to-boxnew/tests/box.input.js b/@navikt/aksel/src/codemod/transforms/box-to-boxnew/tests/box.input.js new file mode 100644 index 0000000000..e67edad889 --- /dev/null +++ b/@navikt/aksel/src/codemod/transforms/box-to-boxnew/tests/box.input.js @@ -0,0 +1,10 @@ +export const MyComponent = () => { + return ( <> + + this is a box with props that need migration + + + this is a box without + + ); +} diff --git a/@navikt/aksel/src/codemod/transforms/box-to-boxnew/tests/box.output.js b/@navikt/aksel/src/codemod/transforms/box-to-boxnew/tests/box.output.js new file mode 100644 index 0000000000..abe00256ac --- /dev/null +++ b/@navikt/aksel/src/codemod/transforms/box-to-boxnew/tests/box.output.js @@ -0,0 +1,10 @@ +export const MyComponent = () => { + return ( <> + + this is a box with props that need migration + + + this is a box without + + ); +}