index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. 'use strict';
  2. var _jestRegexUtil;
  3. function _load_jestRegexUtil() {
  4. return _jestRegexUtil = require('jest-regex-util');
  5. }
  6. /**
  7. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  8. *
  9. * This source code is licensed under the MIT license found in the
  10. * LICENSE file in the root directory of this source tree.
  11. *
  12. *
  13. */
  14. const snapshotDirRegex = new RegExp((0, (_jestRegexUtil || _load_jestRegexUtil()).replacePathSepForRegex)('/__snapshots__/'));
  15. const snapshotFileRegex = new RegExp((0, (_jestRegexUtil || _load_jestRegexUtil()).replacePathSepForRegex)('__snapshots__/(.*).snap'));
  16. const isSnapshotPath = path => !!path.match(snapshotDirRegex);
  17. function compact(array) {
  18. const result = [];
  19. for (let i = 0; i < array.length; ++i) {
  20. const element = array[i];
  21. if (element != null) {
  22. result.push(element);
  23. }
  24. }
  25. return result;
  26. }
  27. /**
  28. * DependencyResolver is used to resolve the direct dependencies of a module or
  29. * to retrieve a list of all transitive inverse dependencies.
  30. */
  31. class DependencyResolver {
  32. constructor(resolver, hasteFS) {
  33. this._resolver = resolver;
  34. this._hasteFS = hasteFS;
  35. }
  36. resolve(file, options) {
  37. const dependencies = this._hasteFS.getDependencies(file);
  38. if (!dependencies) {
  39. return [];
  40. }
  41. return compact(dependencies.map(dependency => {
  42. if (this._resolver.isCoreModule(dependency)) {
  43. return null;
  44. }
  45. try {
  46. return this._resolver.resolveModule(file, dependency, options);
  47. } catch (e) {}
  48. return this._resolver.getMockModule(file, dependency) || null;
  49. }));
  50. }
  51. resolveInverse(paths, filter, options) {
  52. const collectModules = (relatedPaths, moduleMap, changed) => {
  53. const visitedModules = new Set();
  54. while (changed.size) {
  55. changed = new Set(moduleMap.filter(module => !visitedModules.has(module.file) && module.dependencies.some(dep => dep && changed.has(dep))).map(module => {
  56. const file = module.file;
  57. if (filter(file)) {
  58. relatedPaths.add(file);
  59. }
  60. visitedModules.add(file);
  61. return module.file;
  62. }));
  63. }
  64. return relatedPaths;
  65. };
  66. if (!paths.size) {
  67. return [];
  68. }
  69. const relatedPaths = new Set();
  70. const changed = new Set();
  71. for (const path of paths) {
  72. if (this._hasteFS.exists(path)) {
  73. // /path/to/__snapshots__/test.js.snap is always adjacent to
  74. // /path/to/test.js
  75. const modulePath = isSnapshotPath(path) ? path.replace(snapshotFileRegex, '$1') : path;
  76. changed.add(modulePath);
  77. if (filter(modulePath)) {
  78. relatedPaths.add(modulePath);
  79. }
  80. }
  81. }
  82. const modules = this._hasteFS.getAllFiles().map(file => ({
  83. dependencies: this.resolve(file, options),
  84. file
  85. }));
  86. return Array.from(collectModules(relatedPaths, modules, changed));
  87. }
  88. }
  89. module.exports = DependencyResolver;