From bfe02115b301b9b7a2cf1860cacbb76647b91ca1 Mon Sep 17 00:00:00 2001 From: Denis Sokolov Date: Mon, 10 Mar 2025 12:00:00 +0000 Subject: [PATCH 1/2] fix(paths): respect desired path on case-insensitive file systems --- src/utils/get-relative-path.ts | 7 +++++-- test/tests/get-match-portion.test.ts | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 test/tests/get-match-portion.test.ts diff --git a/src/utils/get-relative-path.ts b/src/utils/get-relative-path.ts index 38d1758..f990164 100644 --- a/src/utils/get-relative-path.ts +++ b/src/utils/get-relative-path.ts @@ -46,7 +46,10 @@ function getIsFsCaseSensitive() { return isCaseSensitiveFilesystem; } -function getMatchPortion(from: string, to: string) { +/** + * @private The export is only for unit tests + */ +export function getMatchPortion(from: string, to: string) { const lowerFrom = from.toLocaleLowerCase(); const lowerTo = to.toLocaleLowerCase(); @@ -58,7 +61,7 @@ function getMatchPortion(from: string, to: string) { i++; } - return from.slice(0, i); + return to.slice(0, i); } // endregion diff --git a/test/tests/get-match-portion.test.ts b/test/tests/get-match-portion.test.ts new file mode 100644 index 0000000..81b5482 --- /dev/null +++ b/test/tests/get-match-portion.test.ts @@ -0,0 +1,18 @@ +import { getMatchPortion } from '../../src/utils/get-relative-path' + +describe(`getMatchPortion`, () => { + it("works in a simple case", () => { + expect(getMatchPortion("/foo/bar", "/foo/quux")).toBe("/foo/"); + }); + + // We use the function getMatchPortion to generate a new path for “to”, so let’s preserve + // the case where possible. + // Otherwise we are introducing inconsistency for our users, who may have had import from Foo, + // their file is named Foo, but we rewrite the path to foo. + // Although the file is still accessible in the file system, other tools might reasonably + // complain about the unexpected case mismatch. + it("prioritizes the casing of the “to” parameter", () => { + expect(getMatchPortion("/foo/bar", "/foO/quux")).toBe("/foO/"); + expect(getMatchPortion("/foo/bar", "/foo/Bonk")).toBe("/foo/B"); + }); +}); From c99dd92798aa039e6627b003c282ded24ce55f2f Mon Sep 17 00:00:00 2001 From: Daniel Perez Alvarez Date: Mon, 10 Mar 2025 17:35:25 -0700 Subject: [PATCH 2/2] format code --- src/utils/get-relative-path.ts | 4 +--- test/tests/get-match-portion.test.ts | 28 ++++++++++++++-------------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/utils/get-relative-path.ts b/src/utils/get-relative-path.ts index f990164..791f2da 100644 --- a/src/utils/get-relative-path.ts +++ b/src/utils/get-relative-path.ts @@ -46,9 +46,7 @@ function getIsFsCaseSensitive() { return isCaseSensitiveFilesystem; } -/** - * @private The export is only for unit tests - */ +/** @private The Export is only for unit tests */ export function getMatchPortion(from: string, to: string) { const lowerFrom = from.toLocaleLowerCase(); const lowerTo = to.toLocaleLowerCase(); diff --git a/test/tests/get-match-portion.test.ts b/test/tests/get-match-portion.test.ts index 81b5482..902f3e8 100644 --- a/test/tests/get-match-portion.test.ts +++ b/test/tests/get-match-portion.test.ts @@ -1,18 +1,18 @@ -import { getMatchPortion } from '../../src/utils/get-relative-path' +import { getMatchPortion } from "../../src/utils/get-relative-path"; describe(`getMatchPortion`, () => { - it("works in a simple case", () => { - expect(getMatchPortion("/foo/bar", "/foo/quux")).toBe("/foo/"); - }); + it("works in a simple case", () => { + expect(getMatchPortion("/foo/bar", "/foo/quux")).toBe("/foo/"); + }); - // We use the function getMatchPortion to generate a new path for “to”, so let’s preserve - // the case where possible. - // Otherwise we are introducing inconsistency for our users, who may have had import from Foo, - // their file is named Foo, but we rewrite the path to foo. - // Although the file is still accessible in the file system, other tools might reasonably - // complain about the unexpected case mismatch. - it("prioritizes the casing of the “to” parameter", () => { - expect(getMatchPortion("/foo/bar", "/foO/quux")).toBe("/foO/"); - expect(getMatchPortion("/foo/bar", "/foo/Bonk")).toBe("/foo/B"); - }); + // We use the function getMatchPortion to generate a new path for “to”, so let’s preserve + // the case where possible. + // Otherwise we are introducing inconsistency for our users, who may have had import from Foo, + // their file is named Foo, but we rewrite the path to foo. + // Although the file is still accessible in the file system, other tools might reasonably + // complain about the unexpected case mismatch. + it("prioritizes the casing of the “to” parameter", () => { + expect(getMatchPortion("/foo/bar", "/foO/quux")).toBe("/foO/"); + expect(getMatchPortion("/foo/bar", "/foo/Bonk")).toBe("/foo/B"); + }); });