Skip to content

Commit

Permalink
Make postprocessing sync again (#17)
Browse files Browse the repository at this point in the history
* Add prettier/sync

* Make postprocessing sync again
  • Loading branch information
markerikson authored Dec 20, 2024
1 parent d3f2cef commit 5c375b1
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 54 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
},
"dependencies": {
"@microsoft/tsdoc": "^0.15.0",
"@prettier/sync": "^0.5.2",
"@types/estree": "^1.0.5",
"@types/unist": "^3.0.3",
"mdast-util-mdx-jsx": "^3.1.3",
Expand Down
14 changes: 5 additions & 9 deletions src/transpileCodeblocks/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type PostProcessor = (
files: VirtualFiles,
parentFile?: string,
defaultProcessor?: PostProcessor
) => Promise<VirtualFiles>;
) => VirtualFiles;

export interface TranspileCodeblocksSettings {
compilerSettings: CompilerSettings;
Expand Down Expand Up @@ -212,15 +212,15 @@ ${lines.slice(Math.max(0, diagnostic.line - 5), diagnostic.line + 6).join('\n')}
};
};

export async function defaultAssembleReplacementNodes(
export function defaultAssembleReplacementNodes(
node: CodeNode,
file: VFile,
virtualFolder: string,
virtualFiles: Record<string, VirtualFile>,
transpilationResult: Record<string, TranspiledFile>,
postProcessTs: PostProcessor,
postProcessTranspiledJs: PostProcessor
): Promise<Node[]> {
): Node[] {
return [
{
type: 'mdxJsxFlowElement',
Expand Down Expand Up @@ -334,11 +334,7 @@ export async function defaultAssembleReplacementNodes(
{
...node,
value: rearrangeFiles(
await postProcessTs(
virtualFiles,
file.path,
defaultPostProcessTs
),
postProcessTs(virtualFiles, file.path, defaultPostProcessTs),
virtualFolder
),
} satisfies CodeNode as any,
Expand All @@ -359,7 +355,7 @@ export async function defaultAssembleReplacementNodes(
),
}),
value: rearrangeFiles(
await postProcessTranspiledJs(
postProcessTranspiledJs(
transpilationResult,
file.path,
defaultPostProcessTranspiledJs
Expand Down
79 changes: 34 additions & 45 deletions src/transpileCodeblocks/postProcessing.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,50 @@
import type { VirtualFiles } from './plugin.js';
import prettier from 'prettier';
import type { Options as PrettierOptions } from 'prettier';
import prettierSync from '@prettier/sync';

export async function defaultPostProcessTs(
export function defaultPostProcessTs(
files: VirtualFiles,
parentFile?: string
): Promise<VirtualFiles> {
): VirtualFiles {
return fromEntries(
await Promise.all(
Object.entries(files).map(async ([name, file]) => {
const prettyCode = await prettify(file.code, name, parentFile || name);
Object.entries(files).map(([name, file]) => {
const prettyCode = prettify(file.code, name, parentFile || name);

return [
name,
{
...file,
code: prettyCode.trim(),
},
];
})
)
return [
name,
{
...file,
code: prettyCode.trim(),
},
];
})
);
}

export async function defaultPostProcessTranspiledJs(
export function defaultPostProcessTranspiledJs(
files: VirtualFiles,
parentFile?: string
): Promise<VirtualFiles> {
): VirtualFiles {
return fromEntries(
await Promise.all(
Object.entries(files).map(async ([name, file]) => {
const mangledCode = file.code.replace(
/(\n\s*|)\/\/ (@ts-ignore|@ts-expect-error).*$/gm,
''
);
const prettyCode = await prettify(
mangledCode,
name,
parentFile || name
);
Object.entries(files).map(([name, file]) => {
const mangledCode = file.code.replace(
/(\n\s*|)\/\/ (@ts-ignore|@ts-expect-error).*$/gm,
''
);
const prettyCode = prettify(mangledCode, name, parentFile || name);

return [
name.replace(/.t(sx?)$/, '.j$1'),
{
...file,
code: prettyCode.trim(),
},
];
})
)
return [
name.replace(/.t(sx?)$/, '.j$1'),
{
...file,
code: prettyCode.trim(),
},
];
})
);
}

let lastConfig: prettier.Options | null;
let lastConfig: PrettierOptions | null;
let lastParentFile: string;

/**
Expand All @@ -60,21 +53,17 @@ let lastParentFile: string;
* @param {string} fileName
* @param {string} parentFile
*/
async function prettify(
sourceCode: string,
fileName: string,
parentFile: string
) {
function prettify(sourceCode: string, fileName: string, parentFile: string) {
if (lastParentFile !== parentFile) {
lastConfig = await prettier.resolveConfig(parentFile);
lastConfig = prettierSync.resolveConfig(parentFile);
}
if (!lastConfig) {
console.error(
`no prettier config found for ${parentFile}, skipping prettier step`
);
return sourceCode;
}
return prettier.format(sourceCode, {
return prettierSync.format(sourceCode, {
...lastConfig,
filepath: fileName,
});
Expand Down
19 changes: 19 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,17 @@ __metadata:
languageName: node
linkType: hard

"@prettier/sync@npm:^0.5.2":
version: 0.5.2
resolution: "@prettier/sync@npm:0.5.2"
dependencies:
make-synchronized: "npm:^0.2.8"
peerDependencies:
prettier: "*"
checksum: 10/172cdc62f4103b022f8e8d0a63839350d97bc51468ea476594bce651c2cda311e4810417f16a3c967941a8493a68468a5df27beea4c85eaeaa37e84be3acf399
languageName: node
linkType: hard

"@rollup/rollup-android-arm-eabi@npm:4.21.1":
version: 4.21.1
resolution: "@rollup/rollup-android-arm-eabi@npm:4.21.1"
Expand Down Expand Up @@ -2796,6 +2807,13 @@ __metadata:
languageName: node
linkType: hard

"make-synchronized@npm:^0.2.8":
version: 0.2.9
resolution: "make-synchronized@npm:0.2.9"
checksum: 10/d12c540719641f05f4e40bae5625b7e5ac9dded364e7f97ef3fd8baa1bcbd016f873c5281e4719e321a612635accf0a9e2c07a37d215240b549b99ed0d8a1b3f
languageName: node
linkType: hard

"mdast-util-from-markdown@npm:^2.0.0":
version: 2.0.1
resolution: "mdast-util-from-markdown@npm:2.0.1"
Expand Down Expand Up @@ -4108,6 +4126,7 @@ __metadata:
resolution: "remark-typescript-tools@workspace:."
dependencies:
"@microsoft/tsdoc": "npm:^0.15.0"
"@prettier/sync": "npm:^0.5.2"
"@types/estree": "npm:^1.0.5"
"@types/node": "npm:^22.5.4"
"@types/react": "npm:^18.3.5"
Expand Down

0 comments on commit 5c375b1

Please sign in to comment.