From f5f31b0b41e0e729b3dd59ce26398e2c991eb6e7 Mon Sep 17 00:00:00 2001 From: Gerrit Birkeland Date: Fri, 9 Feb 2024 14:57:24 -0700 Subject: [PATCH] Expose `Context.getNodeComment`, #2498 --- CHANGELOG.md | 4 +--- src/lib/converter/comments/discovery.ts | 23 +++++++++++++++++++++++ src/lib/converter/comments/index.ts | 18 ++++++++++++++++++ src/lib/converter/context.ts | 12 ++++++++++++ 4 files changed, 54 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e1f65e0e4..a5c90bc74 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/lib/converter/comments/discovery.ts b/src/lib/converter/comments/discovery.ts index 86ed6f234..2a15f4a99 100644 --- a/src/lib/converter/comments/discovery.ts +++ b/src/lib/converter/comments/discovery.ts @@ -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, diff --git a/src/lib/converter/comments/index.ts b/src/lib/converter/comments/index.ts index c162751af..da7b776db 100644 --- a/src/lib/converter/comments/index.ts +++ b/src/lib/converter/comments/index.ts @@ -10,6 +10,7 @@ import { DiscoveredComment, discoverComment, discoverFileComment, + discoverNodeComment, discoverSignatureComment, } from "./discovery"; import { lexLineComments } from "./lineLexer"; @@ -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, diff --git a/src/lib/converter/context.ts b/src/lib/converter/context.ts index 63beb4be4..aa4c8f3b7 100644 --- a/src/lib/converter/context.ts +++ b/src/lib/converter/context.ts @@ -18,6 +18,7 @@ import { getComment, getFileComment, getJsDocComment, + getNodeComment, getSignatureComment, } from "./comments"; import { getHumanName } from "../utils/tsutils"; @@ -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,