Skip to content

Commit

Permalink
fix: handle more cases
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremiah-clothier committed May 23, 2024
1 parent 147edc3 commit a7e5830
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 14 deletions.
11 changes: 11 additions & 0 deletions src/bin/eds-migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ export async function run() {
console.log(` ${migration}`);
});
} else if (name) {
const migrations = listMigrations();
if (!migrations.includes(name)) {
console.warn(
chalk.red(`Migrate: Oops we do not have a migration named "${name}"!`),
);
console.log('Here is a list of migrations available');
listMigrations().forEach((migration) => {
console.log(` ${migration}`);
});
return;
}
await runMigration(name, { isVerbose });
} else {
console.warn(
Expand Down
5 changes: 3 additions & 2 deletions src/bin/migrate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ export default async function runMigration(
const pathToMigration = path.join(MIGRATION_DIR, `${name}.js`);
try {
console.log(`Running the following migration: "${name}"`);
const { default: migration } = await import(pathToMigration);
migration(project);
const module = await import(pathToMigration);

Check failure on line 45 in src/bin/migrate/index.ts

View workflow job for this annotation

GitHub Actions / test

Dynamic imports are only supported when the '--module' flag is set to 'es2020', 'es2022', 'esnext', 'commonjs', 'amd', 'system', 'umd', 'node16', or 'nodenext'.
// This syntax seems odd to need when the code is packaged
module.default.default(project);
} catch (error) {
console.error('Error importing module:', error);
}
Expand Down
38 changes: 38 additions & 0 deletions src/bin/migrate/transforms/rename-jsx-import.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,42 @@ describe('transform', () => {
}
`);
});

it('uses the alias to rename the JsxElement', () => {
const sourceFileText = dedent`
import {Button} from '@chanzuckerberg/eds';
export default function Component() {
return (
<div>
<Button variant="secondary">Something over there</Button>
</div>
)
}
`;
const sourceFile = createTestSourceFile(sourceFileText);

transform({
file: sourceFile,
changes: [
{
oldImportName: 'Button',
newImportName: 'ButtonV2',
alias: 'ButtonAlias',
},
],
});

expect(sourceFile.getText()).toEqual(dedent`
import {ButtonV2 as ButtonAlias} from '@chanzuckerberg/eds';
export default function Component() {
return (
<div>
<ButtonAlias variant="secondary">Something over there</ButtonAlias>
</div>
)
}
`);
});
});
34 changes: 22 additions & 12 deletions src/bin/migrate/transforms/rename-jsx-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ export default function transform({ file, changes }: TransformOptions) {
/**
* Used to keep track of which JSXElements need to be renamed
*/
const appliedChanges: Change[] = [];
const jsxElementsToRename: Array<{
oldName: string;
newName: string;
}> = [];

importsToTransform.forEach((importDeclaration) => {
const namedImports = importDeclaration.getNamedImports();

Expand All @@ -39,15 +43,21 @@ export default function transform({ file, changes }: TransformOptions) {
);

if (changeToApply) {
namedImport.getNameNode;
const jsxElementName =
namedImport.getAliasNode()?.getText() || namedImport.getName();

jsxElementsToRename.push({
oldName: jsxElementName,
newName: changeToApply.alias || changeToApply.newImportName,
});

namedImport.setName(changeToApply.newImportName);
namedImport.getNameNode().rename(changeToApply.newImportName);
if (changeToApply.removeAlias) {
namedImport.removeAliasWithRename();
} else if (changeToApply.alias) {
namedImport.renameAlias(changeToApply.alias);
}
appliedChanges.push(changeToApply);
}
});
});
Expand All @@ -58,25 +68,25 @@ export default function transform({ file, changes }: TransformOptions) {
const openingTagNameNode = openingElement.getTagNameNode();
const closingTagNameNode = closingElement.getTagNameNode();

const appliedChange = appliedChanges.find(
(change) => change.oldImportName === openingTagNameNode.getText(),
const renameInfo = jsxElementsToRename.find(
({ oldName }) => oldName === openingTagNameNode.getText(),
);

if (appliedChange) {
openingTagNameNode.replaceWithText(appliedChange.newImportName);
closingTagNameNode.replaceWithText(appliedChange.newImportName);
if (renameInfo) {
openingTagNameNode.replaceWithText(renameInfo.newName);
closingTagNameNode.replaceWithText(renameInfo.newName);
}
});

file
.getDescendantsOfKind(SyntaxKind.JsxSelfClosingElement)
.forEach((element) => {
const tagNameNode = element.getTagNameNode();
const appliedChange = appliedChanges.find(
(change) => change.oldImportName === tagNameNode.getText(),
const renameInfo = jsxElementsToRename.find(
({ oldName }) => oldName === tagNameNode.getText(),
);
if (appliedChange) {
tagNameNode.replaceWithText(appliedChange.newImportName);
if (renameInfo) {
tagNameNode.replaceWithText(renameInfo.newName);
}
});
}

0 comments on commit a7e5830

Please sign in to comment.