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

add auto-migration for MDX html-type comments to JS-type comments #20349

Merged
merged 5 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
44 changes: 32 additions & 12 deletions code/lib/cli/src/automigrate/fixes/mdx-1-to-2.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
/// <reference types="@types/jest" />;

import { dedent } from 'ts-dedent';
import { fixMdxScript } from './mdx-1-to-2';
import { fixMdxStyleTags, fixMdxComments } from './mdx-1-to-2';

describe('fix', () => {
it('fixes badly-formatted style blocks', () => {
expect(
fixMdxScript(dedent`
it('fixMdxStyleTags fixes badly-formatted style blocks', () => {
expect(
fixMdxStyleTags(dedent`
<style>{\`
.foo {}

.bar {}
\`}</style>
`)
).toEqual(dedent`
).toEqual(dedent`
<style>
{\`
.foo {}
Expand All @@ -22,19 +21,19 @@ describe('fix', () => {
\`}
</style>
`);
});
});

it('fixes multiple style blocks', () => {
expect(
fixMdxScript(dedent`
it('fixMdxStyleTags fixes multiple style blocks', () => {
expect(
fixMdxStyleTags(dedent`
<style>{\`
.foo {}
\`}</style>
<style>{\`
.bar {}
\`}</style>
`)
).toMatchInlineSnapshot(`
).toMatchInlineSnapshot(`
<style>
{\`
.foo {}
Expand All @@ -46,5 +45,26 @@ describe('fix', () => {
\`}
</style>
`);
});
});

it('fixMdxComments fixes all comments', () => {
expect(
fixMdxComments(dedent`
# Hello

<!-- This is a comment -->

and this is not

<!-- This is another comment -->
`)
).toMatchInlineSnapshot(`
"# Hello

{/* This is a comment */}

and this is not

{/* This is another comment */}"
`);
});
47 changes: 36 additions & 11 deletions code/lib/cli/src/automigrate/fixes/mdx-1-to-2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,24 @@ import fse from 'fs-extra';
import globby from 'globby';
import type { Fix } from '../types';

const MDX1_SCRIPT_START = /<style>{`/g;
const MDX1_SCRIPT_END = /`}<\/style>/g;
const MDX1_STYLE_START = /<style>{`/g;
const MDX1_STYLE_END = /`}<\/style>/g;
const MDX1_COMMENT = /<!--(.+)-->/g;

export const fixMdxScript = (mdx: string) => {
return mdx.replace(MDX1_SCRIPT_START, '<style>\n {`').replace(MDX1_SCRIPT_END, ' `}\n</style>');
export const fixMdxStyleTags = (mdx: string) => {
return mdx.replace(MDX1_STYLE_START, '<style>\n {`').replace(MDX1_STYLE_END, ' `}\n</style>');
};

export const fixMdxComments = (mdx: string) => {
return mdx.replace(MDX1_COMMENT, (original, group) => `{/*${group}*/}`);
};

const logger = console;

interface Mdx1to2Options {
storiesMdxFiles: string[];
allMdxFiles: string[];
nonStoriesMdx1Files: string[];
}

/**
Expand All @@ -30,7 +37,13 @@ export const mdx1to2: Fix<Mdx1to2Options> = {

async check() {
const storiesMdxFiles = await globby('./!(node_modules)**/*.(story|stories).mdx');
return storiesMdxFiles.length ? { storiesMdxFiles } : undefined;
const allMdxFiles = await globby('./!(node_modules)**/*.mdx');

const nonStoriesMdx1Files = allMdxFiles.filter((fname) => !storiesMdxFiles.includes(fname));

return storiesMdxFiles.length
? { storiesMdxFiles, allMdxFiles, nonStoriesMdx1Files }
: undefined;
},

prompt({ storiesMdxFiles }) {
Expand All @@ -47,11 +60,23 @@ export const mdx1to2: Fix<Mdx1to2Options> = {
`;
},

async run({ result: { storiesMdxFiles }, dryRun }) {
await Promise.all(
storiesMdxFiles.map(async (fname) => {
async run({ result: { storiesMdxFiles, nonStoriesMdx1Files }, dryRun }) {
await Promise.all([
...storiesMdxFiles.map(async (fname) => {
const contents = await fse.readFile(fname, 'utf-8');
const updated = fixMdxComments(fixMdxStyleTags(contents));
ndelangen marked this conversation as resolved.
Show resolved Hide resolved
if (updated === contents) {
logger.info(`🆗 Unmodified ${basename(fname)}`);
} else {
logger.info(`✅ Modified ${basename(fname)}`);
if (!dryRun) {
await fse.writeFile(fname, updated);
}
}
}),
...nonStoriesMdx1Files.map(async (fname) => {
ndelangen marked this conversation as resolved.
Show resolved Hide resolved
ndelangen marked this conversation as resolved.
Show resolved Hide resolved
const contents = await fse.readFile(fname, 'utf-8');
const updated = fixMdxScript(contents);
const updated = fixMdxComments(contents);
if (updated === contents) {
logger.info(`🆗 Unmodified ${basename(fname)}`);
} else {
Expand All @@ -60,7 +85,7 @@ export const mdx1to2: Fix<Mdx1to2Options> = {
await fse.writeFile(fname, updated);
}
}
})
);
}),
]);
},
};