index.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. 'use strict';
  2. function _jestSnapshot() {
  3. const data = require('jest-snapshot');
  4. _jestSnapshot = function _jestSnapshot() {
  5. return data;
  6. };
  7. return data;
  8. }
  9. function _defineProperty(obj, key, value) {
  10. if (key in obj) {
  11. Object.defineProperty(obj, key, {
  12. value: value,
  13. enumerable: true,
  14. configurable: true,
  15. writable: true
  16. });
  17. } else {
  18. obj[key] = value;
  19. }
  20. return obj;
  21. }
  22. /**
  23. * DependencyResolver is used to resolve the direct dependencies of a module or
  24. * to retrieve a list of all transitive inverse dependencies.
  25. */
  26. /* eslint-disable-next-line no-redeclare */
  27. class DependencyResolver {
  28. constructor(resolver, hasteFS, snapshotResolver) {
  29. _defineProperty(this, '_hasteFS', void 0);
  30. _defineProperty(this, '_resolver', void 0);
  31. _defineProperty(this, '_snapshotResolver', void 0);
  32. this._resolver = resolver;
  33. this._hasteFS = hasteFS;
  34. this._snapshotResolver = snapshotResolver;
  35. }
  36. resolve(file, options) {
  37. const dependencies = this._hasteFS.getDependencies(file);
  38. if (!dependencies) {
  39. return [];
  40. }
  41. return dependencies.reduce((acc, dependency) => {
  42. if (this._resolver.isCoreModule(dependency)) {
  43. return acc;
  44. }
  45. let resolvedDependency;
  46. try {
  47. resolvedDependency = this._resolver.resolveModule(
  48. file,
  49. dependency,
  50. options
  51. );
  52. } catch (e) {
  53. resolvedDependency = this._resolver.getMockModule(file, dependency);
  54. }
  55. if (resolvedDependency) {
  56. acc.push(resolvedDependency);
  57. }
  58. return acc;
  59. }, []);
  60. }
  61. resolveInverseModuleMap(paths, filter, options) {
  62. if (!paths.size) {
  63. return [];
  64. }
  65. const collectModules = (related, moduleMap, changed) => {
  66. const visitedModules = new Set();
  67. const result = [];
  68. while (changed.size) {
  69. changed = new Set(
  70. moduleMap.reduce((acc, module) => {
  71. if (
  72. visitedModules.has(module.file) ||
  73. !module.dependencies.some(dep => changed.has(dep))
  74. ) {
  75. return acc;
  76. }
  77. const file = module.file;
  78. if (filter(file)) {
  79. result.push(module);
  80. related.delete(file);
  81. }
  82. visitedModules.add(file);
  83. acc.push(file);
  84. return acc;
  85. }, [])
  86. );
  87. }
  88. return result.concat(
  89. Array.from(related).map(file => ({
  90. dependencies: [],
  91. file
  92. }))
  93. );
  94. };
  95. const relatedPaths = new Set();
  96. const changed = new Set();
  97. var _iteratorNormalCompletion = true;
  98. var _didIteratorError = false;
  99. var _iteratorError = undefined;
  100. try {
  101. for (
  102. var _iterator = paths[Symbol.iterator](), _step;
  103. !(_iteratorNormalCompletion = (_step = _iterator.next()).done);
  104. _iteratorNormalCompletion = true
  105. ) {
  106. const path = _step.value;
  107. if (this._hasteFS.exists(path)) {
  108. const modulePath = (0, _jestSnapshot().isSnapshotPath)(path)
  109. ? this._snapshotResolver.resolveTestPath(path)
  110. : path;
  111. changed.add(modulePath);
  112. if (filter(modulePath)) {
  113. relatedPaths.add(modulePath);
  114. }
  115. }
  116. }
  117. } catch (err) {
  118. _didIteratorError = true;
  119. _iteratorError = err;
  120. } finally {
  121. try {
  122. if (!_iteratorNormalCompletion && _iterator.return != null) {
  123. _iterator.return();
  124. }
  125. } finally {
  126. if (_didIteratorError) {
  127. throw _iteratorError;
  128. }
  129. }
  130. }
  131. const modules = [];
  132. var _iteratorNormalCompletion2 = true;
  133. var _didIteratorError2 = false;
  134. var _iteratorError2 = undefined;
  135. try {
  136. for (
  137. var _iterator2 = this._hasteFS
  138. .getAbsoluteFileIterator()
  139. [Symbol.iterator](),
  140. _step2;
  141. !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done);
  142. _iteratorNormalCompletion2 = true
  143. ) {
  144. const file = _step2.value;
  145. modules.push({
  146. dependencies: this.resolve(file, options),
  147. file
  148. });
  149. }
  150. } catch (err) {
  151. _didIteratorError2 = true;
  152. _iteratorError2 = err;
  153. } finally {
  154. try {
  155. if (!_iteratorNormalCompletion2 && _iterator2.return != null) {
  156. _iterator2.return();
  157. }
  158. } finally {
  159. if (_didIteratorError2) {
  160. throw _iteratorError2;
  161. }
  162. }
  163. }
  164. return collectModules(relatedPaths, modules, changed);
  165. }
  166. resolveInverse(paths, filter, options) {
  167. return this.resolveInverseModuleMap(paths, filter, options).map(
  168. module => module.file
  169. );
  170. }
  171. }
  172. module.exports = DependencyResolver;