index.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*!
  2. * /**
  3. * * Copyright (c) Meta Platforms, Inc. and affiliates.
  4. * *
  5. * * This source code is licensed under the MIT license found in the
  6. * * LICENSE file in the root directory of this source tree.
  7. * * /
  8. */
  9. /******/ (() => { // webpackBootstrap
  10. /******/ "use strict";
  11. /******/ var __webpack_modules__ = ({
  12. /***/ "./src/TestPathPatterns.ts":
  13. /***/ ((__unused_webpack_module, exports) => {
  14. Object.defineProperty(exports, "__esModule", ({
  15. value: true
  16. }));
  17. exports.TestPathPatternsExecutor = exports.TestPathPatterns = void 0;
  18. function path() {
  19. const data = _interopRequireWildcard(require("path"));
  20. path = function () {
  21. return data;
  22. };
  23. return data;
  24. }
  25. function _jestRegexUtil() {
  26. const data = require("jest-regex-util");
  27. _jestRegexUtil = function () {
  28. return data;
  29. };
  30. return data;
  31. }
  32. function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
  33. /**
  34. * Copyright (c) Meta Platforms, Inc. and affiliates.
  35. *
  36. * This source code is licensed under the MIT license found in the
  37. * LICENSE file in the root directory of this source tree.
  38. */
  39. class TestPathPatterns {
  40. constructor(patterns) {
  41. this.patterns = patterns;
  42. }
  43. /**
  44. * Return true if there are any patterns.
  45. */
  46. isSet() {
  47. return this.patterns.length > 0;
  48. }
  49. /**
  50. * Return true if the patterns are valid.
  51. */
  52. isValid() {
  53. return this.toExecutor({
  54. // isValid() doesn't require rootDir to be accurate, so just
  55. // specify a dummy rootDir here
  56. rootDir: '/'
  57. }).isValid();
  58. }
  59. /**
  60. * Return a human-friendly version of the pattern regex.
  61. */
  62. toPretty() {
  63. return this.patterns.join('|');
  64. }
  65. /**
  66. * Return a TestPathPatternsExecutor that can execute the patterns.
  67. */
  68. toExecutor(options) {
  69. return new TestPathPatternsExecutor(this, options);
  70. }
  71. /** For jest serializers */
  72. toJSON() {
  73. return {
  74. patterns: this.patterns,
  75. type: 'TestPathPatterns'
  76. };
  77. }
  78. }
  79. exports.TestPathPatterns = TestPathPatterns;
  80. class TestPathPatternsExecutor {
  81. constructor(patterns, options) {
  82. this.patterns = patterns;
  83. this.options = options;
  84. }
  85. toRegex(s) {
  86. return new RegExp(s, 'i');
  87. }
  88. /**
  89. * Return true if there are any patterns.
  90. */
  91. isSet() {
  92. return this.patterns.isSet();
  93. }
  94. /**
  95. * Return true if the patterns are valid.
  96. */
  97. isValid() {
  98. try {
  99. for (const p of this.patterns.patterns) {
  100. this.toRegex(p);
  101. }
  102. return true;
  103. } catch {
  104. return false;
  105. }
  106. }
  107. /**
  108. * Return true if the given ABSOLUTE path matches the patterns.
  109. *
  110. * Throws an error if the patterns form an invalid regex (see `validate`).
  111. */
  112. isMatch(absPath) {
  113. const relPath = path().relative(this.options.rootDir || '/', absPath);
  114. if (this.patterns.patterns.length === 0) {
  115. return true;
  116. }
  117. for (const p of this.patterns.patterns) {
  118. const pathToTest = path().isAbsolute(p) ? absPath : relPath;
  119. // special case: ./foo.spec.js (and .\foo.spec.js on Windows) should
  120. // match /^foo.spec.js/ after stripping root dir
  121. let regexStr = p.replace(/^\.\//, '^');
  122. if (path().sep === '\\') {
  123. regexStr = regexStr.replace(/^\.\\/, '^');
  124. }
  125. regexStr = (0, _jestRegexUtil().replacePathSepForRegex)(regexStr);
  126. if (this.toRegex(regexStr).test(pathToTest)) {
  127. return true;
  128. }
  129. if (this.toRegex(regexStr).test(absPath)) {
  130. return true;
  131. }
  132. }
  133. return false;
  134. }
  135. /**
  136. * Return a human-friendly version of the pattern regex.
  137. */
  138. toPretty() {
  139. return this.patterns.toPretty();
  140. }
  141. }
  142. exports.TestPathPatternsExecutor = TestPathPatternsExecutor;
  143. /***/ })
  144. /******/ });
  145. /************************************************************************/
  146. /******/ // The module cache
  147. /******/ var __webpack_module_cache__ = {};
  148. /******/
  149. /******/ // The require function
  150. /******/ function __webpack_require__(moduleId) {
  151. /******/ // Check if module is in cache
  152. /******/ var cachedModule = __webpack_module_cache__[moduleId];
  153. /******/ if (cachedModule !== undefined) {
  154. /******/ return cachedModule.exports;
  155. /******/ }
  156. /******/ // Create a new module (and put it into the cache)
  157. /******/ var module = __webpack_module_cache__[moduleId] = {
  158. /******/ // no module.id needed
  159. /******/ // no module.loaded needed
  160. /******/ exports: {}
  161. /******/ };
  162. /******/
  163. /******/ // Execute the module function
  164. /******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
  165. /******/
  166. /******/ // Return the exports of the module
  167. /******/ return module.exports;
  168. /******/ }
  169. /******/
  170. /************************************************************************/
  171. var __webpack_exports__ = {};
  172. // This entry needs to be wrapped in an IIFE because it uses a non-standard name for the exports (exports).
  173. (() => {
  174. var exports = __webpack_exports__;
  175. Object.defineProperty(exports, "__esModule", ({
  176. value: true
  177. }));
  178. Object.defineProperty(exports, "TestPathPatterns", ({
  179. enumerable: true,
  180. get: function () {
  181. return _TestPathPatterns.TestPathPatterns;
  182. }
  183. }));
  184. Object.defineProperty(exports, "TestPathPatternsExecutor", ({
  185. enumerable: true,
  186. get: function () {
  187. return _TestPathPatterns.TestPathPatternsExecutor;
  188. }
  189. }));
  190. var _TestPathPatterns = __webpack_require__("./src/TestPathPatterns.ts");
  191. })();
  192. module.exports = __webpack_exports__;
  193. /******/ })()
  194. ;