* Compute a diff between two files, focusing on function-level changes * @param oldText Previous version of code * @param newText New version of code * @returns A DiffResult with diff text and count of changed lines
(oldText: string, newText: string)
| 263 | * @returns A DiffResult with diff text and count of changed lines |
| 264 | */ |
| 265 | function computeFunctionDiff(oldText: string, newText: string): DiffResult { |
| 266 | // Extract functions from the old and new text |
| 267 | const oldFunctions = extractFunctions(oldText); |
| 268 | const newFunctions = extractFunctions(newText); |
| 269 | |
| 270 | const hunks: DiffHunk[] = []; |
| 271 | let changedFunctionsCount = 0; |
| 272 | |
| 273 | // Compare functions |
| 274 | const allFunctionNames = new Set([...Object.keys(oldFunctions), ...Object.keys(newFunctions)]); |
| 275 | |
| 276 | for (const funcName of allFunctionNames) { |
| 277 | const oldFunc = oldFunctions[funcName]; |
| 278 | const newFunc = newFunctions[funcName]; |
| 279 | |
| 280 | if (!oldFunc) { |
| 281 | // Function was added |
| 282 | changedFunctionsCount++; |
| 283 | hunks.push({ |
| 284 | oldStart: 0, |
| 285 | oldLines: 0, |
| 286 | newStart: newFunc.startLine, |
| 287 | newLines: newFunc.endLine - newFunc.startLine + 1, |
| 288 | lines: newFunc.content.split("\n").map((line) => `+${line}`), |
| 289 | }); |
| 290 | } else if (!newFunc) { |
| 291 | // Function was removed |
| 292 | changedFunctionsCount++; |
| 293 | hunks.push({ |
| 294 | oldStart: oldFunc.startLine, |
| 295 | oldLines: oldFunc.endLine - oldFunc.startLine + 1, |
| 296 | newStart: 0, |
| 297 | newLines: 0, |
| 298 | lines: oldFunc.content.split("\n").map((line) => `-${line}`), |
| 299 | }); |
| 300 | } else if (oldFunc.content !== newFunc.content) { |
| 301 | // Function was modified |
| 302 | changedFunctionsCount++; |
| 303 | |
| 304 | // Use file diff for the function content |
| 305 | const functionDiff = computeFileDiff(oldFunc.content, newFunc.content); |
| 306 | |
| 307 | // Adjust line numbers to be relative to the file |
| 308 | for (const hunk of functionDiff.hunks) { |
| 309 | hunks.push({ |
| 310 | oldStart: oldFunc.startLine + hunk.oldStart, |
| 311 | oldLines: hunk.oldLines, |
| 312 | newStart: newFunc.startLine + hunk.newStart, |
| 313 | newLines: hunk.newLines, |
| 314 | lines: hunk.lines, |
| 315 | }); |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | // Generate the diff text |
| 321 | const diffLines: string[] = []; |
| 322 |
no test coverage detected