No Description

pattern.js 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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_EXPANSIONS_SYMBOLS_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 && BRACE_EXPANSIONS_SYMBOLS_RE.test(pattern)) {
  41. return true;
  42. }
  43. return false;
  44. }
  45. exports.isDynamicPattern = isDynamicPattern;
  46. function convertToPositivePattern(pattern) {
  47. return isNegativePattern(pattern) ? pattern.slice(1) : pattern;
  48. }
  49. exports.convertToPositivePattern = convertToPositivePattern;
  50. function convertToNegativePattern(pattern) {
  51. return '!' + pattern;
  52. }
  53. exports.convertToNegativePattern = convertToNegativePattern;
  54. function isNegativePattern(pattern) {
  55. return pattern.startsWith('!') && pattern[1] !== '(';
  56. }
  57. exports.isNegativePattern = isNegativePattern;
  58. function isPositivePattern(pattern) {
  59. return !isNegativePattern(pattern);
  60. }
  61. exports.isPositivePattern = isPositivePattern;
  62. function getNegativePatterns(patterns) {
  63. return patterns.filter(isNegativePattern);
  64. }
  65. exports.getNegativePatterns = getNegativePatterns;
  66. function getPositivePatterns(patterns) {
  67. return patterns.filter(isPositivePattern);
  68. }
  69. exports.getPositivePatterns = getPositivePatterns;
  70. /**
  71. * Returns patterns that can be applied inside the current directory.
  72. *
  73. * @example
  74. * // ['./*', '*', 'a/*']
  75. * getPatternsInsideCurrentDirectory(['./*', '*', 'a/*', '../*', './../*'])
  76. */
  77. function getPatternsInsideCurrentDirectory(patterns) {
  78. return patterns.filter((pattern) => !isPatternRelatedToParentDirectory(pattern));
  79. }
  80. exports.getPatternsInsideCurrentDirectory = getPatternsInsideCurrentDirectory;
  81. /**
  82. * Returns patterns to be expanded relative to (outside) the current directory.
  83. *
  84. * @example
  85. * // ['../*', './../*']
  86. * getPatternsInsideCurrentDirectory(['./*', '*', 'a/*', '../*', './../*'])
  87. */
  88. function getPatternsOutsideCurrentDirectory(patterns) {
  89. return patterns.filter(isPatternRelatedToParentDirectory);
  90. }
  91. exports.getPatternsOutsideCurrentDirectory = getPatternsOutsideCurrentDirectory;
  92. function isPatternRelatedToParentDirectory(pattern) {
  93. return pattern.startsWith('..') || pattern.startsWith('./..');
  94. }
  95. exports.isPatternRelatedToParentDirectory = isPatternRelatedToParentDirectory;
  96. function getBaseDirectory(pattern) {
  97. return globParent(pattern, { flipBackslashes: false });
  98. }
  99. exports.getBaseDirectory = getBaseDirectory;
  100. function hasGlobStar(pattern) {
  101. return pattern.includes(GLOBSTAR);
  102. }
  103. exports.hasGlobStar = hasGlobStar;
  104. function endsWithSlashGlobStar(pattern) {
  105. return pattern.endsWith('/' + GLOBSTAR);
  106. }
  107. exports.endsWithSlashGlobStar = endsWithSlashGlobStar;
  108. function isAffectDepthOfReadingPattern(pattern) {
  109. const basename = path.basename(pattern);
  110. return endsWithSlashGlobStar(pattern) || isStaticPattern(basename);
  111. }
  112. exports.isAffectDepthOfReadingPattern = isAffectDepthOfReadingPattern;
  113. function expandPatternsWithBraceExpansion(patterns) {
  114. return patterns.reduce((collection, pattern) => {
  115. return collection.concat(expandBraceExpansion(pattern));
  116. }, []);
  117. }
  118. exports.expandPatternsWithBraceExpansion = expandPatternsWithBraceExpansion;
  119. function expandBraceExpansion(pattern) {
  120. return micromatch.braces(pattern, {
  121. expand: true,
  122. nodupes: true
  123. });
  124. }
  125. exports.expandBraceExpansion = expandBraceExpansion;
  126. function getPatternParts(pattern, options) {
  127. let { parts } = micromatch.scan(pattern, Object.assign(Object.assign({}, options), { parts: true }));
  128. /**
  129. * The scan method returns an empty array in some cases.
  130. * See micromatch/picomatch#58 for more details.
  131. */
  132. if (parts.length === 0) {
  133. parts = [pattern];
  134. }
  135. /**
  136. * The scan method does not return an empty part for the pattern with a forward slash.
  137. * This is another part of micromatch/picomatch#58.
  138. */
  139. if (parts[0].startsWith('/')) {
  140. parts[0] = parts[0].slice(1);
  141. parts.unshift('');
  142. }
  143. return parts;
  144. }
  145. exports.getPatternParts = getPatternParts;
  146. function makeRe(pattern, options) {
  147. return micromatch.makeRe(pattern, options);
  148. }
  149. exports.makeRe = makeRe;
  150. function convertPatternsToRe(patterns, options) {
  151. return patterns.map((pattern) => makeRe(pattern, options));
  152. }
  153. exports.convertPatternsToRe = convertPatternsToRe;
  154. function matchAny(entry, patternsRe) {
  155. return patternsRe.some((patternRe) => patternRe.test(entry));
  156. }
  157. exports.matchAny = matchAny;