Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: migrate nextui provider change to use regex #133

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading