Skip to content

Commit 9474bcb

Browse files
only21milclaudemaria-rcks
authored
fix(web): preserve trailing whitespace in review diffs (#25303)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent f8df431 commit 9474bcb

4 files changed

Lines changed: 53 additions & 19 deletions

File tree

‎apps/web/src/components/pullRequest/guide/useGuideDiffFiles.browser.test.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,5 @@ it.each([
6666
expect(result()).toEqual({ loading: true, files: [] });
6767

6868
await act(() => registry.set(first, AsyncResult.success(page("wanted.ts", "FRESH", null))));
69-
expect(result()).toEqual({ loading: false, files: [["wanted.ts", "FRESH"]] });
69+
expect(result()).toEqual({ loading: false, files: [["wanted.ts", "FRESH\n"]] });
7070
});

‎apps/web/src/lib/diffRendering.test.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,50 @@ describe("getRenderablePatch", () => {
219219
if (parsed?.kind !== "files") return;
220220
expect(parsed.files[0]?.hunks[0]?.unifiedLineStart).toBe(47);
221221
});
222+
223+
it("keeps whitespace at the end of the last hunk", () => {
224+
const patch = (lastLines: string[]) =>
225+
[
226+
"diff --git a/example.ts b/example.ts",
227+
"--- a/example.ts",
228+
"+++ b/example.ts",
229+
...lastLines,
230+
"",
231+
].join("\n");
232+
233+
const trailing = getRenderablePatch(patch(["@@ -1 +1 @@", "-a", "+ b \t"]), "review");
234+
expect(trailing?.kind).toBe("files");
235+
if (trailing?.kind !== "files") return;
236+
expect(trailing.files[0]?.additionLines).toEqual([" b \t\n"]);
237+
238+
const blankContext = getRenderablePatch(patch(["@@ -1,2 +1,2 @@", "-a", "+b", " "]), "review");
239+
expect(blankContext?.kind).toBe("files");
240+
if (blankContext?.kind !== "files") return;
241+
expect(blankContext.files[0]?.additionLines).toEqual(["b\n", "\n"]);
242+
expect(blankContext.files[0]?.hunks[0]?.unifiedLineCount).toBe(3);
243+
244+
expect(getRenderablePatch(" \n\t\n", "review")).toBeNull();
245+
});
246+
247+
it("caches patches that differ only in trailing whitespace separately", () => {
248+
const patch = (added: string) =>
249+
[
250+
"diff --git a/example.ts b/example.ts",
251+
"--- a/example.ts",
252+
"+++ b/example.ts",
253+
"@@ -1 +1 @@",
254+
"-a",
255+
`+${added}`,
256+
"",
257+
].join("\n");
258+
259+
const plain = getRenderablePatch(patch("b"), "review");
260+
const tabbed = getRenderablePatch(patch("b\t"), "review");
261+
expect(plain?.kind).toBe("files");
262+
expect(tabbed?.kind).toBe("files");
263+
if (plain?.kind !== "files" || tabbed?.kind !== "files") return;
264+
expect(tabbed.files[0]?.cacheKey).not.toBe(plain.files[0]?.cacheKey);
265+
});
222266
});
223267

224268
describe("diff file reconciliation", () => {

‎apps/web/src/lib/diffRendering.ts‎

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,9 @@ export function fnv1a32(
3434
}
3535

3636
function buildPatchCacheKey(patch: string, scope = "diff-panel"): string {
37-
const normalizedPatch = patch.trim();
38-
const primary = fnv1a32(normalizedPatch, FNV_OFFSET_BASIS_32, FNV_PRIME_32).toString(36);
39-
const secondary = fnv1a32(
40-
normalizedPatch,
41-
SECONDARY_HASH_SEED,
42-
SECONDARY_HASH_MULTIPLIER,
43-
).toString(36);
44-
return `${scope}:${normalizedPatch.length}:${primary}:${secondary}`;
37+
const primary = fnv1a32(patch, FNV_OFFSET_BASIS_32, FNV_PRIME_32).toString(36);
38+
const secondary = fnv1a32(patch, SECONDARY_HASH_SEED, SECONDARY_HASH_MULTIPLIER).toString(36);
39+
return `${scope}:${patch.length}:${primary}:${secondary}`;
4540
}
4641

4742
export type RenderablePatch =
@@ -164,15 +159,10 @@ export function getRenderablePatch(
164159
cacheScope = "diff-panel",
165160
options: RenderablePatchOptions = {},
166161
): RenderablePatch | null {
167-
if (!patch) return null;
168-
const normalizedPatch = patch.trim();
169-
if (normalizedPatch.length === 0) return null;
162+
if (!patch?.trim()) return null;
170163

171164
try {
172-
const parsedPatches = parsePatchFiles(
173-
normalizedPatch,
174-
buildPatchCacheKey(normalizedPatch, cacheScope),
175-
);
165+
const parsedPatches = parsePatchFiles(patch, buildPatchCacheKey(patch, cacheScope));
176166
const sourceFiles = parsedPatches.flatMap((parsedPatch) => parsedPatch.files);
177167
const files = sourceFiles.map((file) => {
178168
const filtered = options.ignoreWhitespace ? hideWhitespaceChanges(file) : file;
@@ -184,13 +174,13 @@ export function getRenderablePatch(
184174

185175
return {
186176
kind: "raw",
187-
text: normalizedPatch,
177+
text: patch,
188178
reason: "Unsupported diff format. Showing raw patch.",
189179
};
190180
} catch {
191181
return {
192182
kind: "raw",
193-
text: normalizedPatch,
183+
text: patch,
194184
reason: "Failed to parse patch. Showing raw patch.",
195185
};
196186
}

‎apps/web/src/lib/toolCallDiff.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ describe("toolCallDiff", () => {
5454
expect(file.additionLines).toHaveLength(operation === "add" ? lines : 0);
5555
expect(file.deletionLines).toHaveLength(operation === "delete" ? lines : 0);
5656
expect((operation === "add" ? file.additionLines : file.deletionLines).join("")).toBe(
57-
recordedContent.replace(/\n$/, ""),
57+
recordedContent,
5858
);
5959
}
6060
}

0 commit comments

Comments
 (0)