-
Notifications
You must be signed in to change notification settings - Fork 30.9k
/
Copy pathindex.js
1611 lines (1603 loc) Β· 54.3 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"use strict";
var __getOwnPropNames = Object.getOwnPropertyNames;
var __commonJS = (cb, mod) => function __require() {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
};
// node_modules/balanced-match/index.js
var require_balanced_match = __commonJS({
"node_modules/balanced-match/index.js"(exports2, module2) {
"use strict";
module2.exports = balanced;
function balanced(a, b, str) {
if (a instanceof RegExp) a = maybeMatch(a, str);
if (b instanceof RegExp) b = maybeMatch(b, str);
var r = range(a, b, str);
return r && {
start: r[0],
end: r[1],
pre: str.slice(0, r[0]),
body: str.slice(r[0] + a.length, r[1]),
post: str.slice(r[1] + b.length)
};
}
function maybeMatch(reg, str) {
var m = str.match(reg);
return m ? m[0] : null;
}
balanced.range = range;
function range(a, b, str) {
var begs, beg, left, right, result;
var ai = str.indexOf(a);
var bi = str.indexOf(b, ai + 1);
var i = ai;
if (ai >= 0 && bi > 0) {
if (a === b) {
return [ai, bi];
}
begs = [];
left = str.length;
while (i >= 0 && !result) {
if (i == ai) {
begs.push(i);
ai = str.indexOf(a, i + 1);
} else if (begs.length == 1) {
result = [begs.pop(), bi];
} else {
beg = begs.pop();
if (beg < left) {
left = beg;
right = bi;
}
bi = str.indexOf(b, i + 1);
}
i = ai < bi && ai >= 0 ? ai : bi;
}
if (begs.length) {
result = [left, right];
}
}
return result;
}
}
});
// node_modules/brace-expansion/index.js
var require_brace_expansion = __commonJS({
"node_modules/brace-expansion/index.js"(exports2, module2) {
var balanced = require_balanced_match();
module2.exports = expandTop;
var escSlash = "\0SLASH" + Math.random() + "\0";
var escOpen = "\0OPEN" + Math.random() + "\0";
var escClose = "\0CLOSE" + Math.random() + "\0";
var escComma = "\0COMMA" + Math.random() + "\0";
var escPeriod = "\0PERIOD" + Math.random() + "\0";
function numeric(str) {
return parseInt(str, 10) == str ? parseInt(str, 10) : str.charCodeAt(0);
}
function escapeBraces(str) {
return str.split("\\\\").join(escSlash).split("\\{").join(escOpen).split("\\}").join(escClose).split("\\,").join(escComma).split("\\.").join(escPeriod);
}
function unescapeBraces(str) {
return str.split(escSlash).join("\\").split(escOpen).join("{").split(escClose).join("}").split(escComma).join(",").split(escPeriod).join(".");
}
function parseCommaParts(str) {
if (!str)
return [""];
var parts = [];
var m = balanced("{", "}", str);
if (!m)
return str.split(",");
var pre = m.pre;
var body = m.body;
var post = m.post;
var p = pre.split(",");
p[p.length - 1] += "{" + body + "}";
var postParts = parseCommaParts(post);
if (post.length) {
p[p.length - 1] += postParts.shift();
p.push.apply(p, postParts);
}
parts.push.apply(parts, p);
return parts;
}
function expandTop(str) {
if (!str)
return [];
if (str.substr(0, 2) === "{}") {
str = "\\{\\}" + str.substr(2);
}
return expand(escapeBraces(str), true).map(unescapeBraces);
}
function embrace(str) {
return "{" + str + "}";
}
function isPadded(el) {
return /^-?0\d/.test(el);
}
function lte(i, y) {
return i <= y;
}
function gte(i, y) {
return i >= y;
}
function expand(str, isTop) {
var expansions = [];
var m = balanced("{", "}", str);
if (!m) return [str];
var pre = m.pre;
var post = m.post.length ? expand(m.post, false) : [""];
if (/\$$/.test(m.pre)) {
for (var k = 0; k < post.length; k++) {
var expansion = pre + "{" + m.body + "}" + post[k];
expansions.push(expansion);
}
} else {
var isNumericSequence = /^-?\d+\.\.-?\d+(?:\.\.-?\d+)?$/.test(m.body);
var isAlphaSequence = /^[a-zA-Z]\.\.[a-zA-Z](?:\.\.-?\d+)?$/.test(m.body);
var isSequence = isNumericSequence || isAlphaSequence;
var isOptions = m.body.indexOf(",") >= 0;
if (!isSequence && !isOptions) {
if (m.post.match(/,.*\}/)) {
str = m.pre + "{" + m.body + escClose + m.post;
return expand(str);
}
return [str];
}
var n;
if (isSequence) {
n = m.body.split(/\.\./);
} else {
n = parseCommaParts(m.body);
if (n.length === 1) {
n = expand(n[0], false).map(embrace);
if (n.length === 1) {
return post.map(function(p) {
return m.pre + n[0] + p;
});
}
}
}
var N;
if (isSequence) {
var x = numeric(n[0]);
var y = numeric(n[1]);
var width = Math.max(n[0].length, n[1].length);
var incr = n.length == 3 ? Math.abs(numeric(n[2])) : 1;
var test = lte;
var reverse = y < x;
if (reverse) {
incr *= -1;
test = gte;
}
var pad = n.some(isPadded);
N = [];
for (var i = x; test(i, y); i += incr) {
var c;
if (isAlphaSequence) {
c = String.fromCharCode(i);
if (c === "\\")
c = "";
} else {
c = String(i);
if (pad) {
var need = width - c.length;
if (need > 0) {
var z = new Array(need + 1).join("0");
if (i < 0)
c = "-" + z + c.slice(1);
else
c = z + c;
}
}
}
N.push(c);
}
} else {
N = [];
for (var j = 0; j < n.length; j++) {
N.push.apply(N, expand(n[j], false));
}
}
for (var j = 0; j < N.length; j++) {
for (var k = 0; k < post.length; k++) {
var expansion = pre + N[j] + post[k];
if (!isTop || isSequence || expansion)
expansions.push(expansion);
}
}
}
return expansions;
}
}
});
// dist/commonjs/assert-valid-pattern.js
var require_assert_valid_pattern = __commonJS({
"dist/commonjs/assert-valid-pattern.js"(exports2) {
"use strict";
Object.defineProperty(exports2, "__esModule", { value: true });
exports2.assertValidPattern = void 0;
var MAX_PATTERN_LENGTH = 1024 * 64;
var assertValidPattern = (pattern) => {
if (typeof pattern !== "string") {
throw new TypeError("invalid pattern");
}
if (pattern.length > MAX_PATTERN_LENGTH) {
throw new TypeError("pattern is too long");
}
};
exports2.assertValidPattern = assertValidPattern;
}
});
// dist/commonjs/brace-expressions.js
var require_brace_expressions = __commonJS({
"dist/commonjs/brace-expressions.js"(exports2) {
"use strict";
Object.defineProperty(exports2, "__esModule", { value: true });
exports2.parseClass = void 0;
var posixClasses = {
"[:alnum:]": ["\\p{L}\\p{Nl}\\p{Nd}", true],
"[:alpha:]": ["\\p{L}\\p{Nl}", true],
"[:ascii:]": ["\\x00-\\x7f", false],
"[:blank:]": ["\\p{Zs}\\t", true],
"[:cntrl:]": ["\\p{Cc}", true],
"[:digit:]": ["\\p{Nd}", true],
"[:graph:]": ["\\p{Z}\\p{C}", true, true],
"[:lower:]": ["\\p{Ll}", true],
"[:print:]": ["\\p{C}", true],
"[:punct:]": ["\\p{P}", true],
"[:space:]": ["\\p{Z}\\t\\r\\n\\v\\f", true],
"[:upper:]": ["\\p{Lu}", true],
"[:word:]": ["\\p{L}\\p{Nl}\\p{Nd}\\p{Pc}", true],
"[:xdigit:]": ["A-Fa-f0-9", false]
};
var braceEscape = (s) => s.replace(/[[\]\\-]/g, "\\$&");
var regexpEscape = (s) => s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
var rangesToString = (ranges) => ranges.join("");
var parseClass = (glob, position) => {
const pos = position;
if (glob.charAt(pos) !== "[") {
throw new Error("not in a brace expression");
}
const ranges = [];
const negs = [];
let i = pos + 1;
let sawStart = false;
let uflag = false;
let escaping = false;
let negate = false;
let endPos = pos;
let rangeStart = "";
WHILE: while (i < glob.length) {
const c = glob.charAt(i);
if ((c === "!" || c === "^") && i === pos + 1) {
negate = true;
i++;
continue;
}
if (c === "]" && sawStart && !escaping) {
endPos = i + 1;
break;
}
sawStart = true;
if (c === "\\") {
if (!escaping) {
escaping = true;
i++;
continue;
}
}
if (c === "[" && !escaping) {
for (const [cls, [unip, u, neg]] of Object.entries(posixClasses)) {
if (glob.startsWith(cls, i)) {
if (rangeStart) {
return ["$.", false, glob.length - pos, true];
}
i += cls.length;
if (neg)
negs.push(unip);
else
ranges.push(unip);
uflag = uflag || u;
continue WHILE;
}
}
}
escaping = false;
if (rangeStart) {
if (c > rangeStart) {
ranges.push(braceEscape(rangeStart) + "-" + braceEscape(c));
} else if (c === rangeStart) {
ranges.push(braceEscape(c));
}
rangeStart = "";
i++;
continue;
}
if (glob.startsWith("-]", i + 1)) {
ranges.push(braceEscape(c + "-"));
i += 2;
continue;
}
if (glob.startsWith("-", i + 1)) {
rangeStart = c;
i += 2;
continue;
}
ranges.push(braceEscape(c));
i++;
}
if (endPos < i) {
return ["", false, 0, false];
}
if (!ranges.length && !negs.length) {
return ["$.", false, glob.length - pos, true];
}
if (negs.length === 0 && ranges.length === 1 && /^\\?.$/.test(ranges[0]) && !negate) {
const r = ranges[0].length === 2 ? ranges[0].slice(-1) : ranges[0];
return [regexpEscape(r), false, endPos - pos, false];
}
const sranges = "[" + (negate ? "^" : "") + rangesToString(ranges) + "]";
const snegs = "[" + (negate ? "" : "^") + rangesToString(negs) + "]";
const comb = ranges.length && negs.length ? "(" + sranges + "|" + snegs + ")" : ranges.length ? sranges : snegs;
return [comb, uflag, endPos - pos, true];
};
exports2.parseClass = parseClass;
}
});
// dist/commonjs/unescape.js
var require_unescape = __commonJS({
"dist/commonjs/unescape.js"(exports2) {
"use strict";
Object.defineProperty(exports2, "__esModule", { value: true });
exports2.unescape = void 0;
var unescape = (s, { windowsPathsNoEscape = false } = {}) => {
return windowsPathsNoEscape ? s.replace(/\[([^\/\\])\]/g, "$1") : s.replace(/((?!\\).|^)\[([^\/\\])\]/g, "$1$2").replace(/\\([^\/])/g, "$1");
};
exports2.unescape = unescape;
}
});
// dist/commonjs/ast.js
var require_ast = __commonJS({
"dist/commonjs/ast.js"(exports2) {
"use strict";
Object.defineProperty(exports2, "__esModule", { value: true });
exports2.AST = void 0;
var brace_expressions_js_1 = require_brace_expressions();
var unescape_js_12 = require_unescape();
var types = /* @__PURE__ */ new Set(["!", "?", "+", "*", "@"]);
var isExtglobType = (c) => types.has(c);
var startNoTraversal = "(?!(?:^|/)\\.\\.?(?:$|/))";
var startNoDot = "(?!\\.)";
var addPatternStart = /* @__PURE__ */ new Set(["[", "."]);
var justDots = /* @__PURE__ */ new Set(["..", "."]);
var reSpecials = new Set("().*{}+?[]^$\\!");
var regExpEscape2 = (s) => s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
var qmark2 = "[^/]";
var star2 = qmark2 + "*?";
var starNoEmpty = qmark2 + "+?";
var AST = class _AST {
type;
#root;
#hasMagic;
#uflag = false;
#parts = [];
#parent;
#parentIndex;
#negs;
#filledNegs = false;
#options;
#toString;
// set to true if it's an extglob with no children
// (which really means one child of '')
#emptyExt = false;
constructor(type, parent, options = {}) {
this.type = type;
if (type)
this.#hasMagic = true;
this.#parent = parent;
this.#root = this.#parent ? this.#parent.#root : this;
this.#options = this.#root === this ? options : this.#root.#options;
this.#negs = this.#root === this ? [] : this.#root.#negs;
if (type === "!" && !this.#root.#filledNegs)
this.#negs.push(this);
this.#parentIndex = this.#parent ? this.#parent.#parts.length : 0;
}
get hasMagic() {
if (this.#hasMagic !== void 0)
return this.#hasMagic;
for (const p of this.#parts) {
if (typeof p === "string")
continue;
if (p.type || p.hasMagic)
return this.#hasMagic = true;
}
return this.#hasMagic;
}
// reconstructs the pattern
toString() {
if (this.#toString !== void 0)
return this.#toString;
if (!this.type) {
return this.#toString = this.#parts.map((p) => String(p)).join("");
} else {
return this.#toString = this.type + "(" + this.#parts.map((p) => String(p)).join("|") + ")";
}
}
#fillNegs() {
if (this !== this.#root)
throw new Error("should only call on root");
if (this.#filledNegs)
return this;
this.toString();
this.#filledNegs = true;
let n;
while (n = this.#negs.pop()) {
if (n.type !== "!")
continue;
let p = n;
let pp = p.#parent;
while (pp) {
for (let i = p.#parentIndex + 1; !pp.type && i < pp.#parts.length; i++) {
for (const part of n.#parts) {
if (typeof part === "string") {
throw new Error("string part in extglob AST??");
}
part.copyIn(pp.#parts[i]);
}
}
p = pp;
pp = p.#parent;
}
}
return this;
}
push(...parts) {
for (const p of parts) {
if (p === "")
continue;
if (typeof p !== "string" && !(p instanceof _AST && p.#parent === this)) {
throw new Error("invalid part: " + p);
}
this.#parts.push(p);
}
}
toJSON() {
const ret = this.type === null ? this.#parts.slice().map((p) => typeof p === "string" ? p : p.toJSON()) : [this.type, ...this.#parts.map((p) => p.toJSON())];
if (this.isStart() && !this.type)
ret.unshift([]);
if (this.isEnd() && (this === this.#root || this.#root.#filledNegs && this.#parent?.type === "!")) {
ret.push({});
}
return ret;
}
isStart() {
if (this.#root === this)
return true;
if (!this.#parent?.isStart())
return false;
if (this.#parentIndex === 0)
return true;
const p = this.#parent;
for (let i = 0; i < this.#parentIndex; i++) {
const pp = p.#parts[i];
if (!(pp instanceof _AST && pp.type === "!")) {
return false;
}
}
return true;
}
isEnd() {
if (this.#root === this)
return true;
if (this.#parent?.type === "!")
return true;
if (!this.#parent?.isEnd())
return false;
if (!this.type)
return this.#parent?.isEnd();
const pl = this.#parent ? this.#parent.#parts.length : 0;
return this.#parentIndex === pl - 1;
}
copyIn(part) {
if (typeof part === "string")
this.push(part);
else
this.push(part.clone(this));
}
clone(parent) {
const c = new _AST(this.type, parent);
for (const p of this.#parts) {
c.copyIn(p);
}
return c;
}
static #parseAST(str, ast, pos, opt) {
let escaping = false;
let inBrace = false;
let braceStart = -1;
let braceNeg = false;
if (ast.type === null) {
let i2 = pos;
let acc2 = "";
while (i2 < str.length) {
const c = str.charAt(i2++);
if (escaping || c === "\\") {
escaping = !escaping;
acc2 += c;
continue;
}
if (inBrace) {
if (i2 === braceStart + 1) {
if (c === "^" || c === "!") {
braceNeg = true;
}
} else if (c === "]" && !(i2 === braceStart + 2 && braceNeg)) {
inBrace = false;
}
acc2 += c;
continue;
} else if (c === "[") {
inBrace = true;
braceStart = i2;
braceNeg = false;
acc2 += c;
continue;
}
if (!opt.noext && isExtglobType(c) && str.charAt(i2) === "(") {
ast.push(acc2);
acc2 = "";
const ext2 = new _AST(c, ast);
i2 = _AST.#parseAST(str, ext2, i2, opt);
ast.push(ext2);
continue;
}
acc2 += c;
}
ast.push(acc2);
return i2;
}
let i = pos + 1;
let part = new _AST(null, ast);
const parts = [];
let acc = "";
while (i < str.length) {
const c = str.charAt(i++);
if (escaping || c === "\\") {
escaping = !escaping;
acc += c;
continue;
}
if (inBrace) {
if (i === braceStart + 1) {
if (c === "^" || c === "!") {
braceNeg = true;
}
} else if (c === "]" && !(i === braceStart + 2 && braceNeg)) {
inBrace = false;
}
acc += c;
continue;
} else if (c === "[") {
inBrace = true;
braceStart = i;
braceNeg = false;
acc += c;
continue;
}
if (isExtglobType(c) && str.charAt(i) === "(") {
part.push(acc);
acc = "";
const ext2 = new _AST(c, part);
part.push(ext2);
i = _AST.#parseAST(str, ext2, i, opt);
continue;
}
if (c === "|") {
part.push(acc);
acc = "";
parts.push(part);
part = new _AST(null, ast);
continue;
}
if (c === ")") {
if (acc === "" && ast.#parts.length === 0) {
ast.#emptyExt = true;
}
part.push(acc);
acc = "";
ast.push(...parts, part);
return i;
}
acc += c;
}
ast.type = null;
ast.#hasMagic = void 0;
ast.#parts = [str.substring(pos - 1)];
return i;
}
static fromGlob(pattern, options = {}) {
const ast = new _AST(null, void 0, options);
_AST.#parseAST(pattern, ast, 0, options);
return ast;
}
// returns the regular expression if there's magic, or the unescaped
// string if not.
toMMPattern() {
if (this !== this.#root)
return this.#root.toMMPattern();
const glob = this.toString();
const [re, body, hasMagic, uflag] = this.toRegExpSource();
const anyMagic = hasMagic || this.#hasMagic || this.#options.nocase && !this.#options.nocaseMagicOnly && glob.toUpperCase() !== glob.toLowerCase();
if (!anyMagic) {
return body;
}
const flags = (this.#options.nocase ? "i" : "") + (uflag ? "u" : "");
return Object.assign(new RegExp(`^${re}$`, flags), {
_src: re,
_glob: glob
});
}
get options() {
return this.#options;
}
// returns the string match, the regexp source, whether there's magic
// in the regexp (so a regular expression is required) and whether or
// not the uflag is needed for the regular expression (for posix classes)
// TODO: instead of injecting the start/end at this point, just return
// the BODY of the regexp, along with the start/end portions suitable
// for binding the start/end in either a joined full-path makeRe context
// (where we bind to (^|/), or a standalone matchPart context (where
// we bind to ^, and not /). Otherwise slashes get duped!
//
// In part-matching mode, the start is:
// - if not isStart: nothing
// - if traversal possible, but not allowed: ^(?!\.\.?$)
// - if dots allowed or not possible: ^
// - if dots possible and not allowed: ^(?!\.)
// end is:
// - if not isEnd(): nothing
// - else: $
//
// In full-path matching mode, we put the slash at the START of the
// pattern, so start is:
// - if first pattern: same as part-matching mode
// - if not isStart(): nothing
// - if traversal possible, but not allowed: /(?!\.\.?(?:$|/))
// - if dots allowed or not possible: /
// - if dots possible and not allowed: /(?!\.)
// end is:
// - if last pattern, same as part-matching mode
// - else nothing
//
// Always put the (?:$|/) on negated tails, though, because that has to be
// there to bind the end of the negated pattern portion, and it's easier to
// just stick it in now rather than try to inject it later in the middle of
// the pattern.
//
// We can just always return the same end, and leave it up to the caller
// to know whether it's going to be used joined or in parts.
// And, if the start is adjusted slightly, can do the same there:
// - if not isStart: nothing
// - if traversal possible, but not allowed: (?:/|^)(?!\.\.?$)
// - if dots allowed or not possible: (?:/|^)
// - if dots possible and not allowed: (?:/|^)(?!\.)
//
// But it's better to have a simpler binding without a conditional, for
// performance, so probably better to return both start options.
//
// Then the caller just ignores the end if it's not the first pattern,
// and the start always gets applied.
//
// But that's always going to be $ if it's the ending pattern, or nothing,
// so the caller can just attach $ at the end of the pattern when building.
//
// So the todo is:
// - better detect what kind of start is needed
// - return both flavors of starting pattern
// - attach $ at the end of the pattern when creating the actual RegExp
//
// Ah, but wait, no, that all only applies to the root when the first pattern
// is not an extglob. If the first pattern IS an extglob, then we need all
// that dot prevention biz to live in the extglob portions, because eg
// +(*|.x*) can match .xy but not .yx.
//
// So, return the two flavors if it's #root and the first child is not an
// AST, otherwise leave it to the child AST to handle it, and there,
// use the (?:^|/) style of start binding.
//
// Even simplified further:
// - Since the start for a join is eg /(?!\.) and the start for a part
// is ^(?!\.), we can just prepend (?!\.) to the pattern (either root
// or start or whatever) and prepend ^ or / at the Regexp construction.
toRegExpSource(allowDot) {
const dot = allowDot ?? !!this.#options.dot;
if (this.#root === this)
this.#fillNegs();
if (!this.type) {
const noEmpty = this.isStart() && this.isEnd();
const src = this.#parts.map((p) => {
const [re, _, hasMagic, uflag] = typeof p === "string" ? _AST.#parseGlob(p, this.#hasMagic, noEmpty) : p.toRegExpSource(allowDot);
this.#hasMagic = this.#hasMagic || hasMagic;
this.#uflag = this.#uflag || uflag;
return re;
}).join("");
let start2 = "";
if (this.isStart()) {
if (typeof this.#parts[0] === "string") {
const dotTravAllowed = this.#parts.length === 1 && justDots.has(this.#parts[0]);
if (!dotTravAllowed) {
const aps = addPatternStart;
const needNoTrav = (
// dots are allowed, and the pattern starts with [ or .
dot && aps.has(src.charAt(0)) || // the pattern starts with \., and then [ or .
src.startsWith("\\.") && aps.has(src.charAt(2)) || // the pattern starts with \.\., and then [ or .
src.startsWith("\\.\\.") && aps.has(src.charAt(4))
);
const needNoDot = !dot && !allowDot && aps.has(src.charAt(0));
start2 = needNoTrav ? startNoTraversal : needNoDot ? startNoDot : "";
}
}
}
let end = "";
if (this.isEnd() && this.#root.#filledNegs && this.#parent?.type === "!") {
end = "(?:$|\\/)";
}
const final2 = start2 + src + end;
return [
final2,
(0, unescape_js_12.unescape)(src),
this.#hasMagic = !!this.#hasMagic,
this.#uflag
];
}
const repeated = this.type === "*" || this.type === "+";
const start = this.type === "!" ? "(?:(?!(?:" : "(?:";
let body = this.#partsToRegExp(dot);
if (this.isStart() && this.isEnd() && !body && this.type !== "!") {
const s = this.toString();
this.#parts = [s];
this.type = null;
this.#hasMagic = void 0;
return [s, (0, unescape_js_12.unescape)(this.toString()), false, false];
}
let bodyDotAllowed = !repeated || allowDot || dot || !startNoDot ? "" : this.#partsToRegExp(true);
if (bodyDotAllowed === body) {
bodyDotAllowed = "";
}
if (bodyDotAllowed) {
body = `(?:${body})(?:${bodyDotAllowed})*?`;
}
let final = "";
if (this.type === "!" && this.#emptyExt) {
final = (this.isStart() && !dot ? startNoDot : "") + starNoEmpty;
} else {
const close = this.type === "!" ? (
// !() must match something,but !(x) can match ''
"))" + (this.isStart() && !dot && !allowDot ? startNoDot : "") + star2 + ")"
) : this.type === "@" ? ")" : this.type === "?" ? ")?" : this.type === "+" && bodyDotAllowed ? ")" : this.type === "*" && bodyDotAllowed ? `)?` : `)${this.type}`;
final = start + body + close;
}
return [
final,
(0, unescape_js_12.unescape)(body),
this.#hasMagic = !!this.#hasMagic,
this.#uflag
];
}
#partsToRegExp(dot) {
return this.#parts.map((p) => {
if (typeof p === "string") {
throw new Error("string type in extglob ast??");
}
const [re, _, _hasMagic, uflag] = p.toRegExpSource(dot);
this.#uflag = this.#uflag || uflag;
return re;
}).filter((p) => !(this.isStart() && this.isEnd()) || !!p).join("|");
}
static #parseGlob(glob, hasMagic, noEmpty = false) {
let escaping = false;
let re = "";
let uflag = false;
for (let i = 0; i < glob.length; i++) {
const c = glob.charAt(i);
if (escaping) {
escaping = false;
re += (reSpecials.has(c) ? "\\" : "") + c;
continue;
}
if (c === "\\") {
if (i === glob.length - 1) {
re += "\\\\";
} else {
escaping = true;
}
continue;
}
if (c === "[") {
const [src, needUflag, consumed, magic] = (0, brace_expressions_js_1.parseClass)(glob, i);
if (consumed) {
re += src;
uflag = uflag || needUflag;
i += consumed - 1;
hasMagic = hasMagic || magic;
continue;
}
}
if (c === "*") {
if (noEmpty && glob === "*")
re += starNoEmpty;
else
re += star2;
hasMagic = true;
continue;
}
if (c === "?") {
re += qmark2;
hasMagic = true;
continue;
}
re += regExpEscape2(c);
}
return [re, (0, unescape_js_12.unescape)(glob), !!hasMagic, uflag];
}
};
exports2.AST = AST;
}
});
// dist/commonjs/escape.js
var require_escape = __commonJS({
"dist/commonjs/escape.js"(exports2) {
"use strict";
Object.defineProperty(exports2, "__esModule", { value: true });
exports2.escape = void 0;
var escape = (s, { windowsPathsNoEscape = false } = {}) => {
return windowsPathsNoEscape ? s.replace(/[?*()[\]]/g, "[$&]") : s.replace(/[?*()[\]\\]/g, "\\$&");
};
exports2.escape = escape;
}
});
// dist/commonjs/index.js
var __importDefault = exports && exports.__importDefault || function(mod) {
return mod && mod.__esModule ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
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;
var brace_expansion_1 = __importDefault(require_brace_expansion());
var assert_valid_pattern_js_1 = require_assert_valid_pattern();
var ast_js_1 = require_ast();
var escape_js_1 = require_escape();
var unescape_js_1 = require_unescape();
var minimatch = (p, pattern, options = {}) => {
(0, assert_valid_pattern_js_1.assertValidPattern)(pattern);
if (!options.nocomment && pattern.charAt(0) === "#") {
return false;
}
return new Minimatch(pattern, options).match(p);
};
exports.minimatch = minimatch;
var starDotExtRE = /^\*+([^+@!?\*\[\(]*)$/;
var starDotExtTest = (ext2) => (f) => !f.startsWith(".") && f.endsWith(ext2);
var starDotExtTestDot = (ext2) => (f) => f.endsWith(ext2);
var starDotExtTestNocase = (ext2) => {
ext2 = ext2.toLowerCase();
return (f) => !f.startsWith(".") && f.toLowerCase().endsWith(ext2);
};
var starDotExtTestNocaseDot = (ext2) => {
ext2 = ext2.toLowerCase();
return (f) => f.toLowerCase().endsWith(ext2);
};
var starDotStarRE = /^\*+\.\*+$/;
var starDotStarTest = (f) => !f.startsWith(".") && f.includes(".");
var starDotStarTestDot = (f) => f !== "." && f !== ".." && f.includes(".");
var dotStarRE = /^\.\*+$/;
var dotStarTest = (f) => f !== "." && f !== ".." && f.startsWith(".");
var starRE = /^\*+$/;
var starTest = (f) => f.length !== 0 && !f.startsWith(".");
var starTestDot = (f) => f.length !== 0 && f !== "." && f !== "..";
var qmarksRE = /^\?+([^+@!?\*\[\(]*)?$/;
var qmarksTestNocase = ([$0, ext2 = ""]) => {
const noext = qmarksTestNoExt([$0]);
if (!ext2)
return noext;
ext2 = ext2.toLowerCase();
return (f) => noext(f) && f.toLowerCase().endsWith(ext2);
};
var qmarksTestNocaseDot = ([$0, ext2 = ""]) => {
const noext = qmarksTestNoExtDot([$0]);
if (!ext2)
return noext;
ext2 = ext2.toLowerCase();
return (f) => noext(f) && f.toLowerCase().endsWith(ext2);
};
var qmarksTestDot = ([$0, ext2 = ""]) => {
const noext = qmarksTestNoExtDot([$0]);
return !ext2 ? noext : (f) => noext(f) && f.endsWith(ext2);
};
var qmarksTest = ([$0, ext2 = ""]) => {
const noext = qmarksTestNoExt([$0]);
return !ext2 ? noext : (f) => noext(f) && f.endsWith(ext2);
};
var qmarksTestNoExt = ([$0]) => {
const len = $0.length;
return (f) => f.length === len && !f.startsWith(".");
};
var qmarksTestNoExtDot = ([$0]) => {
const len = $0.length;
return (f) => f.length === len && f !== "." && f !== "..";
};
var defaultPlatform = typeof process === "object" && process ? typeof process.env === "object" && process.env && process.env.__MINIMATCH_TESTING_PLATFORM__ || process.platform : "posix";
var path = {
win32: { sep: "\\" },
posix: { sep: "/" }
};
exports.sep = defaultPlatform === "win32" ? path.win32.sep : path.posix.sep;
exports.minimatch.sep = exports.sep;
exports.GLOBSTAR = Symbol("globstar **");
exports.minimatch.GLOBSTAR = exports.GLOBSTAR;
var qmark = "[^/]";
var star = qmark + "*?";
var twoStarDot = "(?:(?!(?:\\/|^)(?:\\.{1,2})($|\\/)).)*?";
var twoStarNoDot = "(?:(?!(?:\\/|^)\\.).)*?";
var filter = (pattern, options = {}) => (p) => (0, exports.minimatch)(p, pattern, options);
exports.filter = filter;
exports.minimatch.filter = exports.filter;
var ext = (a, b = {}) => Object.assign({}, a, b);
var defaults = (def) => {
if (!def || typeof def !== "object" || !Object.keys(def).length) {
return exports.minimatch;
}
const orig = exports.minimatch;
const m = (p, pattern, options = {}) => orig(p, pattern, ext(def, options));
return Object.assign(m, {
Minimatch: class Minimatch extends orig.Minimatch {
constructor(pattern, options = {}) {
super(pattern, ext(def, options));
}
static defaults(options) {
return orig.defaults(ext(def, options)).Minimatch;
}
},
AST: class AST extends orig.AST {
/* c8 ignore start */
constructor(type, parent, options = {}) {
super(type, parent, ext(def, options));
}
/* c8 ignore stop */
static fromGlob(pattern, options = {}) {
return orig.AST.fromGlob(pattern, ext(def, options));
}
},
unescape: (s, options = {}) => orig.unescape(s, ext(def, options)),
escape: (s, options = {}) => orig.escape(s, ext(def, options)),
filter: (pattern, options = {}) => orig.filter(pattern, ext(def, options)),
defaults: (options) => orig.defaults(ext(def, options)),
makeRe: (pattern, options = {}) => orig.makeRe(pattern, ext(def, options)),
braceExpand: (pattern, options = {}) => orig.braceExpand(pattern, ext(def, options)),
match: (list, pattern, options = {}) => orig.match(list, pattern, ext(def, options)),
sep: orig.sep,
GLOBSTAR: exports.GLOBSTAR
});
};
exports.defaults = defaults;
exports.minimatch.defaults = exports.defaults;
var braceExpand = (pattern, options = {}) => {
(0, assert_valid_pattern_js_1.assertValidPattern)(pattern);
if (options.nobrace || !/\{(?:(?!\{).)*\}/.test(pattern)) {
return [pattern];
}
return (0, brace_expansion_1.default)(pattern);
};
exports.braceExpand = braceExpand;
exports.minimatch.braceExpand = exports.braceExpand;
var makeRe = (pattern, options = {}) => new Minimatch(pattern, options).makeRe();
exports.makeRe = makeRe;