|
| 1 | +import type { AstIdMap, ParentInformation } from '../lang-4.x/ast/model/processing/decorate'; |
| 2 | +import type { NodeId } from '../lang-4.x/ast/model/processing/node-id'; |
| 3 | +import type { RoxygenTag, RoxygenTagParam } from './roxygen-ast'; |
| 4 | +import { KnownRoxygenTags } from './roxygen-ast'; |
| 5 | +import { RType } from '../lang-4.x/ast/model/type'; |
| 6 | +import type { RNode } from '../lang-4.x/ast/model/model'; |
| 7 | +import { parseRoxygenComment, parseRoxygenCommentsOfNode } from './roxygen-parse'; |
| 8 | + |
| 9 | +export interface DocumentationInfo { |
| 10 | + doc?: Documentation; |
| 11 | +} |
| 12 | +export type Documentation = RoxygenTag | readonly RoxygenTag[]; |
| 13 | + |
| 14 | +type CommentRetriever<Node extends RType> = (node: Extract<RNode<ParentInformation>, { type: Node }>, idMap: AstIdMap<ParentInformation & DocumentationInfo>) => Documentation | undefined; |
| 15 | +type CommentRetrievers = { [Node in RType]?: CommentRetriever<Node> }; |
| 16 | +const CommentRetriever: CommentRetrievers = { |
| 17 | + [RType.Comment]: n => parseRoxygenComment([n.lexeme]), |
| 18 | + [RType.Parameter]: (n, idMap) => { |
| 19 | + // get the documentation of the parent function |
| 20 | + const doc = n.info.parent ? getDocumentationOf(n.info.parent, idMap) : undefined; |
| 21 | + const paramName = n.lexeme; |
| 22 | + if(doc && paramName) { |
| 23 | + if(Array.isArray(doc)) { |
| 24 | + const res = (doc as RoxygenTag[]).filter(t => t.type === KnownRoxygenTags.Param && t.value.name === paramName); |
| 25 | + if(res.length === 1) { |
| 26 | + return res[0]; |
| 27 | + } else { |
| 28 | + return res; |
| 29 | + } |
| 30 | + } else { |
| 31 | + if((doc as RoxygenTag).type === KnownRoxygenTags.Param && (doc as RoxygenTagParam).value.name === paramName) { |
| 32 | + return doc; |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + return undefined; |
| 37 | + } |
| 38 | +}; |
| 39 | + |
| 40 | + |
| 41 | +/** |
| 42 | + * Given a normalized AST and a node ID, returns the Roxygen documentation (if any) associated with that node. |
| 43 | + * Please note that this does more than {@link parseRoxygenCommentsOfNode}, as it also traverses up the AST to find documentation. |
| 44 | + * Additionally, this function instruments the normalized AST to cache the parsed documentation for future queries. |
| 45 | + * @param idMap - The AST ID map to use for looking up nodes and traversing the AST. |
| 46 | + * @param nodeId - The ID of the node to get documentation for. |
| 47 | + */ |
| 48 | +export function getDocumentationOf(nodeId: NodeId, idMap: AstIdMap<ParentInformation & DocumentationInfo>): Documentation | undefined { |
| 49 | + const node = idMap.get(nodeId); |
| 50 | + if(!node) { |
| 51 | + return undefined; |
| 52 | + } else if(node.info.doc) { |
| 53 | + return node.info.doc; |
| 54 | + } |
| 55 | + const retriever = CommentRetriever[node.type as RType] ?? ((c: RNode<ParentInformation>, a: AstIdMap) => parseRoxygenCommentsOfNode(c, a)?.tags); |
| 56 | + const doc = retriever(node as never, idMap); |
| 57 | + if(doc) { |
| 58 | + // cache the documentation for future queries |
| 59 | + const expanded = expandInheritsOfTags(doc, idMap); |
| 60 | + (node.info as DocumentationInfo).doc = expanded; |
| 61 | + return expanded; |
| 62 | + } |
| 63 | + return doc; |
| 64 | +} |
| 65 | + |
| 66 | +function expandInheritsOfTags(tags: RoxygenTag | readonly RoxygenTag[], idMap: AstIdMap<ParentInformation & DocumentationInfo>): RoxygenTag | readonly RoxygenTag[] { |
| 67 | + const expandedTags: RoxygenTag[] = []; |
| 68 | + const tagArray: readonly RoxygenTag[] = Array.isArray(tags) ? tags : [tags]; |
| 69 | + for(const tag of tagArray) { |
| 70 | + const expanded: RoxygenTag | readonly RoxygenTag[] | undefined = expandInheritOfTag(tag, tagArray, idMap); |
| 71 | + if(!expanded) { |
| 72 | + continue; |
| 73 | + } |
| 74 | + if(Array.isArray(expanded)) { |
| 75 | + expandedTags.push(...expanded as readonly RoxygenTag[]); |
| 76 | + } else { |
| 77 | + expandedTags.push(expanded as RoxygenTag); |
| 78 | + } |
| 79 | + } |
| 80 | + if(expandedTags.length === 1) { |
| 81 | + return expandedTags[0]; |
| 82 | + } |
| 83 | + return expandedTags; |
| 84 | +} |
| 85 | + |
| 86 | +function getDocumentationOfByName(name: string, idMap: AstIdMap<ParentInformation & DocumentationInfo>): Documentation | undefined { |
| 87 | + for(const [, node] of idMap) { |
| 88 | + const nodeName = node.lexeme ?? node.info.fullLexeme; |
| 89 | + if(nodeName !== name) { |
| 90 | + continue; |
| 91 | + } |
| 92 | + return getDocumentationOf(node.info.id, idMap); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +function filterDocumentationForParams(doc: Documentation | undefined, filter: (r: RoxygenTag) => boolean): Documentation | undefined { |
| 97 | + if(!doc) { |
| 98 | + return doc; |
| 99 | + } |
| 100 | + if(Array.isArray(doc)) { |
| 101 | + return doc.filter(filter) as readonly RoxygenTag[]; |
| 102 | + } else { |
| 103 | + return filter(doc as RoxygenTag) ? doc : undefined; |
| 104 | + } |
| 105 | + |
| 106 | +} |
| 107 | + |
| 108 | +function expandInheritOfTag(tag: RoxygenTag, otherTags: readonly RoxygenTag[], idMap: AstIdMap<ParentInformation & DocumentationInfo>): Documentation | undefined { |
| 109 | + if(tag.type === KnownRoxygenTags.Inherit) { |
| 110 | + const inheritDoc = getDocumentationOfByName(tag.value.source, idMap); |
| 111 | + return filterDocumentationForParams(inheritDoc, t => tag.value.components.includes(t.type)); |
| 112 | + } else if(tag.type === KnownRoxygenTags.InheritDotParams) { |
| 113 | + const inheritDoc = getDocumentationOfByName(tag.value.source, idMap); |
| 114 | + return filterDocumentationForParams(inheritDoc, t => t.type === KnownRoxygenTags.Param && t.value.name === '...'); |
| 115 | + } else if(tag.type === KnownRoxygenTags.InheritParams) { |
| 116 | + const inheritDoc = getDocumentationOfByName(tag.value, idMap); |
| 117 | + const alreadyExplainedParams = new Set(otherTags.filter(t => t.type === KnownRoxygenTags.Param).map(t => t.value.name)); |
| 118 | + return filterDocumentationForParams(inheritDoc, t => t.type === KnownRoxygenTags.Param && !alreadyExplainedParams.has(t.value.name)); |
| 119 | + } |
| 120 | + return tag; |
| 121 | +} |
0 commit comments