Skip to content

Commit

Permalink
Typescript: Ignore fields marked with @internal tag
Browse files Browse the repository at this point in the history
  • Loading branch information
fuma-nama committed Mar 6, 2024
1 parent b9e99df commit f4aa6b6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/gorgeous-flies-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"fumadocs-typescript": patch
---

Ignore fields marked with `@internal` tag
34 changes: 22 additions & 12 deletions packages/typescript/src/generate/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,30 @@ export interface DocEntry {
interface EntryContext {
program: ts.Program;
checker: ts.TypeChecker;
options: GenerateOptions;
transform?: Transformer;
type: ts.Type;
symbol: ts.Symbol;
}

type Transformer = (
this: EntryContext,
entry: DocEntry,
propertyType: ts.Type,
propertySymbol: ts.Symbol,
) => void;

export interface GenerateOptions {
/**
* Allow fields with `@internal` tag
*
* @defaultValue false
*/
allowInternal?: boolean;

/**
* Modify output property entry
*/
transform?: (
this: EntryContext,
entry: DocEntry,
propertyType: ts.Type,
propertySymbol: ts.Symbol,
) => void;
transform?: Transformer;
}

export interface GenerateDocumentationOptions extends GenerateOptions {
Expand Down Expand Up @@ -66,13 +75,13 @@ export function generateDocumentation(
export function generate(
program: ts.Program,
symbol: ts.Symbol,
options: GenerateOptions,
{ allowInternal = false, transform }: GenerateOptions,
): GeneratedDoc {
const checker = program.getTypeChecker();
const type = checker.getDeclaredTypeOfSymbol(symbol);
const entryContext: EntryContext = {
checker,
options,
transform,
program,
type,
symbol,
Expand All @@ -85,12 +94,13 @@ export function generate(
),
entries: type
.getProperties()
.map((prop) => getDocEntry(prop, entryContext)),
.map((prop) => getDocEntry(prop, entryContext))
.filter((entry) => allowInternal || !('internal' in entry.tags)),
};
}

function getDocEntry(prop: ts.Symbol, context: EntryContext): DocEntry {
const { checker, options } = context;
const { checker, transform } = context;
const subType = checker.getTypeOfSymbol(prop);
const tags = Object.fromEntries(
prop
Expand Down Expand Up @@ -119,7 +129,7 @@ function getDocEntry(prop: ts.Symbol, context: EntryContext): DocEntry {
type: typeName,
};

options.transform?.call(context, entry, subType, prop);
transform?.call(context, entry, subType, prop);

return entry;
}
5 changes: 5 additions & 0 deletions packages/typescript/test/fixtures/test-2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@ export interface Player {
* Returned by API
*/
age: number;

/**
* @internal
*/
privateValue: string
}

0 comments on commit f4aa6b6

Please sign in to comment.