index.js 5.4 KB

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