Files
oh-my-pi/packages/react-edit-benchmark/formatter.ts
T
can1357 440ac33dbf style: reformatted TypeScript code formatting and added benchmark reports
- Reformatted code indentation and spacing across multiple TypeScript files.
- Standardized export statements to single-line format in various modules.
- Fixed async/await precedence and corrected indentation in benchmark files.
- Added benchmark report files for GPT-5.1-codex-mini model performance.
2026-01-19 16:22:47 +01:00

90 lines
2.2 KiB
TypeScript

/**
* Prettier formatting utilities for edit benchmarks.
*/
import { readdirSync } from "node:fs";
import { join, extname } from "node:path";
import * as prettier from "prettier";
export const PRETTIER_OPTIONS: prettier.Options = {
printWidth: 100,
tabWidth: 2,
useTabs: false,
semi: true,
singleQuote: true,
quoteProps: "as-needed",
trailingComma: "all",
bracketSpacing: true,
arrowParens: "always",
endOfLine: "lf",
proseWrap: "preserve",
};
const parsersByExtension: Record<string, prettier.BuiltInParserName[]> = {
".js": ["flow", "babel", "babel-ts"],
".jsx": ["flow", "babel", "babel-ts"],
".ts": ["typescript"],
".tsx": ["typescript"],
".json": ["json"],
".jsonc": ["json"],
".md": ["markdown"],
".mdx": ["mdx"],
".yml": ["yaml"],
".yaml": ["yaml"],
".css": ["css"],
".scss": ["scss"],
".html": ["html"],
};
function listFiles(rootDir: string, subPath = ""): string[] {
const entries = readdirSync(join(rootDir, subPath), { withFileTypes: true });
const files: string[] = [];
for (const entry of entries) {
const relativePath = join(subPath, entry.name);
if (entry.isDirectory()) {
files.push(...listFiles(rootDir, relativePath));
} else if (entry.isFile()) {
files.push(relativePath);
}
}
return files.sort();
}
export interface FormatResult {
formatted: string;
didFormat: boolean;
}
export async function formatContent(filePath: string, content: string): Promise<FormatResult> {
const parsers = parsersByExtension[extname(filePath).toLowerCase()];
if (!parsers) {
return { formatted: content, didFormat: false };
}
for (const parser of parsers) {
try {
const formatted = await prettier.format(content, { ...PRETTIER_OPTIONS, parser });
return { formatted, didFormat: true };
} catch {
// Try the next parser.
}
}
return { formatted: content, didFormat: false };
}
export async function formatDirectory(rootDir: string): Promise<void> {
const files = listFiles(rootDir);
for (const file of files) {
const fullPath = join(rootDir, file);
const content = await Bun.file(fullPath).text();
const result = await formatContent(fullPath, content);
if (result.didFormat && result.formatted !== content) {
await Bun.write(fullPath, result.formatted);
}
}
}