fix(core): preserve patch parser leniency

This commit is contained in:
Aiden Cline
2026-07-21 21:10:50 -05:00
parent 6a2a9eb56e
commit 37d6464764
2 changed files with 15 additions and 8 deletions
+14 -7
View File
@@ -185,6 +185,19 @@ describe("Patch", () => {
])
})
test("allows an end-of-file marker before an explicit chunk", () => {
expect(
parse("*** Begin Patch\n*** Update File: file.txt\n*** End of File\n@@\n-old\n+new\n*** End Patch"),
).toEqual([
{
type: "update",
path: "file.txt",
movePath: undefined,
chunks: [{ oldLines: ["old"], newLines: ["new"], changeContext: undefined }],
},
])
})
test("derives fuzzy line updates while preserving BOM", () => {
const update = Patch.derive("update.txt", [{ oldLines: [" old "], newLines: ["new"] }], "\uFEFFold\n")
expect(update).toEqual({ content: "new\n", bom: true })
@@ -388,16 +401,10 @@ describe("Patch", () => {
),
).toThrow("Invalid hunk at line 2: Update file hunk for path 'old.txt' is empty")
expect(() => parse("*** Begin Patch\n*** Update File: file.txt\n*** End of File\n*** End Patch")).toThrow(
"Invalid hunk at line 3: Update hunk does not contain any lines",
"Invalid hunk at line 2: Update file hunk for path 'file.txt' is empty",
)
})
test("rejects an end-of-file marker before update lines", () => {
expect(() =>
parse("*** Begin Patch\n*** Update File: file.txt\n*** End of File\n@@\n-old\n+new\n*** End Patch"),
).toThrow("Invalid hunk at line 3: Update hunk does not contain any lines")
})
test("rejects an empty update chunk", () => {
expect(() => parse("*** Begin Patch\n*** Update File: file.txt\n@@\n*** End Patch")).toThrow(
"Invalid hunk at line 4: Update hunk does not contain any lines",
+1 -1
View File
@@ -167,7 +167,7 @@ function parseUpdate(
}
if (updateLine === "*** End of File") {
const chunk = chunks.at(-1)
if (!chunk || (chunk.oldLines.length === 0 && chunk.newLines.length === 0)) {
if (chunk && chunk.oldLines.length === 0 && chunk.newLines.length === 0) {
return {
error: new InvalidHunkError({
line: updateLine,