Skip to content

Commit

Permalink
Add tests for brackets in file names
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit0 committed Jan 13, 2025
1 parent 4307dfd commit ec4b4b6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
4 changes: 1 addition & 3 deletions src/lib/models/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -964,9 +964,7 @@ export class ReferenceType extends Type {
symbol.flags & ts.SymbolFlags.TypeParameter
);

const symbolPath = symbol.declarations?.[0]
?.getSourceFile()
.fileName.replace(/\\/g, "/");
const symbolPath = symbol.declarations?.[0]?.getSourceFile().fileName;
if (!symbolPath) return ref;

ref.package = findPackageForPath(symbolPath);
Expand Down
12 changes: 4 additions & 8 deletions src/lib/utils/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,13 @@ export function isDir(path: string) {
export function deriveRootDir(globPaths: string[]): string {
const normalized = globPaths.map(normalizePath);
const globs = createMinimatch(normalized);
const rootPaths = globs.flatMap((glob, i) =>
const rootPaths = globs.flatMap((glob) =>
filterMap(glob.set, (set) => {
const stop = set.findIndex((part) => typeof part !== "string");
if (stop === -1) {
return normalized[i];
return set.join("/");
} else {
const kept = set.slice(0, stop).join("/");
return normalized[i].substring(
0,
normalized[i].indexOf(kept) + kept.length,
);
return set.slice(0, stop).join("/");
}
}),
);
Expand All @@ -52,7 +48,7 @@ export function getCommonDirectory(files: readonly string[]): string {
return "";
}

const roots = files.map((f) => f.split(/\\|\//));
const roots = files.map((f) => f.split("/"));
if (roots.length === 1) {
return roots[0].slice(0, -1).join("/");
}
Expand Down
26 changes: 26 additions & 0 deletions src/test/utils/fs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,35 @@ import { type Project, tempdirProject } from "@typestrong/fs-fixture-builder";
import { type AssertionError, deepStrictEqual as equal } from "assert";
import { basename, dirname, resolve, normalize, join } from "path";
import {
deriveRootDir,
getCommonDirectory,
glob,
inferPackageEntryPointPaths,
} from "../../lib/utils/fs.js";
import { normalizePath } from "../../lib/utils/paths.js";

describe("fs.ts", () => {
describe("deriveRootDir", () => {
it("Ignores glob parts of filenames", () => {
equal(
deriveRootDir(["src/foo/**/*.ts", "src/foo/**/*.tsx"]),
"src/foo",
);

equal(deriveRootDir(["src/foo/**/*.ts", "src/**/**/*.tsx"]), "src");
});

it("Returns a fs path when path contains glob characters #2825", () => {
equal(
deriveRootDir([
"src/foo/\\[abc]/*.ts",
"src/foo/\\[abc]/sub/*.ts",
]),
"src/foo/[abc]",
);
});
});

describe("getCommonDirectory", () => {
it("Returns the empty string if no files are provided", () => {
equal(getCommonDirectory([]), "");
Expand All @@ -31,6 +53,10 @@ describe("fs.ts", () => {
"/a/b",
);
});

it("Does not respect Windows path sep #2825", () => {
equal(getCommonDirectory(["/a/b\\]/c", "/a/b\\]/d"]), "/a/b\\]");
});
});

describe("glob", () => {
Expand Down

0 comments on commit ec4b4b6

Please sign in to comment.