MCPcopy Create free account
hub / github.com/reactjs/react-docgen / resolveToValue

Function resolveToValue

packages/react-docgen/src/utils/resolveToValue.ts:121–232  ·  view source on GitHub ↗
(path: NodePath)

Source from the content-addressed store, hash-verified

119 * Else the path itself is returned.
120 */
121export default function resolveToValue(path: NodePath): NodePath {
122 if (path.isIdentifier()) {
123 if (
124 (path.parentPath.isClass() || path.parentPath.isFunction()) &&
125 path.parentPath.get('id') === path
126 ) {
127 return path.parentPath;
128 }
129
130 const binding = path.scope.getBinding(path.node.name);
131 let resolvedPath: NodePath | null = null;
132
133 if (binding) {
134 // The variable may be assigned a different value after initialization.
135 // We are first trying to find all assignments to the variable in the
136 // block where it is defined (i.e. we are not traversing into statements)
137 resolvedPath = findLastAssignedValue(binding.scope.path, path);
138 if (!resolvedPath) {
139 const bindingMap = binding.path.getOuterBindingIdentifierPaths(true);
140
141 resolvedPath = findScopePath(bindingMap[path.node.name]);
142 }
143 } else {
144 // Initialize our monkey-patching of @babel/traverse 🙈
145 initialize(Scope);
146 const typeBinding = path.scope.getTypeBinding(path.node.name);
147
148 if (typeBinding) {
149 resolvedPath = findScopePath([typeBinding.identifierPath]);
150 }
151 }
152
153 return resolvedPath || path;
154 } else if (path.isVariableDeclarator()) {
155 const init = path.get('init');
156
157 if (init.hasNode()) {
158 return resolveToValue(init);
159 }
160 } else if (path.isMemberExpression()) {
161 const root = getMemberExpressionRoot(path);
162 const resolved = resolveToValue(root);
163
164 if (resolved.isObjectExpression()) {
165 let propertyPath: NodePath | null = resolved;
166
167 for (const propertyName of toArray(path).slice(1)) {
168 if (propertyPath && propertyPath.isObjectExpression()) {
169 propertyPath = getPropertyValuePath(propertyPath, propertyName);
170 }
171 if (!propertyPath) {
172 return path;
173 }
174 propertyPath = resolveToValue(propertyPath);
175 }
176
177 return propertyPath;
178 } else if (isSupportedDefinitionType(resolved)) {

Callers 15

handleTSTypeReferenceFunction · 0.85
handleTSTypeQueryFunction · 0.85
handleTSTypeOperatorFunction · 0.85
resolvesToFinalValueFunction · 0.85
handleKeysHelperFunction · 0.85
getFlowType.tsFile · 0.85

Calls 12

findLastAssignedValueFunction · 0.85
findScopePathFunction · 0.85
initializeFunction · 0.85
getMemberExpressionRootFunction · 0.85
toArrayFunction · 0.85
getPropertyValuePathFunction · 0.85
getMemberValuePathFunction · 0.85
getNameOrValueFunction · 0.85
getMethod · 0.80
getTypeBindingMethod · 0.80
importMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…