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
2 changes: 2 additions & 0 deletions tools/bend-lint/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ 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.

Text previews preserve the line endings of removed and added lines.

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
25 changes: 25 additions & 0 deletions tools/bend-lint/src/lint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,31 @@ describe("lint", () => {
expect(render(res.diags[1])).toContain("Context:");
});

test("fix previews show CRLF preserved on both sides or removed only on the new side", async () => {
const text = "import Base\r\n\r\ndef main() -> U32:\r\n 1\r\n";
const file = fixture("preview-crlf.bend", text);
const rule: LintRule = {
id: "test/preview-crlf",
run: (cx) => {
const beg = cx.root.text.indexOf(" 1");
const span = { file: cx.root, beg, end: beg + 5 };
return [
cx.diag({
message: "Try these edits",
fixes: [
{ title: "Replace the value", applicability: "safe", edits: [{ span, text: " 2\r\n" }] },
{ title: "Use LF", applicability: "safe", edits: [{ span, text: " 1\n" }] },
],
}),
];
},
};
const result = await lint(file, [rule]);
const preview = render(result.diags[0]);
expect(preview).toContain("\n- 1\r\n+ 2\r\n");
expect(preview.endsWith("\n- 1\r\n+ 1")).toBe(true);
});

test("only rules with facts get facts", async () => {
const look: LintRule = {
id: "test/look",
Expand Down
7 changes: 2 additions & 5 deletions tools/bend-lint/src/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,11 +540,8 @@ export function render(d: Diag): string {
const name = file.path;
const range = (n: number): string => (n === 0 ? first : first + 1) + "," + n;
const show = (xs: string[], sign: string): string[] =>
xs.map(
(l) =>
sign +
l.replace(/\r?\n$/, "") +
(l.endsWith("\n") ? "" : "\n\\ No newline at end of file"),
xs.map((l) =>
sign + (l.endsWith("\n") ? l.slice(0, -1) : l + "\n\\ No newline at end of file"),
);
return (
"\n--- " +
Expand Down