Skip to content
TypeScript

fix(web): review comments keep a trailing blank line in their diff (#29575)

Alex Southwell523145993e28
diff --git a/apps/web/src/reviewCommentContext.test.ts b/apps/web/src/reviewCommentContext.test.ts
index b17d4ef106e..1b5e64373bf 100644
--- a/apps/web/src/reviewCommentContext.test.ts
+++ b/apps/web/src/reviewCommentContext.test.ts
@@ -5,6 +5,7 @@ import { describe, expect, it } from "vite-plus/test";
import {
buildDiffReviewComment,
buildFileReviewComment,
+ buildReviewCommentRenderablePatch,
formatReviewCommentFence,
resolveDiffReviewAnchor,
resolveDiffReviewPosition,
@@ -75,6 +76,38 @@ describe("review comment context parsing", () => {
);
});
+ it("keeps a selection's trailing blank line and whitespace in the rendered excerpt", () => {
+ const fileDiff = parsePatchFiles(
+ [
+ "diff --git a/src/app.ts b/src/app.ts",
+ "--- a/src/app.ts",
+ "+++ b/src/app.ts",
+ "@@ -1,4 +1,4 @@",
+ " one",
+ "-two",
+ "+two\t",
+ " ",
+ " four",
+ ].join("\n"),
+ "review-comment-trailing-blank",
+ )[0]!.files[0]!;
+ const comment = (end: number) =>
+ buildDiffReviewComment({
+ id: "comment-blank",
+ sectionId: "turn:1",
+ sectionTitle: "Turn 1",
+ filePath: "src/app.ts",
+ fileDiff,
+ range: { start: 2, side: "additions", end, endSide: "additions" },
+ text: "Insert here.",
+ })!;
+
+ expect(buildReviewCommentRenderablePatch(comment(2))).toMatch(/\n\+two\t$/);
+ expect(buildReviewCommentRenderablePatch(comment(3))).toMatch(
+ /\n@@ -3,1 \+2,2 @@\n\+two\t\n $/,
+ );
+ });
+
it("restores Pierre line selections from persisted diff comment row indexes", () => {
const fileDiff = parsePatchFiles(
[
diff --git a/apps/web/src/reviewCommentContext.ts b/apps/web/src/reviewCommentContext.ts
index 336a506a526..b169dd41885 100644
--- a/apps/web/src/reviewCommentContext.ts
+++ b/apps/web/src/reviewCommentContext.ts
@@ -512,10 +512,10 @@ export function buildReviewCommentRenderablePatch(comment: ReviewCommentContext)
if ((comment.fenceLanguage ?? "diff") !== "diff") {
return "";
}
- const diff = comment.diff.trim();
- if (diff.length === 0) {
+ if (comment.diff.trim().length === 0) {
return "";
}
+ const diff = comment.diff.trimStart().replace(/\n+$/, "");
if (diff.startsWith("diff --git ")) {
return diff;
}
diff --git a/packages/shared/src/composerContextReferences.test.ts b/packages/shared/src/composerContextReferences.test.ts
index 32e09e8b1a9..a12eed6a839 100644
--- a/packages/shared/src/composerContextReferences.test.ts
+++ b/packages/shared/src/composerContextReferences.test.ts
@@ -225,6 +225,29 @@ describe("provider projection", () => {
expect(project("a\n")).toContain("3 | a\n4 | \n</context>");
});
+ it("keeps a review excerpt's trailing blank line and whitespace", () => {
+ const projected = projectComposerContextForProvider({
+ text: "[Comment](t3-context://v1/review-comment/ctx_r)",
+ records: [
+ {
+ version: 1,
+ contextId: ctx("ctx_r"),
+ kind: "review-comment",
+ label: "Comment",
+ sectionId: "turn:1",
+ sectionTitle: "Turn 1",
+ filePath: "src/app.ts",
+ startIndex: 1,
+ endIndex: 2,
+ rangeLabel: "2 to 3",
+ text: "Insert here.",
+ diff: "@@ -3,1 +2,2 @@\n+two\t\n \n",
+ },
+ ],
+ });
+ expect(projected).toContain("diff:\n @@ -3,1 +2,2 @@\n +two\t\n \n</context>");
+ });
+
it("emits no terminal lines when the captured range is inverted", () => {
const projected = projectComposerContextForProvider({
text: "[log](t3-context://v1/terminal/ctx_t)",
diff --git a/packages/shared/src/composerContextReferences.ts b/packages/shared/src/composerContextReferences.ts
index 79eae6ecae3..33e89737961 100644
--- a/packages/shared/src/composerContextReferences.ts
+++ b/packages/shared/src/composerContextReferences.ts
@@ -293,7 +293,7 @@ function formatComposerContextProviderPayload(record: KnownComposerContextRecord
];
if (record.text.trim()) lines.push("comment:", indent(record.text.trim()));
if (record.diff.trim()) {
- lines.push(`${record.fenceLanguage ?? "diff"}:`, indent(record.diff.trimEnd()));
+ lines.push(`${record.fenceLanguage ?? "diff"}:`, indent(record.diff.replace(/\n+$/, "")));
}
return lines.join("\n");
}