|
| 1 | +import { ESLint } from "eslint"; |
| 2 | +import { format, Options } from "prettier"; |
| 3 | +import { Synapse } from "../synapse"; |
| 4 | + |
| 5 | +export interface FormatOptions { |
| 6 | + language: string; |
| 7 | + indent?: number; |
| 8 | + useTabs?: boolean; |
| 9 | + semi?: boolean; |
| 10 | + singleQuote?: boolean; |
| 11 | + printWidth?: number; |
| 12 | +} |
| 13 | + |
| 14 | +export interface LintOptions { |
| 15 | + language: string; |
| 16 | + rules?: Record<string, "error" | "warn" | "off">; |
| 17 | +} |
| 18 | + |
| 19 | +export interface FormatResult { |
| 20 | + success: boolean; |
| 21 | + data?: string; |
| 22 | + error?: string; |
| 23 | +} |
| 24 | + |
| 25 | +export interface LintResult { |
| 26 | + success: boolean; |
| 27 | + data?: { |
| 28 | + issues: Array<{ |
| 29 | + line: number; |
| 30 | + column: number; |
| 31 | + severity: "error" | "warning"; |
| 32 | + rule: string; |
| 33 | + message: string; |
| 34 | + }>; |
| 35 | + }; |
| 36 | + error?: string; |
| 37 | +} |
| 38 | + |
| 39 | +export class Code { |
| 40 | + private synapse: Synapse; |
| 41 | + |
| 42 | + /** |
| 43 | + * Creates a new CodeStyle instance |
| 44 | + * @param synapse The Synapse instance for WebSocket communication |
| 45 | + */ |
| 46 | + constructor(synapse: Synapse) { |
| 47 | + this.synapse = synapse; |
| 48 | + } |
| 49 | + |
| 50 | + private log(message: string): void { |
| 51 | + const timestamp = new Date().toISOString(); |
| 52 | + console.log(`[Code][${timestamp}] ${message}`); |
| 53 | + } |
| 54 | + |
| 55 | + private getParserForLanguage(language: string): string { |
| 56 | + const languageMap: Record<string, string> = { |
| 57 | + javascript: "babel", |
| 58 | + typescript: "typescript", |
| 59 | + json: "json", |
| 60 | + html: "html", |
| 61 | + css: "css", |
| 62 | + markdown: "markdown", |
| 63 | + yaml: "yaml", |
| 64 | + }; |
| 65 | + |
| 66 | + return languageMap[language.toLowerCase()] || "babel"; |
| 67 | + } |
| 68 | + |
| 69 | + private toPrettierOptions( |
| 70 | + language: string, |
| 71 | + options?: FormatOptions, |
| 72 | + ): Options { |
| 73 | + const parser = this.getParserForLanguage(language); |
| 74 | + |
| 75 | + return { |
| 76 | + parser, |
| 77 | + tabWidth: options?.indent || 2, |
| 78 | + useTabs: options?.useTabs || false, |
| 79 | + semi: options?.semi !== undefined ? options.semi : true, |
| 80 | + singleQuote: options?.singleQuote || false, |
| 81 | + printWidth: options?.printWidth || 80, |
| 82 | + }; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Format code according to specified options |
| 87 | + * @param code The code to format |
| 88 | + * @param options Formatting options |
| 89 | + * @returns A promise resolving to the formatting result |
| 90 | + */ |
| 91 | + async format(code: string, options: FormatOptions): Promise<FormatResult> { |
| 92 | + try { |
| 93 | + if (!code || typeof code !== "string") { |
| 94 | + return { |
| 95 | + success: false, |
| 96 | + error: "Invalid code input: code must be a non-empty string", |
| 97 | + }; |
| 98 | + } |
| 99 | + |
| 100 | + if (!options.language) { |
| 101 | + return { |
| 102 | + success: false, |
| 103 | + error: "Language must be specified in format options", |
| 104 | + }; |
| 105 | + } |
| 106 | + |
| 107 | + this.log(`Formatting code with language: ${options.language}`); |
| 108 | + |
| 109 | + const prettierOptions = this.toPrettierOptions(options.language, options); |
| 110 | + const formattedCode = await format(code, prettierOptions); |
| 111 | + |
| 112 | + return { |
| 113 | + success: true, |
| 114 | + data: formattedCode, |
| 115 | + }; |
| 116 | + } catch (error) { |
| 117 | + this.log( |
| 118 | + `Formatting failed: ${error instanceof Error ? error.message : "Unknown error"}`, |
| 119 | + ); |
| 120 | + return { |
| 121 | + success: false, |
| 122 | + error: `Formatting failed: ${error instanceof Error ? error.message : "Unknown error"}`, |
| 123 | + }; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Lint code to identify issues |
| 129 | + * @param code The code to lint |
| 130 | + * @param options Linting options |
| 131 | + * @returns A promise resolving to the linting result |
| 132 | + */ |
| 133 | + async lint(code: string, options: LintOptions): Promise<LintResult> { |
| 134 | + try { |
| 135 | + if (!code || typeof code !== "string") { |
| 136 | + return { |
| 137 | + success: false, |
| 138 | + error: "Invalid code input: code must be a non-empty string", |
| 139 | + }; |
| 140 | + } |
| 141 | + |
| 142 | + if (!options.language) { |
| 143 | + return { |
| 144 | + success: false, |
| 145 | + error: "Language must be specified in lint options", |
| 146 | + }; |
| 147 | + } |
| 148 | + |
| 149 | + this.log(`Linting code with language: ${options.language}`); |
| 150 | + |
| 151 | + const eslintOptions = { |
| 152 | + overrideConfig: { |
| 153 | + languageOptions: { |
| 154 | + ecmaVersion: 2020, |
| 155 | + sourceType: "module", |
| 156 | + }, |
| 157 | + rules: options.rules || {}, |
| 158 | + }, |
| 159 | + overrideConfigFile: true, |
| 160 | + } as ESLint.Options; |
| 161 | + |
| 162 | + const linter = new ESLint(eslintOptions); |
| 163 | + const eslintResult = await linter.lintText(code); |
| 164 | + |
| 165 | + return { |
| 166 | + success: true, |
| 167 | + data: { |
| 168 | + issues: eslintResult.flatMap((result) => |
| 169 | + result.messages.map((message) => ({ |
| 170 | + line: message.line, |
| 171 | + column: message.column, |
| 172 | + severity: message.severity === 2 ? "error" : "warning", |
| 173 | + rule: message.ruleId || "", |
| 174 | + message: message.message, |
| 175 | + })), |
| 176 | + ), |
| 177 | + }, |
| 178 | + }; |
| 179 | + } catch (error) { |
| 180 | + this.log( |
| 181 | + `Linting failed: ${error instanceof Error ? error.message : "Unknown error"}`, |
| 182 | + ); |
| 183 | + return { |
| 184 | + success: false, |
| 185 | + error: `Linting failed: ${error instanceof Error ? error.message : "Unknown error"}`, |
| 186 | + }; |
| 187 | + } |
| 188 | + } |
| 189 | +} |
0 commit comments