Files
oh-my-pi/packages/react-edit-benchmark/formatter.ts
T
can1357 779ca4872b style(deps): migrated from Prettier to Biome and updated formatting rules
- Removed Prettier configuration files (.prettierignore and .prettierrc) and migrated formatting to Biome.
- Updated Biome configuration from version 2.3.11 to 2.3.12 and changed arrowParentheses rule from 'always' to 'asNeeded'.
- Pinned @biomejs/biome dependency to exact version 2.3.12 in package.json and bun.lock.
- Applied consistent arrow function formatting across 489 files by removing unnecessary parentheses around single parameters.
- Removed blank lines after comment blocks and reorganized imports for consistency across the codebase.
2026-01-24 04:20:19 +01:00

89 lines
2.2 KiB
TypeScript

/**
* Prettier formatting utilities for edit benchmarks.
*/
import * as fs from "node:fs/promises";
import * as path 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"],
};
async function listFiles(rootDir: string, subPath = ""): Promise<string[]> {
const entries = await fs.readdir(path.join(rootDir, subPath), { withFileTypes: true });
const files: string[] = [];
for (const entry of entries) {
const relativePath = path.join(subPath, entry.name);
if (entry.isDirectory()) {
files.push(...(await 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[path.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 = await listFiles(rootDir);
for (const file of files) {
const fullPath = path.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);
}
}
}