(path: NodePath)
| 119 | * Else the path itself is returned. |
| 120 | */ |
| 121 | export 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)) { |
no test coverage detected
searching dependent graphs…