Skip to content

Commit ec4b4b6

Browse files
committed
Add tests for brackets in file names
1 parent 4307dfd commit ec4b4b6

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

src/lib/models/types.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -964,9 +964,7 @@ export class ReferenceType extends Type {
964964
symbol.flags & ts.SymbolFlags.TypeParameter
965965
);
966966

967-
const symbolPath = symbol.declarations?.[0]
968-
?.getSourceFile()
969-
.fileName.replace(/\\/g, "/");
967+
const symbolPath = symbol.declarations?.[0]?.getSourceFile().fileName;
970968
if (!symbolPath) return ref;
971969

972970
ref.package = findPackageForPath(symbolPath);

src/lib/utils/fs.ts

+4-8
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,13 @@ export function isDir(path: string) {
2727
export function deriveRootDir(globPaths: string[]): string {
2828
const normalized = globPaths.map(normalizePath);
2929
const globs = createMinimatch(normalized);
30-
const rootPaths = globs.flatMap((glob, i) =>
30+
const rootPaths = globs.flatMap((glob) =>
3131
filterMap(glob.set, (set) => {
3232
const stop = set.findIndex((part) => typeof part !== "string");
3333
if (stop === -1) {
34-
return normalized[i];
34+
return set.join("/");
3535
} else {
36-
const kept = set.slice(0, stop).join("/");
37-
return normalized[i].substring(
38-
0,
39-
normalized[i].indexOf(kept) + kept.length,
40-
);
36+
return set.slice(0, stop).join("/");
4137
}
4238
}),
4339
);
@@ -52,7 +48,7 @@ export function getCommonDirectory(files: readonly string[]): string {
5248
return "";
5349
}
5450

55-
const roots = files.map((f) => f.split(/\\|\//));
51+
const roots = files.map((f) => f.split("/"));
5652
if (roots.length === 1) {
5753
return roots[0].slice(0, -1).join("/");
5854
}

src/test/utils/fs.test.ts

+26
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,35 @@ import { type Project, tempdirProject } from "@typestrong/fs-fixture-builder";
44
import { type AssertionError, deepStrictEqual as equal } from "assert";
55
import { basename, dirname, resolve, normalize, join } from "path";
66
import {
7+
deriveRootDir,
78
getCommonDirectory,
89
glob,
910
inferPackageEntryPointPaths,
1011
} from "../../lib/utils/fs.js";
1112
import { normalizePath } from "../../lib/utils/paths.js";
1213

1314
describe("fs.ts", () => {
15+
describe("deriveRootDir", () => {
16+
it("Ignores glob parts of filenames", () => {
17+
equal(
18+
deriveRootDir(["src/foo/**/*.ts", "src/foo/**/*.tsx"]),
19+
"src/foo",
20+
);
21+
22+
equal(deriveRootDir(["src/foo/**/*.ts", "src/**/**/*.tsx"]), "src");
23+
});
24+
25+
it("Returns a fs path when path contains glob characters #2825", () => {
26+
equal(
27+
deriveRootDir([
28+
"src/foo/\\[abc]/*.ts",
29+
"src/foo/\\[abc]/sub/*.ts",
30+
]),
31+
"src/foo/[abc]",
32+
);
33+
});
34+
});
35+
1436
describe("getCommonDirectory", () => {
1537
it("Returns the empty string if no files are provided", () => {
1638
equal(getCommonDirectory([]), "");
@@ -31,6 +53,10 @@ describe("fs.ts", () => {
3153
"/a/b",
3254
);
3355
});
56+
57+
it("Does not respect Windows path sep #2825", () => {
58+
equal(getCommonDirectory(["/a/b\\]/c", "/a/b\\]/d"]), "/a/b\\]");
59+
});
3460
});
3561

3662
describe("glob", () => {

0 commit comments

Comments
 (0)