|
| 1 | +import * as fs from "fs"; |
| 2 | +import * as path from "path"; |
| 3 | + |
1 | 4 | import type * as ts from "typescript";
|
2 | 5 | import type { Context } from "../converter";
|
3 | 6 | import { Reflection } from "./reflections/abstract";
|
@@ -847,18 +850,28 @@ export class ReferenceType extends Type {
|
847 | 850 | .fileName.replace(/\\/g, "/");
|
848 | 851 | if (!symbolPath) return ref;
|
849 | 852 |
|
850 |
| - let startIndex = symbolPath.indexOf("node_modules/"); |
851 |
| - if (startIndex === -1) return ref; |
852 |
| - startIndex += "node_modules/".length; |
853 |
| - let stopIndex = symbolPath.indexOf("/", startIndex); |
854 |
| - // Scoped package, e.g. `@types/node` |
855 |
| - if (symbolPath[startIndex] === "@") { |
856 |
| - stopIndex = symbolPath.indexOf("/", stopIndex + 1); |
| 853 | + function findPackageForPath(sourcePath: string): string | undefined { |
| 854 | + let basePath = sourcePath; |
| 855 | + for (;;) { |
| 856 | + const nextPath = path.dirname(basePath); |
| 857 | + if (nextPath === basePath) { |
| 858 | + return; |
| 859 | + } |
| 860 | + basePath = nextPath; |
| 861 | + const projectPath = path.join(basePath, "package.json"); |
| 862 | + try { |
| 863 | + const packageJsonData = fs.readFileSync(projectPath, { |
| 864 | + encoding: "utf8", |
| 865 | + }); |
| 866 | + const packageJson = JSON.parse(packageJsonData); |
| 867 | + return packageJson.name; |
| 868 | + } catch (err) { |
| 869 | + continue; |
| 870 | + } |
| 871 | + } |
857 | 872 | }
|
858 | 873 |
|
859 |
| - const packageName = symbolPath.substring(startIndex, stopIndex); |
860 |
| - ref.package = packageName; |
861 |
| - |
| 874 | + ref.package = findPackageForPath(symbolPath); |
862 | 875 | return ref;
|
863 | 876 | }
|
864 | 877 |
|
|
0 commit comments