resolveToValue.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import { Scope, visitors } from '@babel/traverse';
  2. import getMemberExpressionRoot from './getMemberExpressionRoot.js';
  3. import getPropertyValuePath from './getPropertyValuePath.js';
  4. import { Array as toArray } from './expressionTo.js';
  5. import { shallowIgnoreVisitors } from './traverse.js';
  6. import getMemberValuePath, { isSupportedDefinitionType, } from './getMemberValuePath.js';
  7. import initialize from './ts-types/index.js';
  8. import getNameOrValue from './getNameOrValue.js';
  9. function findScopePath(bindingIdentifiers) {
  10. if (bindingIdentifiers && bindingIdentifiers[0]) {
  11. const resolvedParentPath = bindingIdentifiers[0].parentPath;
  12. if (resolvedParentPath.isImportDefaultSpecifier() ||
  13. resolvedParentPath.isImportSpecifier()) {
  14. let exportName;
  15. if (resolvedParentPath.isImportDefaultSpecifier()) {
  16. exportName = 'default';
  17. }
  18. else {
  19. const imported = resolvedParentPath.get('imported');
  20. if (imported.isStringLiteral()) {
  21. exportName = imported.node.value;
  22. }
  23. else if (imported.isIdentifier()) {
  24. exportName = imported.node.name;
  25. }
  26. }
  27. if (!exportName) {
  28. throw new Error('Could not detect export name');
  29. }
  30. const importedPath = resolvedParentPath.hub.import(resolvedParentPath.parentPath, exportName);
  31. if (importedPath) {
  32. return resolveToValue(importedPath);
  33. }
  34. }
  35. return resolveToValue(resolvedParentPath);
  36. }
  37. return null;
  38. }
  39. const explodedVisitors = visitors.explode({
  40. ...shallowIgnoreVisitors,
  41. AssignmentExpression: {
  42. enter: function (assignmentPath, state) {
  43. const node = state.idPath.node;
  44. const left = assignmentPath.get('left');
  45. // Skip anything that is not an assignment to a variable with the
  46. // passed name.
  47. // Ensure the LHS isn't the reference we're trying to resolve.
  48. if (!left.isIdentifier() ||
  49. left.node === node ||
  50. left.node.name !== node.name ||
  51. assignmentPath.node.operator !== '=') {
  52. return assignmentPath.skip();
  53. }
  54. // Ensure the RHS doesn't contain the reference we're trying to resolve.
  55. const candidatePath = assignmentPath.get('right');
  56. if (candidatePath.node === node ||
  57. state.idPath.findParent((parent) => parent.node === candidatePath.node)) {
  58. return assignmentPath.skip();
  59. }
  60. state.resultPath = candidatePath;
  61. return assignmentPath.skip();
  62. },
  63. },
  64. });
  65. /**
  66. * Tries to find the last value assigned to `name` in the scope created by
  67. * `scope`. We are not descending into any statements (blocks).
  68. */
  69. function findLastAssignedValue(path, idPath) {
  70. const state = { idPath };
  71. path.traverse(explodedVisitors, state);
  72. return state.resultPath ? resolveToValue(state.resultPath) : null;
  73. }
  74. /**
  75. * If the path is an identifier, it is resolved in the scope chain.
  76. * If it is an assignment expression, it resolves to the right hand side.
  77. * If it is a member expression it is resolved to it's initialization value.
  78. *
  79. * Else the path itself is returned.
  80. */
  81. export default function resolveToValue(path) {
  82. if (path.isIdentifier()) {
  83. if ((path.parentPath.isClass() || path.parentPath.isFunction()) &&
  84. path.parentPath.get('id') === path) {
  85. return path.parentPath;
  86. }
  87. const binding = path.scope.getBinding(path.node.name);
  88. let resolvedPath = null;
  89. if (binding) {
  90. // The variable may be assigned a different value after initialization.
  91. // We are first trying to find all assignments to the variable in the
  92. // block where it is defined (i.e. we are not traversing into statements)
  93. resolvedPath = findLastAssignedValue(binding.scope.path, path);
  94. if (!resolvedPath) {
  95. const bindingMap = binding.path.getOuterBindingIdentifierPaths(true);
  96. resolvedPath = findScopePath(bindingMap[path.node.name]);
  97. }
  98. }
  99. else {
  100. // Initialize our monkey-patching of @babel/traverse 🙈
  101. initialize(Scope);
  102. const typeBinding = path.scope.getTypeBinding(path.node.name);
  103. if (typeBinding) {
  104. resolvedPath = findScopePath([typeBinding.identifierPath]);
  105. }
  106. }
  107. return resolvedPath || path;
  108. }
  109. else if (path.isVariableDeclarator()) {
  110. const init = path.get('init');
  111. if (init.hasNode()) {
  112. return resolveToValue(init);
  113. }
  114. }
  115. else if (path.isMemberExpression()) {
  116. const root = getMemberExpressionRoot(path);
  117. const resolved = resolveToValue(root);
  118. if (resolved.isObjectExpression()) {
  119. let propertyPath = resolved;
  120. for (const propertyName of toArray(path).slice(1)) {
  121. if (propertyPath && propertyPath.isObjectExpression()) {
  122. propertyPath = getPropertyValuePath(propertyPath, propertyName);
  123. }
  124. if (!propertyPath) {
  125. return path;
  126. }
  127. propertyPath = resolveToValue(propertyPath);
  128. }
  129. return propertyPath;
  130. }
  131. else if (isSupportedDefinitionType(resolved)) {
  132. const property = path.get('property');
  133. if (property.isIdentifier() || property.isStringLiteral()) {
  134. const memberPath = getMemberValuePath(resolved, property.isIdentifier() ? property.node.name : property.node.value);
  135. if (memberPath) {
  136. return resolveToValue(memberPath);
  137. }
  138. }
  139. }
  140. else if (resolved.isImportSpecifier() ||
  141. resolved.isImportDefaultSpecifier() ||
  142. resolved.isImportNamespaceSpecifier()) {
  143. const declaration = resolved.parentPath;
  144. // Handle references to namespace imports, e.g. import * as foo from 'bar'.
  145. // Try to find a specifier that matches the root of the member expression, and
  146. // find the export that matches the property name.
  147. for (const specifier of declaration.get('specifiers')) {
  148. const property = path.get('property');
  149. let propertyName;
  150. if (property.isIdentifier() || property.isStringLiteral()) {
  151. propertyName = getNameOrValue(property);
  152. }
  153. if (specifier.isImportNamespaceSpecifier() &&
  154. root.isIdentifier() &&
  155. propertyName &&
  156. specifier.node.local.name === root.node.name) {
  157. const resolvedPath = path.hub.import(declaration, propertyName);
  158. if (resolvedPath) {
  159. return resolveToValue(resolvedPath);
  160. }
  161. }
  162. }
  163. }
  164. }
  165. else if (path.isTypeCastExpression() ||
  166. path.isTSAsExpression() ||
  167. path.isTSTypeAssertion()) {
  168. return resolveToValue(path.get('expression'));
  169. }
  170. return path;
  171. }