Skip to content

Commit

Permalink
fix: migrate nextui provider change to use regex (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
winchesHe authored Jan 13, 2025
1 parent b3bf4a8 commit af02fbe
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
14 changes: 14 additions & 0 deletions packages/codemod/src/helpers/actions/migrate/migrate-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 {}
Expand Down

0 comments on commit af02fbe

Please sign in to comment.