You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
6.9 KiB

2 years ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.matchAny = exports.convertPatternsToRe = exports.makeRe = exports.getPatternParts = exports.expandBraceExpansion = exports.expandPatternsWithBraceExpansion = exports.isAffectDepthOfReadingPattern = exports.endsWithSlashGlobStar = exports.hasGlobStar = exports.getBaseDirectory = exports.isPatternRelatedToParentDirectory = exports.getPatternsOutsideCurrentDirectory = exports.getPatternsInsideCurrentDirectory = exports.getPositivePatterns = exports.getNegativePatterns = exports.isPositivePattern = exports.isNegativePattern = exports.convertToNegativePattern = exports.convertToPositivePattern = exports.isDynamicPattern = exports.isStaticPattern = void 0;
  4. const path = require("path");
  5. const globParent = require("glob-parent");
  6. const micromatch = require("micromatch");
  7. const GLOBSTAR = '**';
  8. const ESCAPE_SYMBOL = '\\';
  9. const COMMON_GLOB_SYMBOLS_RE = /[*?]|^!/;
  10. const REGEX_CHARACTER_CLASS_SYMBOLS_RE = /\[[^[]*]/;
  11. const REGEX_GROUP_SYMBOLS_RE = /(?:^|[^!*+?@])\([^(]*\|[^|]*\)/;
  12. const GLOB_EXTENSION_SYMBOLS_RE = /[!*+?@]\([^(]*\)/;
  13. const BRACE_EXPANSION_SEPARATORS_RE = /,|\.\./;
  14. function isStaticPattern(pattern, options = {}) {
  15. return !isDynamicPattern(pattern, options);
  16. }
  17. exports.isStaticPattern = isStaticPattern;
  18. function isDynamicPattern(pattern, options = {}) {
  19. /**
  20. * A special case with an empty string is necessary for matching patterns that start with a forward slash.
  21. * An empty string cannot be a dynamic pattern.
  22. * For example, the pattern `/lib/*` will be spread into parts: '', 'lib', '*'.
  23. */
  24. if (pattern === '') {
  25. return false;
  26. }
  27. /**
  28. * When the `caseSensitiveMatch` option is disabled, all patterns must be marked as dynamic, because we cannot check
  29. * filepath directly (without read directory).
  30. */
  31. if (options.caseSensitiveMatch === false || pattern.includes(ESCAPE_SYMBOL)) {
  32. return true;
  33. }
  34. if (COMMON_GLOB_SYMBOLS_RE.test(pattern) || REGEX_CHARACTER_CLASS_SYMBOLS_RE.test(pattern) || REGEX_GROUP_SYMBOLS_RE.test(pattern)) {
  35. return true;
  36. }
  37. if (options.extglob !== false && GLOB_EXTENSION_SYMBOLS_RE.test(pattern)) {
  38. return true;
  39. }
  40. if (options.braceExpansion !== false && hasBraceExpansion(pattern)) {
  41. return true;
  42. }
  43. return false;
  44. }
  45. exports.isDynamicPattern = isDynamicPattern;
  46. function hasBraceExpansion(pattern) {
  47. const openingBraceIndex = pattern.indexOf('{');
  48. if (openingBraceIndex === -1) {
  49. return false;
  50. }
  51. const closingBraceIndex = pattern.indexOf('}', openingBraceIndex + 1);
  52. if (closingBraceIndex === -1) {
  53. return false;
  54. }
  55. const braceContent = pattern.slice(openingBraceIndex, closingBraceIndex);
  56. return BRACE_EXPANSION_SEPARATORS_RE.test(braceContent);
  57. }
  58. function convertToPositivePattern(pattern) {
  59. return isNegativePattern(pattern) ? pattern.slice(1) : pattern;
  60. }
  61. exports.convertToPositivePattern = convertToPositivePattern;
  62. function convertToNegativePattern(pattern) {
  63. return '!' + pattern;
  64. }
  65. exports.convertToNegativePattern = convertToNegativePattern;
  66. function isNegativePattern(pattern) {
  67. return pattern.startsWith('!') && pattern[1] !== '(';
  68. }
  69. exports.isNegativePattern = isNegativePattern;
  70. function isPositivePattern(pattern) {
  71. return !isNegativePattern(pattern);
  72. }
  73. exports.isPositivePattern = isPositivePattern;
  74. function getNegativePatterns(patterns) {
  75. return patterns.filter(isNegativePattern);
  76. }
  77. exports.getNegativePatterns = getNegativePatterns;
  78. function getPositivePatterns(patterns) {
  79. return patterns.filter(isPositivePattern);
  80. }
  81. exports.getPositivePatterns = getPositivePatterns;
  82. /**
  83. * Returns patterns that can be applied inside the current directory.
  84. *
  85. * @example
  86. * // ['./*', '*', 'a/*']
  87. * getPatternsInsideCurrentDirectory(['./*', '*', 'a/*', '../*', './../*'])
  88. */
  89. function getPatternsInsideCurrentDirectory(patterns) {
  90. return patterns.filter((pattern) => !isPatternRelatedToParentDirectory(pattern));
  91. }
  92. exports.getPatternsInsideCurrentDirectory = getPatternsInsideCurrentDirectory;
  93. /**
  94. * Returns patterns to be expanded relative to (outside) the current directory.
  95. *
  96. * @example
  97. * // ['../*', './../*']
  98. * getPatternsInsideCurrentDirectory(['./*', '*', 'a/*', '../*', './../*'])
  99. */
  100. function getPatternsOutsideCurrentDirectory(patterns) {
  101. return patterns.filter(isPatternRelatedToParentDirectory);
  102. }
  103. exports.getPatternsOutsideCurrentDirectory = getPatternsOutsideCurrentDirectory;
  104. function isPatternRelatedToParentDirectory(pattern) {
  105. return pattern.startsWith('..') || pattern.startsWith('./..');
  106. }
  107. exports.isPatternRelatedToParentDirectory = isPatternRelatedToParentDirectory;
  108. function getBaseDirectory(pattern) {
  109. return globParent(pattern, { flipBackslashes: false });
  110. }
  111. exports.getBaseDirectory = getBaseDirectory;
  112. function hasGlobStar(pattern) {
  113. return pattern.includes(GLOBSTAR);
  114. }
  115. exports.hasGlobStar = hasGlobStar;
  116. function endsWithSlashGlobStar(pattern) {
  117. return pattern.endsWith('/' + GLOBSTAR);
  118. }
  119. exports.endsWithSlashGlobStar = endsWithSlashGlobStar;
  120. function isAffectDepthOfReadingPattern(pattern) {
  121. const basename = path.basename(pattern);
  122. return endsWithSlashGlobStar(pattern) || isStaticPattern(basename);
  123. }
  124. exports.isAffectDepthOfReadingPattern = isAffectDepthOfReadingPattern;
  125. function expandPatternsWithBraceExpansion(patterns) {
  126. return patterns.reduce((collection, pattern) => {
  127. return collection.concat(expandBraceExpansion(pattern));
  128. }, []);
  129. }
  130. exports.expandPatternsWithBraceExpansion = expandPatternsWithBraceExpansion;
  131. function expandBraceExpansion(pattern) {
  132. return micromatch.braces(pattern, {
  133. expand: true,
  134. nodupes: true
  135. });
  136. }
  137. exports.expandBraceExpansion = expandBraceExpansion;
  138. function getPatternParts(pattern, options) {
  139. let { parts } = micromatch.scan(pattern, Object.assign(Object.assign({}, options), { parts: true }));
  140. /**
  141. * The scan method returns an empty array in some cases.
  142. * See micromatch/picomatch#58 for more details.
  143. */
  144. if (parts.length === 0) {
  145. parts = [pattern];
  146. }
  147. /**
  148. * The scan method does not return an empty part for the pattern with a forward slash.
  149. * This is another part of micromatch/picomatch#58.
  150. */
  151. if (parts[0].startsWith('/')) {
  152. parts[0] = parts[0].slice(1);
  153. parts.unshift('');
  154. }
  155. return parts;
  156. }
  157. exports.getPatternParts = getPatternParts;
  158. function makeRe(pattern, options) {
  159. return micromatch.makeRe(pattern, options);
  160. }
  161. exports.makeRe = makeRe;
  162. function convertPatternsToRe(patterns, options) {
  163. return patterns.map((pattern) => makeRe(pattern, options));
  164. }
  165. exports.convertPatternsToRe = convertPatternsToRe;
  166. function matchAny(entry, patternsRe) {
  167. return patternsRe.some((patternRe) => patternRe.test(entry));
  168. }
  169. exports.matchAny = matchAny;