( sourceCode: string, unifiedDiffText: string, )
| 41 | * @throws Error if the diff cannot be cleanly applied to the source |
| 42 | */ |
| 43 | export function applyUnifiedDiff( |
| 44 | sourceCode: string, |
| 45 | unifiedDiffText: string, |
| 46 | ): DiffLine[] { |
| 47 | const sourceLines = sourceCode.split(/\r?\n/); |
| 48 | const hunks = parseUnifiedDiff(unifiedDiffText); |
| 49 | const diffResult: DiffLine[] = []; |
| 50 | let currentPos = 0; // pointer in sourceLines |
| 51 | |
| 52 | for (const hunk of hunks) { |
| 53 | const hunkBeforeLines = extractBeforeLines(hunk.lines); |
| 54 | const hunkStart = findHunkInSource( |
| 55 | sourceLines, |
| 56 | hunkBeforeLines, |
| 57 | currentPos, |
| 58 | ); |
| 59 | if (hunkStart === -1) { |
| 60 | // All hunks must be found in the source code. If not, throw an error. |
| 61 | throw new Error("Hunk could not be applied cleanly to source code."); |
| 62 | } |
| 63 | |
| 64 | // Emit any unchanged lines that come before this hunk. |
| 65 | for (let i = currentPos; i < hunkStart; i++) { |
| 66 | diffResult.push({ type: "same", line: sourceLines[i] }); |
| 67 | } |
| 68 | let hunkSourcePos = hunkStart; |
| 69 | |
| 70 | for (const dline of hunk.lines) { |
| 71 | const srcLine = sourceLines[hunkSourcePos]; |
| 72 | if (dline.startsWith("+")) { |
| 73 | // Insertion: output new line (strip the '+' marker) |
| 74 | diffResult.push({ type: "new", line: dline.substring(1) }); |
| 75 | } else if (dline.startsWith("-")) { |
| 76 | // Removal: output the removed (old) line and advance the pointer. |
| 77 | diffResult.push({ type: "old", line: srcLine }); |
| 78 | hunkSourcePos++; |
| 79 | } else { |
| 80 | // Context line: use the source line (in case the diff’s context has a minor whitespace error) |
| 81 | // and advance the pointer. |
| 82 | diffResult.push({ type: "same", line: srcLine }); |
| 83 | hunkSourcePos++; |
| 84 | } |
| 85 | } |
| 86 | currentPos = hunkSourcePos; |
| 87 | } |
| 88 | |
| 89 | for (let i = currentPos; i < sourceLines.length; i++) { |
| 90 | diffResult.push({ type: "same", line: sourceLines[i] }); |
| 91 | } |
| 92 | return diffResult; |
| 93 | } |
| 94 | |
| 95 | interface Hunk { |
| 96 | lines: string[]; |
no test coverage detected