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 3 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
78 changes: 66 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,60 @@ 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 */}"
`);
});

it('fixMdxComments keeps html comments in codeblocks', () => {
expect(
fixMdxComments(dedent`
# Hello

~~~html
<!-- This is a comment -->
~~~

~~~html
<!-- This is a comment -->
~~~
ndelangen marked this conversation as resolved.
Show resolved Hide resolved

and this is not

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

~~~html
<!-- This is a comment -->
~~~

~~~html
<!-- This is a comment -->
~~~

and this is not

{/* This is another comment */}"
`);
});
35 changes: 26 additions & 9 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,11 +5,28 @@ 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;
const MDX1_CODEBLOCK = /(?:\n~~~(?:\n|.)*?\n~~~)|(?:\n```(?:\n|.)*?\n```)/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) => {
const codeblocks = mdx.matchAll(MDX1_CODEBLOCK);

// separate the mdx into sections without codeblocks & replace html comments NOT in codeblocks
const sections = mdx
.split(MDX1_CODEBLOCK)
.map((v) => v.replace(MDX1_COMMENT, (original, group) => `{/*${group}*/}`));

// interleave the original codeblocks with the replaced sections
return sections.reduce((acc, item, i) => {
const next = codeblocks.next();
return next.done ? acc + item : acc + item + next.value[0];
}, '');
};

const logger = console;
Expand Down Expand Up @@ -48,10 +65,10 @@ export const mdx1to2: Fix<Mdx1to2Options> = {
},

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