1 import { visitors } from "@babel/traverse"
2 import { utils, builtinResolvers, FileState, NodePath } from "react-docgen"
3 import { ComponentNodePath } from "react-docgen/dist/resolver/index.js"
4 import TypedocManager from "../classes/typedoc-manager.js"
5
6 type State = {
7 foundDefinitions: Set<ComponentNodePath>
8 }
9 /**
10 * This resolver extends react-docgen's FindAllDefinitionsResolver
11 * + adds the ability to resolve variable components such as:
12 *
13 * ```tsx
14 * const Value = SelectPrimitive.Value
15 * Value.displayName = "Select.Value"
16 * ```
17 */
18 export default class CustomResolver
19 implements builtinResolvers.FindAllDefinitionsResolver
20 {
21 private typedocManager: TypedocManager
22 constructor(typedocManager: TypedocManager) {
23 this.typedocManager = typedocManager
24 }
25 resolve(file: FileState): ComponentNodePath[] {
26 const state = {
27 foundDefinitions: new Set<ComponentNodePath>(),
28 }
29 file.traverse(
30 visitors.explode({
31 FunctionDeclaration: { enter: this.statelessVisitor },
32 FunctionExpression: { enter: this.statelessVisitor },
33 ObjectMethod: { enter: this.statelessVisitor },
34 ArrowFunctionExpression: { enter: this.statelessVisitor },
35 ClassExpression: { enter: this.classVisitor },
36 ClassDeclaration: { enter: this.classVisitor },
37 VariableDeclaration: {
38 enter: (path, state: State) => {
39 const found = path.node.declarations.some((declaration) => {
40 if (
41 "name" in declaration.id &&
42 this.typedocManager.isReactComponent(declaration.id.name) &&
43 declaration.init
44 ) {
45 const init = path.get("declarations")[0].get("init")
46 if (init.isMemberExpression()) {
47 state.foundDefinitions.add(
48 path as unknown as ComponentNodePath
49 )
50 return true
51 }
52
53 return false
54 }
55 })
56
57 if (found) {
58 return path.skip()
59 }
60 },
61 },
62 CallExpression: {
63 enter: (path, state: State) => {
64 const argument = path.get("arguments")[0]
65 if (!argument) {
66 return
67 }
68 if (utils.isReactForwardRefCall(path)) {
69 // If the inner function was previously identified as a component
70 // replace it with the parent node
71 const inner = utils.resolveToValue(argument) as ComponentNodePath
72 state.foundDefinitions.delete(inner)
73 state.foundDefinitions.add(path)
74 // Do not traverse into arguments
75 return path.skip()
76 } else if (utils.isReactCreateClassCall(path)) {
77 const resolvedPath = utils.resolveToValue(argument)
78 if (resolvedPath.isObjectExpression()) {
79 state.foundDefinitions.add(resolvedPath)
80 }
81 // Do not traverse into arguments
82 return path.skip()
83 }
84 },
85 },
86 }),
87 state
88 )
89 return Array.from(state.foundDefinitions)
90 }
91
92 classVisitor(path: NodePath, state: State) {
93 if (utils.isReactComponentClass(path)) {
94 utils.normalizeClassDefinition(path)
95 state.foundDefinitions.add(path)
96 }
97 path.skip()
98 }
99 statelessVisitor(path: NodePath, state: State) {
100 if (utils.isStatelessComponent(path)) {
101 state.foundDefinitions.add(path)
102 }
103 path.skip()
104 }
105 }