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:
@@ -45,10 +45,36 @@ describe("computeLineHash", () => {
|
|||||||
expect(hash1).not.toBe(hash2)
|
expect(hash1).not.toBe(hash2)
|
||||||
})
|
})
|
||||||
|
|
||||||
it("ignores whitespace differences", () => {
|
it("produces different hashes for different leading indentation", () => {
|
||||||
//#given
|
//#given
|
||||||
const content1 = "function hello() {"
|
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
|
//#when
|
||||||
const hash1 = computeLineHash(1, content1)
|
const hash1 = computeLineHash(1, content1)
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { createHashlineChunkFormatter } from "./hashline-chunk-formatter"
|
|||||||
const RE_SIGNIFICANT = /[\p{L}\p{N}]/u
|
const RE_SIGNIFICANT = /[\p{L}\p{N}]/u
|
||||||
|
|
||||||
export function computeLineHash(lineNumber: number, content: string): string {
|
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 seed = RE_SIGNIFICANT.test(stripped) ? 0 : lineNumber
|
||||||
const hash = Bun.hash.xxHash32(stripped, seed)
|
const hash = Bun.hash.xxHash32(stripped, seed)
|
||||||
const index = hash % 256
|
const index = hash % 256
|
||||||
|
|||||||
Reference in New Issue
Block a user