@opencode-ai/util 0.0.0-next-16093 → 0.0.0-next-16095

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/patch.js +30 -4
  2. package/package.json +1 -1
package/dist/patch.js CHANGED
@@ -41,7 +41,7 @@ export function parse(patchText) {
41
41
  }
42
42
  if (header.startsWith("*** Add File: ")) {
43
43
  const path = header.slice("*** Add File: ".length).trim();
44
- const parsed = parseAdd(lines, index + 1, end);
44
+ const parsed = parseAdd(lines, index + 1, end, path);
45
45
  if ("error" in parsed)
46
46
  return Result.fail(parsed.error);
47
47
  hunks.push({ type: "add", path, contents: parsed.content });
@@ -50,6 +50,17 @@ export function parse(patchText) {
50
50
  }
51
51
  if (header.startsWith("*** Delete File: ")) {
52
52
  const path = header.slice("*** Delete File: ".length).trim();
53
+ const next = lines[index + 1]?.trim();
54
+ if (index + 1 < end && next !== undefined && !isBoundary(next)) {
55
+ if (next.startsWith("*** ")) {
56
+ return Result.fail(new InvalidHunkError({ line: next, lineNumber: index + 2 }));
57
+ }
58
+ return Result.fail(new InvalidHunkError({
59
+ line: next,
60
+ lineNumber: index + 2,
61
+ reason: `Unexpected line after Delete File '${path}': '${next}'. Delete hunks do not contain body lines`,
62
+ }));
63
+ }
53
64
  hunks.push({ type: "delete", path });
54
65
  index++;
55
66
  continue;
@@ -64,7 +75,11 @@ export function parse(patchText) {
64
75
  if (move === "*** Move to:" || move?.startsWith("*** Move to: ")) {
65
76
  movePath = move.slice("*** Move to: ".length).trim();
66
77
  if (!movePath) {
67
- return Result.fail(new InvalidHunkError({ line: lines[next].trim(), lineNumber: next + 1 }));
78
+ return Result.fail(new InvalidHunkError({
79
+ line: lines[next].trim(),
80
+ lineNumber: next + 1,
81
+ reason: `Move destination for '${path}' must not be empty`,
82
+ }));
68
83
  }
69
84
  next++;
70
85
  }
@@ -97,12 +112,19 @@ export function joinBom(text, bom) {
97
112
  const stripped = splitBom(text).text;
98
113
  return bom ? `\uFEFF${stripped}` : stripped;
99
114
  }
100
- function parseAdd(lines, start, end) {
115
+ function parseAdd(lines, start, end, path) {
101
116
  const content = [];
102
117
  let index = start;
103
118
  while (index < end && !isBoundary(lines[index].trim())) {
104
119
  if (!lines[index].startsWith("+")) {
105
- return { error: new InvalidHunkError({ line: lines[index].trim(), lineNumber: index + 1 }) };
120
+ const line = lines[index].trim();
121
+ return {
122
+ error: new InvalidHunkError({
123
+ line,
124
+ lineNumber: index + 1,
125
+ reason: `Invalid Add File line for '${path}': expected a line starting with '+', got '${line}'`,
126
+ }),
127
+ };
106
128
  }
107
129
  content.push(lines[index].slice(1));
108
130
  index++;
@@ -263,6 +285,10 @@ function computeReplacements(lines, path, chunks) {
263
285
  newLines = newLines.slice(0, -1);
264
286
  found = seek(lines, oldLines, lineIndex, chunk.endOfFile);
265
287
  }
288
+ if (found === -1 && chunk.oldLines.every((line) => line === "")) {
289
+ const expected = chunk.oldLines.length === 1 ? "an expected blank line" : `${chunk.oldLines.length} consecutive blank lines`;
290
+ throw new Error(`Failed to find ${expected} in ${path}`);
291
+ }
266
292
  if (found === -1)
267
293
  throw new Error(`Failed to find expected lines in ${path}:\n${chunk.oldLines.join("\n")}`);
268
294
  replacements.push([found, oldLines.length, newLines]);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
3
  "name": "@opencode-ai/util",
4
- "version": "0.0.0-next-16093",
4
+ "version": "0.0.0-next-16095",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "repository": {