diff --git a/src/head-text.test.ts b/src/head-text.test.ts new file mode 100644 index 00000000..514fc638 --- /dev/null +++ b/src/head-text.test.ts @@ -0,0 +1,65 @@ +import dedent from "ts-dedent"; +import { + ModuleKind, + ModuleResolutionKind, + Project, + ScriptTarget, +} from "ts-morph"; +import { expect, test } from "vitest"; +import { headText } from "./head-text"; + +test("return text from declaration before body without JSDoc", () => { + const project = new Project({ + useInMemoryFileSystem: true, + compilerOptions: { + lib: ["lib.esnext.full.d.ts"], + target: ScriptTarget.ESNext, + module: ModuleKind.ESNext, + moduleResolution: ModuleResolutionKind.Bundler, + }, + }); + const indexFile = project.createSourceFile( + "index.ts", + dedent` + /** Docs for FooInterface */ + interface FooInterface { + bar: number; + } + + /** Docs for FooClass */ + class FooClass extends FooInterface { + bar; + constructor() { + this.bar = 1; + } + } + + /** Docs for FooEnum */ + enum FooEnum { + Bar, + Baz + } + + /** Docs for FooNamespace */ + namespace FooNamespace { + /** Docs for FooNamespaceClass */ + class FooNamespaceClass { + bar; + constructor() { + this.bar = 1; + } + } + } + `, + ); + expect(headText(indexFile.getInterfaceOrThrow("FooInterface"))).toBe( + "interface FooInterface {}", + ); + expect(headText(indexFile.getClassOrThrow("FooClass"))).toBe( + "class FooClass extends FooInterface {}", + ); + expect(headText(indexFile.getEnumOrThrow("FooEnum"))).toBe("enum FooEnum {}"); + expect(headText(indexFile.getModuleOrThrow("FooNamespace"))).toBe( + "namespace FooNamespace {}", + ); +}); diff --git a/src/head-text.ts b/src/head-text.ts index 588e3bb8..a79c6b05 100644 --- a/src/head-text.ts +++ b/src/head-text.ts @@ -21,7 +21,11 @@ export const headText = ( } if (node.getKind() === SyntaxKind.OpenBraceToken) { // Stop at body start, marked by the first `{` - // found in a class or interface declaration. + // found in a class, interface or enum declaration. + break; + } + if (node.getKind() === SyntaxKind.ModuleBlock) { + // Stop at module body start, found in a namespace declaration. break; } parts.push(node.getText());