index.js 3.2 KB

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