Merge pull request #2516 from code-yeongyu/fix/hashline-strict-whitespace-hash

fix(hashline): use strict whitespace hashing (trimEnd only, preserve leading indentation)
This commit is contained in:
YeonGyu-Kim
2026-03-12 16:52:30 +09:00
committed by GitHub
2 changed files with 29 additions and 3 deletions

View File

@@ -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)

View File

@@ -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