diff --git a/src/utils/get-relative-path.ts b/src/utils/get-relative-path.ts index 38d1758..791f2da 100644 --- a/src/utils/get-relative-path.ts +++ b/src/utils/get-relative-path.ts @@ -46,7 +46,8 @@ 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 +59,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..902f3e8 --- /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"); + }); +});