Skip to content
This repository has been archived by the owner on Feb 16, 2024. It is now read-only.

Commit

Permalink
fix(parser): parser bugfixing
Browse files Browse the repository at this point in the history
  • Loading branch information
grzpab committed Dec 22, 2023
1 parent 4b6fa5d commit fa87353
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 85 deletions.
83 changes: 52 additions & 31 deletions readme-parser/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const DATA = `
## Description
This is an amazing codemod
This is an amazing codemod which does \`the thing\`
### WARNING
Expand Down Expand Up @@ -40,6 +40,8 @@ http.get<PathParamsType, ReqBodyType>('/resource', (req, res, ctx) => {
});
\`\`\`
### **engine.tsx**
### Before
\`\`\`ts
Expand Down Expand Up @@ -81,7 +83,7 @@ Maybe more...
### Owner
[Intuita](https://github.com/intuita-inc)
[The Author](https://github.com/author)
### Links for more info
Expand All @@ -96,44 +98,45 @@ describe('parse/yaml', function () {
deepEqual(parseResult, {
name: 'Do the thing',
description:
'This is an amazing codemod\n### WARNING\nThis codemod does the thing\n' +
'This is an amazing codemod which does `the thing`\n\n### WARNING\n\nThis codemod does the thing\n' +
'Following the original msw [upgrade guide](https://mswjs.io/docs/migrations/1.x-to-2.x/#imports), ' +
'there are certain imports that changed their location and/or naming. This codemod will adjust your imports to the new location and naming.\n' +
'- `setupWorker` is now imported from `msw/browser`\n' +
'- `rest` from `msw` is now named `http`\n' +
'- `RestHandler` from `msw` is now named `HttpHandler`',
' - `setupWorker` is now imported from `msw/browser`\n' +
' - `rest` from `msw` is now named `http`\n' +
' - `RestHandler` from `msw` is now named `HttpHandler`',
examples:
'### tsconfig.json\n' +
'\n### `tsconfig.json`\n\n' +
'### Before\n\n' +
'```ts\n' +
'```ts\n\n' +
"http.get<ReqBodyType, PathParamsType>('/resource', (req, res, ctx) => {\n" +
" return res(ctx.json({ firstName: 'John' }));\n" +
'});\n' +
'});\n\n' +
'```\n\n' +
'### After\n\n' +
'```ts\n' +
'```ts\n\n' +
"http.get<PathParamsType, ReqBodyType>('/resource', (req, res, ctx) => {\n" +
" return res(ctx.json({ firstName: 'John' }));\n" +
'});\n' +
'});\n\n' +
'```\n\n' +
'### **engine.tsx**\n\n' +
'### Before\n\n' +
'```ts\n' +
'```ts\n\n' +
"http.get<ReqBodyType>('/resource', (req, res, ctx) => {\n" +
" return res(ctx.json({ firstName: 'John' }));\n" +
'});\n' +
'});\n\n' +
'```\n\n' +
'### After\n\n' +
'```ts\n' +
'```ts\n\n' +
"http.get<any, ReqBodyType>('/resource', (req, res, ctx) => {\n" +
" return res(ctx.json({ firstName: 'John' }));\n" +
'});\n' +
'```\n',
applicability: 'MSW >= 1.0.0',
'});\n\n' +
'```',
applicability: '`MSW` >= 1.0.0',
version: '1.0.0',
changeMode: 'assistive',
engine: 'ts-morph',
timeSave: '5 minutes/occurrence\nMaybe more...',
owner: 'Intuita',
owner: 'The Author',
links: 'https://example.com/,https://example1.com/',
});
});
Expand Down Expand Up @@ -166,60 +169,73 @@ describe('parse/yaml', function () {
created-on: ${date.toISOString()}
f_long-description: >-
## Description
\n
This is an amazing codemod
This is an amazing codemod which does \`the thing\`
### WARNING
This codemod does the thing
Following the original msw [upgrade guide](https://mswjs.io/docs/migrations/1.x-to-2.x/#imports), there are certain imports that changed their location and/or naming. This codemod will adjust your imports to the new location and naming.
- \`setupWorker\` is now imported from \`msw/browser\`
- \`rest\` from \`msw\` is now named \`http\`
- \`RestHandler\` from \`msw\` is now named \`HttpHandler\`
- \`setupWorker\` is now imported from \`msw/browser\`
- \`rest\` from \`msw\` is now named \`http\`
- \`RestHandler\` from \`msw\` is now named \`HttpHandler\`
### tsconfig.json
### \`tsconfig.json\`
### Before
\`\`\`ts
http.get<ReqBodyType, PathParamsType>('/resource', (req, res, ctx) => {
return res(ctx.json({ firstName: 'John' }));
});
\`\`\`
### After
\`\`\`ts
http.get<PathParamsType, ReqBodyType>('/resource', (req, res, ctx) => {
return res(ctx.json({ firstName: 'John' }));
});
\`\`\`
### **engine.tsx**
### Before
\`\`\`ts
http.get<ReqBodyType>('/resource', (req, res, ctx) => {
return res(ctx.json({ firstName: 'John' }));
});
\`\`\`
### After
\`\`\`ts
http.get<any, ReqBodyType>('/resource', (req, res, ctx) => {
return res(ctx.json({ firstName: 'John' }));
});
\`\`\`
\`\`\`
f_github-link: https://github.com/intuita-inc/codemod-registry/tree/main/codemods/msw/2/imports
f_vs-code-link: vscode://intuita.intuita-vscode-extension/showCodemod?chd=${vscodeHashDigest}
f_codemod-studio-link: n/a
f_cli-command: intuita msw/2/imports
f_framework: cms/framework/msw.md
f_applicability-criteria: MSW >= 1.0.0
f_verified-codemod: true
f_author: cms/authors/intuita.md
f_applicability-criteria: "\`MSW\` >= 1.0.0"
f_verified-codemod: false
f_author: cms/authors/the-author.md
layout: "[automations].html"
slug: msw-2-imports
title: Do the thing
title: Msw V2 - Do the thing
f_slug-name: msw-2-imports
f_codemod-engine: cms/codemod-engines/ts-morph.md
f_change-mode-2: Assistive
Expand All @@ -229,7 +245,12 @@ f_estimated-time-saving: >-
tags: automations
updated-on: ${date.toISOString()}
published-on: ${date.toISOString()}
seo: n/a
seo:
title: Msw V2 - Do the thing | Intuita Automations
og:title: Msw V2 - Do the thing | Intuita Automations
twitter:title: Msw V2 - Do the thing | Intuita Automations
description: This is an amazing codemod which does \`the thing\`
twitter:card: This is an amazing codemod which does \`the thing\`
`.trim(),
);
});
Expand Down
106 changes: 68 additions & 38 deletions readme-parser/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,37 @@ const noFirstLetterLowerCase = (str: string) =>
const capitalize = (str: string) =>
str[0] ? str[0].toUpperCase() + str.slice(1) : str;

// TODO:
// const getStyledValue = (node) => {};

const getTextFromNode = (
node: RootContent | PhrasingContent | null,
style?: boolean,
style = false,
): string | null => {
if (!node) {
return null;
}

if ('value' in node) {
if (node.type === 'inlineCode') {
return `\`${node.value}\``;
}

return node.value;
}

if ('children' in node) {
let textContent = '';
for (const child of node.children) {
if (!style) {
textContent += getTextFromNode(child);
textContent += getTextFromNode(child, style);
continue;
}

if (child.type === 'listItem') {
textContent += `${getTextFromNode(child, style)}`;
} else if (child.type === 'inlineCode') {
textContent += `\`${getTextFromNode(child, style)}\``;
if (node.type === 'strong') {
textContent += `**${getTextFromNode(child, style)}**`;
} else {
textContent += getTextFromNode(child);
textContent += getTextFromNode(child, style);
}
}

Expand Down Expand Up @@ -143,11 +148,23 @@ const getTextByHeader = (
rc.type === 'heading' &&
rc.depth > heading.depth &&
idx === 0 &&
(child.type === 'text' || child.type === 'inlineCode')
(child.type === 'text' ||
child.type === 'inlineCode' ||
child.type === 'strong')
) {
return `${'#'.repeat(rc.depth)} ${
child.value
}${delimiter}`;
const conditionalDelimiter = delimiter.repeat(
isDescription ? 2 : 1,
);
return `${conditionalDelimiter}${'#'.repeat(
rc.depth,
)} ${getTextFromNode(
child,
true,
)}${conditionalDelimiter}`;
}

if (child.type === 'inlineCode') {
return `\`${child.value}\``;
}

if (child.type === 'text') {
Expand All @@ -161,7 +178,7 @@ const getTextByHeader = (

if (child.type === 'listItem') {
if (isDescription) {
return `- ${getTextFromNode(
return ` - ${getTextFromNode(
child.children[0] ?? null,
true,
)}${delimiter}`;
Expand Down Expand Up @@ -212,9 +229,7 @@ const getTextByHeader = (

if ('value' in rc) {
if (rc.type === 'code') {
textParts.push(
`\n\`\`\`${rc.lang}\n${rc.value}\n\`\`\`${delimiter}\n`,
);
textParts.push(`\n\`\`\`${rc.lang}\n\n${rc.value}\n\n\`\`\`\n`);
} else {
textParts.push(`${rc.value}${delimiter}`);
}
Expand All @@ -223,7 +238,7 @@ const getTextByHeader = (

// Trim last el to remove delimiter
textParts[textParts.length - 1] =
textParts.at(-1)?.replace(/(\W)$/, '') ?? '';
textParts.at(-1)?.replace(new RegExp(`${delimiter}$`), '') ?? '';

return textParts.join('');
};
Expand Down Expand Up @@ -363,11 +378,13 @@ export const convertToYaml = (

let slug: string | null = null;
let framework: string | null = null;
let frameworkVersion: string | null = null;
let cliCommand: string | null = null;
let cleanPath: string | null = null;
let codemodName: string | null = null;
if (path) {
cleanPath = path.split('/').slice(0, -1).join('/');
const splitPath = path.split('/');
cleanPath = splitPath.slice(0, -1).join('/');

const parts = __dirname.split('/');
const pivot = parts.indexOf('readme-parser');
Expand All @@ -376,7 +393,8 @@ export const convertToYaml = (
cleanPath,
);

framework = path.split('/').at(1) ?? null;
framework = splitPath.at(1) ?? null;
frameworkVersion = splitPath.at(2) ?? null;

try {
const config = readFileSync(
Expand All @@ -401,49 +419,61 @@ export const convertToYaml = (
.digest('base64url');
}

let titleWithVersion = title;
if (framework) {
if (frameworkVersion) {
titleWithVersion = `${framework} V${frameworkVersion} - ${title}`;
} else {
titleWithVersion = `${framework} - ${title}`;
}
}
titleWithVersion = capitalize(titleWithVersion);

const shortDescription = description.split('\n').at(0);

const res = `
created-on: ${new Date().toISOString()}
f_long-description: >-
## Description
\n
${description.replace(/\n/g, '\n ')}
\n
${examples.replace(/\n/g, '\n ')}
f_github-link: ${
${examples.replace(/\n/g, '\n ')}${
path
? `https://github.com/intuita-inc/codemod-registry/tree/main/${cleanPath}`
: 'n/a'
}
f_vs-code-link: ${
? `\nf_github-link: https://github.com/intuita-inc/codemod-registry/tree/main/${cleanPath}`
: ''
}${
vscodeHashDigest
? `vscode://intuita.intuita-vscode-extension/showCodemod?chd=${vscodeHashDigest}`
: 'n/a'
}
f_codemod-studio-link: n/a
f_cli-command: ${cliCommand ?? 'n/a'}
f_framework: ${framework ? `cms/framework/${framework}.md` : 'n/a'}
f_applicability-criteria: ${applicability}
? `\nf_vs-code-link: vscode://intuita.intuita-vscode-extension/showCodemod?chd=${vscodeHashDigest}`
: ''
}${cliCommand ? `\nf_cli-command: ${cliCommand}` : ''}${
framework ? `\nf_framework: cms/framework/${framework}.md` : ''
}
f_applicability-criteria: "${applicability}"
f_verified-codemod: ${owner === 'Intuita' ? 'true' : 'false'}
f_author: ${
owner === 'Intuita'
? 'cms/authors/intuita.md'
: `cms/authors/${codemodName?.split('/')?.[0] ?? ''}.md`
: `cms/authors/${owner?.toLowerCase().replace(/ /g, '-') ?? ''}.md`
}
layout: "[automations].html"
slug: ${slug ?? 'n/a'}
title: ${title}
f_slug-name: ${slug ?? 'n/a'}
layout: "[automations].html"${slug ? `\nslug: ${slug}` : ''}
title: ${capitalize(titleWithVersion)}${slug ? `\nf_slug-name: ${slug}` : ''}
f_codemod-engine: cms/codemod-engines/${engine}.md
f_change-mode-2: ${capitalize(changeMode)}
f_estimated-time-saving: ${
timeSave.includes('\n')
? `>-\n ${timeSave.replace(/\n/, '\n ')}`
: timeSave
: `"${timeSave}"`
}
tags: automations
updated-on: ${new Date().toISOString()}
published-on: ${new Date().toISOString()}
seo: n/a
seo:
title: ${titleWithVersion} | Intuita Automations
og:title: ${titleWithVersion} | Intuita Automations
twitter:title: ${titleWithVersion} | Intuita Automations
description: ${shortDescription}
twitter:card: ${shortDescription}
`.trim();

return res;
Expand Down
Loading

0 comments on commit fa87353

Please sign in to comment.