Skip to content
Merged
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
50 changes: 44 additions & 6 deletions code/core/src/common/utils/formatter.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,46 @@
export async function getPrettier() {
return import('prettier').catch((e) => ({
resolveConfig: async () => null,
format: (content: string) => content,
}));
// Prettier interface definition
// Note: We want to avoid importing prettier directly to prevent bundling its type import
// because prettier is an optional peer dependency and might not be available
interface Prettier {
resolveConfig: (filePath: string, options?: { editorconfig?: boolean }) => Promise<any>;
format: (content: string, options?: any) => Promise<string> | string;
check: (content: string, options?: any) => Promise<boolean>;
clearConfigCache: () => Promise<void>;
formatWithCursor: (
content: string,
options?: any
) => Promise<{ formatted: string; cursorOffset: number }>;
getFileInfo: (
filePath: string,
options?: any
) => Promise<{ ignored: boolean; inferredParser: string | null }>;
getSupportInfo: () => Promise<{ languages: any[]; options: any[] }>;
resolveConfigFile: (filePath?: string) => Promise<string | null>;
version: string;
AstPath: any;
doc: any;
util: any;
}

export async function getPrettier(): Promise<Prettier> {
try {
return await import('prettier');
} catch {
return {
AstPath: class {} as any,
doc: {} as any,
util: {} as any,
version: '0.0.0-fallback',
resolveConfig: async () => null,
format: (content: string) => Promise.resolve(content),
check: () => Promise.resolve(false),
clearConfigCache: () => Promise.resolve(undefined),
formatWithCursor: () => Promise.resolve({ formatted: '', cursorOffset: 0 }),
getFileInfo: async () => ({ ignored: false, inferredParser: null }),
getSupportInfo: () => Promise.resolve({ languages: [], options: [] }),
resolveConfigFile: async () => null,
};
}
}

/**
Expand Down Expand Up @@ -30,7 +68,7 @@ export async function formatFileContent(filePath: string, content: string): Prom
}
}

async function formatWithEditorConfig(filePath: string, content: string) {
async function formatWithEditorConfig(filePath: string, content: string): Promise<string> {
const { resolveConfig, format } = await getPrettier();

const config = await resolveConfig(filePath, { editorconfig: true });
Expand Down
Loading