Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 7 additions & 2 deletions apps/oxfmt/src-js/cli/worker-proxy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Tinypool from "tinypool";
import { resolvePlugins } from "../libs/prettier";
import type { FormatEmbeddedCodeParam, FormatFileParam } from "../libs/prettier";
import type { Options } from "prettier";

// Worker pool for parallel Prettier formatting
Expand All @@ -20,7 +21,9 @@ export async function formatEmbeddedCode(
tagName: string,
code: string,
): Promise<string> {
return pool!.run({ options, code, tagName }, { name: "formatEmbeddedCode" });
return pool!.run({ options, code, tagName } satisfies FormatEmbeddedCodeParam, {
name: "formatEmbeddedCode",
});
}

export async function formatFile(
Expand All @@ -29,5 +32,7 @@ export async function formatFile(
fileName: string,
code: string,
): Promise<string> {
return pool!.run({ options, code, fileName, parserName }, { name: "formatFile" });
return pool!.run({ options, code, fileName, parserName } satisfies FormatFileParam, {
name: "formatFile",
});
}
30 changes: 19 additions & 11 deletions apps/oxfmt/src-js/libs/prettier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export async function resolvePlugins(): Promise<string[]> {
return [];
}

// ---

const TAG_TO_PARSER: Record<string, string> = {
// CSS
css: "css",
Expand All @@ -33,6 +35,12 @@ const TAG_TO_PARSER: Record<string, string> = {
markdown: "markdown",
};

export type FormatEmbeddedCodeParam = {
code: string;
tagName: string;
options: Options;
};

/**
* Format xxx-in-js code snippets
*
Expand All @@ -44,11 +52,7 @@ export async function formatEmbeddedCode({
code,
tagName,
options,
}: {
code: string;
tagName: string;
options: Options;
}): Promise<string> {
}: FormatEmbeddedCodeParam): Promise<string> {
// TODO: This should be resolved in Rust side
const parserName = TAG_TO_PARSER[tagName];

Expand All @@ -67,6 +71,15 @@ export async function formatEmbeddedCode({
.catch(() => code);
}

// ---

export type FormatFileParam = {
code: string;
parserName: string;
fileName: string;
options: Options;
};

/**
* Format non-js file
*
Expand All @@ -77,12 +90,7 @@ export async function formatFile({
parserName,
fileName,
options,
}: {
code: string;
parserName: string;
fileName: string;
options: Options;
}): Promise<string> {
}: FormatFileParam): Promise<string> {
if (!prettierCache) {
prettierCache = await import("prettier");
}
Expand Down
Loading