diff --git a/src/tools/hashline-edit/hash-computation.test.ts b/src/tools/hashline-edit/hash-computation.test.ts index fe4fff0cd..bea1bba0d 100644 --- a/src/tools/hashline-edit/hash-computation.test.ts +++ b/src/tools/hashline-edit/hash-computation.test.ts @@ -45,10 +45,36 @@ describe("computeLineHash", () => { expect(hash1).not.toBe(hash2) }) - it("ignores whitespace differences", () => { + it("produces different hashes for different leading indentation", () => { //#given const content1 = "function hello() {" - const content2 = " function hello() { " + const content2 = " function hello() {" + + //#when + const hash1 = computeLineHash(1, content1) + const hash2 = computeLineHash(1, content2) + + //#then + expect(hash1).not.toBe(hash2) + }) + + it("ignores trailing whitespace differences", () => { + //#given + const content1 = "function hello() {" + const content2 = "function hello() { " + + //#when + const hash1 = computeLineHash(1, content1) + const hash2 = computeLineHash(1, content2) + + //#then + expect(hash1).toBe(hash2) + }) + + it("produces same hash for CRLF and LF line endings", () => { + //#given + const content1 = "function hello() {" + const content2 = "function hello() {\r" //#when const hash1 = computeLineHash(1, content1) diff --git a/src/tools/hashline-edit/hash-computation.ts b/src/tools/hashline-edit/hash-computation.ts index ec6773a9c..25b05cbf4 100644 --- a/src/tools/hashline-edit/hash-computation.ts +++ b/src/tools/hashline-edit/hash-computation.ts @@ -4,7 +4,7 @@ import { createHashlineChunkFormatter } from "./hashline-chunk-formatter" const RE_SIGNIFICANT = /[\p{L}\p{N}]/u export function computeLineHash(lineNumber: number, content: string): string { - const stripped = content.endsWith("\r") ? content.slice(0, -1).replace(/\s+/g, "") : content.replace(/\s+/g, "") + const stripped = content.replace(/\r/g, "").trimEnd() const seed = RE_SIGNIFICANT.test(stripped) ? 0 : lineNumber const hash = Bun.hash.xxHash32(stripped, seed) const index = hash % 256