* Extract functions from text * @param text Source code text * @returns Map of function names to their content and line numbers
( text: string, )
| 340 | * @returns Map of function names to their content and line numbers |
| 341 | */ |
| 342 | function extractFunctions( |
| 343 | text: string, |
| 344 | ): Record<string, { content: string; startLine: number; endLine: number }> { |
| 345 | const functions: Record<string, { content: string; startLine: number; endLine: number }> = {}; |
| 346 | const lines = text.split("\n"); |
| 347 | |
| 348 | // Simple regex to find function declarations |
| 349 | // This is a basic implementation and might need to be enhanced for different languages |
| 350 | const functionRegex = /^\s*(function|async function)\s+(\w+)\s*\(/; |
| 351 | |
| 352 | let currentFunction: string | null = null; |
| 353 | let functionStart = -1; |
| 354 | let braceCount = 0; |
| 355 | |
| 356 | for (let i = 0; i < lines.length; i++) { |
| 357 | const line = lines[i]; |
| 358 | |
| 359 | if (currentFunction === null) { |
| 360 | // Look for function declaration |
| 361 | const match = line.match(functionRegex); |
| 362 | if (match) { |
| 363 | currentFunction = match[2]; |
| 364 | functionStart = i; |
| 365 | braceCount = 0; |
| 366 | |
| 367 | // Count opening braces in this line |
| 368 | for (const char of line) { |
| 369 | if (char === "{") braceCount++; |
| 370 | else if (char === "}") braceCount--; |
| 371 | } |
| 372 | } |
| 373 | } else { |
| 374 | // Count braces to find the end of the function |
| 375 | for (const char of line) { |
| 376 | if (char === "{") braceCount++; |
| 377 | else if (char === "}") braceCount--; |
| 378 | } |
| 379 | |
| 380 | // If braces are balanced, we've found the end of the function |
| 381 | if (braceCount === 0) { |
| 382 | functions[currentFunction] = { |
| 383 | content: lines.slice(functionStart, i + 1).join("\n"), |
| 384 | startLine: functionStart, |
| 385 | endLine: i, |
| 386 | }; |
| 387 | currentFunction = null; |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | return functions; |
| 393 | } |
| 394 | |
| 395 | /** |
| 396 | * Apply a diff to a text |
no outgoing calls
no test coverage detected