MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / getPrecedingDocstring

Function getPrecedingDocstring

src/extraction/tree-sitter-helpers.ts:95–127  ·  view source on GitHub ↗
(node: SyntaxNode, source: string)

Source from the content-addressed store, hash-verified

93 * Get the docstring/comment preceding a node
94 */
95export function getPrecedingDocstring(node: SyntaxNode, source: string): string | undefined {
96 // Climb out of any wrapper(s) so a comment preceding the WHOLE construct
97 // (export-, decorator-, or const-arrow-wrapped) is reachable as a sibling.
98 // The emitted node's own `previousNamedSibling` is empty (export/const) or a
99 // decorator (Python) in those cases, so without this the docstring was
100 // dropped. (#780)
101 let anchor = node;
102 while (anchor.parent && DOCSTRING_WRAPPER_TYPES.has(anchor.parent.type)) {
103 anchor = anchor.parent;
104 }
105
106 let sibling = anchor.previousNamedSibling;
107 const comments: string[] = [];
108
109 while (sibling) {
110 if (
111 sibling.type === 'comment' ||
112 sibling.type === 'line_comment' ||
113 sibling.type === 'block_comment' ||
114 sibling.type === 'documentation_comment'
115 ) {
116 comments.unshift(getNodeText(sibling, source));
117 sibling = sibling.previousNamedSibling;
118 } else {
119 break;
120 }
121 }
122
123 if (comments.length === 0) return undefined;
124
125 // Strip each comment's syntax markers (language-aware), then join.
126 return comments.map(cleanCommentMarkers).join('\n').trim();
127}

Callers 13

extractFunctionMethod · 0.90
extractClassMethod · 0.90
extractMethodMethod · 0.90
extractInterfaceMethod · 0.90
extractStructMethod · 0.90
extractEnumMethod · 0.90
extractPropertyMethod · 0.90
extractFieldMethod · 0.90
extractVariableMethod · 0.90
extractTypeAliasMethod · 0.90
handleFunDeclFunction · 0.90

Calls 3

getNodeTextFunction · 0.85
hasMethod · 0.80
joinMethod · 0.45

Tested by

no test coverage detected