1 import { utils, DocumentationBuilder, NodePath } from "react-docgen"
2 import { ComponentNode } from "react-docgen/dist/resolver/index.js"
3 import { getDocblock } from "react-docgen/dist/utils/docblock.js"
4 import TypedocManager from "../classes/typedoc-manager.js"
5 import emptyPropDescriptor from "../utils/empty-prop-descriptor.js"
6 import { Node } from "@babel/core"
7 import isEmptyPropDescriptor from "../utils/is-empty-prop-descriptor.js"
8
9 function resolveDocumentation(
10 documentation: DocumentationBuilder,
11 path: NodePath<ComponentNode>,
12 typedocManager?: TypedocManager
13 ) {
14 if (!path.isObjectExpression() && !path.isObjectPattern()) {
15 return
16 }
17 path.get("properties").forEach((propertyPath) => {
18 if (propertyPath.isSpreadElement() || propertyPath.isRestElement()) {
19 const resolvedValuePath = utils.resolveToValue(
20 propertyPath.get("argument")
21 ) as NodePath<ComponentNode>
22 resolveDocumentation(documentation, resolvedValuePath)
23 } else if (
24 propertyPath.isObjectProperty() ||
25 propertyPath.isObjectMethod()
26 ) {
27 const propertyName = utils.getPropertyName(propertyPath)
28 const propDescriptor = propertyName
29 ? documentation.getPropDescriptor(propertyName)
30 : undefined
31 const description =
32 propDescriptor?.description || getDocblock(propertyPath)
33 const propExists = propertyName !== null && propDescriptor !== undefined
34 const shouldRemoveProp =
35 description?.includes("@ignore") ||
36 (!description &&
37 (!propDescriptor || isEmptyPropDescriptor(propDescriptor)))
38
39 // remove property if it doesn't have a description or
40 // if its description includes the `@ignore` tag.
41 if (!propExists || shouldRemoveProp) {
42 if (shouldRemoveProp && propExists) {
43 // prop is removed if its descriptor is empty,
44 // so we empty it to remove it.
45 emptyPropDescriptor(propDescriptor)
46 }
47 return
48 }
49 // set description
50 utils.setPropDescription(documentation, propertyPath)
51
52 // set type if missing
53 if (typedocManager && !propDescriptor.tsType) {
54 const typeAnnotation = utils.getTypeAnnotation(path)
55 if (typeAnnotation?.isTSTypeReference()) {
56 const typeName = typeAnnotation.get("typeName")
57 if (
58 !Array.isArray(typeName) &&
59 typeName.hasNode() &&
60 typeName.isIdentifier()
61 ) {
62 const tsType = typedocManager.resolveChildType(
63 typeName.node.name,
64 propertyName
65 )
66
67 if (tsType) {
68 propDescriptor.tsType = tsType
69 }
70 }
71 }
72 } else if (
73 propDescriptor.tsType &&
74 (typedocManager?.onlyHasName(propDescriptor.tsType) ||
75 typedocManager?.isRawTypeEmpty(propDescriptor.tsType))
76 ) {
77 // see if the type needs to be resolved.
78 const typeReflection = typedocManager?.getReflectionByName(
79 propDescriptor.tsType.name
80 )
81 if (typeReflection && typeReflection.type) {
82 propDescriptor.tsType =
83 typedocManager?.getTsType(typeReflection.type) ||
84 propDescriptor.tsType
85 }
86 }
87 }
88 })
89 }
90
91 /**
92 * A handler that resolves props from arguments and
93 * sets their description, type, etc...
94 */
95 const argsPropHandler = (
96 documentation: DocumentationBuilder,
97 componentDefinition: NodePath<ComponentNode>,
98 typedocManager?: TypedocManager
99 ) => {
100 let componentParams: NodePath<Node>[] = []
101 if (componentDefinition.isCallExpression()) {
102 const args = componentDefinition.get("arguments")
103 args.forEach((arg) => {
104 const params = arg.get("params")
105 if (Array.isArray(params)) {
106 componentParams.push(...params)
107 } else {
108 componentParams.push(params)
109 }
110 })
111 } else if (componentDefinition.isArrowFunctionExpression()) {
112 componentParams = componentDefinition.get("params")
113 }
114
115 componentParams.forEach((param) => {
116 const resolvedParam = utils.resolveToValue(param) as NodePath<ComponentNode>
117
118 if (!resolvedParam) {
119 return
120 }
121
122 // set description and type of prop
123 resolveDocumentation(documentation, resolvedParam, typedocManager)
124 })
125 }
126
127 export default argsPropHandler