index.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.DependencyResolver = void 0;
  6. function path() {
  7. const data = _interopRequireWildcard(require('path'));
  8. path = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _jestSnapshot() {
  14. const data = require('jest-snapshot');
  15. _jestSnapshot = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _getRequireWildcardCache(nodeInterop) {
  21. if (typeof WeakMap !== 'function') return null;
  22. var cacheBabelInterop = new WeakMap();
  23. var cacheNodeInterop = new WeakMap();
  24. return (_getRequireWildcardCache = function (nodeInterop) {
  25. return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
  26. })(nodeInterop);
  27. }
  28. function _interopRequireWildcard(obj, nodeInterop) {
  29. if (!nodeInterop && obj && obj.__esModule) {
  30. return obj;
  31. }
  32. if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
  33. return {default: obj};
  34. }
  35. var cache = _getRequireWildcardCache(nodeInterop);
  36. if (cache && cache.has(obj)) {
  37. return cache.get(obj);
  38. }
  39. var newObj = {};
  40. var hasPropertyDescriptor =
  41. Object.defineProperty && Object.getOwnPropertyDescriptor;
  42. for (var key in obj) {
  43. if (key !== 'default' && Object.prototype.hasOwnProperty.call(obj, key)) {
  44. var desc = hasPropertyDescriptor
  45. ? Object.getOwnPropertyDescriptor(obj, key)
  46. : null;
  47. if (desc && (desc.get || desc.set)) {
  48. Object.defineProperty(newObj, key, desc);
  49. } else {
  50. newObj[key] = obj[key];
  51. }
  52. }
  53. }
  54. newObj.default = obj;
  55. if (cache) {
  56. cache.set(obj, newObj);
  57. }
  58. return newObj;
  59. }
  60. function _defineProperty(obj, key, value) {
  61. if (key in obj) {
  62. Object.defineProperty(obj, key, {
  63. value: value,
  64. enumerable: true,
  65. configurable: true,
  66. writable: true
  67. });
  68. } else {
  69. obj[key] = value;
  70. }
  71. return obj;
  72. }
  73. /**
  74. * DependencyResolver is used to resolve the direct dependencies of a module or
  75. * to retrieve a list of all transitive inverse dependencies.
  76. */
  77. class DependencyResolver {
  78. constructor(resolver, hasteFS, snapshotResolver) {
  79. _defineProperty(this, '_hasteFS', void 0);
  80. _defineProperty(this, '_resolver', void 0);
  81. _defineProperty(this, '_snapshotResolver', void 0);
  82. this._resolver = resolver;
  83. this._hasteFS = hasteFS;
  84. this._snapshotResolver = snapshotResolver;
  85. }
  86. resolve(file, options) {
  87. const dependencies = this._hasteFS.getDependencies(file);
  88. if (!dependencies) {
  89. return [];
  90. }
  91. return dependencies.reduce((acc, dependency) => {
  92. if (this._resolver.isCoreModule(dependency)) {
  93. return acc;
  94. }
  95. let resolvedDependency;
  96. let resolvedMockDependency;
  97. try {
  98. resolvedDependency = this._resolver.resolveModule(
  99. file,
  100. dependency,
  101. options
  102. );
  103. } catch {
  104. try {
  105. resolvedDependency = this._resolver.getMockModule(file, dependency);
  106. } catch {
  107. // leave resolvedDependency as undefined if nothing can be found
  108. }
  109. }
  110. if (!resolvedDependency) {
  111. return acc;
  112. }
  113. acc.push(resolvedDependency); // If we resolve a dependency, then look for a mock dependency
  114. // of the same name in that dependency's directory.
  115. try {
  116. resolvedMockDependency = this._resolver.getMockModule(
  117. resolvedDependency,
  118. path().basename(dependency)
  119. );
  120. } catch {
  121. // leave resolvedMockDependency as undefined if nothing can be found
  122. }
  123. if (resolvedMockDependency) {
  124. const dependencyMockDir = path().resolve(
  125. path().dirname(resolvedDependency),
  126. '__mocks__'
  127. );
  128. resolvedMockDependency = path().resolve(resolvedMockDependency); // make sure mock is in the correct directory
  129. if (dependencyMockDir === path().dirname(resolvedMockDependency)) {
  130. acc.push(resolvedMockDependency);
  131. }
  132. }
  133. return acc;
  134. }, []);
  135. }
  136. resolveInverseModuleMap(paths, filter, options) {
  137. if (!paths.size) {
  138. return [];
  139. }
  140. const collectModules = (related, moduleMap, changed) => {
  141. const visitedModules = new Set();
  142. const result = [];
  143. while (changed.size) {
  144. changed = new Set(
  145. moduleMap.reduce((acc, module) => {
  146. if (
  147. visitedModules.has(module.file) ||
  148. !module.dependencies.some(dep => changed.has(dep))
  149. ) {
  150. return acc;
  151. }
  152. const file = module.file;
  153. if (filter(file)) {
  154. result.push(module);
  155. related.delete(file);
  156. }
  157. visitedModules.add(file);
  158. acc.push(file);
  159. return acc;
  160. }, [])
  161. );
  162. }
  163. return result.concat(
  164. Array.from(related).map(file => ({
  165. dependencies: [],
  166. file
  167. }))
  168. );
  169. };
  170. const relatedPaths = new Set();
  171. const changed = new Set();
  172. for (const path of paths) {
  173. if (this._hasteFS.exists(path)) {
  174. const modulePath = (0, _jestSnapshot().isSnapshotPath)(path)
  175. ? this._snapshotResolver.resolveTestPath(path)
  176. : path;
  177. changed.add(modulePath);
  178. if (filter(modulePath)) {
  179. relatedPaths.add(modulePath);
  180. }
  181. }
  182. }
  183. const modules = [];
  184. for (const file of this._hasteFS.getAbsoluteFileIterator()) {
  185. modules.push({
  186. dependencies: this.resolve(file, options),
  187. file
  188. });
  189. }
  190. return collectModules(relatedPaths, modules, changed);
  191. }
  192. resolveInverse(paths, filter, options) {
  193. return this.resolveInverseModuleMap(paths, filter, options).map(
  194. module => module.file
  195. );
  196. }
  197. }
  198. exports.DependencyResolver = DependencyResolver;