From 21104c4bbef91922a00c9f38849414287be5daa9 Mon Sep 17 00:00:00 2001 From: oxura Date: Tue, 24 Nov 2026 12:35:20 +0600 Subject: [PATCH] Keep CRLF in bend-lint fix previews - Preserve the removed and added lines so the diff applies to CRLF inputs. Fixes #2306 --- tools/bend-lint/README.md | 2 ++ tools/bend-lint/src/lint.test.ts | 25 +++++++++++++++++++++++++ tools/bend-lint/src/lint.ts | 7 ++----- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/tools/bend-lint/README.md b/tools/bend-lint/README.md index bf4d13107e..2c48b80c81 100644 --- a/tools/bend-lint/README.md +++ b/tools/bend-lint/README.md @@ -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. diff --git a/tools/bend-lint/src/lint.test.ts b/tools/bend-lint/src/lint.test.ts index 525de5e1e3..6e54c0e8c6 100644 --- a/tools/bend-lint/src/lint.test.ts +++ b/tools/bend-lint/src/lint.test.ts @@ -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", diff --git a/tools/bend-lint/src/lint.ts b/tools/bend-lint/src/lint.ts index a4c0e456e1..ecab582bf9 100644 --- a/tools/bend-lint/src/lint.ts +++ b/tools/bend-lint/src/lint.ts @@ -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--- " +