minimatch 5.1.4 → 6.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,850 @@
1
+ export const minimatch = (p, pattern, options = {}) => {
2
+ assertValidPattern(pattern);
3
+ // shortcut: comments match nothing.
4
+ if (!options.nocomment && pattern.charAt(0) === '#') {
5
+ return false;
6
+ }
7
+ return new Minimatch(pattern, options).match(p);
8
+ };
9
+ export default minimatch;
10
+ /* c8 ignore start */
11
+ const platform = typeof process === 'object' && process
12
+ ? (typeof process.env === 'object' &&
13
+ process.env &&
14
+ process.env.__MINIMATCH_TESTING_PLATFORM__) ||
15
+ process.platform
16
+ : 'posix';
17
+ const isWindows = platform === 'win32';
18
+ const path = isWindows ? { sep: '\\' } : { sep: '/' };
19
+ /* c8 ignore stop */
20
+ export const sep = path.sep;
21
+ minimatch.sep = sep;
22
+ export const GLOBSTAR = Symbol('globstar **');
23
+ minimatch.GLOBSTAR = GLOBSTAR;
24
+ import expand from 'brace-expansion';
25
+ const plTypes = {
26
+ '!': { open: '(?:(?!(?:', close: '))[^/]*?)' },
27
+ '?': { open: '(?:', close: ')?' },
28
+ '+': { open: '(?:', close: ')+' },
29
+ '*': { open: '(?:', close: ')*' },
30
+ '@': { open: '(?:', close: ')' },
31
+ };
32
+ // any single thing other than /
33
+ // don't need to escape / when using new RegExp()
34
+ const qmark = '[^/]';
35
+ // * => any number of characters
36
+ const star = qmark + '*?';
37
+ // ** when dots are allowed. Anything goes, except .. and .
38
+ // not (^ or / followed by one or two dots followed by $ or /),
39
+ // followed by anything, any number of times.
40
+ const twoStarDot = '(?:(?!(?:\\/|^)(?:\\.{1,2})($|\\/)).)*?';
41
+ // not a ^ or / followed by a dot,
42
+ // followed by anything, any number of times.
43
+ const twoStarNoDot = '(?:(?!(?:\\/|^)\\.).)*?';
44
+ // "abc" -> { a:true, b:true, c:true }
45
+ const charSet = (s) => s.split('').reduce((set, c) => {
46
+ set[c] = true;
47
+ return set;
48
+ }, {});
49
+ // characters that need to be escaped in RegExp.
50
+ const reSpecials = charSet('().*{}+?[]^$\\!');
51
+ // characters that indicate we have to add the pattern start
52
+ const addPatternStartSet = charSet('[.(');
53
+ // normalizes slashes.
54
+ const slashSplit = /\/+/;
55
+ export const filter = (pattern, options = {}) => (p) => minimatch(p, pattern, options);
56
+ minimatch.filter = filter;
57
+ const ext = (a, b = {}) => Object.assign({}, a, b);
58
+ export const defaults = (def) => {
59
+ if (!def || typeof def !== 'object' || !Object.keys(def).length) {
60
+ return minimatch;
61
+ }
62
+ const orig = minimatch;
63
+ const m = (p, pattern, options = {}) => orig(p, pattern, ext(def, options));
64
+ return Object.assign(m, {
65
+ Minimatch: class Minimatch extends orig.Minimatch {
66
+ constructor(pattern, options = {}) {
67
+ super(pattern, ext(def, options));
68
+ }
69
+ static defaults(options) {
70
+ return orig.defaults(ext(def, options)).Minimatch;
71
+ }
72
+ },
73
+ filter: (pattern, options = {}) => orig.filter(pattern, ext(def, options)),
74
+ defaults: (options) => orig.defaults(ext(def, options)),
75
+ makeRe: (pattern, options = {}) => orig.makeRe(pattern, ext(def, options)),
76
+ braceExpand: (pattern, options = {}) => orig.braceExpand(pattern, ext(def, options)),
77
+ match: (list, pattern, options = {}) => orig.match(list, pattern, ext(def, options)),
78
+ sep: orig.sep,
79
+ GLOBSTAR: GLOBSTAR,
80
+ });
81
+ };
82
+ minimatch.defaults = defaults;
83
+ // Brace expansion:
84
+ // a{b,c}d -> abd acd
85
+ // a{b,}c -> abc ac
86
+ // a{0..3}d -> a0d a1d a2d a3d
87
+ // a{b,c{d,e}f}g -> abg acdfg acefg
88
+ // a{b,c}d{e,f}g -> abdeg acdeg abdeg abdfg
89
+ //
90
+ // Invalid sets are not expanded.
91
+ // a{2..}b -> a{2..}b
92
+ // a{b}c -> a{b}c
93
+ export const braceExpand = (pattern, options = {}) => {
94
+ assertValidPattern(pattern);
95
+ // Thanks to Yeting Li <https://github.com/yetingli> for
96
+ // improving this regexp to avoid a ReDOS vulnerability.
97
+ if (options.nobrace || !/\{(?:(?!\{).)*\}/.test(pattern)) {
98
+ // shortcut. no need to expand.
99
+ return [pattern];
100
+ }
101
+ return expand(pattern);
102
+ };
103
+ minimatch.braceExpand = braceExpand;
104
+ const MAX_PATTERN_LENGTH = 1024 * 64;
105
+ const assertValidPattern = (pattern) => {
106
+ if (typeof pattern !== 'string') {
107
+ throw new TypeError('invalid pattern');
108
+ }
109
+ if (pattern.length > MAX_PATTERN_LENGTH) {
110
+ throw new TypeError('pattern is too long');
111
+ }
112
+ };
113
+ // parse a component of the expanded set.
114
+ // At this point, no pattern may contain "/" in it
115
+ // so we're going to return a 2d array, where each entry is the full
116
+ // pattern, split on '/', and then turned into a regular expression.
117
+ // A regexp is made at the end which joins each array with an
118
+ // escaped /, and another full one which joins each regexp with |.
119
+ //
120
+ // Following the lead of Bash 4.1, note that "**" only has special meaning
121
+ // when it is the *only* thing in a path portion. Otherwise, any series
122
+ // of * is equivalent to a single *. Globstar behavior is enabled by
123
+ // default, and can be disabled by setting options.noglobstar.
124
+ const SUBPARSE = Symbol('subparse');
125
+ export const makeRe = (pattern, options = {}) => new Minimatch(pattern, options).makeRe();
126
+ minimatch.makeRe = makeRe;
127
+ export const match = (list, pattern, options = {}) => {
128
+ const mm = new Minimatch(pattern, options);
129
+ list = list.filter(f => mm.match(f));
130
+ if (mm.options.nonull && !list.length) {
131
+ list.push(pattern);
132
+ }
133
+ return list;
134
+ };
135
+ minimatch.match = match;
136
+ // replace stuff like \* with *
137
+ const globUnescape = (s) => s.replace(/\\(.)/g, '$1');
138
+ const charUnescape = (s) => s.replace(/\\([^-\]])/g, '$1');
139
+ const regExpEscape = (s) => s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
140
+ const braExpEscape = (s) => s.replace(/[[\]\\]/g, '\\$&');
141
+ export class Minimatch {
142
+ options;
143
+ set;
144
+ pattern;
145
+ windowsPathsNoEscape;
146
+ nonegate;
147
+ negate;
148
+ comment;
149
+ empty;
150
+ partial;
151
+ globSet;
152
+ globParts;
153
+ regexp;
154
+ constructor(pattern, options = {}) {
155
+ assertValidPattern(pattern);
156
+ options = options || {};
157
+ this.options = options;
158
+ this.set = [];
159
+ this.pattern = pattern;
160
+ this.windowsPathsNoEscape =
161
+ !!options.windowsPathsNoEscape || options.allowWindowsEscape === false;
162
+ if (this.windowsPathsNoEscape) {
163
+ this.pattern = this.pattern.replace(/\\/g, '/');
164
+ }
165
+ this.regexp = null;
166
+ this.negate = false;
167
+ this.nonegate = !!options.nonegate;
168
+ this.comment = false;
169
+ this.empty = false;
170
+ this.partial = !!options.partial;
171
+ // make the set of regexps etc.
172
+ this.make();
173
+ }
174
+ debug(..._) { }
175
+ make() {
176
+ const pattern = this.pattern;
177
+ const options = this.options;
178
+ // empty patterns and comments match nothing.
179
+ if (!options.nocomment && pattern.charAt(0) === '#') {
180
+ this.comment = true;
181
+ return;
182
+ }
183
+ if (!pattern) {
184
+ this.empty = true;
185
+ return;
186
+ }
187
+ // step 1: figure out negation, etc.
188
+ this.parseNegate();
189
+ // step 2: expand braces
190
+ const globSet = (this.globSet = this.braceExpand());
191
+ if (options.debug) {
192
+ this.debug = (...args) => console.error(...args);
193
+ }
194
+ this.debug(this.pattern, globSet);
195
+ // step 3: now we have a set, so turn each one into a series of path-portion
196
+ // matching patterns.
197
+ // These will be regexps, except in the case of "**", which is
198
+ // set to the GLOBSTAR object for globstar behavior,
199
+ // and will not contain any / characters
200
+ const globParts = (this.globParts = globSet.map(s => s.split(slashSplit)));
201
+ this.debug(this.pattern, globParts);
202
+ // glob --> regexps
203
+ let set = globParts.map((s, _, __) => s.map(ss => this.parse(ss)));
204
+ this.debug(this.pattern, set);
205
+ // filter out everything that didn't compile properly.
206
+ this.set = set.filter(s => s.indexOf(false) === -1);
207
+ this.debug(this.pattern, this.set);
208
+ }
209
+ parseNegate() {
210
+ if (this.nonegate)
211
+ return;
212
+ const pattern = this.pattern;
213
+ let negate = false;
214
+ let negateOffset = 0;
215
+ for (let i = 0; i < pattern.length && pattern.charAt(i) === '!'; i++) {
216
+ negate = !negate;
217
+ negateOffset++;
218
+ }
219
+ if (negateOffset)
220
+ this.pattern = pattern.slice(negateOffset);
221
+ this.negate = negate;
222
+ }
223
+ // set partial to true to test if, for example,
224
+ // "/a/b" matches the start of "/*/b/*/d"
225
+ // Partial means, if you run out of file before you run
226
+ // out of pattern, then that's fine, as long as all
227
+ // the parts match.
228
+ matchOne(file, pattern, partial = false) {
229
+ var options = this.options;
230
+ this.debug('matchOne', { this: this, file: file, pattern: pattern });
231
+ this.debug('matchOne', file.length, pattern.length);
232
+ for (var fi = 0, pi = 0, fl = file.length, pl = pattern.length; fi < fl && pi < pl; fi++, pi++) {
233
+ this.debug('matchOne loop');
234
+ var p = pattern[pi];
235
+ var f = file[fi];
236
+ this.debug(pattern, p, f);
237
+ // should be impossible.
238
+ // some invalid regexp stuff in the set.
239
+ /* c8 ignore start */
240
+ if (p === false) {
241
+ return false;
242
+ }
243
+ /* c8 ignore stop */
244
+ if (p === GLOBSTAR) {
245
+ this.debug('GLOBSTAR', [pattern, p, f]);
246
+ // "**"
247
+ // a/**/b/**/c would match the following:
248
+ // a/b/x/y/z/c
249
+ // a/x/y/z/b/c
250
+ // a/b/x/b/x/c
251
+ // a/b/c
252
+ // To do this, take the rest of the pattern after
253
+ // the **, and see if it would match the file remainder.
254
+ // If so, return success.
255
+ // If not, the ** "swallows" a segment, and try again.
256
+ // This is recursively awful.
257
+ //
258
+ // a/**/b/**/c matching a/b/x/y/z/c
259
+ // - a matches a
260
+ // - doublestar
261
+ // - matchOne(b/x/y/z/c, b/**/c)
262
+ // - b matches b
263
+ // - doublestar
264
+ // - matchOne(x/y/z/c, c) -> no
265
+ // - matchOne(y/z/c, c) -> no
266
+ // - matchOne(z/c, c) -> no
267
+ // - matchOne(c, c) yes, hit
268
+ var fr = fi;
269
+ var pr = pi + 1;
270
+ if (pr === pl) {
271
+ this.debug('** at the end');
272
+ // a ** at the end will just swallow the rest.
273
+ // We have found a match.
274
+ // however, it will not swallow /.x, unless
275
+ // options.dot is set.
276
+ // . and .. are *never* matched by **, for explosively
277
+ // exponential reasons.
278
+ for (; fi < fl; fi++) {
279
+ if (file[fi] === '.' ||
280
+ file[fi] === '..' ||
281
+ (!options.dot && file[fi].charAt(0) === '.'))
282
+ return false;
283
+ }
284
+ return true;
285
+ }
286
+ // ok, let's see if we can swallow whatever we can.
287
+ while (fr < fl) {
288
+ var swallowee = file[fr];
289
+ this.debug('\nglobstar while', file, fr, pattern, pr, swallowee);
290
+ // XXX remove this slice. Just pass the start index.
291
+ if (this.matchOne(file.slice(fr), pattern.slice(pr), partial)) {
292
+ this.debug('globstar found match!', fr, fl, swallowee);
293
+ // found a match.
294
+ return true;
295
+ }
296
+ else {
297
+ // can't swallow "." or ".." ever.
298
+ // can only swallow ".foo" when explicitly asked.
299
+ if (swallowee === '.' ||
300
+ swallowee === '..' ||
301
+ (!options.dot && swallowee.charAt(0) === '.')) {
302
+ this.debug('dot detected!', file, fr, pattern, pr);
303
+ break;
304
+ }
305
+ // ** swallows a segment, and continue.
306
+ this.debug('globstar swallow a segment, and continue');
307
+ fr++;
308
+ }
309
+ }
310
+ // no match was found.
311
+ // However, in partial mode, we can't say this is necessarily over.
312
+ /* c8 ignore start */
313
+ if (partial) {
314
+ // ran out of file
315
+ this.debug('\n>>> no match, partial?', file, fr, pattern, pr);
316
+ if (fr === fl) {
317
+ return true;
318
+ }
319
+ }
320
+ /* c8 ignore stop */
321
+ return false;
322
+ }
323
+ // something other than **
324
+ // non-magic patterns just have to match exactly
325
+ // patterns with magic have been turned into regexps.
326
+ let hit;
327
+ if (typeof p === 'string') {
328
+ hit = f === p;
329
+ this.debug('string match', p, f, hit);
330
+ }
331
+ else {
332
+ hit = p.test(f);
333
+ this.debug('pattern match', p, f, hit);
334
+ }
335
+ if (!hit)
336
+ return false;
337
+ }
338
+ // Note: ending in / means that we'll get a final ""
339
+ // at the end of the pattern. This can only match a
340
+ // corresponding "" at the end of the file.
341
+ // If the file ends in /, then it can only match a
342
+ // a pattern that ends in /, unless the pattern just
343
+ // doesn't have any more for it. But, a/b/ should *not*
344
+ // match "a/b/*", even though "" matches against the
345
+ // [^/]*? pattern, except in partial mode, where it might
346
+ // simply not be reached yet.
347
+ // However, a/b/ should still satisfy a/*
348
+ // now either we fell off the end of the pattern, or we're done.
349
+ if (fi === fl && pi === pl) {
350
+ // ran out of pattern and filename at the same time.
351
+ // an exact hit!
352
+ return true;
353
+ }
354
+ else if (fi === fl) {
355
+ // ran out of file, but still had pattern left.
356
+ // this is ok if we're doing the match as part of
357
+ // a glob fs traversal.
358
+ return partial;
359
+ }
360
+ else if (pi === pl) {
361
+ // ran out of pattern, still have file left.
362
+ // this is only acceptable if we're on the very last
363
+ // empty segment of a file with a trailing slash.
364
+ // a/* should match a/b/
365
+ return fi === fl - 1 && file[fi] === '';
366
+ /* c8 ignore start */
367
+ }
368
+ else {
369
+ // should be unreachable.
370
+ throw new Error('wtf?');
371
+ }
372
+ /* c8 ignore stop */
373
+ }
374
+ braceExpand() {
375
+ return braceExpand(this.pattern, this.options);
376
+ }
377
+ parse(pattern, isSub) {
378
+ assertValidPattern(pattern);
379
+ const options = this.options;
380
+ // shortcuts
381
+ if (pattern === '**') {
382
+ if (!options.noglobstar)
383
+ return GLOBSTAR;
384
+ else
385
+ pattern = '*';
386
+ }
387
+ if (pattern === '')
388
+ return '';
389
+ let re = '';
390
+ let hasMagic = false;
391
+ let escaping = false;
392
+ // ? => one single character
393
+ const patternListStack = [];
394
+ const negativeLists = [];
395
+ let stateChar = false;
396
+ let inClass = false;
397
+ let reClassStart = -1;
398
+ let classStart = -1;
399
+ let cs;
400
+ let pl;
401
+ let sp;
402
+ // . and .. never match anything that doesn't start with .,
403
+ // even when options.dot is set.
404
+ const patternStart = pattern.charAt(0) === '.'
405
+ ? '' // anything
406
+ : // not (start or / followed by . or .. followed by / or end)
407
+ options.dot
408
+ ? '(?!(?:^|\\/)\\.{1,2}(?:$|\\/))'
409
+ : '(?!\\.)';
410
+ const clearStateChar = () => {
411
+ if (stateChar) {
412
+ // we had some state-tracking character
413
+ // that wasn't consumed by this pass.
414
+ switch (stateChar) {
415
+ case '*':
416
+ re += star;
417
+ hasMagic = true;
418
+ break;
419
+ case '?':
420
+ re += qmark;
421
+ hasMagic = true;
422
+ break;
423
+ default:
424
+ re += '\\' + stateChar;
425
+ break;
426
+ }
427
+ this.debug('clearStateChar %j %j', stateChar, re);
428
+ stateChar = false;
429
+ }
430
+ };
431
+ for (let i = 0, c; i < pattern.length && (c = pattern.charAt(i)); i++) {
432
+ this.debug('%s\t%s %s %j', pattern, i, re, c);
433
+ // skip over any that are escaped.
434
+ if (escaping) {
435
+ // completely not allowed, even escaped.
436
+ // should be impossible.
437
+ /* c8 ignore start */
438
+ if (c === '/') {
439
+ return false;
440
+ }
441
+ /* c8 ignore stop */
442
+ if (reSpecials[c]) {
443
+ re += '\\';
444
+ }
445
+ re += c;
446
+ escaping = false;
447
+ continue;
448
+ }
449
+ switch (c) {
450
+ // Should already be path-split by now.
451
+ /* c8 ignore start */
452
+ case '/': {
453
+ return false;
454
+ }
455
+ /* c8 ignore stop */
456
+ case '\\':
457
+ if (inClass && pattern.charAt(i + 1) === '-') {
458
+ re += c;
459
+ continue;
460
+ }
461
+ clearStateChar();
462
+ escaping = true;
463
+ continue;
464
+ // the various stateChar values
465
+ // for the "extglob" stuff.
466
+ case '?':
467
+ case '*':
468
+ case '+':
469
+ case '@':
470
+ case '!':
471
+ this.debug('%s\t%s %s %j <-- stateChar', pattern, i, re, c);
472
+ // all of those are literals inside a class, except that
473
+ // the glob [!a] means [^a] in regexp
474
+ if (inClass) {
475
+ this.debug(' in class');
476
+ if (c === '!' && i === classStart + 1)
477
+ c = '^';
478
+ re += c;
479
+ continue;
480
+ }
481
+ // if we already have a stateChar, then it means
482
+ // that there was something like ** or +? in there.
483
+ // Handle the stateChar, then proceed with this one.
484
+ this.debug('call clearStateChar %j', stateChar);
485
+ clearStateChar();
486
+ stateChar = c;
487
+ // if extglob is disabled, then +(asdf|foo) isn't a thing.
488
+ // just clear the statechar *now*, rather than even diving into
489
+ // the patternList stuff.
490
+ if (options.noext)
491
+ clearStateChar();
492
+ continue;
493
+ case '(':
494
+ if (inClass) {
495
+ re += '(';
496
+ continue;
497
+ }
498
+ if (!stateChar) {
499
+ re += '\\(';
500
+ continue;
501
+ }
502
+ patternListStack.push({
503
+ type: stateChar,
504
+ start: i - 1,
505
+ reStart: re.length,
506
+ open: plTypes[stateChar].open,
507
+ close: plTypes[stateChar].close,
508
+ });
509
+ // negation is (?:(?!js)[^/]*)
510
+ re += stateChar === '!' ? '(?:(?!(?:' : '(?:';
511
+ this.debug('plType %j %j', stateChar, re);
512
+ stateChar = false;
513
+ continue;
514
+ case ')':
515
+ if (inClass || !patternListStack.length) {
516
+ re += '\\)';
517
+ continue;
518
+ }
519
+ clearStateChar();
520
+ hasMagic = true;
521
+ pl = patternListStack.pop();
522
+ // negation is (?:(?!js)[^/]*)
523
+ // The others are (?:<pattern>)<type>
524
+ re += pl.close;
525
+ if (pl.type === '!') {
526
+ negativeLists.push(Object.assign(pl, { reEnd: re.length }));
527
+ }
528
+ continue;
529
+ case '|':
530
+ if (inClass || !patternListStack.length) {
531
+ re += '\\|';
532
+ continue;
533
+ }
534
+ clearStateChar();
535
+ re += '|';
536
+ continue;
537
+ // these are mostly the same in regexp and glob
538
+ case '[':
539
+ // swallow any state-tracking char before the [
540
+ clearStateChar();
541
+ if (inClass) {
542
+ re += '\\' + c;
543
+ continue;
544
+ }
545
+ inClass = true;
546
+ classStart = i;
547
+ reClassStart = re.length;
548
+ re += c;
549
+ continue;
550
+ case ']':
551
+ // a right bracket shall lose its special
552
+ // meaning and represent itself in
553
+ // a bracket expression if it occurs
554
+ // first in the list. -- POSIX.2 2.8.3.2
555
+ if (i === classStart + 1 || !inClass) {
556
+ re += '\\' + c;
557
+ continue;
558
+ }
559
+ // split where the last [ was, make sure we don't have
560
+ // an invalid re. if so, re-walk the contents of the
561
+ // would-be class to re-translate any characters that
562
+ // were passed through as-is
563
+ // TODO: It would probably be faster to determine this
564
+ // without a try/catch and a new RegExp, but it's tricky
565
+ // to do safely. For now, this is safe and works.
566
+ cs = pattern.substring(classStart + 1, i);
567
+ try {
568
+ RegExp('[' + braExpEscape(charUnescape(cs)) + ']');
569
+ // looks good, finish up the class.
570
+ re += c;
571
+ }
572
+ catch (er) {
573
+ // out of order ranges in JS are errors, but in glob syntax,
574
+ // they're just a range that matches nothing.
575
+ re = re.substring(0, reClassStart) + '(?:$.)'; // match nothing ever
576
+ }
577
+ hasMagic = true;
578
+ inClass = false;
579
+ continue;
580
+ default:
581
+ // swallow any state char that wasn't consumed
582
+ clearStateChar();
583
+ if (reSpecials[c] && !(c === '^' && inClass)) {
584
+ re += '\\';
585
+ }
586
+ re += c;
587
+ break;
588
+ } // switch
589
+ } // for
590
+ // handle the case where we left a class open.
591
+ // "[abc" is valid, equivalent to "\[abc"
592
+ if (inClass) {
593
+ // split where the last [ was, and escape it
594
+ // this is a huge pita. We now have to re-walk
595
+ // the contents of the would-be class to re-translate
596
+ // any characters that were passed through as-is
597
+ cs = pattern.slice(classStart + 1);
598
+ sp = this.parse(cs, SUBPARSE);
599
+ re = re.substring(0, reClassStart) + '\\[' + sp[0];
600
+ hasMagic = hasMagic || sp[1];
601
+ }
602
+ // handle the case where we had a +( thing at the *end*
603
+ // of the pattern.
604
+ // each pattern list stack adds 3 chars, and we need to go through
605
+ // and escape any | chars that were passed through as-is for the regexp.
606
+ // Go through and escape them, taking care not to double-escape any
607
+ // | chars that were already escaped.
608
+ for (pl = patternListStack.pop(); pl; pl = patternListStack.pop()) {
609
+ let tail;
610
+ tail = re.slice(pl.reStart + pl.open.length);
611
+ this.debug('setting tail', re, pl);
612
+ // maybe some even number of \, then maybe 1 \, followed by a |
613
+ tail = tail.replace(/((?:\\{2}){0,64})(\\?)\|/g, (_, $1, $2) => {
614
+ if (!$2) {
615
+ // the | isn't already escaped, so escape it.
616
+ $2 = '\\';
617
+ // should already be done
618
+ /* c8 ignore start */
619
+ }
620
+ /* c8 ignore stop */
621
+ // need to escape all those slashes *again*, without escaping the
622
+ // one that we need for escaping the | character. As it works out,
623
+ // escaping an even number of slashes can be done by simply repeating
624
+ // it exactly after itself. That's why this trick works.
625
+ //
626
+ // I am sorry that you have to see this.
627
+ return $1 + $1 + $2 + '|';
628
+ });
629
+ this.debug('tail=%j\n %s', tail, tail, pl, re);
630
+ const t = pl.type === '*' ? star : pl.type === '?' ? qmark : '\\' + pl.type;
631
+ hasMagic = true;
632
+ re = re.slice(0, pl.reStart) + t + '\\(' + tail;
633
+ }
634
+ // handle trailing things that only matter at the very end.
635
+ clearStateChar();
636
+ if (escaping) {
637
+ // trailing \\
638
+ re += '\\\\';
639
+ }
640
+ // only need to apply the nodot start if the re starts with
641
+ // something that could conceivably capture a dot
642
+ const addPatternStart = addPatternStartSet[re.charAt(0)];
643
+ // Hack to work around lack of negative lookbehind in JS
644
+ // A pattern like: *.!(x).!(y|z) needs to ensure that a name
645
+ // like 'a.xyz.yz' doesn't match. So, the first negative
646
+ // lookahead, has to look ALL the way ahead, to the end of
647
+ // the pattern.
648
+ for (let n = negativeLists.length - 1; n > -1; n--) {
649
+ const nl = negativeLists[n];
650
+ const nlBefore = re.slice(0, nl.reStart);
651
+ const nlFirst = re.slice(nl.reStart, nl.reEnd - 8);
652
+ let nlAfter = re.slice(nl.reEnd);
653
+ const nlLast = re.slice(nl.reEnd - 8, nl.reEnd) + nlAfter;
654
+ // Handle nested stuff like *(*.js|!(*.json)), where open parens
655
+ // mean that we should *not* include the ) in the bit that is considered
656
+ // "after" the negated section.
657
+ const openParensBefore = nlBefore.split('(').length - 1;
658
+ let cleanAfter = nlAfter;
659
+ for (let i = 0; i < openParensBefore; i++) {
660
+ cleanAfter = cleanAfter.replace(/\)[+*?]?/, '');
661
+ }
662
+ nlAfter = cleanAfter;
663
+ const dollar = nlAfter === '' && isSub !== SUBPARSE ? '(?:$|\\/)' : '';
664
+ re = nlBefore + nlFirst + nlAfter + dollar + nlLast;
665
+ }
666
+ // if the re is not "" at this point, then we need to make sure
667
+ // it doesn't match against an empty path part.
668
+ // Otherwise a/* will match a/, which it should not.
669
+ if (re !== '' && hasMagic) {
670
+ re = '(?=.)' + re;
671
+ }
672
+ if (addPatternStart) {
673
+ re = patternStart + re;
674
+ }
675
+ // parsing just a piece of a larger pattern.
676
+ if (isSub === SUBPARSE) {
677
+ return [re, hasMagic];
678
+ }
679
+ // if it's nocase, and the lcase/uppercase don't match, it's magic
680
+ if (options.nocase && !hasMagic) {
681
+ hasMagic = pattern.toUpperCase() !== pattern.toLowerCase();
682
+ }
683
+ // skip the regexp for non-magical patterns
684
+ // unescape anything in it, though, so that it'll be
685
+ // an exact match against a file etc.
686
+ if (!hasMagic) {
687
+ return globUnescape(pattern);
688
+ }
689
+ const flags = options.nocase ? 'i' : '';
690
+ try {
691
+ return Object.assign(new RegExp('^' + re + '$', flags), {
692
+ _glob: pattern,
693
+ _src: re,
694
+ });
695
+ /* c8 ignore start */
696
+ }
697
+ catch (er) {
698
+ // should be impossible
699
+ // If it was an invalid regular expression, then it can't match
700
+ // anything. This trick looks for a character after the end of
701
+ // the string, which is of course impossible, except in multi-line
702
+ // mode, but it's not a /m regex.
703
+ this.debug('invalid regexp', er);
704
+ return new RegExp('$.');
705
+ }
706
+ /* c8 ignore stop */
707
+ }
708
+ makeRe() {
709
+ if (this.regexp || this.regexp === false)
710
+ return this.regexp;
711
+ // at this point, this.set is a 2d array of partial
712
+ // pattern strings, or "**".
713
+ //
714
+ // It's better to use .match(). This function shouldn't
715
+ // be used, really, but it's pretty convenient sometimes,
716
+ // when you just want to work with a regex.
717
+ const set = this.set;
718
+ if (!set.length) {
719
+ this.regexp = false;
720
+ return this.regexp;
721
+ }
722
+ const options = this.options;
723
+ const twoStar = options.noglobstar
724
+ ? star
725
+ : options.dot
726
+ ? twoStarDot
727
+ : twoStarNoDot;
728
+ const flags = options.nocase ? 'i' : '';
729
+ // coalesce globstars and regexpify non-globstar patterns
730
+ // if it's the only item, then we just do one twoStar
731
+ // if it's the first, and there are more, prepend (\/|twoStar\/)? to next
732
+ // if it's the last, append (\/twoStar|) to previous
733
+ // if it's in the middle, append (\/|\/twoStar\/) to previous
734
+ // then filter out GLOBSTAR symbols
735
+ let re = set
736
+ .map(pattern => {
737
+ const pp = pattern
738
+ .map(p => typeof p === 'string'
739
+ ? regExpEscape(p)
740
+ : p === GLOBSTAR
741
+ ? GLOBSTAR
742
+ : p._src)
743
+ .reduce((set, p) => {
744
+ if ((set[set.length - 1] === GLOBSTAR && p === GLOBSTAR) ||
745
+ p === undefined) {
746
+ return set;
747
+ }
748
+ set.push(p);
749
+ return set;
750
+ }, []);
751
+ pp.forEach((p, i) => {
752
+ const next = pp[i + 1];
753
+ const prev = pp[i - 1];
754
+ if (p !== GLOBSTAR || prev === GLOBSTAR) {
755
+ return;
756
+ }
757
+ if (prev === undefined) {
758
+ if (next !== undefined && next !== GLOBSTAR) {
759
+ pp[i + 1] = '(?:\\/|' + twoStar + '\\/)?' + next;
760
+ }
761
+ else {
762
+ pp[i] = twoStar;
763
+ }
764
+ }
765
+ else if (next === undefined) {
766
+ pp[i - 1] = prev + '(?:\\/|' + twoStar + ')?';
767
+ }
768
+ else if (next !== GLOBSTAR) {
769
+ pp[i - 1] = prev + '(?:\\/|\\/' + twoStar + '\\/)' + next;
770
+ pp[i + 1] = GLOBSTAR;
771
+ }
772
+ });
773
+ return pp.filter(p => p !== GLOBSTAR).join('/');
774
+ })
775
+ .join('|');
776
+ // must match entire pattern
777
+ // ending in a * or ** will make it less strict.
778
+ re = '^(?:' + re + ')$';
779
+ // can match anything, as long as it's not this.
780
+ if (this.negate)
781
+ re = '^(?!' + re + ').*$';
782
+ try {
783
+ this.regexp = new RegExp(re, flags);
784
+ /* c8 ignore start */
785
+ }
786
+ catch (ex) {
787
+ // should be impossible
788
+ this.regexp = false;
789
+ }
790
+ /* c8 ignore stop */
791
+ return this.regexp;
792
+ }
793
+ match(f, partial = this.partial) {
794
+ this.debug('match', f, this.pattern);
795
+ // short-circuit in the case of busted things.
796
+ // comments, etc.
797
+ if (this.comment) {
798
+ return false;
799
+ }
800
+ if (this.empty) {
801
+ return f === '';
802
+ }
803
+ if (f === '/' && partial)
804
+ return true;
805
+ const options = this.options;
806
+ // windows: need to use /, not \
807
+ if (path.sep !== '/') {
808
+ f = f.split(path.sep).join('/');
809
+ }
810
+ // treat the test path as a set of pathparts.
811
+ const ff = f.split(slashSplit);
812
+ this.debug(this.pattern, 'split', ff);
813
+ // just ONE of the pattern sets in this.set needs to match
814
+ // in order for it to be valid. If negating, then just one
815
+ // match means that we have failed.
816
+ // Either way, return on the first hit.
817
+ const set = this.set;
818
+ this.debug(this.pattern, 'set', set);
819
+ // Find the basename of the path by looking for the last non-empty segment
820
+ let filename = ff[ff.length - 1];
821
+ if (!filename) {
822
+ for (let i = ff.length - 2; !filename && i >= 0; i--) {
823
+ filename = ff[i];
824
+ }
825
+ }
826
+ for (let i = 0; i < set.length; i++) {
827
+ const pattern = set[i];
828
+ let file = ff;
829
+ if (options.matchBase && pattern.length === 1) {
830
+ file = [filename];
831
+ }
832
+ const hit = this.matchOne(file, pattern, partial);
833
+ if (hit) {
834
+ if (options.flipNegate)
835
+ return true;
836
+ return !this.negate;
837
+ }
838
+ }
839
+ // didn't get any hits. this is success if it's a negative
840
+ // pattern, failure otherwise.
841
+ if (options.flipNegate)
842
+ return false;
843
+ return this.negate;
844
+ }
845
+ static defaults(def) {
846
+ return minimatch.defaults(def).Minimatch;
847
+ }
848
+ }
849
+ minimatch.Minimatch = Minimatch;
850
+ //# sourceMappingURL=index.js.map