Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions packages/definitions-parser/src/lib/module-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,12 @@ export function allReferencedFiles(
return { types, tests };

function recur({ text, exact }: Reference): void {
if (seenReferences.has(text)) {
const resolvedFilename = exact ? text : resolveModule(text, fs);
if (seenReferences.has(resolvedFilename)) {
return;
}
seenReferences.add(text);
seenReferences.add(resolvedFilename);

const resolvedFilename = exact ? text : resolveModule(text, fs);
// tslint:disable-next-line:non-literal-fs-path -- Not a reference to the fs package
if (fs.exists(resolvedFilename)) {
const src = createSourceFile(resolvedFilename, readFileAndThrowOnBOM(resolvedFilename, fs));
Expand Down Expand Up @@ -241,16 +241,15 @@ function findReferencedFiles(src: ts.SourceFile, packageName: string, subDirecto

function addReference(ref: Reference): void {
// `path.normalize` may add windows slashes
const full = normalizeSlashes(
let full = normalizeSlashes(
path.normalize(joinPaths(subDirectory, assertNoWindowsSlashes(src.fileName, ref.text)))
);
// allow files in typesVersions directories (i.e. 'ts3.1') to reference files in parent directory
if (full.startsWith("../" + packageName + "/")) {
ref.text = full.slice(packageName.length + 4);
refs.push(ref);
return;
}
if (
full = full.slice(packageName.length + 4);
} else if (baseDirectory && full.startsWith("../" + baseDirectory + "/")) {
full = full.slice(baseDirectory.length + 4);
} else if (
full.startsWith("..") &&
(baseDirectory === "" || path.normalize(joinPaths(baseDirectory, full)).startsWith(".."))
) {
Expand All @@ -266,7 +265,13 @@ function findReferencedFiles(src: ts.SourceFile, packageName: string, subDirecto

/** boring/foo -> ./foo when subDirectory === '.'; ../foo when it's === 'x'; ../../foo when it's 'x/y' */
function convertToRelativeReference(name: string) {
const relative = "." + "/..".repeat(subDirectory === "." ? 0 : subDirectory.split("/").length);
let relative = ".";
if (subDirectory !== ".") {
relative += "/..".repeat(subDirectory.split("/").length);
if (baseDirectory && subDirectory.startsWith("..")) {
relative = relative.slice(0, -2) + baseDirectory;
}
}
return relative + name.slice(packageName.length);
}
}
Expand Down
20 changes: 20 additions & 0 deletions packages/definitions-parser/test/module-info.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,26 @@ testo({
const i = getModuleInfo("fail", types);
expect(i.dependencies).toEqual(new Set([]));
},
selfInTypesVersionsParent() {
const pkg = new Dir(undefined);
const ts20 = pkg.subdir("ts2.0");
ts20.set(
"index.d.ts",
`/// <reference path="../ts1.0/index.d.ts" />
`
);
ts20.set("component.d.ts", "");
const ts10 = pkg.subdir("ts1.0");
ts10.set(
"index.d.ts",
`import "mock/component";
`
);
const memFS = new InMemoryFS(ts20, "types/mock/ts2.0");
const { types, tests } = allReferencedFiles(["index.d.ts"], memFS, "mock", "types/mock");
expect(Array.from(types.keys())).toEqual(["index.d.ts", "../ts1.0/index.d.ts", "component.d.ts"]);
expect(Array.from(tests.keys())).toEqual([]);
},
getTestDependenciesWorks() {
const { types, tests } = getBoringReferences();
const i = getModuleInfo("boring", types);
Expand Down