Skip to content

Commit

Permalink
Expose Context.getNodeComment, #2498
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit0 committed Feb 9, 2024
1 parent d26f76f commit f5f31b0
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
4 changes: 1 addition & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
- Added a new `--sitemapBaseUrl` option. When specified, TypeDoc will generate a `sitemap.xml` in your output folder that describes the site, #2480.
- Added support for the `@class` tag. When added to a comment on a variable or function, TypeDoc will convert the member as a class, #2479.
Note: This should only be used on symbols which actually represent a class, but are not declared as a class for some reason.

## Features

- Added support for `@groupDescription` and `@categoryDescription` to provide a description of groups and categories, #2494.
- Exposed `Context.getNodeComment` for plugin use, #2498.

## Bug Fixes

Expand Down
23 changes: 23 additions & 0 deletions src/lib/converter/comments/discovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,29 @@ export function discoverFileComment(
}
}

export function discoverNodeComment(
node: ts.Node,
commentStyle: CommentStyle,
): DiscoveredComment | undefined {
const text = node.getSourceFile().text;
const comments = collectCommentRanges(
ts.getLeadingCommentRanges(text, node.pos),
);
comments.reverse();

const selectedDocComment = comments.find((ranges) =>
permittedRange(text, ranges, commentStyle),
);

if (selectedDocComment) {
return {
file: node.getSourceFile(),
ranges: selectedDocComment,
jsDoc: findJsDocForComment(node, selectedDocComment),
};
}
}

export function discoverComment(
symbol: ts.Symbol,
kind: ReflectionKind,
Expand Down
18 changes: 18 additions & 0 deletions src/lib/converter/comments/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
DiscoveredComment,
discoverComment,
discoverFileComment,
discoverNodeComment,
discoverSignatureComment,
} from "./discovery";
import { lexLineComments } from "./lineLexer";
Expand Down Expand Up @@ -171,6 +172,23 @@ export function getComment(
return comment;
}

export function getNodeComment(
node: ts.Node,
kind: ReflectionKind,
config: CommentParserConfig,
logger: Logger,
commentStyle: CommentStyle,
checker: ts.TypeChecker | undefined,
) {
return getCommentImpl(
discoverNodeComment(node, commentStyle),
config,
logger,
kind === ReflectionKind.Module,
checker,
);
}

export function getFileComment(
file: ts.SourceFile,
config: CommentParserConfig,
Expand Down
12 changes: 12 additions & 0 deletions src/lib/converter/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
getComment,
getFileComment,
getJsDocComment,
getNodeComment,
getSignatureComment,
} from "./comments";
import { getHumanName } from "../utils/tsutils";
Expand Down Expand Up @@ -270,6 +271,17 @@ export class Context {
);
}

getNodeComment(node: ts.Node, kind: ReflectionKind) {
return getNodeComment(
node,
kind,
this.converter.config,
this.logger,
this.converter.commentStyle,
this.converter.useTsLinkResolution ? this.checker : undefined,
);
}

getFileComment(node: ts.SourceFile) {
return getFileComment(
node,
Expand Down

0 comments on commit f5f31b0

Please sign in to comment.