TypeScript
fix(web): preserve trailing whitespace in review diffs (#25303)
Victor Vogel9474bcbae1ed
diff --git a/apps/web/src/components/pullRequest/guide/useGuideDiffFiles.browser.test.tsx b/apps/web/src/components/pullRequest/guide/useGuideDiffFiles.browser.test.tsxindex cdb7d4099e5..8a92f2c2bf0 100644--- a/apps/web/src/components/pullRequest/guide/useGuideDiffFiles.browser.test.tsx+++ b/apps/web/src/components/pullRequest/guide/useGuideDiffFiles.browser.test.tsx@@ -66,5 +66,5 @@ it.each([expect(result()).toEqual({ loading: true, files: [] });await act(() => registry.set(first, AsyncResult.success(page("wanted.ts", "FRESH", null))));- expect(result()).toEqual({ loading: false, files: [["wanted.ts", "FRESH"]] });+ expect(result()).toEqual({ loading: false, files: [["wanted.ts", "FRESH\n"]] });});diff --git a/apps/web/src/lib/diffRendering.test.ts b/apps/web/src/lib/diffRendering.test.tsindex 7634177ec74..dc8ced6d257 100644--- a/apps/web/src/lib/diffRendering.test.ts+++ b/apps/web/src/lib/diffRendering.test.ts@@ -219,6 +219,50 @@ describe("getRenderablePatch", () => {if (parsed?.kind !== "files") return;expect(parsed.files[0]?.hunks[0]?.unifiedLineStart).toBe(47);});++ it("keeps whitespace at the end of the last hunk", () => {+ const patch = (lastLines: string[]) =>+ [+ "diff --git a/example.ts b/example.ts",+ "--- a/example.ts",+ "+++ b/example.ts",+ ...lastLines,+ "",+ ].join("\n");++ const trailing = getRenderablePatch(patch(["@@ -1 +1 @@", "-a", "+ b \t"]), "review");+ expect(trailing?.kind).toBe("files");+ if (trailing?.kind !== "files") return;+ expect(trailing.files[0]?.additionLines).toEqual([" b \t\n"]);++ const blankContext = getRenderablePatch(patch(["@@ -1,2 +1,2 @@", "-a", "+b", " "]), "review");+ expect(blankContext?.kind).toBe("files");+ if (blankContext?.kind !== "files") return;+ expect(blankContext.files[0]?.additionLines).toEqual(["b\n", "\n"]);+ expect(blankContext.files[0]?.hunks[0]?.unifiedLineCount).toBe(3);++ expect(getRenderablePatch(" \n\t\n", "review")).toBeNull();+ });++ it("caches patches that differ only in trailing whitespace separately", () => {+ const patch = (added: string) =>+ [+ "diff --git a/example.ts b/example.ts",+ "--- a/example.ts",+ "+++ b/example.ts",+ "@@ -1 +1 @@",+ "-a",+ `+${added}`,+ "",+ ].join("\n");++ const plain = getRenderablePatch(patch("b"), "review");+ const tabbed = getRenderablePatch(patch("b\t"), "review");+ expect(plain?.kind).toBe("files");+ expect(tabbed?.kind).toBe("files");+ if (plain?.kind !== "files" || tabbed?.kind !== "files") return;+ expect(tabbed.files[0]?.cacheKey).not.toBe(plain.files[0]?.cacheKey);+ });});describe("diff file reconciliation", () => {diff --git a/apps/web/src/lib/diffRendering.ts b/apps/web/src/lib/diffRendering.tsindex aac9b1f3e63..2add48cad57 100644--- a/apps/web/src/lib/diffRendering.ts+++ b/apps/web/src/lib/diffRendering.ts@@ -34,14 +34,9 @@ export function fnv1a32(}function buildPatchCacheKey(patch: string, scope = "diff-panel"): string {- const normalizedPatch = patch.trim();- const primary = fnv1a32(normalizedPatch, FNV_OFFSET_BASIS_32, FNV_PRIME_32).toString(36);- const secondary = fnv1a32(- normalizedPatch,- SECONDARY_HASH_SEED,- SECONDARY_HASH_MULTIPLIER,- ).toString(36);- return `${scope}:${normalizedPatch.length}:${primary}:${secondary}`;+ const primary = fnv1a32(patch, FNV_OFFSET_BASIS_32, FNV_PRIME_32).toString(36);+ const secondary = fnv1a32(patch, SECONDARY_HASH_SEED, SECONDARY_HASH_MULTIPLIER).toString(36);+ return `${scope}:${patch.length}:${primary}:${secondary}`;}export type RenderablePatch =@@ -164,15 +159,10 @@ export function getRenderablePatch(cacheScope = "diff-panel",options: RenderablePatchOptions = {},): RenderablePatch | null {- if (!patch) return null;- const normalizedPatch = patch.trim();- if (normalizedPatch.length === 0) return null;+ if (!patch?.trim()) return null;try {- const parsedPatches = parsePatchFiles(- normalizedPatch,- buildPatchCacheKey(normalizedPatch, cacheScope),- );+ const parsedPatches = parsePatchFiles(patch, buildPatchCacheKey(patch, cacheScope));const sourceFiles = parsedPatches.flatMap((parsedPatch) => parsedPatch.files);const files = sourceFiles.map((file) => {const filtered = options.ignoreWhitespace ? hideWhitespaceChanges(file) : file;@@ -184,13 +174,13 @@ export function getRenderablePatch(return {kind: "raw",- text: normalizedPatch,+ text: patch,reason: "Unsupported diff format. Showing raw patch.",};} catch {return {kind: "raw",- text: normalizedPatch,+ text: patch,reason: "Failed to parse patch. Showing raw patch.",};}diff --git a/apps/web/src/lib/toolCallDiff.test.ts b/apps/web/src/lib/toolCallDiff.test.tsindex cb33dd45cfb..8efd08ddfa3 100644--- a/apps/web/src/lib/toolCallDiff.test.ts+++ b/apps/web/src/lib/toolCallDiff.test.ts@@ -54,7 +54,7 @@ describe("toolCallDiff", () => {expect(file.additionLines).toHaveLength(operation === "add" ? lines : 0);expect(file.deletionLines).toHaveLength(operation === "delete" ? lines : 0);expect((operation === "add" ? file.additionLines : file.deletionLines).join("")).toBe(- recordedContent.replace(/\n$/, ""),+ recordedContent,);}}