Skip to content

Commit 019bb5c

Browse files
fix: respect desired path on case-insensitive file systems (#397)
Today typescript-transform-paths changes the case of import statements on a case-insensitive file system. I have a file `Foo` and an import `from "./Foo"`, yet after the transformation with typescript-transform-paths, the import looks like `"./foo"`. Even if that import would work on a case-insensitive file system, it seems clearly undesirable.
1 parent 5d24262 commit 019bb5c

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

Diff for: src/utils/get-relative-path.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ function getIsFsCaseSensitive() {
4646
return isCaseSensitiveFilesystem;
4747
}
4848

49-
function getMatchPortion(from: string, to: string) {
49+
/** @private The Export is only for unit tests */
50+
export function getMatchPortion(from: string, to: string) {
5051
const lowerFrom = from.toLocaleLowerCase();
5152
const lowerTo = to.toLocaleLowerCase();
5253

@@ -58,7 +59,7 @@ function getMatchPortion(from: string, to: string) {
5859
i++;
5960
}
6061

61-
return from.slice(0, i);
62+
return to.slice(0, i);
6263
}
6364

6465
// endregion

Diff for: test/tests/get-match-portion.test.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { getMatchPortion } from "../../src/utils/get-relative-path";
2+
3+
describe(`getMatchPortion`, () => {
4+
it("works in a simple case", () => {
5+
expect(getMatchPortion("/foo/bar", "/foo/quux")).toBe("/foo/");
6+
});
7+
8+
// We use the function getMatchPortion to generate a new path for “to”, so let’s preserve
9+
// the case where possible.
10+
// Otherwise we are introducing inconsistency for our users, who may have had import from Foo,
11+
// their file is named Foo, but we rewrite the path to foo.
12+
// Although the file is still accessible in the file system, other tools might reasonably
13+
// complain about the unexpected case mismatch.
14+
it("prioritizes the casing of the “to” parameter", () => {
15+
expect(getMatchPortion("/foo/bar", "/foO/quux")).toBe("/foO/");
16+
expect(getMatchPortion("/foo/bar", "/foo/Bonk")).toBe("/foo/B");
17+
});
18+
});

0 commit comments

Comments
 (0)