@better-fs-tools/write 0.0.0-stage → 0.1.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.
- package/LICENSE +21 -0
- package/README.md +442 -2
- package/dist/authorize/ask-before-write.d.ts +11 -0
- package/dist/authorize/ask-before-write.js +49 -0
- package/dist/authorize/index.d.ts +3 -0
- package/dist/authorize/index.js +3 -0
- package/dist/authorize/protect-paths.d.ts +12 -0
- package/dist/authorize/protect-paths.js +74 -0
- package/dist/authorize/write-authorizers.d.ts +9 -0
- package/dist/authorize/write-authorizers.js +48 -0
- package/dist/bundle/create-fs-tools.d.ts +71 -0
- package/dist/bundle/create-fs-tools.js +125 -0
- package/dist/codecs/index.d.ts +1 -0
- package/dist/codecs/index.js +1 -0
- package/dist/codecs/utf8.d.ts +7 -0
- package/dist/codecs/utf8.js +61 -0
- package/dist/contract/codec.d.ts +26 -0
- package/dist/contract/codec.js +0 -0
- package/dist/contract/context.d.ts +14 -0
- package/dist/contract/context.js +0 -0
- package/dist/contract/deps.d.ts +55 -0
- package/dist/contract/deps.js +0 -0
- package/dist/contract/extensions.d.ts +103 -0
- package/dist/contract/extensions.js +0 -0
- package/dist/contract/format.d.ts +15 -0
- package/dist/contract/format.js +0 -0
- package/dist/contract/input.d.ts +35 -0
- package/dist/contract/input.js +0 -0
- package/dist/contract/limits.d.ts +26 -0
- package/dist/contract/limits.js +0 -0
- package/dist/contract/locks.d.ts +14 -0
- package/dist/contract/locks.js +0 -0
- package/dist/contract/matcher.d.ts +30 -0
- package/dist/contract/matcher.js +0 -0
- package/dist/contract/messages.d.ts +211 -0
- package/dist/contract/messages.js +0 -0
- package/dist/contract/patch.d.ts +50 -0
- package/dist/contract/patch.js +0 -0
- package/dist/contract/preconditions.d.ts +9 -0
- package/dist/contract/preconditions.js +5 -0
- package/dist/contract/result.d.ts +102 -0
- package/dist/contract/result.js +0 -0
- package/dist/core/abort.d.ts +4 -0
- package/dist/core/abort.js +23 -0
- package/dist/core/authorize.d.ts +9 -0
- package/dist/core/authorize.js +87 -0
- package/dist/core/bytes.d.ts +3 -0
- package/dist/core/bytes.js +14 -0
- package/dist/core/commit-patch.d.ts +20 -0
- package/dist/core/commit-patch.js +224 -0
- package/dist/core/commit.d.ts +9 -0
- package/dist/core/commit.js +111 -0
- package/dist/core/create-tools.d.ts +23 -0
- package/dist/core/create-tools.js +54 -0
- package/dist/core/deps.d.ts +11 -0
- package/dist/core/deps.js +207 -0
- package/dist/core/diff.d.ts +10 -0
- package/dist/core/diff.js +221 -0
- package/dist/core/encode.d.ts +3 -0
- package/dist/core/encode.js +26 -0
- package/dist/core/extension-error.d.ts +8 -0
- package/dist/core/extension-error.js +17 -0
- package/dist/core/format.d.ts +4 -0
- package/dist/core/format.js +36 -0
- package/dist/core/guards.d.ts +3 -0
- package/dist/core/guards.js +45 -0
- package/dist/core/hints.d.ts +14 -0
- package/dist/core/hints.js +75 -0
- package/dist/core/hooks.d.ts +15 -0
- package/dist/core/hooks.js +115 -0
- package/dist/core/input.d.ts +20 -0
- package/dist/core/input.js +91 -0
- package/dist/core/limits.d.ts +10 -0
- package/dist/core/limits.js +48 -0
- package/dist/core/line-index.d.ts +10 -0
- package/dist/core/line-index.js +38 -0
- package/dist/core/load.d.ts +14 -0
- package/dist/core/load.js +221 -0
- package/dist/core/lock.d.ts +2 -0
- package/dist/core/lock.js +56 -0
- package/dist/core/match.d.ts +15 -0
- package/dist/core/match.js +66 -0
- package/dist/core/messages.d.ts +8 -0
- package/dist/core/messages.js +147 -0
- package/dist/core/outcomes.d.ts +21 -0
- package/dist/core/outcomes.js +127 -0
- package/dist/core/parse-patch.d.ts +3 -0
- package/dist/core/parse-patch.js +64 -0
- package/dist/core/patch-hunks.d.ts +25 -0
- package/dist/core/patch-hunks.js +177 -0
- package/dist/core/patch-pipeline.d.ts +4 -0
- package/dist/core/patch-pipeline.js +285 -0
- package/dist/core/pipeline.d.ts +22 -0
- package/dist/core/pipeline.js +153 -0
- package/dist/core/plan-edit.d.ts +13 -0
- package/dist/core/plan-edit.js +215 -0
- package/dist/core/plan-patch.d.ts +29 -0
- package/dist/core/plan-patch.js +169 -0
- package/dist/core/plan-write.d.ts +6 -0
- package/dist/core/plan-write.js +27 -0
- package/dist/core/planned.d.ts +35 -0
- package/dist/core/planned.js +36 -0
- package/dist/core/precondition.d.ts +10 -0
- package/dist/core/precondition.js +76 -0
- package/dist/core/record.d.ts +3 -0
- package/dist/core/record.js +58 -0
- package/dist/core/resolve.d.ts +3 -0
- package/dist/core/resolve.js +65 -0
- package/dist/core/scope.d.ts +33 -0
- package/dist/core/scope.js +109 -0
- package/dist/core/snippet.d.ts +4 -0
- package/dist/core/snippet.js +36 -0
- package/dist/core/splice.d.ts +10 -0
- package/dist/core/splice.js +19 -0
- package/dist/core/target.d.ts +10 -0
- package/dist/core/target.js +46 -0
- package/dist/formatters/default.d.ts +16 -0
- package/dist/formatters/default.js +122 -0
- package/dist/formatters/index.d.ts +2 -0
- package/dist/formatters/index.js +1 -0
- package/dist/guards/generated-file.d.ts +10 -0
- package/dist/guards/generated-file.js +39 -0
- package/dist/guards/index.d.ts +13 -0
- package/dist/guards/index.js +25 -0
- package/dist/guards/non-text.d.ts +3 -0
- package/dist/guards/non-text.js +46 -0
- package/dist/guards/omission.d.ts +9 -0
- package/dist/guards/omission.js +48 -0
- package/dist/guards/read-prefix.d.ts +12 -0
- package/dist/guards/read-prefix.js +87 -0
- package/dist/guards/shared.d.ts +11 -0
- package/dist/guards/shared.js +46 -0
- package/dist/guards/syntax.d.ts +10 -0
- package/dist/guards/syntax.js +54 -0
- package/dist/guards/truncation-notice.d.ts +8 -0
- package/dist/guards/truncation-notice.js +32 -0
- package/dist/hooks/executable-shebang.d.ts +9 -0
- package/dist/hooks/executable-shebang.js +46 -0
- package/dist/hooks/index.d.ts +2 -0
- package/dist/hooks/index.js +2 -0
- package/dist/hooks/verify-write.d.ts +9 -0
- package/dist/hooks/verify-write.js +63 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.js +15 -0
- package/dist/locks/index.d.ts +1 -0
- package/dist/locks/index.js +1 -0
- package/dist/locks/memory-locks.d.ts +9 -0
- package/dist/locks/memory-locks.js +94 -0
- package/dist/matchers/block-anchor.d.ts +9 -0
- package/dist/matchers/block-anchor.js +69 -0
- package/dist/matchers/escape.d.ts +6 -0
- package/dist/matchers/escape.js +32 -0
- package/dist/matchers/exact.d.ts +4 -0
- package/dist/matchers/exact.js +29 -0
- package/dist/matchers/fold.d.ts +11 -0
- package/dist/matchers/fold.js +144 -0
- package/dist/matchers/indentation.d.ts +3 -0
- package/dist/matchers/indentation.js +81 -0
- package/dist/matchers/index.d.ts +11 -0
- package/dist/matchers/index.js +18 -0
- package/dist/matchers/line-trimmed.d.ts +3 -0
- package/dist/matchers/line-trimmed.js +32 -0
- package/dist/matchers/lines.d.ts +24 -0
- package/dist/matchers/lines.js +74 -0
- package/dist/matchers/normalized.d.ts +7 -0
- package/dist/matchers/normalized.js +69 -0
- package/dist/patch/grammar.d.ts +5 -0
- package/dist/patch/grammar.js +24 -0
- package/dist/patch/index.d.ts +4 -0
- package/dist/patch/index.js +3 -0
- package/dist/patch/parse.d.ts +11 -0
- package/dist/patch/parse.js +209 -0
- package/dist/signature/contract.d.ts +8 -0
- package/dist/signature/contract.js +0 -0
- package/dist/signature/edit.d.ts +19 -0
- package/dist/signature/edit.js +158 -0
- package/dist/signature/index.d.ts +6 -0
- package/dist/signature/index.js +4 -0
- package/dist/signature/matching.d.ts +3 -0
- package/dist/signature/matching.js +25 -0
- package/dist/signature/messages.d.ts +4 -0
- package/dist/signature/messages.js +9 -0
- package/dist/signature/patch.d.ts +6 -0
- package/dist/signature/patch.js +55 -0
- package/dist/signature/schema.d.ts +19 -0
- package/dist/signature/schema.js +117 -0
- package/dist/signature/write.d.ts +9 -0
- package/dist/signature/write.js +52 -0
- package/dist/state/index.d.ts +2 -0
- package/dist/state/index.js +1 -0
- package/dist/state/invalidate.d.ts +29 -0
- package/dist/state/invalidate.js +37 -0
- package/docs/architecture.md +221 -0
- package/docs/hosts.md +95 -0
- package/docs/result-schema.md +268 -0
- package/package.json +54 -4
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { LineIndex } from "./line-index.js";
|
|
2
|
+
const MAX_SNIPPET_LINE_LENGTH = 2_000;
|
|
3
|
+
export function buildSnippets(after, ranges, limits) {
|
|
4
|
+
const index = typeof after === "string" ? new LineIndex(after) : after;
|
|
5
|
+
const total = index.count;
|
|
6
|
+
if (total === 0)
|
|
7
|
+
return [];
|
|
8
|
+
const windows = [];
|
|
9
|
+
for (const [first, last] of [...ranges].sort((a, b) => a[0] - b[0])) {
|
|
10
|
+
const from = Math.max(1, Math.min(first, total) - limits.snippetLines);
|
|
11
|
+
const to = Math.min(total, Math.max(last, first) + limits.snippetLines);
|
|
12
|
+
const previous = windows.at(-1);
|
|
13
|
+
if (previous !== undefined && from <= previous[1] + 1)
|
|
14
|
+
previous[1] = Math.max(previous[1], to);
|
|
15
|
+
else
|
|
16
|
+
windows.push([from, to]);
|
|
17
|
+
}
|
|
18
|
+
const snippets = [];
|
|
19
|
+
let left = limits.maxSnippetLines;
|
|
20
|
+
for (const [from, to] of windows) {
|
|
21
|
+
if (left <= 0)
|
|
22
|
+
break;
|
|
23
|
+
const last = Math.min(to, from + left - 1);
|
|
24
|
+
const lines = [];
|
|
25
|
+
for (let line = from; line <= last; line += 1)
|
|
26
|
+
lines.push(clamp(index.line(line)));
|
|
27
|
+
snippets.push({ startLine: from, lines });
|
|
28
|
+
left -= lines.length;
|
|
29
|
+
}
|
|
30
|
+
return snippets;
|
|
31
|
+
}
|
|
32
|
+
function clamp(line) {
|
|
33
|
+
if (line.length <= MAX_SNIPPET_LINE_LENGTH)
|
|
34
|
+
return line;
|
|
35
|
+
return `${line.slice(0, MAX_SNIPPET_LINE_LENGTH)}… [line truncated at ${MAX_SNIPPET_LINE_LENGTH} chars]`;
|
|
36
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface Splice {
|
|
2
|
+
readonly start: number;
|
|
3
|
+
readonly end: number;
|
|
4
|
+
readonly text: string;
|
|
5
|
+
}
|
|
6
|
+
export declare function spliceAll(text: string, splices: readonly Splice[]): string;
|
|
7
|
+
export declare function placed(splices: readonly Splice[]): {
|
|
8
|
+
start: number;
|
|
9
|
+
end: number;
|
|
10
|
+
}[];
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export function spliceAll(text, splices) {
|
|
2
|
+
const parts = [];
|
|
3
|
+
let tail = text.length;
|
|
4
|
+
for (let index = splices.length - 1; index >= 0; index -= 1) {
|
|
5
|
+
const splice = splices[index];
|
|
6
|
+
parts.push(text.slice(splice.end, tail), splice.text);
|
|
7
|
+
tail = splice.start;
|
|
8
|
+
}
|
|
9
|
+
parts.push(text.slice(0, tail));
|
|
10
|
+
return parts.reverse().join("");
|
|
11
|
+
}
|
|
12
|
+
export function placed(splices) {
|
|
13
|
+
let shift = 0;
|
|
14
|
+
return splices.map((splice) => {
|
|
15
|
+
const start = splice.start + shift;
|
|
16
|
+
shift += splice.text.length - (splice.end - splice.start);
|
|
17
|
+
return { start, end: start + splice.text.length };
|
|
18
|
+
});
|
|
19
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { FileStat, WritableFileSystem } from "@better-fs-tools/fs";
|
|
2
|
+
import type { WritePhase } from "../contract/result.ts";
|
|
3
|
+
import type { MutationScope } from "./scope.ts";
|
|
4
|
+
export interface Target {
|
|
5
|
+
readonly requestedPath: string;
|
|
6
|
+
readonly path: string;
|
|
7
|
+
}
|
|
8
|
+
export declare function statTarget<THost>(scope: MutationScope<THost>, fs: WritableFileSystem, target: Target, phase?: WritePhase): Promise<FileStat>;
|
|
9
|
+
export declare function statAgain<THost>(scope: MutationScope<THost>, fs: WritableFileSystem, target: Target, first: FileStat): Promise<FileStat>;
|
|
10
|
+
export declare function ioFailure<THost>(scope: MutationScope<THost>, path: string | null, detail: string): import("./outcomes.ts").WriteStop;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { AbortStop } from "./abort.js";
|
|
2
|
+
import { isRecord } from "./input.js";
|
|
3
|
+
import { isBackendError, messageOf } from "./outcomes.js";
|
|
4
|
+
export async function statTarget(scope, fs, target, phase = "stat") {
|
|
5
|
+
scope.enter(phase);
|
|
6
|
+
scope.checkAbort();
|
|
7
|
+
const { signal } = scope;
|
|
8
|
+
let outcome;
|
|
9
|
+
try {
|
|
10
|
+
outcome = await scope.race(() => fs.stat(target.path, signal === undefined ? {} : { signal }));
|
|
11
|
+
}
|
|
12
|
+
catch (error) {
|
|
13
|
+
if (error instanceof AbortStop)
|
|
14
|
+
throw error;
|
|
15
|
+
throw ioFailure(scope, target.requestedPath, messageOf(error));
|
|
16
|
+
}
|
|
17
|
+
if (!isRecord(outcome))
|
|
18
|
+
throw ioFailure(scope, target.requestedPath, "malformed stat outcome");
|
|
19
|
+
if (outcome.ok === false && isBackendError(outcome.error)) {
|
|
20
|
+
throw scope.backendFailure(target.requestedPath, outcome.error);
|
|
21
|
+
}
|
|
22
|
+
if (outcome.ok !== true || !isFileStat(outcome.stat)) {
|
|
23
|
+
throw ioFailure(scope, target.requestedPath, "malformed stat outcome");
|
|
24
|
+
}
|
|
25
|
+
return outcome.stat;
|
|
26
|
+
}
|
|
27
|
+
export async function statAgain(scope, fs, target, first) {
|
|
28
|
+
const second = await statTarget(scope, fs, target);
|
|
29
|
+
if (second.resolvedPath !== first.resolvedPath) {
|
|
30
|
+
const { messages } = scope.deps;
|
|
31
|
+
throw scope.stop("STALE", messages.stale({ tool: scope.tool, path: target.requestedPath }));
|
|
32
|
+
}
|
|
33
|
+
return second;
|
|
34
|
+
}
|
|
35
|
+
export function ioFailure(scope, path, detail) {
|
|
36
|
+
return scope.stop("IO_ERROR", scope.deps.messages.ioError({ path }), { detail });
|
|
37
|
+
}
|
|
38
|
+
function isFileStat(value) {
|
|
39
|
+
if (!isRecord(value) || typeof value.resolvedPath !== "string")
|
|
40
|
+
return false;
|
|
41
|
+
if (typeof value.displayPath !== "string")
|
|
42
|
+
return false;
|
|
43
|
+
if (value.exists === false)
|
|
44
|
+
return Array.isArray(value.missingDirectories);
|
|
45
|
+
return value.exists === true && typeof value.version === "string";
|
|
46
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Note } from "@better-fs-tools/read";
|
|
2
|
+
import type { WriteToolName } from "../contract/context.ts";
|
|
3
|
+
import type { WriteFormatter } from "../contract/format.ts";
|
|
4
|
+
export interface WriteFormatterOptions {
|
|
5
|
+
/** Line-number prefix for edit snippets. Default: `${line}|`, as the read tool shows. */
|
|
6
|
+
readonly gutter?: (line: number) => string;
|
|
7
|
+
/** Add each change's diff to the model text. Default false. */
|
|
8
|
+
readonly diff?: boolean;
|
|
9
|
+
/** Default: `[${tool}:${note.code}] ${note.message}`. */
|
|
10
|
+
readonly noteLine?: (note: Note, tool: WriteToolName) => string;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Formats a result as a line for each file, then the notes. An error shows the
|
|
14
|
+
* notes only. "view" mode leaves the notes out.
|
|
15
|
+
*/
|
|
16
|
+
export declare function defaultWriteFormatter(options?: WriteFormatterOptions): WriteFormatter<unknown>;
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Formats a result as a line for each file, then the notes. An error shows the
|
|
3
|
+
* notes only. "view" mode leaves the notes out.
|
|
4
|
+
*/
|
|
5
|
+
export function defaultWriteFormatter(options = {}) {
|
|
6
|
+
if (options === null || typeof options !== "object" || Array.isArray(options)) {
|
|
7
|
+
throw new TypeError("defaultWriteFormatter options must be an object");
|
|
8
|
+
}
|
|
9
|
+
const { gutter = defaultGutter, diff = false, noteLine = defaultNoteLine } = options;
|
|
10
|
+
if (typeof gutter !== "function") {
|
|
11
|
+
throw new TypeError("gutter must be a function");
|
|
12
|
+
}
|
|
13
|
+
if (typeof diff !== "boolean")
|
|
14
|
+
throw new TypeError("diff must be a boolean");
|
|
15
|
+
if (typeof noteLine !== "function")
|
|
16
|
+
throw new TypeError("noteLine must be a function");
|
|
17
|
+
return Object.freeze({
|
|
18
|
+
id: "default",
|
|
19
|
+
format(report, ctx) {
|
|
20
|
+
const main = [
|
|
21
|
+
...body(report, gutter, ctx.limits.maxListedMatches),
|
|
22
|
+
...(diff ? diffBlocks(report.changes) : []),
|
|
23
|
+
];
|
|
24
|
+
if (ctx.mode === "view")
|
|
25
|
+
return main.join("\n");
|
|
26
|
+
const notes = report.notes.map((note) => noteLine(note, report.tool));
|
|
27
|
+
if (notes.length === 0)
|
|
28
|
+
return main.join("\n");
|
|
29
|
+
if (main.length === 0)
|
|
30
|
+
return notes.join("\n");
|
|
31
|
+
return `${main.join("\n")}\n\n${notes.join("\n")}`;
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
function defaultNoteLine(note, tool) {
|
|
36
|
+
return `[${tool}:${note.code}] ${note.message}`;
|
|
37
|
+
}
|
|
38
|
+
function defaultGutter(line) {
|
|
39
|
+
return `${line}|`;
|
|
40
|
+
}
|
|
41
|
+
function body(report, gutter, listed) {
|
|
42
|
+
if (report.status === "error")
|
|
43
|
+
return [];
|
|
44
|
+
if (report.status === "no-change") {
|
|
45
|
+
const reason = report.tool === "write" ? ": the content is the same" : "";
|
|
46
|
+
return report.unchanged.map((path) => `No change to ${path}${reason}.`);
|
|
47
|
+
}
|
|
48
|
+
if (report.tool === "edit") {
|
|
49
|
+
return report.changes.flatMap((change) => [
|
|
50
|
+
editLine(change, listed),
|
|
51
|
+
...snippetLines(change.snippets, gutter),
|
|
52
|
+
]);
|
|
53
|
+
}
|
|
54
|
+
if (report.tool === "apply_patch") {
|
|
55
|
+
return ["Success. Updated the following files:", ...report.changes.map(patchLine)];
|
|
56
|
+
}
|
|
57
|
+
return report.changes.map(changeLine);
|
|
58
|
+
}
|
|
59
|
+
// Follows the Codex apply_patch output: `A`, `M`, or `D` and the path.
|
|
60
|
+
function patchLine(change) {
|
|
61
|
+
const letter = change.kind === "create" ? "A" : change.kind === "delete" ? "D" : "M";
|
|
62
|
+
const details = [
|
|
63
|
+
...(change.movedFrom === null ? [] : [`moved from ${change.movedFrom}`]),
|
|
64
|
+
...change.matches
|
|
65
|
+
.filter((match) => match.fuzzy)
|
|
66
|
+
.map((match) => `hunk ${match.index + 1} matched by ${match.matcher}`),
|
|
67
|
+
];
|
|
68
|
+
return `${letter} ${change.path}${details.length === 0 ? "" : ` (${details.join(", ")})`}`;
|
|
69
|
+
}
|
|
70
|
+
function editLine(change, listed) {
|
|
71
|
+
if (change.userModified) {
|
|
72
|
+
return `Edited ${change.path} with the user's changes (+${change.linesAdded} -${change.linesRemoved} lines).`;
|
|
73
|
+
}
|
|
74
|
+
const total = change.matches.reduce((sum, match) => sum + match.count, 0);
|
|
75
|
+
const all = change.matches.some((match) => match.count > 1);
|
|
76
|
+
const ranges = change.matches.flatMap((match) => match.replaced).sort((a, b) => a[0] - b[0]);
|
|
77
|
+
const shown = ranges
|
|
78
|
+
.slice(0, listed)
|
|
79
|
+
.map(([first, last]) => (first === last ? `${first}` : `${first}-${last}`));
|
|
80
|
+
const single = total === 1 && ranges.length === 1 && ranges[0]?.[0] === ranges[0]?.[1];
|
|
81
|
+
const more = total > shown.length ? ", …" : "";
|
|
82
|
+
const counted = `${total} replacement${total === 1 ? "" : "s"}${all ? " (replace all)" : ""}`;
|
|
83
|
+
return `Edited ${change.path}: ${counted} at line${single ? "" : "s"} ${shown.join(", ")}${more}.`;
|
|
84
|
+
}
|
|
85
|
+
function snippetLines(snippets, gutter) {
|
|
86
|
+
return snippets.flatMap((snippet, index) => [
|
|
87
|
+
...(index === 0 ? [] : ["..."]),
|
|
88
|
+
...snippet.lines.map((line, offset) => `${gutter(snippet.startLine + offset)}${line}`),
|
|
89
|
+
]);
|
|
90
|
+
}
|
|
91
|
+
function changeLine(change) {
|
|
92
|
+
const counts = `(+${change.linesAdded} -${change.linesRemoved} lines)`;
|
|
93
|
+
switch (change.kind) {
|
|
94
|
+
case "create": {
|
|
95
|
+
const lines = change.linesAdded;
|
|
96
|
+
return `Created ${change.path} (${lines} line${lines === 1 ? "" : "s"}).`;
|
|
97
|
+
}
|
|
98
|
+
case "update":
|
|
99
|
+
return `Updated ${change.path} ${counts}.`;
|
|
100
|
+
case "delete":
|
|
101
|
+
return `Deleted ${change.path}.`;
|
|
102
|
+
default:
|
|
103
|
+
return `Moved ${change.movedFrom ?? "a file"} to ${change.path} ${counts}.`;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
function diffBlocks(changes) {
|
|
107
|
+
const blocks = [];
|
|
108
|
+
for (const change of changes) {
|
|
109
|
+
if (change.diff === "")
|
|
110
|
+
continue;
|
|
111
|
+
const text = change.diff.endsWith("\n") ? change.diff.slice(0, -1) : change.diff;
|
|
112
|
+
const fence = "`".repeat(Math.max(3, longestRun(text) + 1));
|
|
113
|
+
blocks.push(`${fence}diff`, text, ...(change.diffTruncated ? ["…"] : []), fence);
|
|
114
|
+
}
|
|
115
|
+
return blocks;
|
|
116
|
+
}
|
|
117
|
+
function longestRun(text) {
|
|
118
|
+
let longest = 0;
|
|
119
|
+
for (const match of text.matchAll(/`+/gu))
|
|
120
|
+
longest = Math.max(longest, match[0].length);
|
|
121
|
+
return longest;
|
|
122
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { defaultWriteFormatter } from "./default.js";
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Guard } from "../contract/extensions.ts";
|
|
2
|
+
/**
|
|
3
|
+
* Opt-in. Refuses an update to a file that looks generated: its name matches
|
|
4
|
+
* one of `names`, or one of `markers` appears in its first 1 024 characters.
|
|
5
|
+
* Creating a file is allowed.
|
|
6
|
+
*/
|
|
7
|
+
export declare function generatedFileGuard(options?: {
|
|
8
|
+
readonly markers?: readonly RegExp[];
|
|
9
|
+
readonly names?: readonly RegExp[];
|
|
10
|
+
}): Guard<unknown>;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { ALLOW, refuse, regExpList } from "./shared.js";
|
|
2
|
+
const DEFAULT_NAMES = [/\.min\./u, /\.generated\./u, /_pb2\.py$/u, /\.pb\.go$/u];
|
|
3
|
+
const DEFAULT_MARKERS = [/@generated\b/u, /DO NOT EDIT/u, /auto-generated/iu];
|
|
4
|
+
const MARKER_SEARCH_CHARS = 1_024;
|
|
5
|
+
/**
|
|
6
|
+
* Opt-in. Refuses an update to a file that looks generated: its name matches
|
|
7
|
+
* one of `names`, or one of `markers` appears in its first 1 024 characters.
|
|
8
|
+
* Creating a file is allowed.
|
|
9
|
+
*/
|
|
10
|
+
export function generatedFileGuard(options = {}) {
|
|
11
|
+
const markers = options.markers === undefined ? DEFAULT_MARKERS : regExpList(options.markers, "markers");
|
|
12
|
+
const names = options.names === undefined ? DEFAULT_NAMES : regExpList(options.names, "names");
|
|
13
|
+
return Object.freeze({
|
|
14
|
+
id: "generated-file",
|
|
15
|
+
check(change) {
|
|
16
|
+
if (change.before === null)
|
|
17
|
+
return ALLOW;
|
|
18
|
+
const path = change.resolvedPath;
|
|
19
|
+
const reason = nameReason(names, path.slice(path.lastIndexOf("/") + 1)) ??
|
|
20
|
+
markerReason(markers, change.before.text.slice(0, MARKER_SEARCH_CHARS));
|
|
21
|
+
if (reason === null)
|
|
22
|
+
return ALLOW;
|
|
23
|
+
return refuse("generated-file", `${change.displayPath} looks generated: ${reason}. Change its source or generator instead, then regenerate it.`, { reason });
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
function nameReason(names, name) {
|
|
28
|
+
return names.some((pattern) => pattern.test(name))
|
|
29
|
+
? `its name, ${name}, is a generated-file name`
|
|
30
|
+
: null;
|
|
31
|
+
}
|
|
32
|
+
function markerReason(markers, head) {
|
|
33
|
+
for (const pattern of markers) {
|
|
34
|
+
const match = pattern.exec(head);
|
|
35
|
+
if (match !== null)
|
|
36
|
+
return `its first lines hold "${match[0]}"`;
|
|
37
|
+
}
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Guard } from "../contract/extensions.ts";
|
|
2
|
+
export { generatedFileGuard } from "./generated-file.ts";
|
|
3
|
+
export { nonTextGuard } from "./non-text.ts";
|
|
4
|
+
export { omissionGuard } from "./omission.ts";
|
|
5
|
+
export { readPrefixGuard } from "./read-prefix.ts";
|
|
6
|
+
export { syntaxGuard } from "./syntax.ts";
|
|
7
|
+
export { truncationNoticeGuard } from "./truncation-notice.ts";
|
|
8
|
+
/**
|
|
9
|
+
* The guards every write tool runs by default, in order: read prefix,
|
|
10
|
+
* truncation notice, omission, syntax (JSON), and non-text.
|
|
11
|
+
* generatedFileGuard is opt-in.
|
|
12
|
+
*/
|
|
13
|
+
export declare function defaultGuards(): readonly Guard<unknown>[];
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { nonTextGuard } from "./non-text.js";
|
|
2
|
+
import { omissionGuard } from "./omission.js";
|
|
3
|
+
import { readPrefixGuard } from "./read-prefix.js";
|
|
4
|
+
import { syntaxGuard } from "./syntax.js";
|
|
5
|
+
import { truncationNoticeGuard } from "./truncation-notice.js";
|
|
6
|
+
export { generatedFileGuard } from "./generated-file.js";
|
|
7
|
+
export { nonTextGuard } from "./non-text.js";
|
|
8
|
+
export { omissionGuard } from "./omission.js";
|
|
9
|
+
export { readPrefixGuard } from "./read-prefix.js";
|
|
10
|
+
export { syntaxGuard } from "./syntax.js";
|
|
11
|
+
export { truncationNoticeGuard } from "./truncation-notice.js";
|
|
12
|
+
/**
|
|
13
|
+
* The guards every write tool runs by default, in order: read prefix,
|
|
14
|
+
* truncation notice, omission, syntax (JSON), and non-text.
|
|
15
|
+
* generatedFileGuard is opt-in.
|
|
16
|
+
*/
|
|
17
|
+
export function defaultGuards() {
|
|
18
|
+
return Object.freeze([
|
|
19
|
+
readPrefixGuard(),
|
|
20
|
+
truncationNoticeGuard(),
|
|
21
|
+
omissionGuard(),
|
|
22
|
+
syntaxGuard(),
|
|
23
|
+
nonTextGuard(),
|
|
24
|
+
]);
|
|
25
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { ALLOW, refuse } from "./shared.js";
|
|
2
|
+
const ENCODER = new TextEncoder();
|
|
3
|
+
const BOM = Uint8Array.of(0xef, 0xbb, 0xbf);
|
|
4
|
+
/** Refuses new content that the classifiers find is not text, such as a notebook, NUL bytes, or an image. */
|
|
5
|
+
export function nonTextGuard() {
|
|
6
|
+
return Object.freeze({
|
|
7
|
+
id: "non-text",
|
|
8
|
+
check(change, ctx) {
|
|
9
|
+
if (change.after === null)
|
|
10
|
+
return ALLOW;
|
|
11
|
+
const sample = sampleOf(change.after, ctx.limits.sampleBytes, change.displayPath);
|
|
12
|
+
const found = unsupported(ctx.classifiers, sample);
|
|
13
|
+
if (found === null)
|
|
14
|
+
return ALLOW;
|
|
15
|
+
return refuse("non-text", `The new content for ${change.displayPath} is not plain text (${found.code}), so this tool will not write it. Send text content, or use a tool made for this kind of file.`, { code: found.code, classifier: found.classifier });
|
|
16
|
+
},
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
function sampleOf(after, max, path) {
|
|
20
|
+
const unitsCoveringMaxBytes = max + 1;
|
|
21
|
+
let head = after.text.slice(0, unitsCoveringMaxBytes);
|
|
22
|
+
if (after.style.eol === "crlf")
|
|
23
|
+
head = head.replaceAll("\n", "\r\n");
|
|
24
|
+
const body = ENCODER.encode(head);
|
|
25
|
+
const bytes = new Uint8Array((after.style.bom ? BOM.byteLength : 0) + body.byteLength);
|
|
26
|
+
if (after.style.bom)
|
|
27
|
+
bytes.set(BOM);
|
|
28
|
+
bytes.set(body, bytes.byteLength - body.byteLength);
|
|
29
|
+
return {
|
|
30
|
+
bytes: bytes.subarray(0, max),
|
|
31
|
+
complete: after.text.length <= max && bytes.byteLength <= max,
|
|
32
|
+
path,
|
|
33
|
+
mimeType: null,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
function unsupported(classifiers, sample) {
|
|
37
|
+
for (const classifier of classifiers) {
|
|
38
|
+
const decision = classifier.classify(sample);
|
|
39
|
+
if (decision === null)
|
|
40
|
+
continue;
|
|
41
|
+
if (decision.kind === "text")
|
|
42
|
+
return null;
|
|
43
|
+
return { code: decision.code, classifier: classifier.id };
|
|
44
|
+
}
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Guard } from "../contract/extensions.ts";
|
|
2
|
+
/**
|
|
3
|
+
* Refuses a change that replaces real lines with a placeholder comment such
|
|
4
|
+
* as `// ... rest of code`. Only new text shorter than the old text is
|
|
5
|
+
* checked, and a placeholder already in the old text is allowed.
|
|
6
|
+
*/
|
|
7
|
+
export declare function omissionGuard(options?: {
|
|
8
|
+
readonly patterns?: readonly RegExp[];
|
|
9
|
+
}): Guard<unknown>;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { ALLOW, linesOf, newTextParam, quoted, refuse, regExpList } from "./shared.js";
|
|
2
|
+
const OPEN = String.raw `(?:\/\/+|#+|\/\*+|\{\s*\/\*+|\*|<!--|--|;+|%+)`;
|
|
3
|
+
const WORD = String.raw `(?:rest|remaining|existing|unchanged|unmodified|previous|same|omitted|original|other|code)`;
|
|
4
|
+
// Placeholder comments. A bare "..." line is left alone: it is a Python stub.
|
|
5
|
+
const DEFAULT_PATTERNS = [
|
|
6
|
+
new RegExp(String.raw `^\s*${OPEN}\s*[([]?\s*(?:\.{3}|…)\s*.{0,60}\b${WORD}\b.{0,60}$`, "iu"),
|
|
7
|
+
new RegExp(String.raw `^\s*${OPEN}\s*[([]?\s*(?:the\s+)?${WORD}\b.{0,60}(?:\.{3}|…)\s*[)\]]?\s*(?:\*\/\}?|-->)?\s*$`, "iu"),
|
|
8
|
+
new RegExp(String.raw `^\s*${OPEN}\s*[([]?\s*(?:the\s+)?(?:rest\s+of\s+(?:the\s+)?\w+|(?:existing|remaining|previous|original|other)\s+\w+(?:\s+\w+)?)\s+(?:(?:is|are|remains?|stays?)\s+)?(?:unchanged|omitted|(?:the\s+)?same|as\s+before)\.?\s*[)\]]?\s*(?:\*\/\}?|-->)?\s*$`, "iu"),
|
|
9
|
+
];
|
|
10
|
+
/**
|
|
11
|
+
* Refuses a change that replaces real lines with a placeholder comment such
|
|
12
|
+
* as `// ... rest of code`. Only new text shorter than the old text is
|
|
13
|
+
* checked, and a placeholder already in the old text is allowed.
|
|
14
|
+
*/
|
|
15
|
+
export function omissionGuard(options = {}) {
|
|
16
|
+
const patterns = options.patterns === undefined ? DEFAULT_PATTERNS : regExpList(options.patterns, "patterns");
|
|
17
|
+
return Object.freeze({
|
|
18
|
+
id: "omission",
|
|
19
|
+
check(change, ctx) {
|
|
20
|
+
for (const fragment of change.fragments) {
|
|
21
|
+
if (fragment.newText.length >= fragment.oldText.length)
|
|
22
|
+
continue;
|
|
23
|
+
const placeholder = findPlaceholder(fragment, patterns);
|
|
24
|
+
if (placeholder !== null)
|
|
25
|
+
return refusal(change, ctx, placeholder);
|
|
26
|
+
}
|
|
27
|
+
return ALLOW;
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
function findPlaceholder(fragment, patterns) {
|
|
32
|
+
let old = null;
|
|
33
|
+
for (const line of linesOf(fragment.newText)) {
|
|
34
|
+
if (!patterns.some((pattern) => pattern.test(line)))
|
|
35
|
+
continue;
|
|
36
|
+
old ??= new Set(linesOf(fragment.oldText).map((entry) => entry.trim()));
|
|
37
|
+
if (!old.has(line.trim()))
|
|
38
|
+
return line;
|
|
39
|
+
}
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
function refusal(change, ctx, line) {
|
|
43
|
+
const param = newTextParam(change, ctx);
|
|
44
|
+
const next = change.tool === "write"
|
|
45
|
+
? "Send the complete file content, or use the edit tool to change only part of the file."
|
|
46
|
+
: `Write those lines out in full, or make the ${change.tool === "edit" ? ctx.messages.param("oldText") : "hunk"} smaller so they stay unchanged.`;
|
|
47
|
+
return refuse("omission", `The ${param} for ${change.displayPath} has a placeholder instead of code: "${quoted(line)}". It would delete the lines it stands for. ${next}`, { line: quoted(line) });
|
|
48
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Guard } from "../contract/extensions.ts";
|
|
2
|
+
/**
|
|
3
|
+
* Refuses new text copied from read output with its line-number prefixes.
|
|
4
|
+
* A change is refused when at least 2, and at least `ratio` (default 0.5), of
|
|
5
|
+
* its new non-empty lines start with consecutive line numbers. Lines already
|
|
6
|
+
* in the file do not count, and a file that already looks like this is not
|
|
7
|
+
* checked.
|
|
8
|
+
*/
|
|
9
|
+
export declare function readPrefixGuard(options?: {
|
|
10
|
+
readonly gutter?: RegExp;
|
|
11
|
+
readonly ratio?: number;
|
|
12
|
+
}): Guard<unknown>;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { ALLOW, existingLines, withoutStatefulFlags, linesOf, newTextParam, quoted, refuse, } from "./shared.js";
|
|
2
|
+
// Read output gutters such as "12|", "12:a3|", "12: ", "12→", and " 12\t".
|
|
3
|
+
// Group 2 is the weak ": " form, which YAML keys also match.
|
|
4
|
+
const DEFAULT_GUTTER = /^[ \t]*(\d+)(?::[0-9A-Za-z_-]+)?(?:\||→|(: ))|^ +(\d+)\t/u;
|
|
5
|
+
/**
|
|
6
|
+
* Refuses new text copied from read output with its line-number prefixes.
|
|
7
|
+
* A change is refused when at least 2, and at least `ratio` (default 0.5), of
|
|
8
|
+
* its new non-empty lines start with consecutive line numbers. Lines already
|
|
9
|
+
* in the file do not count, and a file that already looks like this is not
|
|
10
|
+
* checked.
|
|
11
|
+
*/
|
|
12
|
+
export function readPrefixGuard(options = {}) {
|
|
13
|
+
const { gutter = DEFAULT_GUTTER, ratio = 0.5 } = options;
|
|
14
|
+
if (!(gutter instanceof RegExp))
|
|
15
|
+
throw new TypeError("gutter must be a regular expression");
|
|
16
|
+
if (typeof ratio !== "number" || !(ratio > 0 && ratio <= 1)) {
|
|
17
|
+
throw new TypeError("ratio must be a number above 0 and at most 1");
|
|
18
|
+
}
|
|
19
|
+
const test = withoutStatefulFlags(gutter);
|
|
20
|
+
const builtIn = gutter === DEFAULT_GUTTER;
|
|
21
|
+
return Object.freeze({
|
|
22
|
+
id: "read-prefix",
|
|
23
|
+
check(change, ctx) {
|
|
24
|
+
if (change.after === null)
|
|
25
|
+
return ALLOW;
|
|
26
|
+
const known = existingLines(change);
|
|
27
|
+
let beforeChecked = false;
|
|
28
|
+
for (const fragment of change.fragments) {
|
|
29
|
+
const lines = linesOf(fragment.newText).filter((line) => line.trim() !== "");
|
|
30
|
+
const hits = consecutiveHits(lines.flatMap((line, index) => hit(test, line, index)));
|
|
31
|
+
if (hits.length < 2)
|
|
32
|
+
continue;
|
|
33
|
+
const fresh = lines.filter((line) => !known(line));
|
|
34
|
+
const counted = hits.filter((entry) => !known(lines[entry.line]));
|
|
35
|
+
if (counted.length < 2 || counted.length < ratio * fresh.length)
|
|
36
|
+
continue;
|
|
37
|
+
if (builtIn && counted.length < 3 && counted.every((entry) => entry.weak))
|
|
38
|
+
continue;
|
|
39
|
+
if (!beforeChecked) {
|
|
40
|
+
beforeChecked = true;
|
|
41
|
+
if (change.before !== null && gutterShare(test, change.before.text) >= ratio) {
|
|
42
|
+
return ALLOW;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
const sample = counted[0].gutter;
|
|
46
|
+
return refuse("read-prefix", `The ${newTextParam(change, ctx)} for ${change.displayPath} starts ${counted.length} of its ${fresh.length} new lines with read tool line numbers, such as "${quoted(sample)}". Send only the file text, without the line-number prefixes.`, { lines: counted.length, gutter: sample });
|
|
47
|
+
}
|
|
48
|
+
return ALLOW;
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
function hit(test, text, line) {
|
|
53
|
+
const match = test.exec(text);
|
|
54
|
+
if (match === null)
|
|
55
|
+
return [];
|
|
56
|
+
const digits = match.slice(1).find((group) => group !== undefined && /^\d+$/u.test(group));
|
|
57
|
+
return [
|
|
58
|
+
{
|
|
59
|
+
line,
|
|
60
|
+
number: digits === undefined ? null : Number(digits),
|
|
61
|
+
weak: match[2] !== undefined,
|
|
62
|
+
gutter: match[0],
|
|
63
|
+
},
|
|
64
|
+
];
|
|
65
|
+
}
|
|
66
|
+
function consecutiveHits(hits) {
|
|
67
|
+
return hits.filter((entry, index) => {
|
|
68
|
+
if (entry.number === null)
|
|
69
|
+
return true;
|
|
70
|
+
const before = hits[index - 1];
|
|
71
|
+
const after = hits[index + 1];
|
|
72
|
+
return ((before?.number !== undefined && before.number === entry.number - 1) ||
|
|
73
|
+
(after?.number !== undefined && after.number === entry.number + 1));
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
function gutterShare(test, text) {
|
|
77
|
+
let lines = 0;
|
|
78
|
+
let hits = 0;
|
|
79
|
+
for (const line of linesOf(text)) {
|
|
80
|
+
if (line.trim() === "")
|
|
81
|
+
continue;
|
|
82
|
+
lines += 1;
|
|
83
|
+
if (test.test(line))
|
|
84
|
+
hits += 1;
|
|
85
|
+
}
|
|
86
|
+
return lines === 0 ? 0 : hits / lines;
|
|
87
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { JsonObject } from "@better-fs-tools/read";
|
|
2
|
+
import type { GuardContext, GuardDecision, PlannedChange } from "../contract/extensions.ts";
|
|
3
|
+
export declare const ALLOW: GuardDecision;
|
|
4
|
+
export declare function refuse(code: string, message: string, data?: JsonObject): GuardDecision;
|
|
5
|
+
export declare function linesOf(text: string): string[];
|
|
6
|
+
export declare function newTextParam(change: PlannedChange, ctx: GuardContext<unknown>): string;
|
|
7
|
+
export declare function withoutStatefulFlags(pattern: RegExp): RegExp;
|
|
8
|
+
export declare function regExpList(value: unknown, name: string): readonly RegExp[];
|
|
9
|
+
export declare function extensionOf(path: string): string | null;
|
|
10
|
+
export declare function quoted(line: string): string;
|
|
11
|
+
export declare function existingLines(change: PlannedChange): (line: string) => boolean;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
export const ALLOW = Object.freeze({ allow: true });
|
|
2
|
+
export function refuse(code, message, data) {
|
|
3
|
+
return {
|
|
4
|
+
allow: false,
|
|
5
|
+
note: { code, severity: "warning", message, ...(data === undefined ? {} : { data }) },
|
|
6
|
+
};
|
|
7
|
+
}
|
|
8
|
+
export function linesOf(text) {
|
|
9
|
+
return text.split("\n").map((line) => (line.endsWith("\r") ? line.slice(0, -1) : line));
|
|
10
|
+
}
|
|
11
|
+
export function newTextParam(change, ctx) {
|
|
12
|
+
if (change.tool === "edit")
|
|
13
|
+
return ctx.messages.param("newText");
|
|
14
|
+
if (change.tool === "apply_patch")
|
|
15
|
+
return ctx.messages.param("patch");
|
|
16
|
+
return ctx.messages.param("content");
|
|
17
|
+
}
|
|
18
|
+
export function withoutStatefulFlags(pattern) {
|
|
19
|
+
return new RegExp(pattern.source, pattern.flags.replace(/[gy]/gu, ""));
|
|
20
|
+
}
|
|
21
|
+
export function regExpList(value, name) {
|
|
22
|
+
if (!Array.isArray(value) || !value.every((entry) => entry instanceof RegExp)) {
|
|
23
|
+
throw new TypeError(`${name} must be an array of regular expressions`);
|
|
24
|
+
}
|
|
25
|
+
return Object.freeze(value.map(withoutStatefulFlags));
|
|
26
|
+
}
|
|
27
|
+
export function extensionOf(path) {
|
|
28
|
+
const name = path.slice(path.lastIndexOf("/") + 1);
|
|
29
|
+
const index = name.lastIndexOf(".");
|
|
30
|
+
if (index <= 0 || index === name.length - 1)
|
|
31
|
+
return null;
|
|
32
|
+
return name.slice(index + 1).toLowerCase();
|
|
33
|
+
}
|
|
34
|
+
export function quoted(line) {
|
|
35
|
+
const trimmed = line.trim();
|
|
36
|
+
return trimmed.length > 80 ? `${trimmed.slice(0, 80)}…` : trimmed;
|
|
37
|
+
}
|
|
38
|
+
export function existingLines(change) {
|
|
39
|
+
let lines = null;
|
|
40
|
+
return (line) => {
|
|
41
|
+
if (change.before === null)
|
|
42
|
+
return false;
|
|
43
|
+
lines ??= new Set(linesOf(change.before.text));
|
|
44
|
+
return lines.has(line);
|
|
45
|
+
};
|
|
46
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Guard } from "../contract/extensions.ts";
|
|
2
|
+
/**
|
|
3
|
+
* Refuses an update that makes a file fail to parse when it parsed before.
|
|
4
|
+
* `.json` uses JSON.parse by default. `parsers` adds or replaces parsers by
|
|
5
|
+
* extension, with or without the dot, and each must throw on bad text.
|
|
6
|
+
* Creates and files that already failed to parse are not checked.
|
|
7
|
+
*/
|
|
8
|
+
export declare function syntaxGuard(options?: {
|
|
9
|
+
readonly parsers?: Readonly<Record<string, (text: string) => void>>;
|
|
10
|
+
}): Guard<unknown>;
|