// Extracted from recast export interface CodeFormatOptions { tabWidth?: number; useTabs?: boolean; wrapColumn?: number; quote?: "single" | "double"; trailingComma?: boolean; arrayBracketSpacing?: boolean; objectCurlySpacing?: boolean; arrowParensAlways?: boolean; useSemi?: boolean; } export function detectCodeFormat( code: string, userStyles: CodeFormatOptions = {}, ): CodeFormatOptions { // Detect features only user not specified const detect = { wrapColumn: userStyles.wrapColumn === undefined, indent: userStyles.tabWidth === undefined || userStyles.useTabs === undefined, quote: userStyles.quote === undefined, arrowParens: userStyles.arrowParensAlways === undefined, trailingComma: userStyles.trailingComma === undefined, }; // Frequency counters and state let codeIndent = 2; let tabUsages = 0; let semiUsages = 0; let maxLineLength = 0; let multiLineTrailingCommaUsages = 0; // Syntax detection regex // TODO: Perf: Compile only for features we need to detect const syntaxDetectRegex = /(?"[^"]+")|(?'[^']+')|(?\([^),]+\)\s*=>)|(?,\s*[\]}])/g; const syntaxUsages = { doubleQuote: 0, singleQuote: 0, singleParam: 0, trailingComma: 0, }; // Line by line analysis const lines = (code || "").split("\n"); let previousLineTrailing = false; for (const line of lines) { // Trim line // TODO: Trim comments const trimmitedLine = line.trim(); // Skip empty lines if (trimmitedLine.length === 0) { continue; } // Max width if (detect.wrapColumn && line.length > maxLineLength) { maxLineLength = line.length; } // Indentation analysis if (detect.indent) { const lineIndent = line.match(/^\s+/)?.[0] || ""; if (lineIndent.length > 0) { if (lineIndent.length > 0 && lineIndent.length < codeIndent) { codeIndent = lineIndent.length; } if (lineIndent[0] === "\t") { tabUsages++; } else if (lineIndent.length > 0) { tabUsages--; } } } // Line ending analysis if (trimmitedLine.at(-1) === ";") { semiUsages++; } else if (trimmitedLine.length > 0) { semiUsages--; } // Syntax analysis if (detect.quote || detect.arrowParens) { const matches = trimmitedLine.matchAll(syntaxDetectRegex); for (const match of matches) { if (!match.groups) { continue; } for (const key in syntaxUsages) { if (match.groups[key]) { // @ts-expect-error indexing with a template literal type key syntaxUsages[key]++; } } } } if (detect.trailingComma) { if (line.startsWith("}") || line.startsWith("]")) { if (previousLineTrailing) { multiLineTrailingCommaUsages++; } else { multiLineTrailingCommaUsages--; } } previousLineTrailing = trimmitedLine.endsWith(","); } } const format = { wrapColumn: maxLineLength, useTabs: tabUsages > 0, tabWidth: codeIndent, quote: syntaxUsages.singleQuote > syntaxUsages.doubleQuote ? "single" : "double", arrowParensAlways: syntaxUsages.singleParam > 0, trailingComma: multiLineTrailingCommaUsages > 0 || syntaxUsages.trailingComma > 0, useSemi: semiUsages > 0, // TODO: detect arrayBracketSpacing / objectCurlySpacing }; // Only apply options the user actually specified. `undefined` means "not // specified" here (see `detect` above), so it must not overwrite a detected // value, and it must not end up as an own key either: recast resolves its // defaults with `hasOwnProperty`, so a key holding `undefined` shadows the // default rather than falling back to it (e.g. `objectCurlySpacing` would // become falsy and print `{a}` instead of `{ a }`). for (const key of Object.keys(userStyles) as (keyof CodeFormatOptions)[]) { if (userStyles[key] !== undefined) { format[key] = userStyles[key] as any; } } return format; }