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