@criogaid/pi-hashline-edit 0.13.0 → 0.15.0

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 (152) hide show
  1. package/README.md +18 -12
  2. package/README.zh-CN.md +18 -12
  3. package/package.json +8 -1
  4. package/src/core/AGENTS.md +3 -1
  5. package/src/core/apply.ts +110 -117
  6. package/src/core/character-set.ts +2 -3
  7. package/src/core/copy-span.ts +15 -0
  8. package/src/core/errors.ts +24 -9
  9. package/src/core/lines.ts +71 -17
  10. package/src/core/ranges.ts +5 -4
  11. package/src/core/replace-schema.ts +3 -1
  12. package/src/core/replace.ts +50 -29
  13. package/src/core/search-analysis.ts +21 -22
  14. package/src/core/search.ts +15 -32
  15. package/src/core/types.ts +3 -0
  16. package/src/index.ts +47 -39
  17. package/src/pi/AGENTS.md +30 -19
  18. package/src/pi/action-fusion.ts +284 -211
  19. package/src/pi/argument-diagnostics.ts +50 -37
  20. package/src/pi/argument-error.ts +2 -2
  21. package/src/pi/argument-validation.ts +163 -66
  22. package/src/pi/budgets.ts +12 -0
  23. package/src/pi/diagnostic-buffer.ts +12 -11
  24. package/src/pi/edit-tool.ts +138 -115
  25. package/src/pi/error-text.ts +10 -2
  26. package/src/pi/failure-context.ts +24 -37
  27. package/src/pi/file-commit.ts +131 -164
  28. package/src/pi/file-read.ts +122 -31
  29. package/src/pi/fusion-arguments.ts +214 -0
  30. package/src/pi/fusion-card.ts +102 -60
  31. package/src/pi/grep-candidates.ts +35 -22
  32. package/src/pi/grep-output.ts +197 -190
  33. package/src/pi/grep-render.ts +22 -13
  34. package/src/pi/grep-reread.ts +7 -6
  35. package/src/pi/grep-scope.ts +31 -16
  36. package/src/pi/grep-search.ts +56 -47
  37. package/src/pi/grep-tool.ts +35 -33
  38. package/src/pi/grep-worker.ts +3 -4
  39. package/src/pi/mutation-diff.ts +238 -0
  40. package/src/pi/mutation-result.ts +102 -79
  41. package/src/pi/mutation-runner.ts +107 -84
  42. package/src/pi/native-bash.ts +41 -0
  43. package/src/pi/path.ts +59 -6
  44. package/src/pi/read-tool.ts +173 -138
  45. package/src/pi/regex-worker.ts +14 -8
  46. package/src/pi/render.ts +101 -54
  47. package/src/pi/replace-regex.ts +1 -1
  48. package/src/pi/replace-tool.ts +14 -7
  49. package/src/pi/report-cause.ts +34 -17
  50. package/src/pi/report-schema.ts +184 -85
  51. package/src/pi/report.ts +133 -64
  52. package/src/pi/rg-process.ts +68 -48
  53. package/src/pi/schema.ts +4 -0
  54. package/src/pi/text-stream.ts +81 -87
  55. package/src/pi/workspace-cwd.js +2 -0
  56. package/src/pi/workspace.ts +46 -0
  57. package/src/pi/write-tool.ts +19 -13
  58. package/src/core/apply.test.ts +0 -644
  59. package/src/core/case-fold.test.ts +0 -35
  60. package/src/core/character-set.test.ts +0 -97
  61. package/src/core/errors.test.ts +0 -39
  62. package/src/core/hash.test.ts +0 -64
  63. package/src/core/lines.test.ts +0 -63
  64. package/src/core/ranges.test.ts +0 -123
  65. package/src/core/replace.test.ts +0 -187
  66. package/src/core/search-analysis.test.ts +0 -210
  67. package/src/core/search-matrix.test.ts +0 -64
  68. package/src/core/search-matrix.testing.ts +0 -171
  69. package/src/core/search.test.ts +0 -132
  70. package/src/core/text.test.ts +0 -52
  71. package/src/integration/config-registration.test.ts +0 -291
  72. package/src/integration/fusion-dispatch.test.ts +0 -320
  73. package/src/integration/grep-errors.test.ts +0 -81
  74. package/src/integration/grep-flags.test.ts +0 -76
  75. package/src/integration/grep-js-completeness.test.ts +0 -278
  76. package/src/integration/grep-line-candidates.test.ts +0 -201
  77. package/src/integration/grep-matrix.test.ts +0 -118
  78. package/src/integration/grep-order.test.ts +0 -78
  79. package/src/integration/grep-path-matrix.test.ts +0 -128
  80. package/src/integration/grep-prefilter-coverage.test.ts +0 -204
  81. package/src/integration/grep-process-limits.test.ts +0 -122
  82. package/src/integration/grep-regex.test.ts +0 -412
  83. package/src/integration/grep-rg.test.ts +0 -1659
  84. package/src/integration/grep-scan-budget.test.ts +0 -111
  85. package/src/integration/grep-translation.test.ts +0 -199
  86. package/src/integration/large-text.test.ts +0 -70
  87. package/src/integration/model-budget-lifecycle.test.ts +0 -186
  88. package/src/integration/mutation-file-types.test.ts +0 -30
  89. package/src/integration/mutation-file-types.testing.ts +0 -33
  90. package/src/integration/npm-package.test.ts +0 -19
  91. package/src/integration/npm-package.testing.ts +0 -243
  92. package/src/integration/publication-read-changes.test.ts +0 -454
  93. package/src/integration/read-changes.test.ts +0 -98
  94. package/src/integration/replace-package.test.ts +0 -22
  95. package/src/integration/replace-package.testing.ts +0 -36
  96. package/src/integration/replace-worker-boundary.test.ts +0 -127
  97. package/src/integration/report-replay.test.ts +0 -91
  98. package/src/integration/utf8-errors.test.ts +0 -46
  99. package/src/pi/action-fusion.test.ts +0 -659
  100. package/src/pi/action-fusion.testing.ts +0 -38
  101. package/src/pi/anchor-format.test.ts +0 -124
  102. package/src/pi/argument-error.testing.ts +0 -46
  103. package/src/pi/argument-format.test.ts +0 -196
  104. package/src/pi/argument-validation.test.ts +0 -1007
  105. package/src/pi/codemode.test.ts +0 -101
  106. package/src/pi/config.test.ts +0 -204
  107. package/src/pi/diagnostic-buffer.test.ts +0 -115
  108. package/src/pi/directory-sync.test.ts +0 -57
  109. package/src/pi/execute.test.ts +0 -1257
  110. package/src/pi/extension-loading.test.ts +0 -14
  111. package/src/pi/extension-loading.testing.ts +0 -3
  112. package/src/pi/failure-context.test.ts +0 -370
  113. package/src/pi/failure-path.test.ts +0 -289
  114. package/src/pi/file-commit.test.ts +0 -628
  115. package/src/pi/file-read.test.ts +0 -29
  116. package/src/pi/file-snapshot.test.ts +0 -22
  117. package/src/pi/fusion-card-interaction.test.ts +0 -143
  118. package/src/pi/fusion-card.test.ts +0 -353
  119. package/src/pi/grep-candidates.test.ts +0 -26
  120. package/src/pi/grep-matrix.test.ts +0 -68
  121. package/src/pi/grep-order.test.ts +0 -447
  122. package/src/pi/grep-output.testing.ts +0 -24
  123. package/src/pi/grep-rg.testing.ts +0 -73
  124. package/src/pi/grep.test.ts +0 -1670
  125. package/src/pi/model-budgets.test.ts +0 -96
  126. package/src/pi/mutation-outcome.testing.ts +0 -20
  127. package/src/pi/mutation-result.test.ts +0 -96
  128. package/src/pi/output-budgets.test.ts +0 -221
  129. package/src/pi/pi.test.ts +0 -158
  130. package/src/pi/read-delegation.test.ts +0 -119
  131. package/src/pi/read.test.ts +0 -735
  132. package/src/pi/regex-worker.test.ts +0 -42
  133. package/src/pi/registration.test.ts +0 -549
  134. package/src/pi/render.test.ts +0 -841
  135. package/src/pi/replace.test.ts +0 -634
  136. package/src/pi/report-failure.testing.ts +0 -27
  137. package/src/pi/report.test.ts +0 -533
  138. package/src/pi/result-safety.test.ts +0 -597
  139. package/src/pi/rg-process.test.ts +0 -133
  140. package/src/pi/rg-regex.test.ts +0 -40
  141. package/src/pi/text-stream.test.ts +0 -281
  142. package/src/pi/tool-call.testing.ts +0 -61
  143. package/src/pi/tool-factories.testing.ts +0 -59
  144. package/src/pi/write.test.ts +0 -256
  145. package/src/testing/extension-loader.testing.ts +0 -24
  146. package/src/testing/installation.testing.ts +0 -77
  147. package/src/testing/process.testing.ts +0 -33
  148. package/src/testing/regex-host.testing.ts +0 -20
  149. package/src/testing/regex-translation-corpus.ts +0 -267
  150. package/src/testing/rg-line-listing.fixture.mjs +0 -7
  151. package/src/testing/run-tests.ts +0 -47
  152. package/src/testing/session.testing.ts +0 -127
@@ -1,97 +0,0 @@
1
- import assert from "node:assert/strict";
2
- import { test } from "node:test";
3
- import { RegExpParser } from "@eslint-community/regexpp";
4
- import {
5
- contains,
6
- intersect,
7
- subtract,
8
- nativeCharacterRanges,
9
- surrogatePair,
10
- } from "./character-set.ts";
11
- import { unsupportedByHost } from "../testing/regex-host.testing.ts";
12
-
13
- for (const [source, flags] of [
14
- ["\\uD83D", ""],
15
- [".", ""],
16
- [".", "s"],
17
- ] as const) {
18
- test(`native character intervals ${source} /${flags} → agree with the host engine`, {
19
- skip: unsupportedByHost([source], flags),
20
- }, () => {
21
- const ranges = nativeCharacterRanges(source, flags);
22
- const native = new RegExp(`^(?:${source})`, flags);
23
- for (const char of [
24
- "a",
25
- "A",
26
- "1",
27
- "\n",
28
- "\r",
29
- "\u2028",
30
- "\u0558",
31
- "ſ",
32
- "K",
33
- "Σ",
34
- "ς",
35
- "é",
36
- "𐐀",
37
- "😀",
38
- "\ud83d",
39
- "�",
40
- ]) {
41
- const isUnit = char.length === 1;
42
- assert.equal(
43
- contains(ranges, char.codePointAt(0)!),
44
- isUnit && native.exec(char)?.[0] === char,
45
- `${source}: ${JSON.stringify(char)}`,
46
- );
47
- }
48
- });
49
- }
50
-
51
- test("interval intersection and difference → preserve included and excluded endpoints", () => {
52
- assert.deepEqual(
53
- intersect(
54
- [
55
- [1, 4],
56
- [8, 10],
57
- ],
58
- [[3, 8]],
59
- ),
60
- [
61
- [3, 4],
62
- [8, 8],
63
- ],
64
- );
65
- assert.deepEqual(
66
- subtract(
67
- [[1, 10]],
68
- [
69
- [0, 1],
70
- [4, 7],
71
- [10, 12],
72
- ],
73
- ),
74
- [
75
- [2, 3],
76
- [8, 9],
77
- ],
78
- );
79
- });
80
-
81
- test("adjacent surrogate Characters → decode a code point without joining other elements", () => {
82
- const parser = new RegExpParser();
83
- for (const [pattern, decoded] of [
84
- ["\\uD800\\uDC00", "\u{10000}"],
85
- ["\\uDBFF\\uDFFF", "\u{10ffff}"],
86
- ["😀", "😀"],
87
- ["\\uD83D\\uDE00", "😀"],
88
- ["\\uDE00\\uD83D", undefined],
89
- ["\\uD83D", undefined],
90
- ["\\uD83Dx", undefined],
91
- ["x\\uDE00", undefined],
92
- ["\\uD83D(?:)\\uDE00", undefined],
93
- ] as const) {
94
- const [element, next] = parser.parsePattern(pattern).alternatives[0].elements;
95
- assert.equal(surrogatePair(element, next), decoded?.codePointAt(0), pattern);
96
- }
97
- });
@@ -1,39 +0,0 @@
1
- import assert from "node:assert/strict";
2
- import { test } from "node:test";
3
- import { filesystemErrorCode, HashlineError, PATH_ERROR_MESSAGES } from "./errors.ts";
4
-
5
- // npm run typecheck checks these invalid callers; they must never run.
6
- function invalidErrorConstruction() {
7
- // @ts-expect-error NO_MATCH requires its unmatched fact.
8
- new HashlineError("NO_MATCH", "missing");
9
- // @ts-expect-error NO_MATCH facts cannot contain unrelated fields.
10
- new HashlineError("NO_MATCH", "missing", { facts: { unmatched: [], ids: [] } });
11
- // @ts-expect-error TARGET_EXISTS has no code facts.
12
- new HashlineError("TARGET_EXISTS", "exists", { facts: { unmatched: [] } });
13
- // @ts-expect-error Only PATH_NOT_FOUND permits useCreate recovery.
14
- new HashlineError("NO_MATCH", "missing", { facts: { unmatched: [] }, next: "useCreate" });
15
- }
16
-
17
- test("classified code with required facts → retains facts and native cause", () => {
18
- const cause = new Error("native failure");
19
- const failure = new HashlineError("NO_MATCH", "missing", {
20
- facts: { unmatched: ["replacements[2]"] },
21
- cause,
22
- });
23
- assert.equal(failure.errorCode, "NO_MATCH");
24
- assert.deepEqual(failure.facts, { unmatched: ["replacements[2]"] });
25
- assert.equal(failure.cause, cause);
26
- });
27
-
28
- test("filesystem errors → classify missing paths and directories consistently", () => {
29
- assert.equal(PATH_ERROR_MESSAGES.PATH_NOT_FOUND, "Path not found.");
30
- assert.equal(PATH_ERROR_MESSAGES.NOT_REGULAR_FILE, "Path is not a regular file.");
31
- for (const [code, expected] of [
32
- ["ENOENT", "PATH_NOT_FOUND"],
33
- ["EISDIR", "NOT_REGULAR_FILE"],
34
- ["EPERM", "FILESYSTEM_ERROR"],
35
- ] as const) {
36
- assert.equal(filesystemErrorCode({ code }), expected);
37
- }
38
- assert.equal(filesystemErrorCode(new Error()), undefined);
39
- });
@@ -1,64 +0,0 @@
1
- import { test } from "node:test";
2
- import assert from "node:assert/strict";
3
- import { computeLineHash } from "./hash.ts";
4
-
5
- const ALLOWED = new Set("0123456789ABCDEFGHJKMNPQRSTVWXYZ");
6
-
7
- test("computeLineHash is stable and uses the configured base32 length", () => {
8
- const a = computeLineHash(3, "code", 4);
9
- const b = computeLineHash(3, "code", 4);
10
- assert.equal(a, b);
11
- assert.equal(a.length, 4);
12
- for (const ch of a) assert.ok(ALLOWED.has(ch), `bad char ${ch}`);
13
- for (let i = 0; i < 2000; i++) {
14
- const hash = computeLineHash(i + 1, `line ${i}`, 4);
15
- for (const ch of hash) assert.ok(ALLOWED.has(ch), `bad char ${ch} in ${hash}`);
16
- }
17
- for (let i = 1; i <= 20; i++) {
18
- assert.equal(computeLineHash(i, `content ${i}`, 8).length, 8);
19
- }
20
- });
21
-
22
- test("different line number → different hash (even for identical content)", () => {
23
- assert.notEqual(computeLineHash(2, "", 4), computeLineHash(5, "", 4));
24
- assert.notEqual(computeLineHash(1, "}", 4), computeLineHash(2, "}", 4));
25
- });
26
-
27
- test("different content → different hash", () => {
28
- assert.notEqual(computeLineHash(1, "a", 4), computeLineHash(1, "b", 4));
29
- });
30
-
31
- test("truncated checksums can collide", () => {
32
- assert.equal(computeLineHash(1, "const value = 558;", 4), "TM02");
33
- assert.equal(computeLineHash(1, "const value = 9344;", 4), "TM02");
34
- });
35
-
36
- test("computeLineHash produces output identical to hashing full interpolated string", () => {
37
- function referenceHash(line: number, content: string, len: number): string {
38
- let h = 0x811c9dc5;
39
- const str = `${line}\n${content}`;
40
- for (let i = 0; i < str.length; i++) {
41
- h ^= str.charCodeAt(i);
42
- h = Math.imul(h, 0x01000193);
43
- }
44
- let s = "";
45
- const BASE32 = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
46
- let n = h >>> 0;
47
- for (let i = 0; i < len; i++) {
48
- s = BASE32[n & 31] + s;
49
- n = n >>> 5;
50
- }
51
- return s;
52
- }
53
- for (const line of [1, 9, 10, 99, 100, 999, 10000]) {
54
- for (const content of [
55
- "",
56
- "a",
57
- "function test() {}",
58
- "long line with symbols !@#$%^&*()_+-=[]{}|;:,.<>?/`~",
59
- ]) {
60
- assert.equal(computeLineHash(line, content, 4), referenceHash(line, content, 4));
61
- assert.equal(computeLineHash(line, content, 6), referenceHash(line, content, 6));
62
- }
63
- }
64
- });
@@ -1,63 +0,0 @@
1
- import { test } from "node:test";
2
- import assert from "node:assert/strict";
3
- import {
4
- createLfTextView,
5
- normalizeLineEndings,
6
- splitLines,
7
- detectLineEnding,
8
- hasFinalNewline,
9
- } from "./lines.ts";
10
-
11
- test("splitLines edge cases", () => {
12
- assert.deepEqual(splitLines(""), []);
13
- assert.deepEqual(splitLines("a"), ["a"]);
14
- assert.deepEqual(splitLines("a\nb"), ["a", "b"]);
15
- assert.deepEqual(splitLines("a\nb\n"), ["a", "b"]);
16
- assert.deepEqual(splitLines("a\n\n"), ["a", ""]);
17
- assert.deepEqual(splitLines("\n"), [""]);
18
- });
19
-
20
- test("splitLines strips CRLF \\r", () => {
21
- assert.deepEqual(splitLines("a\r\nb\r\n"), ["a", "b"]);
22
- assert.deepEqual(splitLines("a\r\nb"), ["a", "b"]);
23
- });
24
-
25
- test("splitLines handles mixed line endings and standalone CR correctly", () => {
26
- assert.deepEqual(splitLines("a\r\nb\nc\r\n"), ["a", "b", "c"]);
27
- assert.deepEqual(splitLines("a\rb\r\n"), ["a\rb"]);
28
- assert.deepEqual(splitLines("a\rb\nc"), ["a\rb", "c"]);
29
- assert.deepEqual(splitLines("\r\n"), [""]);
30
- assert.deepEqual(splitLines("\r\n\r\n"), ["", ""]);
31
- assert.deepEqual(splitLines("a\r\n\r\nb"), ["a", "", "b"]);
32
- assert.deepEqual(splitLines("a\r\n\r\n"), ["a", ""]);
33
- });
34
-
35
- test("hasFinalNewline", () => {
36
- assert.equal(hasFinalNewline("a\nb"), false);
37
- assert.equal(hasFinalNewline("a\nb\n"), true);
38
- assert.equal(hasFinalNewline("a\r\nb"), false);
39
- assert.equal(hasFinalNewline("a\r\nb\r\n"), true);
40
- assert.equal(hasFinalNewline("a"), false);
41
- // no lines, no terminator — rejoining [] yields "" either way
42
- assert.equal(hasFinalNewline(""), true);
43
- });
44
-
45
- test("detectLineEnding", () => {
46
- assert.equal(detectLineEnding("a\nb\n"), "lf");
47
- assert.equal(detectLineEnding("a\r\nb\r\n"), "crlf");
48
- assert.equal(detectLineEnding("a\nb\r\nc\n"), "crlf");
49
- });
50
-
51
- test("LF views map every UTF-16 boundary back to original CRLF bytes", () => {
52
- for (const source of ["", "a\nb", "\uFEFF😀\r\nb\nc\r\n", "\r\n\r\n", "a\rb\r\r\nc"]) {
53
- const view = createLfTextView(source);
54
- assert.equal(view.text, normalizeLineEndings(source));
55
- assert.equal(view.sourceOffset(view.text.length), source.length);
56
- for (let offset = 0; offset <= view.text.length; offset++) {
57
- assert.equal(
58
- normalizeLineEndings(source.slice(0, view.sourceOffset(offset))),
59
- view.text.slice(0, offset),
60
- );
61
- }
62
- }
63
- });
@@ -1,123 +0,0 @@
1
- import assert from "node:assert/strict";
2
- import { test } from "node:test";
3
- import { findSortedRangeConflict, mergeRanges } from "./ranges.ts";
4
- type HalfOpenRange = Parameters<typeof findSortedRangeConflict>[0][number];
5
-
6
- test("sorted ranges sharing or crossing boundaries → report conflicts according to insertion policy", () => {
7
- const cases: [HalfOpenRange[], number | undefined, number | undefined][] = [
8
- [[], undefined, undefined],
9
- [
10
- [
11
- [0, 2],
12
- [2, 4],
13
- ],
14
- undefined,
15
- undefined,
16
- ],
17
- [
18
- [
19
- [0, 2],
20
- [2, 2],
21
- ],
22
- undefined,
23
- undefined,
24
- ],
25
- [
26
- [
27
- [0, 0],
28
- [0, 0],
29
- ],
30
- 1,
31
- 1,
32
- ],
33
- [
34
- [
35
- [0, 0],
36
- [0, 2],
37
- ],
38
- 1,
39
- undefined,
40
- ],
41
- [
42
- [
43
- [0, 0],
44
- [0, 0],
45
- [0, 2],
46
- ],
47
- 1,
48
- 1,
49
- ],
50
- [
51
- [
52
- [0, 3],
53
- [1, 1],
54
- ],
55
- 1,
56
- 1,
57
- ],
58
- [
59
- [
60
- [0, 3],
61
- [1, 2],
62
- ],
63
- 1,
64
- 1,
65
- ],
66
- [
67
- [
68
- [0, 2],
69
- [0, 3],
70
- ],
71
- 1,
72
- 1,
73
- ],
74
- [
75
- [
76
- [0, 2],
77
- [0, 2],
78
- ],
79
- 1,
80
- 1,
81
- ],
82
- [
83
- [
84
- [0, 1],
85
- [1, 2],
86
- [1, 3],
87
- ],
88
- 2,
89
- 2,
90
- ],
91
- ];
92
- for (const [ranges, strictConflict, insertionBeforeRangeConflict] of cases) {
93
- for (const insertBeforeRange of [false, true]) {
94
- const expected = insertBeforeRange ? insertionBeforeRangeConflict : strictConflict;
95
- for (const shift of [0, 10]) {
96
- assert.equal(
97
- findSortedRangeConflict(
98
- ranges.map(([start, end]) => [start + shift, end + shift]),
99
- insertBeforeRange,
100
- ),
101
- expected,
102
- JSON.stringify({ ranges, insertBeforeRange, shift }),
103
- );
104
- }
105
- }
106
- }
107
- });
108
-
109
- test("range merging preserves input and coalesces overlap, containment, and adjacency", () => {
110
- const ranges: HalfOpenRange[] = [
111
- [7, 9],
112
- [1, 3],
113
- [2, 3],
114
- [3, 5],
115
- ];
116
- const before = structuredClone(ranges);
117
- assert.deepEqual(mergeRanges(ranges), [
118
- [1, 5],
119
- [7, 9],
120
- ]);
121
- assert.deepEqual(ranges, before);
122
- assert.deepEqual(mergeRanges([]), []);
123
- });
@@ -1,187 +0,0 @@
1
- import { test } from "node:test";
2
- import assert from "node:assert/strict";
3
- import { applyReplacements } from "./replace.ts";
4
- import type { Replacement } from "./replace-schema.ts";
5
- import { HashlineError } from "./errors.ts";
6
-
7
- test("replacement at the file start → retains the original BOM at byte zero", async (t) => {
8
- const cases = [
9
- {
10
- name: "zero-width insertion",
11
- source: "\uFEFFfoo\r\nbar\n",
12
- find: "^",
13
- replace: "X",
14
- expected: "\uFEFFXfoo\r\nbar\n",
15
- },
16
- {
17
- name: "whole-file replacement",
18
- source: "\uFEFFfoo",
19
- find: "^[\\s\\S]*$",
20
- replace: "X",
21
- expected: "\uFEFFX",
22
- },
23
- { name: "copied BOM", source: "\uFEFFfoo", find: "^.", replace: "$&X", expected: "\uFEFFXfoo" },
24
- { name: "consumed BOM", source: "\uFEFFfoo", find: "^.", replace: "", expected: "\uFEFFfoo" },
25
- {
26
- name: "inserted body BOM",
27
- source: "\uFEFFfoo",
28
- find: "^",
29
- replace: "\uFEFF",
30
- expected: "\uFEFF\uFEFFfoo",
31
- },
32
- {
33
- name: "capture copied into body",
34
- source: "\uFEFFfoo",
35
- find: "^(.*)",
36
- replace: "X$1",
37
- expected: "\uFEFFX\uFEFFfoo",
38
- },
39
- { name: "BOM-only file", source: "\uFEFF", find: "^", replace: "X", expected: "\uFEFFX" },
40
- {
41
- name: "body BOM",
42
- source: "foo\uFEFFbar",
43
- find: "^",
44
- replace: "X",
45
- expected: "Xfoo\uFEFFbar",
46
- },
47
- ];
48
- for (const { name, source, find, replace, expected } of cases) {
49
- await t.test(name, () => {
50
- assert.deepEqual(applyReplacements(source, [{ literal: false, find, replace }]), {
51
- text: expected,
52
- count: 1,
53
- });
54
- });
55
- }
56
- });
57
-
58
- test("replacement joins beside LF → preserve standalone CR content", async (t) => {
59
- const literal = (find: string, replace: string): Replacement => ({
60
- find,
61
- replace,
62
- literal: true,
63
- });
64
- const cases = [
65
- {
66
- name: "replacement before original LF",
67
- source: "a\nb",
68
- rules: [literal("a", "x\r")],
69
- expected: "x\r\r\nb",
70
- },
71
- {
72
- name: "original CR before replacement LF",
73
- source: "a\rb",
74
- rules: [literal("b", "\nB")],
75
- expected: "a\r\r\nB",
76
- },
77
- {
78
- name: "deletion joins original CR and LF",
79
- source: "a\rx\nb",
80
- rules: [literal("x", "")],
81
- expected: "a\r\r\nb",
82
- },
83
- {
84
- name: "replacement contains CR before a new LF",
85
- source: "a\nb",
86
- rules: [literal("a", "x\r\r\ny")],
87
- expected: "x\r\r\ny\nb",
88
- },
89
- {
90
- name: "replacement contains CR before a reused LF",
91
- source: "a\nb",
92
- rules: [literal("a\nb", "x\r\r\ny")],
93
- expected: "x\r\r\ny",
94
- },
95
- {
96
- name: "captured CR before replacement LF",
97
- source: "a\rb\nc",
98
- rules: [{ find: "(a\r)b", replace: "$1\nB", literal: false }],
99
- expected: "a\r\r\nB\nc",
100
- },
101
- {
102
- name: "adjacent replacements",
103
- source: "ab",
104
- rules: [literal("a", "x\r"), literal("b", "\ny")],
105
- expected: "x\r\r\ny",
106
- },
107
- {
108
- name: "adjacent replacements separated by a deletion",
109
- source: "axb",
110
- rules: [literal("a", "x\r"), literal("x", ""), literal("b", "\ny")],
111
- expected: "x\r\r\ny",
112
- },
113
- {
114
- name: "zero-width insertion before LF",
115
- source: "a\nb",
116
- rules: [{ find: "(?=\\n)", replace: "\r", literal: false }],
117
- expected: "a\r\r\nb",
118
- },
119
- {
120
- name: "replacement before original CRLF",
121
- source: "a\r\nb",
122
- rules: [literal("a", "x\r")],
123
- expected: "x\r\r\nb",
124
- },
125
- {
126
- name: "unchanged CR content and CRLF separator",
127
- source: "a\r\r\nb",
128
- rules: [literal("a\r\r\nb", "a\r\r\nb")],
129
- expected: "a\r\r\nb",
130
- },
131
- {
132
- name: "CR at EOF needs no separator",
133
- source: "ab",
134
- rules: [literal("b", "x\r")],
135
- expected: "ax\r",
136
- },
137
- ];
138
- for (const { name, source, rules, expected } of cases) {
139
- await t.test(name, () => {
140
- assert.equal(applyReplacements(source, rules).text, expected);
141
- });
142
- }
143
- });
144
-
145
- test("replacement rejection → reports all unmatched rules or the conflicting rule pair and line", async (t) => {
146
- const literal = (find: string): Replacement => ({ find, replace: "x", literal: true });
147
- const cases = [
148
- {
149
- name: "multiple unmatched rules",
150
- source: "present",
151
- rules: [literal("missing"), literal("present"), literal("absent")],
152
- code: "NO_MATCH",
153
- message: "Rules match nothing in the file.",
154
- facts: { unmatched: ["replacements[0]", "replacements[2]"] },
155
- },
156
- ...["\n", "\r\n"].map((eol) => ({
157
- name: `overlap in reversed input order with ${JSON.stringify(eol)}`,
158
- source: `head${eol}abcd${eol}tail`,
159
- rules: [literal("cd"), literal("bc")],
160
- code: "OVERLAPPING_MATCHES",
161
- message: "Matches of two rules overlap.",
162
- facts: { conflict: ["replacements[0]", "replacements[1]"], line: 2 },
163
- })),
164
- {
165
- name: "overlap on first line",
166
- source: "abcd",
167
- rules: [literal("ab"), literal("bc")],
168
- code: "OVERLAPPING_MATCHES",
169
- message: "Matches of two rules overlap.",
170
- facts: { conflict: ["replacements[0]", "replacements[1]"], line: 1 },
171
- },
172
- ];
173
- for (const { name, source, rules, code, message, facts } of cases) {
174
- await t.test(name, () => {
175
- assert.throws(
176
- () => applyReplacements(source, rules),
177
- (error: unknown) => {
178
- assert.ok(error instanceof HashlineError);
179
- assert.equal(error.errorCode, code);
180
- assert.equal(error.message, message);
181
- assert.deepEqual(error.facts, facts);
182
- return true;
183
- },
184
- );
185
- });
186
- }
187
- });