index.js 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.unescape = exports.escape = exports.AST = exports.Minimatch = exports.match = exports.makeRe = exports.braceExpand = exports.defaults = exports.filter = exports.GLOBSTAR = exports.sep = exports.minimatch = void 0;
  7. const brace_expansion_1 = __importDefault(require("brace-expansion"));
  8. const assert_valid_pattern_js_1 = require("./assert-valid-pattern.js");
  9. const ast_js_1 = require("./ast.js");
  10. const escape_js_1 = require("./escape.js");
  11. const unescape_js_1 = require("./unescape.js");
  12. const minimatch = (p, pattern, options = {}) => {
  13. (0, assert_valid_pattern_js_1.assertValidPattern)(pattern);
  14. // shortcut: comments match nothing.
  15. if (!options.nocomment && pattern.charAt(0) === '#') {
  16. return false;
  17. }
  18. return new Minimatch(pattern, options).match(p);
  19. };
  20. exports.minimatch = minimatch;
  21. // Optimized checking for the most common glob patterns.
  22. const starDotExtRE = /^\*+([^+@!?\*\[\(]*)$/;
  23. const starDotExtTest = (ext) => (f) => !f.startsWith('.') && f.endsWith(ext);
  24. const starDotExtTestDot = (ext) => (f) => f.endsWith(ext);
  25. const starDotExtTestNocase = (ext) => {
  26. ext = ext.toLowerCase();
  27. return (f) => !f.startsWith('.') && f.toLowerCase().endsWith(ext);
  28. };
  29. const starDotExtTestNocaseDot = (ext) => {
  30. ext = ext.toLowerCase();
  31. return (f) => f.toLowerCase().endsWith(ext);
  32. };
  33. const starDotStarRE = /^\*+\.\*+$/;
  34. const starDotStarTest = (f) => !f.startsWith('.') && f.includes('.');
  35. const starDotStarTestDot = (f) => f !== '.' && f !== '..' && f.includes('.');
  36. const dotStarRE = /^\.\*+$/;
  37. const dotStarTest = (f) => f !== '.' && f !== '..' && f.startsWith('.');
  38. const starRE = /^\*+$/;
  39. const starTest = (f) => f.length !== 0 && !f.startsWith('.');
  40. const starTestDot = (f) => f.length !== 0 && f !== '.' && f !== '..';
  41. const qmarksRE = /^\?+([^+@!?\*\[\(]*)?$/;
  42. const qmarksTestNocase = ([$0, ext = '']) => {
  43. const noext = qmarksTestNoExt([$0]);
  44. if (!ext)
  45. return noext;
  46. ext = ext.toLowerCase();
  47. return (f) => noext(f) && f.toLowerCase().endsWith(ext);
  48. };
  49. const qmarksTestNocaseDot = ([$0, ext = '']) => {
  50. const noext = qmarksTestNoExtDot([$0]);
  51. if (!ext)
  52. return noext;
  53. ext = ext.toLowerCase();
  54. return (f) => noext(f) && f.toLowerCase().endsWith(ext);
  55. };
  56. const qmarksTestDot = ([$0, ext = '']) => {
  57. const noext = qmarksTestNoExtDot([$0]);
  58. return !ext ? noext : (f) => noext(f) && f.endsWith(ext);
  59. };
  60. const qmarksTest = ([$0, ext = '']) => {
  61. const noext = qmarksTestNoExt([$0]);
  62. return !ext ? noext : (f) => noext(f) && f.endsWith(ext);
  63. };
  64. const qmarksTestNoExt = ([$0]) => {
  65. const len = $0.length;
  66. return (f) => f.length === len && !f.startsWith('.');
  67. };
  68. const qmarksTestNoExtDot = ([$0]) => {
  69. const len = $0.length;
  70. return (f) => f.length === len && f !== '.' && f !== '..';
  71. };
  72. /* c8 ignore start */
  73. const defaultPlatform = (typeof process === 'object' && process
  74. ? (typeof process.env === 'object' &&
  75. process.env &&
  76. process.env.__MINIMATCH_TESTING_PLATFORM__) ||
  77. process.platform
  78. : 'posix');
  79. const path = {
  80. win32: { sep: '\\' },
  81. posix: { sep: '/' },
  82. };
  83. /* c8 ignore stop */
  84. exports.sep = defaultPlatform === 'win32' ? path.win32.sep : path.posix.sep;
  85. exports.minimatch.sep = exports.sep;
  86. exports.GLOBSTAR = Symbol('globstar **');
  87. exports.minimatch.GLOBSTAR = exports.GLOBSTAR;
  88. // any single thing other than /
  89. // don't need to escape / when using new RegExp()
  90. const qmark = '[^/]';
  91. // * => any number of characters
  92. const star = qmark + '*?';
  93. // ** when dots are allowed. Anything goes, except .. and .
  94. // not (^ or / followed by one or two dots followed by $ or /),
  95. // followed by anything, any number of times.
  96. const twoStarDot = '(?:(?!(?:\\/|^)(?:\\.{1,2})($|\\/)).)*?';
  97. // not a ^ or / followed by a dot,
  98. // followed by anything, any number of times.
  99. const twoStarNoDot = '(?:(?!(?:\\/|^)\\.).)*?';
  100. const filter = (pattern, options = {}) => (p) => (0, exports.minimatch)(p, pattern, options);
  101. exports.filter = filter;
  102. exports.minimatch.filter = exports.filter;
  103. const ext = (a, b = {}) => Object.assign({}, a, b);
  104. const defaults = (def) => {
  105. if (!def || typeof def !== 'object' || !Object.keys(def).length) {
  106. return exports.minimatch;
  107. }
  108. const orig = exports.minimatch;
  109. const m = (p, pattern, options = {}) => orig(p, pattern, ext(def, options));
  110. return Object.assign(m, {
  111. Minimatch: class Minimatch extends orig.Minimatch {
  112. constructor(pattern, options = {}) {
  113. super(pattern, ext(def, options));
  114. }
  115. static defaults(options) {
  116. return orig.defaults(ext(def, options)).Minimatch;
  117. }
  118. },
  119. AST: class AST extends orig.AST {
  120. /* c8 ignore start */
  121. constructor(type, parent, options = {}) {
  122. super(type, parent, ext(def, options));
  123. }
  124. /* c8 ignore stop */
  125. static fromGlob(pattern, options = {}) {
  126. return orig.AST.fromGlob(pattern, ext(def, options));
  127. }
  128. },
  129. unescape: (s, options = {}) => orig.unescape(s, ext(def, options)),
  130. escape: (s, options = {}) => orig.escape(s, ext(def, options)),
  131. filter: (pattern, options = {}) => orig.filter(pattern, ext(def, options)),
  132. defaults: (options) => orig.defaults(ext(def, options)),
  133. makeRe: (pattern, options = {}) => orig.makeRe(pattern, ext(def, options)),
  134. braceExpand: (pattern, options = {}) => orig.braceExpand(pattern, ext(def, options)),
  135. match: (list, pattern, options = {}) => orig.match(list, pattern, ext(def, options)),
  136. sep: orig.sep,
  137. GLOBSTAR: exports.GLOBSTAR,
  138. });
  139. };
  140. exports.defaults = defaults;
  141. exports.minimatch.defaults = exports.defaults;
  142. // Brace expansion:
  143. // a{b,c}d -> abd acd
  144. // a{b,}c -> abc ac
  145. // a{0..3}d -> a0d a1d a2d a3d
  146. // a{b,c{d,e}f}g -> abg acdfg acefg
  147. // a{b,c}d{e,f}g -> abdeg acdeg abdeg abdfg
  148. //
  149. // Invalid sets are not expanded.
  150. // a{2..}b -> a{2..}b
  151. // a{b}c -> a{b}c
  152. const braceExpand = (pattern, options = {}) => {
  153. (0, assert_valid_pattern_js_1.assertValidPattern)(pattern);
  154. // Thanks to Yeting Li <https://github.com/yetingli> for
  155. // improving this regexp to avoid a ReDOS vulnerability.
  156. if (options.nobrace || !/\{(?:(?!\{).)*\}/.test(pattern)) {
  157. // shortcut. no need to expand.
  158. return [pattern];
  159. }
  160. return (0, brace_expansion_1.default)(pattern);
  161. };
  162. exports.braceExpand = braceExpand;
  163. exports.minimatch.braceExpand = exports.braceExpand;
  164. // parse a component of the expanded set.
  165. // At this point, no pattern may contain "/" in it
  166. // so we're going to return a 2d array, where each entry is the full
  167. // pattern, split on '/', and then turned into a regular expression.
  168. // A regexp is made at the end which joins each array with an
  169. // escaped /, and another full one which joins each regexp with |.
  170. //
  171. // Following the lead of Bash 4.1, note that "**" only has special meaning
  172. // when it is the *only* thing in a path portion. Otherwise, any series
  173. // of * is equivalent to a single *. Globstar behavior is enabled by
  174. // default, and can be disabled by setting options.noglobstar.
  175. const makeRe = (pattern, options = {}) => new Minimatch(pattern, options).makeRe();
  176. exports.makeRe = makeRe;
  177. exports.minimatch.makeRe = exports.makeRe;
  178. const match = (list, pattern, options = {}) => {
  179. const mm = new Minimatch(pattern, options);
  180. list = list.filter(f => mm.match(f));
  181. if (mm.options.nonull && !list.length) {
  182. list.push(pattern);
  183. }
  184. return list;
  185. };
  186. exports.match = match;
  187. exports.minimatch.match = exports.match;
  188. // replace stuff like \* with *
  189. const globMagic = /[?*]|[+@!]\(.*?\)|\[|\]/;
  190. const regExpEscape = (s) => s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
  191. class Minimatch {
  192. options;
  193. set;
  194. pattern;
  195. windowsPathsNoEscape;
  196. nonegate;
  197. negate;
  198. comment;
  199. empty;
  200. preserveMultipleSlashes;
  201. partial;
  202. globSet;
  203. globParts;
  204. nocase;
  205. isWindows;
  206. platform;
  207. windowsNoMagicRoot;
  208. regexp;
  209. constructor(pattern, options = {}) {
  210. (0, assert_valid_pattern_js_1.assertValidPattern)(pattern);
  211. options = options || {};
  212. this.options = options;
  213. this.pattern = pattern;
  214. this.platform = options.platform || defaultPlatform;
  215. this.isWindows = this.platform === 'win32';
  216. this.windowsPathsNoEscape =
  217. !!options.windowsPathsNoEscape || options.allowWindowsEscape === false;
  218. if (this.windowsPathsNoEscape) {
  219. this.pattern = this.pattern.replace(/\\/g, '/');
  220. }
  221. this.preserveMultipleSlashes = !!options.preserveMultipleSlashes;
  222. this.regexp = null;
  223. this.negate = false;
  224. this.nonegate = !!options.nonegate;
  225. this.comment = false;
  226. this.empty = false;
  227. this.partial = !!options.partial;
  228. this.nocase = !!this.options.nocase;
  229. this.windowsNoMagicRoot =
  230. options.windowsNoMagicRoot !== undefined
  231. ? options.windowsNoMagicRoot
  232. : !!(this.isWindows && this.nocase);
  233. this.globSet = [];
  234. this.globParts = [];
  235. this.set = [];
  236. // make the set of regexps etc.
  237. this.make();
  238. }
  239. hasMagic() {
  240. if (this.options.magicalBraces && this.set.length > 1) {
  241. return true;
  242. }
  243. for (const pattern of this.set) {
  244. for (const part of pattern) {
  245. if (typeof part !== 'string')
  246. return true;
  247. }
  248. }
  249. return false;
  250. }
  251. debug(..._) { }
  252. make() {
  253. const pattern = this.pattern;
  254. const options = this.options;
  255. // empty patterns and comments match nothing.
  256. if (!options.nocomment && pattern.charAt(0) === '#') {
  257. this.comment = true;
  258. return;
  259. }
  260. if (!pattern) {
  261. this.empty = true;
  262. return;
  263. }
  264. // step 1: figure out negation, etc.
  265. this.parseNegate();
  266. // step 2: expand braces
  267. this.globSet = [...new Set(this.braceExpand())];
  268. if (options.debug) {
  269. this.debug = (...args) => console.error(...args);
  270. }
  271. this.debug(this.pattern, this.globSet);
  272. // step 3: now we have a set, so turn each one into a series of
  273. // path-portion matching patterns.
  274. // These will be regexps, except in the case of "**", which is
  275. // set to the GLOBSTAR object for globstar behavior,
  276. // and will not contain any / characters
  277. //
  278. // First, we preprocess to make the glob pattern sets a bit simpler
  279. // and deduped. There are some perf-killing patterns that can cause
  280. // problems with a glob walk, but we can simplify them down a bit.
  281. const rawGlobParts = this.globSet.map(s => this.slashSplit(s));
  282. this.globParts = this.preprocess(rawGlobParts);
  283. this.debug(this.pattern, this.globParts);
  284. // glob --> regexps
  285. let set = this.globParts.map((s, _, __) => {
  286. if (this.isWindows && this.windowsNoMagicRoot) {
  287. // check if it's a drive or unc path.
  288. const isUNC = s[0] === '' &&
  289. s[1] === '' &&
  290. (s[2] === '?' || !globMagic.test(s[2])) &&
  291. !globMagic.test(s[3]);
  292. const isDrive = /^[a-z]:/i.test(s[0]);
  293. if (isUNC) {
  294. return [...s.slice(0, 4), ...s.slice(4).map(ss => this.parse(ss))];
  295. }
  296. else if (isDrive) {
  297. return [s[0], ...s.slice(1).map(ss => this.parse(ss))];
  298. }
  299. }
  300. return s.map(ss => this.parse(ss));
  301. });
  302. this.debug(this.pattern, set);
  303. // filter out everything that didn't compile properly.
  304. this.set = set.filter(s => s.indexOf(false) === -1);
  305. // do not treat the ? in UNC paths as magic
  306. if (this.isWindows) {
  307. for (let i = 0; i < this.set.length; i++) {
  308. const p = this.set[i];
  309. if (p[0] === '' &&
  310. p[1] === '' &&
  311. this.globParts[i][2] === '?' &&
  312. typeof p[3] === 'string' &&
  313. /^[a-z]:$/i.test(p[3])) {
  314. p[2] = '?';
  315. }
  316. }
  317. }
  318. this.debug(this.pattern, this.set);
  319. }
  320. // various transforms to equivalent pattern sets that are
  321. // faster to process in a filesystem walk. The goal is to
  322. // eliminate what we can, and push all ** patterns as far
  323. // to the right as possible, even if it increases the number
  324. // of patterns that we have to process.
  325. preprocess(globParts) {
  326. // if we're not in globstar mode, then turn all ** into *
  327. if (this.options.noglobstar) {
  328. for (let i = 0; i < globParts.length; i++) {
  329. for (let j = 0; j < globParts[i].length; j++) {
  330. if (globParts[i][j] === '**') {
  331. globParts[i][j] = '*';
  332. }
  333. }
  334. }
  335. }
  336. const { optimizationLevel = 1 } = this.options;
  337. if (optimizationLevel >= 2) {
  338. // aggressive optimization for the purpose of fs walking
  339. globParts = this.firstPhasePreProcess(globParts);
  340. globParts = this.secondPhasePreProcess(globParts);
  341. }
  342. else if (optimizationLevel >= 1) {
  343. // just basic optimizations to remove some .. parts
  344. globParts = this.levelOneOptimize(globParts);
  345. }
  346. else {
  347. // just collapse multiple ** portions into one
  348. globParts = this.adjascentGlobstarOptimize(globParts);
  349. }
  350. return globParts;
  351. }
  352. // just get rid of adjascent ** portions
  353. adjascentGlobstarOptimize(globParts) {
  354. return globParts.map(parts => {
  355. let gs = -1;
  356. while (-1 !== (gs = parts.indexOf('**', gs + 1))) {
  357. let i = gs;
  358. while (parts[i + 1] === '**') {
  359. i++;
  360. }
  361. if (i !== gs) {
  362. parts.splice(gs, i - gs);
  363. }
  364. }
  365. return parts;
  366. });
  367. }
  368. // get rid of adjascent ** and resolve .. portions
  369. levelOneOptimize(globParts) {
  370. return globParts.map(parts => {
  371. parts = parts.reduce((set, part) => {
  372. const prev = set[set.length - 1];
  373. if (part === '**' && prev === '**') {
  374. return set;
  375. }
  376. if (part === '..') {
  377. if (prev && prev !== '..' && prev !== '.' && prev !== '**') {
  378. set.pop();
  379. return set;
  380. }
  381. }
  382. set.push(part);
  383. return set;
  384. }, []);
  385. return parts.length === 0 ? [''] : parts;
  386. });
  387. }
  388. levelTwoFileOptimize(parts) {
  389. if (!Array.isArray(parts)) {
  390. parts = this.slashSplit(parts);
  391. }
  392. let didSomething = false;
  393. do {
  394. didSomething = false;
  395. // <pre>/<e>/<rest> -> <pre>/<rest>
  396. if (!this.preserveMultipleSlashes) {
  397. for (let i = 1; i < parts.length - 1; i++) {
  398. const p = parts[i];
  399. // don't squeeze out UNC patterns
  400. if (i === 1 && p === '' && parts[0] === '')
  401. continue;
  402. if (p === '.' || p === '') {
  403. didSomething = true;
  404. parts.splice(i, 1);
  405. i--;
  406. }
  407. }
  408. if (parts[0] === '.' &&
  409. parts.length === 2 &&
  410. (parts[1] === '.' || parts[1] === '')) {
  411. didSomething = true;
  412. parts.pop();
  413. }
  414. }
  415. // <pre>/<p>/../<rest> -> <pre>/<rest>
  416. let dd = 0;
  417. while (-1 !== (dd = parts.indexOf('..', dd + 1))) {
  418. const p = parts[dd - 1];
  419. if (p && p !== '.' && p !== '..' && p !== '**') {
  420. didSomething = true;
  421. parts.splice(dd - 1, 2);
  422. dd -= 2;
  423. }
  424. }
  425. } while (didSomething);
  426. return parts.length === 0 ? [''] : parts;
  427. }
  428. // First phase: single-pattern processing
  429. // <pre> is 1 or more portions
  430. // <rest> is 1 or more portions
  431. // <p> is any portion other than ., .., '', or **
  432. // <e> is . or ''
  433. //
  434. // **/.. is *brutal* for filesystem walking performance, because
  435. // it effectively resets the recursive walk each time it occurs,
  436. // and ** cannot be reduced out by a .. pattern part like a regexp
  437. // or most strings (other than .., ., and '') can be.
  438. //
  439. // <pre>/**/../<p>/<p>/<rest> -> {<pre>/../<p>/<p>/<rest>,<pre>/**/<p>/<p>/<rest>}
  440. // <pre>/<e>/<rest> -> <pre>/<rest>
  441. // <pre>/<p>/../<rest> -> <pre>/<rest>
  442. // **/**/<rest> -> **/<rest>
  443. //
  444. // **/*/<rest> -> */**/<rest> <== not valid because ** doesn't follow
  445. // this WOULD be allowed if ** did follow symlinks, or * didn't
  446. firstPhasePreProcess(globParts) {
  447. let didSomething = false;
  448. do {
  449. didSomething = false;
  450. // <pre>/**/../<p>/<p>/<rest> -> {<pre>/../<p>/<p>/<rest>,<pre>/**/<p>/<p>/<rest>}
  451. for (let parts of globParts) {
  452. let gs = -1;
  453. while (-1 !== (gs = parts.indexOf('**', gs + 1))) {
  454. let gss = gs;
  455. while (parts[gss + 1] === '**') {
  456. // <pre>/**/**/<rest> -> <pre>/**/<rest>
  457. gss++;
  458. }
  459. // eg, if gs is 2 and gss is 4, that means we have 3 **
  460. // parts, and can remove 2 of them.
  461. if (gss > gs) {
  462. parts.splice(gs + 1, gss - gs);
  463. }
  464. let next = parts[gs + 1];
  465. const p = parts[gs + 2];
  466. const p2 = parts[gs + 3];
  467. if (next !== '..')
  468. continue;
  469. if (!p ||
  470. p === '.' ||
  471. p === '..' ||
  472. !p2 ||
  473. p2 === '.' ||
  474. p2 === '..') {
  475. continue;
  476. }
  477. didSomething = true;
  478. // edit parts in place, and push the new one
  479. parts.splice(gs, 1);
  480. const other = parts.slice(0);
  481. other[gs] = '**';
  482. globParts.push(other);
  483. gs--;
  484. }
  485. // <pre>/<e>/<rest> -> <pre>/<rest>
  486. if (!this.preserveMultipleSlashes) {
  487. for (let i = 1; i < parts.length - 1; i++) {
  488. const p = parts[i];
  489. // don't squeeze out UNC patterns
  490. if (i === 1 && p === '' && parts[0] === '')
  491. continue;
  492. if (p === '.' || p === '') {
  493. didSomething = true;
  494. parts.splice(i, 1);
  495. i--;
  496. }
  497. }
  498. if (parts[0] === '.' &&
  499. parts.length === 2 &&
  500. (parts[1] === '.' || parts[1] === '')) {
  501. didSomething = true;
  502. parts.pop();
  503. }
  504. }
  505. // <pre>/<p>/../<rest> -> <pre>/<rest>
  506. let dd = 0;
  507. while (-1 !== (dd = parts.indexOf('..', dd + 1))) {
  508. const p = parts[dd - 1];
  509. if (p && p !== '.' && p !== '..' && p !== '**') {
  510. didSomething = true;
  511. const needDot = dd === 1 && parts[dd + 1] === '**';
  512. const splin = needDot ? ['.'] : [];
  513. parts.splice(dd - 1, 2, ...splin);
  514. if (parts.length === 0)
  515. parts.push('');
  516. dd -= 2;
  517. }
  518. }
  519. }
  520. } while (didSomething);
  521. return globParts;
  522. }
  523. // second phase: multi-pattern dedupes
  524. // {<pre>/*/<rest>,<pre>/<p>/<rest>} -> <pre>/*/<rest>
  525. // {<pre>/<rest>,<pre>/<rest>} -> <pre>/<rest>
  526. // {<pre>/**/<rest>,<pre>/<rest>} -> <pre>/**/<rest>
  527. //
  528. // {<pre>/**/<rest>,<pre>/**/<p>/<rest>} -> <pre>/**/<rest>
  529. // ^-- not valid because ** doens't follow symlinks
  530. secondPhasePreProcess(globParts) {
  531. for (let i = 0; i < globParts.length - 1; i++) {
  532. for (let j = i + 1; j < globParts.length; j++) {
  533. const matched = this.partsMatch(globParts[i], globParts[j], !this.preserveMultipleSlashes);
  534. if (matched) {
  535. globParts[i] = [];
  536. globParts[j] = matched;
  537. break;
  538. }
  539. }
  540. }
  541. return globParts.filter(gs => gs.length);
  542. }
  543. partsMatch(a, b, emptyGSMatch = false) {
  544. let ai = 0;
  545. let bi = 0;
  546. let result = [];
  547. let which = '';
  548. while (ai < a.length && bi < b.length) {
  549. if (a[ai] === b[bi]) {
  550. result.push(which === 'b' ? b[bi] : a[ai]);
  551. ai++;
  552. bi++;
  553. }
  554. else if (emptyGSMatch && a[ai] === '**' && b[bi] === a[ai + 1]) {
  555. result.push(a[ai]);
  556. ai++;
  557. }
  558. else if (emptyGSMatch && b[bi] === '**' && a[ai] === b[bi + 1]) {
  559. result.push(b[bi]);
  560. bi++;
  561. }
  562. else if (a[ai] === '*' &&
  563. b[bi] &&
  564. (this.options.dot || !b[bi].startsWith('.')) &&
  565. b[bi] !== '**') {
  566. if (which === 'b')
  567. return false;
  568. which = 'a';
  569. result.push(a[ai]);
  570. ai++;
  571. bi++;
  572. }
  573. else if (b[bi] === '*' &&
  574. a[ai] &&
  575. (this.options.dot || !a[ai].startsWith('.')) &&
  576. a[ai] !== '**') {
  577. if (which === 'a')
  578. return false;
  579. which = 'b';
  580. result.push(b[bi]);
  581. ai++;
  582. bi++;
  583. }
  584. else {
  585. return false;
  586. }
  587. }
  588. // if we fall out of the loop, it means they two are identical
  589. // as long as their lengths match
  590. return a.length === b.length && result;
  591. }
  592. parseNegate() {
  593. if (this.nonegate)
  594. return;
  595. const pattern = this.pattern;
  596. let negate = false;
  597. let negateOffset = 0;
  598. for (let i = 0; i < pattern.length && pattern.charAt(i) === '!'; i++) {
  599. negate = !negate;
  600. negateOffset++;
  601. }
  602. if (negateOffset)
  603. this.pattern = pattern.slice(negateOffset);
  604. this.negate = negate;
  605. }
  606. // set partial to true to test if, for example,
  607. // "/a/b" matches the start of "/*/b/*/d"
  608. // Partial means, if you run out of file before you run
  609. // out of pattern, then that's fine, as long as all
  610. // the parts match.
  611. matchOne(file, pattern, partial = false) {
  612. const options = this.options;
  613. // UNC paths like //?/X:/... can match X:/... and vice versa
  614. // Drive letters in absolute drive or unc paths are always compared
  615. // case-insensitively.
  616. if (this.isWindows) {
  617. const fileDrive = typeof file[0] === 'string' && /^[a-z]:$/i.test(file[0]);
  618. const fileUNC = !fileDrive &&
  619. file[0] === '' &&
  620. file[1] === '' &&
  621. file[2] === '?' &&
  622. /^[a-z]:$/i.test(file[3]);
  623. const patternDrive = typeof pattern[0] === 'string' && /^[a-z]:$/i.test(pattern[0]);
  624. const patternUNC = !patternDrive &&
  625. pattern[0] === '' &&
  626. pattern[1] === '' &&
  627. pattern[2] === '?' &&
  628. typeof pattern[3] === 'string' &&
  629. /^[a-z]:$/i.test(pattern[3]);
  630. const fdi = fileUNC ? 3 : fileDrive ? 0 : undefined;
  631. const pdi = patternUNC ? 3 : patternDrive ? 0 : undefined;
  632. if (typeof fdi === 'number' && typeof pdi === 'number') {
  633. const [fd, pd] = [file[fdi], pattern[pdi]];
  634. if (fd.toLowerCase() === pd.toLowerCase()) {
  635. pattern[pdi] = fd;
  636. if (pdi > fdi) {
  637. pattern = pattern.slice(pdi);
  638. }
  639. else if (fdi > pdi) {
  640. file = file.slice(fdi);
  641. }
  642. }
  643. }
  644. }
  645. // resolve and reduce . and .. portions in the file as well.
  646. // dont' need to do the second phase, because it's only one string[]
  647. const { optimizationLevel = 1 } = this.options;
  648. if (optimizationLevel >= 2) {
  649. file = this.levelTwoFileOptimize(file);
  650. }
  651. this.debug('matchOne', this, { file, pattern });
  652. this.debug('matchOne', file.length, pattern.length);
  653. for (var fi = 0, pi = 0, fl = file.length, pl = pattern.length; fi < fl && pi < pl; fi++, pi++) {
  654. this.debug('matchOne loop');
  655. var p = pattern[pi];
  656. var f = file[fi];
  657. this.debug(pattern, p, f);
  658. // should be impossible.
  659. // some invalid regexp stuff in the set.
  660. /* c8 ignore start */
  661. if (p === false) {
  662. return false;
  663. }
  664. /* c8 ignore stop */
  665. if (p === exports.GLOBSTAR) {
  666. this.debug('GLOBSTAR', [pattern, p, f]);
  667. // "**"
  668. // a/**/b/**/c would match the following:
  669. // a/b/x/y/z/c
  670. // a/x/y/z/b/c
  671. // a/b/x/b/x/c
  672. // a/b/c
  673. // To do this, take the rest of the pattern after
  674. // the **, and see if it would match the file remainder.
  675. // If so, return success.
  676. // If not, the ** "swallows" a segment, and try again.
  677. // This is recursively awful.
  678. //
  679. // a/**/b/**/c matching a/b/x/y/z/c
  680. // - a matches a
  681. // - doublestar
  682. // - matchOne(b/x/y/z/c, b/**/c)
  683. // - b matches b
  684. // - doublestar
  685. // - matchOne(x/y/z/c, c) -> no
  686. // - matchOne(y/z/c, c) -> no
  687. // - matchOne(z/c, c) -> no
  688. // - matchOne(c, c) yes, hit
  689. var fr = fi;
  690. var pr = pi + 1;
  691. if (pr === pl) {
  692. this.debug('** at the end');
  693. // a ** at the end will just swallow the rest.
  694. // We have found a match.
  695. // however, it will not swallow /.x, unless
  696. // options.dot is set.
  697. // . and .. are *never* matched by **, for explosively
  698. // exponential reasons.
  699. for (; fi < fl; fi++) {
  700. if (file[fi] === '.' ||
  701. file[fi] === '..' ||
  702. (!options.dot && file[fi].charAt(0) === '.'))
  703. return false;
  704. }
  705. return true;
  706. }
  707. // ok, let's see if we can swallow whatever we can.
  708. while (fr < fl) {
  709. var swallowee = file[fr];
  710. this.debug('\nglobstar while', file, fr, pattern, pr, swallowee);
  711. // XXX remove this slice. Just pass the start index.
  712. if (this.matchOne(file.slice(fr), pattern.slice(pr), partial)) {
  713. this.debug('globstar found match!', fr, fl, swallowee);
  714. // found a match.
  715. return true;
  716. }
  717. else {
  718. // can't swallow "." or ".." ever.
  719. // can only swallow ".foo" when explicitly asked.
  720. if (swallowee === '.' ||
  721. swallowee === '..' ||
  722. (!options.dot && swallowee.charAt(0) === '.')) {
  723. this.debug('dot detected!', file, fr, pattern, pr);
  724. break;
  725. }
  726. // ** swallows a segment, and continue.
  727. this.debug('globstar swallow a segment, and continue');
  728. fr++;
  729. }
  730. }
  731. // no match was found.
  732. // However, in partial mode, we can't say this is necessarily over.
  733. /* c8 ignore start */
  734. if (partial) {
  735. // ran out of file
  736. this.debug('\n>>> no match, partial?', file, fr, pattern, pr);
  737. if (fr === fl) {
  738. return true;
  739. }
  740. }
  741. /* c8 ignore stop */
  742. return false;
  743. }
  744. // something other than **
  745. // non-magic patterns just have to match exactly
  746. // patterns with magic have been turned into regexps.
  747. let hit;
  748. if (typeof p === 'string') {
  749. hit = f === p;
  750. this.debug('string match', p, f, hit);
  751. }
  752. else {
  753. hit = p.test(f);
  754. this.debug('pattern match', p, f, hit);
  755. }
  756. if (!hit)
  757. return false;
  758. }
  759. // Note: ending in / means that we'll get a final ""
  760. // at the end of the pattern. This can only match a
  761. // corresponding "" at the end of the file.
  762. // If the file ends in /, then it can only match a
  763. // a pattern that ends in /, unless the pattern just
  764. // doesn't have any more for it. But, a/b/ should *not*
  765. // match "a/b/*", even though "" matches against the
  766. // [^/]*? pattern, except in partial mode, where it might
  767. // simply not be reached yet.
  768. // However, a/b/ should still satisfy a/*
  769. // now either we fell off the end of the pattern, or we're done.
  770. if (fi === fl && pi === pl) {
  771. // ran out of pattern and filename at the same time.
  772. // an exact hit!
  773. return true;
  774. }
  775. else if (fi === fl) {
  776. // ran out of file, but still had pattern left.
  777. // this is ok if we're doing the match as part of
  778. // a glob fs traversal.
  779. return partial;
  780. }
  781. else if (pi === pl) {
  782. // ran out of pattern, still have file left.
  783. // this is only acceptable if we're on the very last
  784. // empty segment of a file with a trailing slash.
  785. // a/* should match a/b/
  786. return fi === fl - 1 && file[fi] === '';
  787. /* c8 ignore start */
  788. }
  789. else {
  790. // should be unreachable.
  791. throw new Error('wtf?');
  792. }
  793. /* c8 ignore stop */
  794. }
  795. braceExpand() {
  796. return (0, exports.braceExpand)(this.pattern, this.options);
  797. }
  798. parse(pattern) {
  799. (0, assert_valid_pattern_js_1.assertValidPattern)(pattern);
  800. const options = this.options;
  801. // shortcuts
  802. if (pattern === '**')
  803. return exports.GLOBSTAR;
  804. if (pattern === '')
  805. return '';
  806. // far and away, the most common glob pattern parts are
  807. // *, *.*, and *.<ext> Add a fast check method for those.
  808. let m;
  809. let fastTest = null;
  810. if ((m = pattern.match(starRE))) {
  811. fastTest = options.dot ? starTestDot : starTest;
  812. }
  813. else if ((m = pattern.match(starDotExtRE))) {
  814. fastTest = (options.nocase
  815. ? options.dot
  816. ? starDotExtTestNocaseDot
  817. : starDotExtTestNocase
  818. : options.dot
  819. ? starDotExtTestDot
  820. : starDotExtTest)(m[1]);
  821. }
  822. else if ((m = pattern.match(qmarksRE))) {
  823. fastTest = (options.nocase
  824. ? options.dot
  825. ? qmarksTestNocaseDot
  826. : qmarksTestNocase
  827. : options.dot
  828. ? qmarksTestDot
  829. : qmarksTest)(m);
  830. }
  831. else if ((m = pattern.match(starDotStarRE))) {
  832. fastTest = options.dot ? starDotStarTestDot : starDotStarTest;
  833. }
  834. else if ((m = pattern.match(dotStarRE))) {
  835. fastTest = dotStarTest;
  836. }
  837. const re = ast_js_1.AST.fromGlob(pattern, this.options).toMMPattern();
  838. if (fastTest && typeof re === 'object') {
  839. // Avoids overriding in frozen environments
  840. Reflect.defineProperty(re, 'test', { value: fastTest });
  841. }
  842. return re;
  843. }
  844. makeRe() {
  845. if (this.regexp || this.regexp === false)
  846. return this.regexp;
  847. // at this point, this.set is a 2d array of partial
  848. // pattern strings, or "**".
  849. //
  850. // It's better to use .match(). This function shouldn't
  851. // be used, really, but it's pretty convenient sometimes,
  852. // when you just want to work with a regex.
  853. const set = this.set;
  854. if (!set.length) {
  855. this.regexp = false;
  856. return this.regexp;
  857. }
  858. const options = this.options;
  859. const twoStar = options.noglobstar
  860. ? star
  861. : options.dot
  862. ? twoStarDot
  863. : twoStarNoDot;
  864. const flags = new Set(options.nocase ? ['i'] : []);
  865. // regexpify non-globstar patterns
  866. // if ** is only item, then we just do one twoStar
  867. // if ** is first, and there are more, prepend (\/|twoStar\/)? to next
  868. // if ** is last, append (\/twoStar|) to previous
  869. // if ** is in the middle, append (\/|\/twoStar\/) to previous
  870. // then filter out GLOBSTAR symbols
  871. let re = set
  872. .map(pattern => {
  873. const pp = pattern.map(p => {
  874. if (p instanceof RegExp) {
  875. for (const f of p.flags.split(''))
  876. flags.add(f);
  877. }
  878. return typeof p === 'string'
  879. ? regExpEscape(p)
  880. : p === exports.GLOBSTAR
  881. ? exports.GLOBSTAR
  882. : p._src;
  883. });
  884. pp.forEach((p, i) => {
  885. const next = pp[i + 1];
  886. const prev = pp[i - 1];
  887. if (p !== exports.GLOBSTAR || prev === exports.GLOBSTAR) {
  888. return;
  889. }
  890. if (prev === undefined) {
  891. if (next !== undefined && next !== exports.GLOBSTAR) {
  892. pp[i + 1] = '(?:\\/|' + twoStar + '\\/)?' + next;
  893. }
  894. else {
  895. pp[i] = twoStar;
  896. }
  897. }
  898. else if (next === undefined) {
  899. pp[i - 1] = prev + '(?:\\/|' + twoStar + ')?';
  900. }
  901. else if (next !== exports.GLOBSTAR) {
  902. pp[i - 1] = prev + '(?:\\/|\\/' + twoStar + '\\/)' + next;
  903. pp[i + 1] = exports.GLOBSTAR;
  904. }
  905. });
  906. return pp.filter(p => p !== exports.GLOBSTAR).join('/');
  907. })
  908. .join('|');
  909. // need to wrap in parens if we had more than one thing with |,
  910. // otherwise only the first will be anchored to ^ and the last to $
  911. const [open, close] = set.length > 1 ? ['(?:', ')'] : ['', ''];
  912. // must match entire pattern
  913. // ending in a * or ** will make it less strict.
  914. re = '^' + open + re + close + '$';
  915. // can match anything, as long as it's not this.
  916. if (this.negate)
  917. re = '^(?!' + re + ').+$';
  918. try {
  919. this.regexp = new RegExp(re, [...flags].join(''));
  920. /* c8 ignore start */
  921. }
  922. catch (ex) {
  923. // should be impossible
  924. this.regexp = false;
  925. }
  926. /* c8 ignore stop */
  927. return this.regexp;
  928. }
  929. slashSplit(p) {
  930. // if p starts with // on windows, we preserve that
  931. // so that UNC paths aren't broken. Otherwise, any number of
  932. // / characters are coalesced into one, unless
  933. // preserveMultipleSlashes is set to true.
  934. if (this.preserveMultipleSlashes) {
  935. return p.split('/');
  936. }
  937. else if (this.isWindows && /^\/\/[^\/]+/.test(p)) {
  938. // add an extra '' for the one we lose
  939. return ['', ...p.split(/\/+/)];
  940. }
  941. else {
  942. return p.split(/\/+/);
  943. }
  944. }
  945. match(f, partial = this.partial) {
  946. this.debug('match', f, this.pattern);
  947. // short-circuit in the case of busted things.
  948. // comments, etc.
  949. if (this.comment) {
  950. return false;
  951. }
  952. if (this.empty) {
  953. return f === '';
  954. }
  955. if (f === '/' && partial) {
  956. return true;
  957. }
  958. const options = this.options;
  959. // windows: need to use /, not \
  960. if (this.isWindows) {
  961. f = f.split('\\').join('/');
  962. }
  963. // treat the test path as a set of pathparts.
  964. const ff = this.slashSplit(f);
  965. this.debug(this.pattern, 'split', ff);
  966. // just ONE of the pattern sets in this.set needs to match
  967. // in order for it to be valid. If negating, then just one
  968. // match means that we have failed.
  969. // Either way, return on the first hit.
  970. const set = this.set;
  971. this.debug(this.pattern, 'set', set);
  972. // Find the basename of the path by looking for the last non-empty segment
  973. let filename = ff[ff.length - 1];
  974. if (!filename) {
  975. for (let i = ff.length - 2; !filename && i >= 0; i--) {
  976. filename = ff[i];
  977. }
  978. }
  979. for (let i = 0; i < set.length; i++) {
  980. const pattern = set[i];
  981. let file = ff;
  982. if (options.matchBase && pattern.length === 1) {
  983. file = [filename];
  984. }
  985. const hit = this.matchOne(file, pattern, partial);
  986. if (hit) {
  987. if (options.flipNegate) {
  988. return true;
  989. }
  990. return !this.negate;
  991. }
  992. }
  993. // didn't get any hits. this is success if it's a negative
  994. // pattern, failure otherwise.
  995. if (options.flipNegate) {
  996. return false;
  997. }
  998. return this.negate;
  999. }
  1000. static defaults(def) {
  1001. return exports.minimatch.defaults(def).Minimatch;
  1002. }
  1003. }
  1004. exports.Minimatch = Minimatch;
  1005. /* c8 ignore start */
  1006. var ast_js_2 = require("./ast.js");
  1007. Object.defineProperty(exports, "AST", { enumerable: true, get: function () { return ast_js_2.AST; } });
  1008. var escape_js_2 = require("./escape.js");
  1009. Object.defineProperty(exports, "escape", { enumerable: true, get: function () { return escape_js_2.escape; } });
  1010. var unescape_js_2 = require("./unescape.js");
  1011. Object.defineProperty(exports, "unescape", { enumerable: true, get: function () { return unescape_js_2.unescape; } });
  1012. /* c8 ignore stop */
  1013. exports.minimatch.AST = ast_js_1.AST;
  1014. exports.minimatch.Minimatch = Minimatch;
  1015. exports.minimatch.escape = escape_js_1.escape;
  1016. exports.minimatch.unescape = unescape_js_1.unescape;
  1017. //# sourceMappingURL=index.js.map