Skip to content

Commit

Permalink
Try looking for a node_modules path first, and cache expensive lookups.
Browse files Browse the repository at this point in the history
  • Loading branch information
bodil committed Jul 27, 2022
1 parent d248684 commit 65dfba3
Showing 1 changed file with 41 additions and 18 deletions.
59 changes: 41 additions & 18 deletions src/lib/models/types.ts
Expand Up @@ -850,27 +850,21 @@ export class ReferenceType extends Type {
.fileName.replace(/\\/g, "/");
if (!symbolPath) return ref;

function findPackageForPath(sourcePath: string): string | undefined {
let basePath = sourcePath;
for (;;) {
const nextPath = path.dirname(basePath);
if (nextPath === basePath) {
return;
}
basePath = nextPath;
const projectPath = path.join(basePath, "package.json");
try {
const packageJsonData = fs.readFileSync(projectPath, {
encoding: "utf8",
});
const packageJson = JSON.parse(packageJsonData);
return packageJson.name;
} catch (err) {
continue;
}
// Attempt to decide package name from path if it contains "node_modules"
let startIndex = symbolPath.lastIndexOf("node_modules/");
if (startIndex !== -1) {
startIndex += "node_modules/".length;
let stopIndex = symbolPath.indexOf("/", startIndex);
// Scoped package, e.g. `@types/node`
if (symbolPath[startIndex] === "@") {
stopIndex = symbolPath.indexOf("/", stopIndex + 1);
}
const packageName = symbolPath.substring(startIndex, stopIndex);
ref.package = packageName;
return ref;
}

// Otherwise, look for a "package.json" file in a parent path
ref.package = findPackageForPath(symbolPath);
return ref;
}
Expand Down Expand Up @@ -1300,3 +1294,32 @@ export class UnknownType extends Type {
};
}
}

const packageJsonLookupCache: Record<string, string> = {};

function findPackageForPath(sourcePath: string): string | undefined {
if (packageJsonLookupCache[sourcePath] !== undefined) {
return packageJsonLookupCache[sourcePath];
}
let basePath = sourcePath;
for (;;) {
const nextPath = path.dirname(basePath);
if (nextPath === basePath) {
return;
}
basePath = nextPath;
const projectPath = path.join(basePath, "package.json");
try {
const packageJsonData = fs.readFileSync(projectPath, {
encoding: "utf8",
});
const packageJson = JSON.parse(packageJsonData);
if (packageJson.name !== undefined) {
packageJsonLookupCache[sourcePath] = packageJson.name;
}
return packageJson.name;
} catch (err) {
continue;
}
}
}

0 comments on commit 65dfba3

Please sign in to comment.