The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 03:15 UTC
fix(review-guide): preserve excerpt line terminators (#30717)
Johan Vaz committed
Nov 6, 2026 at 12:36 UTC
cae7e5cc57945b1e17704fb44b939cd5ac620967
2 files changed
+101
-22
packages/client-runtime/src/reviewGuideTour.test.ts
+92
-17
@@ -33,6 +33,56 @@ function parse(patch: string) {
33
34
const rows = fileDiffRows(parse(PATCH));
35
36
+const LINE_ENDING_CASES = [
37
+ {
38
+ name: "adding the final newline",
39
+ body: "@@ -1 +1 @@\n-same\n\\ No newline at end of file\n+same\n",
40
+ oldContents: "same",
41
+ newContents: "same\n",
42
+ },
43
+ {
44
+ name: "removing the final newline",
45
+ body: "@@ -1 +1 @@\n-same\n+same\n\\ No newline at end of file\n",
46
+ oldContents: "same\n",
47
+ newContents: "same",
48
+ },
49
+ {
50
+ name: "neither side having a final newline",
51
+ body: "@@ -1 +1 @@\n-old\n\\ No newline at end of file\n+new\n\\ No newline at end of file\n",
52
+ oldContents: "old",
53
+ newContents: "new",
54
+ },
55
+ {
56
+ name: "CRLF lines",
57
+ body: "@@ -1 +1 @@\n-old\r\n+new\r\n",
58
+ oldContents: "old\r\n",
59
+ newContents: "new\r\n",
60
+ },
61
+ {
62
+ name: "changing CRLF to LF",
63
+ body: "@@ -1 +1 @@\n-same\r\n+same\n",
64
+ oldContents: "same\r\n",
65
+ newContents: "same\n",
66
+ },
67
+ {
68
+ name: "context without a final newline",
69
+ body: "@@ -1,2 +1,2 @@\n-old\n+new\n tail\n\\ No newline at end of file\n",
70
+ oldContents: "old\ntail",
71
+ newContents: "new\ntail",
72
+ },
73
+ {
74
+ name: "CRLF context",
75
+ body: "@@ -1,2 +1,2 @@\n-old\r\n+new\r\n tail\r\n",
76
+ oldContents: "old\r\ntail\r\n",
77
+ newContents: "new\r\ntail\r\n",
78
+ },
79
+].map(({ body, ...testCase }) => ({
80
+ ...testCase,
81
+ patch: `diff --git a/src/tray.ts b/src/tray.ts\n--- a/src/tray.ts\n+++ b/src/tray.ts\n${body}`,
82
+ start: 1,
83
+ end: 2,
84
+}));
85
+
86
describe("fileDiffRows", () => {
87
it("numbers both sides through context, deletions and additions", () => {
88
expect(rows.map((row) => [row.kind, row.oldLine, row.newLine])).toEqual([
@@ -63,13 +113,20 @@ describe("excerptRows", () => {
113
});
114
115
describe("rowsToPatch", () => {
66
- it("cuts a slice that parses back to the same lines and numbers", () => {
67
- const shown = excerptRows(rows, { side: "new", start: 10, end: 13 });
68
- const slice = parse(rowsToPatch("src/tray.ts", "src/tray.ts", shown));
69
- expect(fileDiffRows(slice).map((row) => [row.kind, row.oldLine, row.newLine])).toEqual(
70
- shown.map((row) => [row.kind, row.oldLine, row.newLine]),
71
- );
72
- });
116
+ it.each([{ name: "LF slice", patch: PATCH, start: 10, end: 13 }, ...LINE_ENDING_CASES])(
117
+ "cuts a slice that parses back to the same lines and numbers: $name",
118
+ ({ patch, start, end }) => {
119
+ const original = parse(patch);
120
+ const shown = excerptRows(fileDiffRows(original), { side: "new", start, end });
121
+ const slice = parse(rowsToPatch("src/tray.ts", "src/tray.ts", shown));
122
+ expect(fileDiffRows(slice)).toEqual(shown);
123
+ expect(slice.hunks.map((hunk) => [hunk.noEOFCRDeletions, hunk.noEOFCRAdditions])).toEqual(
124
+ original.hunks
125
+ .filter((_, index) => shown.some((row) => row.hunk === index))
126
+ .map((hunk) => [hunk.noEOFCRDeletions, hunk.noEOFCRAdditions]),
127
+ );
128
+ },
129
+ );
130
131
it("starts a hunk per source hunk so separate hunks stay separate", () => {
132
const shown = excerptRows(rows, { side: "old", start: 14, end: 42 });
@@ -95,16 +152,34 @@ describe("excerptExpansionOldContents", () => {
152
newFile.splice(40, 2, "keep();\n", "tail();\n");
153
const newContents = newFile.join("");
154
98
- it("puts back only the excerpt's lines and keeps both sides numbered as in the file", () => {
99
- const shown = excerptRows(rows, { side: "new", start: 11, end: 12 });
100
- const old = excerptExpansionOldContents(newContents, shown);
101
- if (old === null) throw new Error("expected contents");
102
- const oldLines = old.split("\n");
103
- expect(oldLines[10]).toBe("const size = pick();");
104
- expect(oldLines[9]).toBe("const a = 1;");
105
- expect(oldLines[11]).toBe("const b = 2;");
106
- expect(oldLines.length).toBe(newContents.split("\n").length - 1);
107
- });
155
+ it.each([
156
+ {
157
+ name: "LF slice",
158
+ patch: PATCH,
159
+ start: 11,
160
+ end: 12,
161
+ newContents,
162
+ oldContents: [...newFile.slice(0, 10), "const size = pick();\n", ...newFile.slice(12)].join(
163
+ "",
164
+ ),
165
+ },
166
+ ...LINE_ENDING_CASES,
167
+ {
168
+ name: "partial replacement of an unterminated old EOF",
169
+ patch:
170
+ "diff --git a/src/tray.ts b/src/tray.ts\n--- a/src/tray.ts\n+++ b/src/tray.ts\n@@ -1 +1,3 @@\n-a\n\\ No newline at end of file\n+a\n+b\n+c\n",
171
+ start: 1,
172
+ end: 1,
173
+ newContents: "a\nb\nc\n",
174
+ oldContents: "a\nb\nc\n",
175
+ },
176
+ ])(
177
+ "puts back only the excerpt's lines and keeps both sides numbered as in the file: $name",
178
+ ({ patch, start, end, newContents, oldContents }) => {
179
+ const shown = excerptRows(fileDiffRows(parse(patch)), { side: "new", start, end });
180
+ expect(excerptExpansionOldContents(newContents, shown)).toBe(oldContents);
181
+ },
182
+ );
183
184
it("does not try to expand an excerpt that spans hunks", () => {
185
const shown = excerptRows(rows, { side: "old", start: 14, end: 42 });
packages/client-runtime/src/reviewGuideTour.ts
+9
-5
@@ -140,7 +140,10 @@ export function rowsToPatch(
140
const oldStart = oldCount === 0 ? Math.max(head.oldAt - 1, 0) : head.oldAt;
141
const newStart = newCount === 0 ? Math.max(head.newAt - 1, 0) : head.newAt;
142
lines.push(`@@ -${oldStart},${oldCount} +${newStart},${newCount} @@`);
143
- for (const row of group) lines.push(`${ROW_PREFIX[row.kind]}${row.text.replace(/\r?\n$/, "")}`);
143
+ for (const row of group) {
144
+ lines.push(`${ROW_PREFIX[row.kind]}${row.text.replace(/\n$/, "")}`);
145
+ if (!row.text.endsWith("\n")) lines.push("\\ No newline at end of file");
146
+ }
147
start = end + 1;
148
}
149
return `${lines.join("\n")}\n`;
@@ -165,10 +168,11 @@ export function excerptExpansionOldContents(
168
oldBefore >= prefix.length
169
? [...Array.from({ length: oldBefore - prefix.length }, () => "\n"), ...prefix]
170
: prefix.slice(prefix.length - oldBefore);
168
- const oldRows = rows
169
- .filter((row) => row.oldLine !== null)
170
- .map((row) => (row.text.endsWith("\n") ? row.text : `${row.text}\n`));
171
- return [...oldPrefix, ...oldRows, ...newLines.slice(before + newCount)].join("");
171
+ const oldRows = rows.filter((row) => row.oldLine !== null).map((row) => row.text);
172
+ const suffix = newLines.slice(before + newCount);
173
+ // Synthetic context still needs its own line after an old EOF row.
174
+ if (suffix.length > 0 && oldRows.at(-1)?.endsWith("\n") === false) oldRows.push("\n");
175
+ return [...oldPrefix, ...oldRows, ...suffix].join("");
176
}
177
178
export function letterStopNotes(stop: PullRequestReviewGuideStop) {