Skip to content

Commit

Permalink
feat: update headText
Browse files Browse the repository at this point in the history
Fix handling of namespaces.
Add tests.
  • Loading branch information
velut committed Jan 16, 2024
1 parent ecc213d commit 08f3a91
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
65 changes: 65 additions & 0 deletions src/head-text.test.ts
Original file line number Diff line number Diff line change
@@ -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 {}",
);
});
6 changes: 5 additions & 1 deletion src/head-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit 08f3a91

Please sign in to comment.