Skip to content

Commit ecffb2d

Browse files
committed
feat: add getPackageDeclarations
1 parent c79caaf commit ecffb2d

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { goTry } from "go-go-try";
2+
import { dedent } from "ts-dedent";
3+
import { ModuleKind, ModuleResolutionKind, Project, ScriptTarget } from "ts-morph";
4+
import { afterEach, expect, test, vi } from "vitest";
5+
import { extractDeclarations } from "./extract-declarations.ts";
6+
import { getPackageDeclarations } from "./get-package-declarations.ts";
7+
8+
vi.mock("./extract-declarations", () => ({
9+
extractDeclarations: vi.fn(),
10+
}));
11+
12+
afterEach(() => {
13+
vi.clearAllMocks();
14+
});
15+
16+
test("success", async () => {
17+
const project = new Project({
18+
useInMemoryFileSystem: true,
19+
compilerOptions: {
20+
lib: ["lib.esnext.full.d.ts"],
21+
target: ScriptTarget.ESNext,
22+
module: ModuleKind.ESNext,
23+
moduleResolution: ModuleResolutionKind.Bundler,
24+
},
25+
});
26+
const indexFile = project.createSourceFile(
27+
"index.ts",
28+
dedent`
29+
export {};
30+
`,
31+
);
32+
vi.mocked(extractDeclarations).mockResolvedValue([]);
33+
const [err, declarations] = await goTry(
34+
getPackageDeclarations({
35+
pkgName: "foo",
36+
project,
37+
indexFile,
38+
maxDepth: 5,
39+
}),
40+
);
41+
expect(err).toBeUndefined();
42+
expect(declarations).toStrictEqual([]);
43+
});
44+
45+
test("failure", async () => {
46+
const project = new Project({
47+
useInMemoryFileSystem: true,
48+
compilerOptions: {
49+
lib: ["lib.esnext.full.d.ts"],
50+
target: ScriptTarget.ESNext,
51+
module: ModuleKind.ESNext,
52+
moduleResolution: ModuleResolutionKind.Bundler,
53+
},
54+
});
55+
const indexFile = project.createSourceFile(
56+
"index.ts",
57+
dedent`
58+
export {};
59+
`,
60+
);
61+
vi.mocked(extractDeclarations).mockRejectedValue(new Error("test"));
62+
const [err, declarations] = await goTry(
63+
getPackageDeclarations({
64+
pkgName: "foo",
65+
project,
66+
indexFile,
67+
maxDepth: 5,
68+
}),
69+
);
70+
expect(err).toBeDefined();
71+
expect(declarations).toBeUndefined();
72+
});

src/get-package-declarations.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import type { Project, SourceFile } from "ts-morph";
2+
import { extractDeclarations } from "./extract-declarations.ts";
3+
import type { ExtractedDeclaration } from "./types.ts";
4+
5+
/** `GetPackageDeclarationsOptions` contains the options for calling {@link getPackageDeclarations}. */
6+
export interface GetPackageDeclarationsOptions {
7+
/** Package name. */
8+
pkgName: string;
9+
10+
/** `Project` created with `ts-morph`. */
11+
project: Project;
12+
13+
/** `SourceFile` created with `ts-morph` representing the index file. */
14+
indexFile: SourceFile;
15+
16+
/** Depth limit for the extraction. */
17+
maxDepth: number;
18+
}
19+
20+
/** `getPackageDeclarations` extracts the declarations exported from the given project representing a package. */
21+
export async function getPackageDeclarations({
22+
pkgName,
23+
project,
24+
indexFile,
25+
maxDepth,
26+
}: GetPackageDeclarationsOptions): Promise<ExtractedDeclaration[]> {
27+
return await extractDeclarations({
28+
containerName: "",
29+
container: indexFile,
30+
maxDepth,
31+
project,
32+
pkgName,
33+
});
34+
}

0 commit comments

Comments
 (0)