Skip to content

Commit 6017349

Browse files
committed
feat: add getPackageOverview
1 parent 066ba25 commit 6017349

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

src/get-package-overview.test.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { dedent } from "ts-dedent";
2+
import { ModuleKind, ModuleResolutionKind, Project, ScriptTarget } from "ts-morph";
3+
import { expect, test } from "vitest";
4+
import { getPackageOverview } from "./get-package-overview.ts";
5+
6+
test("no overview", () => {
7+
const project = new Project({
8+
useInMemoryFileSystem: true,
9+
compilerOptions: {
10+
lib: ["lib.esnext.full.d.ts"],
11+
target: ScriptTarget.ESNext,
12+
module: ModuleKind.ESNext,
13+
moduleResolution: ModuleResolutionKind.Bundler,
14+
},
15+
});
16+
const indexFile = project.createSourceFile(
17+
"index.ts",
18+
dedent`
19+
export {};
20+
`,
21+
);
22+
expect(getPackageOverview(indexFile)).toBeUndefined();
23+
});
24+
25+
test("with overview", () => {
26+
const project = new Project({
27+
useInMemoryFileSystem: true,
28+
compilerOptions: {
29+
lib: ["lib.esnext.full.d.ts"],
30+
target: ScriptTarget.ESNext,
31+
module: ModuleKind.ESNext,
32+
moduleResolution: ModuleResolutionKind.Bundler,
33+
},
34+
});
35+
const indexFile = project.createSourceFile(
36+
"index.ts",
37+
dedent`
38+
/**
39+
@packageDocumentation
40+
This is the overview.
41+
*/
42+
43+
export {};
44+
`,
45+
);
46+
expect(getPackageOverview(indexFile)).toBe(dedent`
47+
/**
48+
@packageDocumentation
49+
This is the overview.
50+
*/
51+
`);
52+
});

src/get-package-overview.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { SourceFile, SyntaxKind } from "ts-morph";
2+
3+
export function getPackageOverview(indexFile: SourceFile): string | undefined {
4+
return indexFile
5+
.getDescendantsOfKind(SyntaxKind.JSDocTag)
6+
.find((tag) => tag.getTagName() === "packageDocumentation")
7+
?.getParentIfKind(SyntaxKind.JSDoc)
8+
?.getText();
9+
}

0 commit comments

Comments
 (0)