From af02fbe776fe5194a7fa93bcb28c73e9d775ea12 Mon Sep 17 00:00:00 2001 From: winches <329487092@qq.com> Date: Mon, 13 Jan 2025 20:03:42 +0800 Subject: [PATCH] fix: migrate nextui provider change to use regex (#133) --- .../src/helpers/actions/migrate/migrate-common.ts | 14 ++++++++++++++ .../actions/migrate/migrate-nextui-provider.ts | 14 ++++++-------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/packages/codemod/src/helpers/actions/migrate/migrate-common.ts b/packages/codemod/src/helpers/actions/migrate/migrate-common.ts index 9a69548..58d50b7 100644 --- a/packages/codemod/src/helpers/actions/migrate/migrate-common.ts +++ b/packages/codemod/src/helpers/actions/migrate/migrate-common.ts @@ -100,6 +100,20 @@ export function migrateJSXElementName( return dirtyFlag; } +export function migrateByRegex(rawContent: string, match: string, replace: string) { + const regex = new RegExp(match, 'g'); + const dirtyFlag = regex.test(rawContent); + + if (dirtyFlag) { + rawContent = rawContent.replace(regex, replace); + } + + return { + dirtyFlag, + rawContent + }; +} + /** * Migrate the name of the CallExpression * @example diff --git a/packages/codemod/src/helpers/actions/migrate/migrate-nextui-provider.ts b/packages/codemod/src/helpers/actions/migrate/migrate-nextui-provider.ts index c99be60..0a39f23 100644 --- a/packages/codemod/src/helpers/actions/migrate/migrate-nextui-provider.ts +++ b/packages/codemod/src/helpers/actions/migrate/migrate-nextui-provider.ts @@ -1,7 +1,7 @@ import {HEROUI_PROVIDER, NEXTUI_PROVIDER} from '../../../constants/prefix'; import {getStore, writeFileAndUpdateStore} from '../../store'; -import {migrateImportName, migrateJSXElementName} from './migrate-common'; +import {migrateByRegex} from './migrate-common'; /** * Migrate the NextUIProvider to HeroUIProvider will directly write the file @@ -12,22 +12,20 @@ import {migrateImportName, migrateJSXElementName} from './migrate-common'; export function migrateNextuiProvider(paths: string[]) { for (const path of paths) { try { - const parsedContent = getStore(path, 'parsedContent'); + let rawContent = getStore(path, 'rawContent'); let dirtyFlag = false; - if (!parsedContent) { + if (!rawContent) { continue; } // Replace JSX element NextUIProvider with HeroUIProvider - dirtyFlag = migrateJSXElementName(parsedContent, NEXTUI_PROVIDER, HEROUI_PROVIDER); - // Replace NextUIProvider with HeroUIProvider in import statements - if (dirtyFlag) { - migrateImportName(parsedContent, NEXTUI_PROVIDER, HEROUI_PROVIDER); + ({dirtyFlag, rawContent} = migrateByRegex(rawContent, NEXTUI_PROVIDER, HEROUI_PROVIDER)); + if (dirtyFlag) { // Write the modified content back to the file - writeFileAndUpdateStore(path, 'parsedContent', parsedContent); + writeFileAndUpdateStore(path, 'rawContent', rawContent); } // eslint-disable-next-line no-empty } catch {}