Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tools/bend-lint/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ also edits another file is skipped. When two fixes overlap, the later one is
skipped. Run it again to get it.
All edit offsets refer to the original text. Accepted edits are assembled in one
pass rather than copying the whole file for each edit.
In a multi-file preview, files appear in the order their first edit was reported.

Exit code is 0 if there were no errors (warnings don't count), 1 for a checker
or rule error, and 2 for bad arguments or a crash.
Expand Down
21 changes: 21 additions & 0 deletions tools/bend-lint/src/lint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1671,6 +1671,27 @@ describe("review fixes", () => {
}])).toEqual({ text: "a😀<B>c!", skipped: 0, elsewhere: 0 });
});

test("a multi-file patch preview keeps file order and groups interleaved edits", () => {
const first = { path: "b.bend", text: "# abcd\n", root: true, base: false };
const second = { path: "a.bend", text: "# wxyz\n", root: false, base: false };
const edit = (file: Source, at: number, text: string): Edit =>
({ span: { file, beg: at, end: at + 1 }, text });
const preview = render({
code: "test/preview",
message: "update comments",
severity: "warning",
fixes: [{
title: "update comments",
applicability: "safe",
edits: [edit(first, 2, "A"), edit(second, 3, "X"), edit(first, 4, "C"), edit(second, 5, "Z")],
}],
});
expect(preview.slice(preview.indexOf("\n---"))).toBe(
"\n--- b.bend\n+++ b.bend\n@@ -1,1 +1,1 @@\n-# abcd\n+# AbCd"
+ "\n--- a.bend\n+++ a.bend\n@@ -1,1 +1,1 @@\n-# wxyz\n+# wXyZ",
);
});

test("a fix that also edits another file is skipped whole", () => {
const file: Source = { path: "<fix>", text: "abc", root: true, base: false };
const other: Source = { path: "<other>", text: "xyz", root: false, base: false };
Expand Down
30 changes: 21 additions & 9 deletions tools/bend-lint/src/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -513,20 +513,31 @@ export function applyFixes(
// bend's own error layout; the head names the severity and the code, and
// each fix follows as a unified diff of the lines it touches.
export function render(d: Diag): string {
const fixes = d.fixes.map(
(fix) =>
const fixes = d.fixes.map((fix) => {
const files = new Map<Source, { edits: Edit[]; beg: number; end: number }>();
for (const edit of fix.edits) {
const { file, beg, end } = edit.span;
const group = files.get(file);
if (group === undefined) {
files.set(file, { edits: [edit], beg, end });
} else {
group.edits.push(edit);
group.beg = Math.min(group.beg, beg);
group.end = Math.max(group.end, end);
}
}
return (
"\n\nFix: " +
fix.title +
" [" +
fix.applicability +
"]" +
[...new Set(fix.edits.map((e) => e.span.file))]
.map((file) => {
const mine = fix.edits.filter((e) => e.span.file === file);
[...files]
.map(([file, { edits: mine, beg, end }]) => {
const ls = starts(file, file.text);
const first = line(ls, Math.min(...mine.map((e) => e.span.beg)));
const first = line(ls, beg);
const from = ls[first];
const to = ls[line(ls, Math.max(...mine.map((e) => e.span.end))) + 1] ?? file.text.length;
const to = ls[line(ls, end) + 1] ?? file.text.length;
const old = file.text.slice(from, to);
const gone = old.match(LINE) ?? [];
const came =
Expand Down Expand Up @@ -559,8 +570,9 @@ export function render(d: Diag): string {
[...show(gone, "-"), ...show(came, "+")].join("\n")
);
})
.join(""),
);
.join("")
);
});
return layout(loaded, d, HEAD[d.severity] + " [" + d.code + "]:") + fixes.join("");
}

Expand Down