|
| 1 | +import { |
| 2 | + Node, |
| 3 | + SyntaxKind, |
| 4 | + CommentRange, |
| 5 | + getLeadingCommentRanges |
| 6 | +} from 'typescript'; |
| 7 | +import { TextRange } from '@microsoft/tsdoc'; |
| 8 | + |
| 9 | +export interface IFoundComment { |
| 10 | + compilerNode: Node; |
| 11 | + textRange: TextRange; |
| 12 | +} |
| 13 | + |
| 14 | +function isDeclarationKind(kind: SyntaxKind): boolean { |
| 15 | + switch (kind) { |
| 16 | + case SyntaxKind.ArrowFunction: |
| 17 | + case SyntaxKind.BindingElement: |
| 18 | + case SyntaxKind.ClassDeclaration: |
| 19 | + case SyntaxKind.ClassExpression: |
| 20 | + case SyntaxKind.Constructor: |
| 21 | + case SyntaxKind.EnumDeclaration: |
| 22 | + case SyntaxKind.EnumMember: |
| 23 | + case SyntaxKind.ExportSpecifier: |
| 24 | + case SyntaxKind.FunctionDeclaration: |
| 25 | + case SyntaxKind.FunctionExpression: |
| 26 | + case SyntaxKind.GetAccessor: |
| 27 | + case SyntaxKind.ImportEqualsDeclaration: |
| 28 | + case SyntaxKind.ImportSpecifier: |
| 29 | + case SyntaxKind.InterfaceDeclaration: |
| 30 | + case SyntaxKind.JsxAttribute: |
| 31 | + case SyntaxKind.MethodDeclaration: |
| 32 | + case SyntaxKind.MethodSignature: |
| 33 | + case SyntaxKind.ModuleDeclaration: |
| 34 | + case SyntaxKind.NamespaceExportDeclaration: |
| 35 | + case SyntaxKind.NamespaceImport: |
| 36 | + case SyntaxKind.Parameter: |
| 37 | + case SyntaxKind.PropertyAssignment: |
| 38 | + case SyntaxKind.PropertyDeclaration: |
| 39 | + case SyntaxKind.PropertySignature: |
| 40 | + case SyntaxKind.SetAccessor: |
| 41 | + case SyntaxKind.ShorthandPropertyAssignment: |
| 42 | + case SyntaxKind.TypeAliasDeclaration: |
| 43 | + case SyntaxKind.TypeParameter: |
| 44 | + case SyntaxKind.VariableDeclaration: |
| 45 | + case SyntaxKind.JSDocTypedefTag: |
| 46 | + case SyntaxKind.JSDocCallbackTag: |
| 47 | + case SyntaxKind.JSDocPropertyTag: |
| 48 | + return true; |
| 49 | + } |
| 50 | + return false; |
| 51 | +} |
| 52 | + |
| 53 | +function getJSDocCommentRanges(node: Node, text: string): CommentRange[] { |
| 54 | + const commentRanges: CommentRange[] = []; |
| 55 | + |
| 56 | + switch (node.kind) { |
| 57 | + case SyntaxKind.Parameter: |
| 58 | + case SyntaxKind.TypeParameter: |
| 59 | + case SyntaxKind.FunctionExpression: |
| 60 | + case SyntaxKind.ArrowFunction: |
| 61 | + case SyntaxKind.ParenthesizedExpression: |
| 62 | + return commentRanges; |
| 63 | + } |
| 64 | + |
| 65 | + // Collect comment ranges |
| 66 | + const ranges: CommentRange[] | undefined = getLeadingCommentRanges(text, node.getFullStart()); |
| 67 | + if (ranges) { |
| 68 | + commentRanges.push(...ranges); |
| 69 | + } |
| 70 | + |
| 71 | + return commentRanges; |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Recursively walks through the TypeScript AST and discovers JSDoc comments associated with declaration nodes. |
| 76 | + * |
| 77 | + * @param node - The TypeScript AST node to analyze |
| 78 | + * @param foundComments - Array to collect discovered comments |
| 79 | + */ |
| 80 | +export function discoverComments(node: Node, foundComments: IFoundComment[]): void { |
| 81 | + const buffer: string = node.getSourceFile().getFullText(); // don't use getText() here! |
| 82 | + if (isDeclarationKind(node.kind)) { |
| 83 | + const comments: CommentRange[] = getJSDocCommentRanges(node, buffer); |
| 84 | + |
| 85 | + if (comments.length > 0) { |
| 86 | + for (const comment of comments) { |
| 87 | + foundComments.push({ |
| 88 | + compilerNode: node, |
| 89 | + textRange: TextRange.fromStringRange(buffer, comment.pos, comment.end) |
| 90 | + }); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + return node.forEachChild((child) => discoverComments(child, foundComments)); |
| 95 | +} |
0 commit comments