eslint-plugin-slonik 1.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.
Files changed (35) hide show
  1. package/LICENSE +48 -0
  2. package/README.md +368 -0
  3. package/dist/config.cjs +61 -0
  4. package/dist/config.cjs.map +1 -0
  5. package/dist/config.d.cts +192 -0
  6. package/dist/config.d.mts +192 -0
  7. package/dist/config.d.ts +192 -0
  8. package/dist/config.mjs +59 -0
  9. package/dist/config.mjs.map +1 -0
  10. package/dist/index.cjs +27 -0
  11. package/dist/index.cjs.map +1 -0
  12. package/dist/index.d.cts +319 -0
  13. package/dist/index.d.mts +319 -0
  14. package/dist/index.d.ts +319 -0
  15. package/dist/index.mjs +20 -0
  16. package/dist/index.mjs.map +1 -0
  17. package/dist/shared/eslint-plugin-slonik.1m1xlVmw.d.cts +611 -0
  18. package/dist/shared/eslint-plugin-slonik.1m1xlVmw.d.mts +611 -0
  19. package/dist/shared/eslint-plugin-slonik.1m1xlVmw.d.ts +611 -0
  20. package/dist/shared/eslint-plugin-slonik.BxexVlk1.cjs +1539 -0
  21. package/dist/shared/eslint-plugin-slonik.BxexVlk1.cjs.map +1 -0
  22. package/dist/shared/eslint-plugin-slonik.C0xTyWZ2.mjs +2866 -0
  23. package/dist/shared/eslint-plugin-slonik.C0xTyWZ2.mjs.map +1 -0
  24. package/dist/shared/eslint-plugin-slonik.DbzoLz5_.mjs +1514 -0
  25. package/dist/shared/eslint-plugin-slonik.DbzoLz5_.mjs.map +1 -0
  26. package/dist/shared/eslint-plugin-slonik.rlOTrCdf.cjs +2929 -0
  27. package/dist/shared/eslint-plugin-slonik.rlOTrCdf.cjs.map +1 -0
  28. package/dist/workers/check-sql.worker.cjs +2436 -0
  29. package/dist/workers/check-sql.worker.cjs.map +1 -0
  30. package/dist/workers/check-sql.worker.d.cts +171 -0
  31. package/dist/workers/check-sql.worker.d.mts +171 -0
  32. package/dist/workers/check-sql.worker.d.ts +171 -0
  33. package/dist/workers/check-sql.worker.mjs +2412 -0
  34. package/dist/workers/check-sql.worker.mjs.map +1 -0
  35. package/package.json +103 -0
@@ -0,0 +1,2866 @@
1
+ import path$1 from 'path';
2
+ import postgres from 'postgres';
3
+ import crypto from 'crypto';
4
+ import fs from 'fs';
5
+ import { match as match$1 } from 'ts-pattern';
6
+ import { TSESTree } from '@typescript-eslint/utils';
7
+ import * as E from 'fp-ts/lib/Either.js';
8
+ import { pipe } from 'fp-ts/lib/function.js';
9
+ import 'fp-ts/lib/Option.js';
10
+ import * as TE from 'fp-ts/lib/TaskEither.js';
11
+ import 'fp-ts/lib/Json.js';
12
+ import pgConnectionString from 'pg-connection-string';
13
+
14
+ const balanced = (a, b, str) => {
15
+ const ma = a instanceof RegExp ? maybeMatch(a, str) : a;
16
+ const mb = b instanceof RegExp ? maybeMatch(b, str) : b;
17
+ const r = ma !== null && mb != null && range(ma, mb, str);
18
+ return (r && {
19
+ start: r[0],
20
+ end: r[1],
21
+ pre: str.slice(0, r[0]),
22
+ body: str.slice(r[0] + ma.length, r[1]),
23
+ post: str.slice(r[1] + mb.length),
24
+ });
25
+ };
26
+ const maybeMatch = (reg, str) => {
27
+ const m = str.match(reg);
28
+ return m ? m[0] : null;
29
+ };
30
+ const range = (a, b, str) => {
31
+ let begs, beg, left, right = undefined, result;
32
+ let ai = str.indexOf(a);
33
+ let bi = str.indexOf(b, ai + 1);
34
+ let i = ai;
35
+ if (ai >= 0 && bi > 0) {
36
+ if (a === b) {
37
+ return [ai, bi];
38
+ }
39
+ begs = [];
40
+ left = str.length;
41
+ while (i >= 0 && !result) {
42
+ if (i === ai) {
43
+ begs.push(i);
44
+ ai = str.indexOf(a, i + 1);
45
+ }
46
+ else if (begs.length === 1) {
47
+ const r = begs.pop();
48
+ if (r !== undefined)
49
+ result = [r, bi];
50
+ }
51
+ else {
52
+ beg = begs.pop();
53
+ if (beg !== undefined && beg < left) {
54
+ left = beg;
55
+ right = bi;
56
+ }
57
+ bi = str.indexOf(b, i + 1);
58
+ }
59
+ i = ai < bi && ai >= 0 ? ai : bi;
60
+ }
61
+ if (begs.length && right !== undefined) {
62
+ result = [left, right];
63
+ }
64
+ }
65
+ return result;
66
+ };
67
+
68
+ const escSlash = '\0SLASH' + Math.random() + '\0';
69
+ const escOpen = '\0OPEN' + Math.random() + '\0';
70
+ const escClose = '\0CLOSE' + Math.random() + '\0';
71
+ const escComma = '\0COMMA' + Math.random() + '\0';
72
+ const escPeriod = '\0PERIOD' + Math.random() + '\0';
73
+ const escSlashPattern = new RegExp(escSlash, 'g');
74
+ const escOpenPattern = new RegExp(escOpen, 'g');
75
+ const escClosePattern = new RegExp(escClose, 'g');
76
+ const escCommaPattern = new RegExp(escComma, 'g');
77
+ const escPeriodPattern = new RegExp(escPeriod, 'g');
78
+ const slashPattern = /\\\\/g;
79
+ const openPattern = /\\{/g;
80
+ const closePattern = /\\}/g;
81
+ const commaPattern = /\\,/g;
82
+ const periodPattern = /\\./g;
83
+ function numeric(str) {
84
+ return !isNaN(str) ? parseInt(str, 10) : str.charCodeAt(0);
85
+ }
86
+ function escapeBraces(str) {
87
+ return str
88
+ .replace(slashPattern, escSlash)
89
+ .replace(openPattern, escOpen)
90
+ .replace(closePattern, escClose)
91
+ .replace(commaPattern, escComma)
92
+ .replace(periodPattern, escPeriod);
93
+ }
94
+ function unescapeBraces(str) {
95
+ return str
96
+ .replace(escSlashPattern, '\\')
97
+ .replace(escOpenPattern, '{')
98
+ .replace(escClosePattern, '}')
99
+ .replace(escCommaPattern, ',')
100
+ .replace(escPeriodPattern, '.');
101
+ }
102
+ /**
103
+ * Basically just str.split(","), but handling cases
104
+ * where we have nested braced sections, which should be
105
+ * treated as individual members, like {a,{b,c},d}
106
+ */
107
+ function parseCommaParts(str) {
108
+ if (!str) {
109
+ return [''];
110
+ }
111
+ const parts = [];
112
+ const m = balanced('{', '}', str);
113
+ if (!m) {
114
+ return str.split(',');
115
+ }
116
+ const { pre, body, post } = m;
117
+ const p = pre.split(',');
118
+ p[p.length - 1] += '{' + body + '}';
119
+ const postParts = parseCommaParts(post);
120
+ if (post.length) {
121
+ p[p.length - 1] += postParts.shift();
122
+ p.push.apply(p, postParts);
123
+ }
124
+ parts.push.apply(parts, p);
125
+ return parts;
126
+ }
127
+ function expand(str) {
128
+ if (!str) {
129
+ return [];
130
+ }
131
+ // I don't know why Bash 4.3 does this, but it does.
132
+ // Anything starting with {} will have the first two bytes preserved
133
+ // but *only* at the top level, so {},a}b will not expand to anything,
134
+ // but a{},b}c will be expanded to [a}c,abc].
135
+ // One could argue that this is a bug in Bash, but since the goal of
136
+ // this module is to match Bash's rules, we escape a leading {}
137
+ if (str.slice(0, 2) === '{}') {
138
+ str = '\\{\\}' + str.slice(2);
139
+ }
140
+ return expand_(escapeBraces(str), true).map(unescapeBraces);
141
+ }
142
+ function embrace(str) {
143
+ return '{' + str + '}';
144
+ }
145
+ function isPadded(el) {
146
+ return /^-?0\d/.test(el);
147
+ }
148
+ function lte(i, y) {
149
+ return i <= y;
150
+ }
151
+ function gte(i, y) {
152
+ return i >= y;
153
+ }
154
+ function expand_(str, isTop) {
155
+ /** @type {string[]} */
156
+ const expansions = [];
157
+ const m = balanced('{', '}', str);
158
+ if (!m)
159
+ return [str];
160
+ // no need to expand pre, since it is guaranteed to be free of brace-sets
161
+ const pre = m.pre;
162
+ const post = m.post.length ? expand_(m.post, false) : [''];
163
+ if (/\$$/.test(m.pre)) {
164
+ for (let k = 0; k < post.length; k++) {
165
+ const expansion = pre + '{' + m.body + '}' + post[k];
166
+ expansions.push(expansion);
167
+ }
168
+ }
169
+ else {
170
+ const isNumericSequence = /^-?\d+\.\.-?\d+(?:\.\.-?\d+)?$/.test(m.body);
171
+ const isAlphaSequence = /^[a-zA-Z]\.\.[a-zA-Z](?:\.\.-?\d+)?$/.test(m.body);
172
+ const isSequence = isNumericSequence || isAlphaSequence;
173
+ const isOptions = m.body.indexOf(',') >= 0;
174
+ if (!isSequence && !isOptions) {
175
+ // {a},b}
176
+ if (m.post.match(/,(?!,).*\}/)) {
177
+ str = m.pre + '{' + m.body + escClose + m.post;
178
+ return expand_(str);
179
+ }
180
+ return [str];
181
+ }
182
+ let n;
183
+ if (isSequence) {
184
+ n = m.body.split(/\.\./);
185
+ }
186
+ else {
187
+ n = parseCommaParts(m.body);
188
+ if (n.length === 1 && n[0] !== undefined) {
189
+ // x{{a,b}}y ==> x{a}y x{b}y
190
+ n = expand_(n[0], false).map(embrace);
191
+ //XXX is this necessary? Can't seem to hit it in tests.
192
+ /* c8 ignore start */
193
+ if (n.length === 1) {
194
+ return post.map(p => m.pre + n[0] + p);
195
+ }
196
+ /* c8 ignore stop */
197
+ }
198
+ }
199
+ // at this point, n is the parts, and we know it's not a comma set
200
+ // with a single entry.
201
+ let N;
202
+ if (isSequence && n[0] !== undefined && n[1] !== undefined) {
203
+ const x = numeric(n[0]);
204
+ const y = numeric(n[1]);
205
+ const width = Math.max(n[0].length, n[1].length);
206
+ let incr = n.length === 3 && n[2] !== undefined ? Math.abs(numeric(n[2])) : 1;
207
+ let test = lte;
208
+ const reverse = y < x;
209
+ if (reverse) {
210
+ incr *= -1;
211
+ test = gte;
212
+ }
213
+ const pad = n.some(isPadded);
214
+ N = [];
215
+ for (let i = x; test(i, y); i += incr) {
216
+ let c;
217
+ if (isAlphaSequence) {
218
+ c = String.fromCharCode(i);
219
+ if (c === '\\') {
220
+ c = '';
221
+ }
222
+ }
223
+ else {
224
+ c = String(i);
225
+ if (pad) {
226
+ const need = width - c.length;
227
+ if (need > 0) {
228
+ const z = new Array(need + 1).join('0');
229
+ if (i < 0) {
230
+ c = '-' + z + c.slice(1);
231
+ }
232
+ else {
233
+ c = z + c;
234
+ }
235
+ }
236
+ }
237
+ }
238
+ N.push(c);
239
+ }
240
+ }
241
+ else {
242
+ N = [];
243
+ for (let j = 0; j < n.length; j++) {
244
+ N.push.apply(N, expand_(n[j], false));
245
+ }
246
+ }
247
+ for (let j = 0; j < N.length; j++) {
248
+ for (let k = 0; k < post.length; k++) {
249
+ const expansion = pre + N[j] + post[k];
250
+ if (!isTop || isSequence || expansion) {
251
+ expansions.push(expansion);
252
+ }
253
+ }
254
+ }
255
+ }
256
+ return expansions;
257
+ }
258
+
259
+ const MAX_PATTERN_LENGTH = 1024 * 64;
260
+ const assertValidPattern = (pattern) => {
261
+ if (typeof pattern !== 'string') {
262
+ throw new TypeError('invalid pattern');
263
+ }
264
+ if (pattern.length > MAX_PATTERN_LENGTH) {
265
+ throw new TypeError('pattern is too long');
266
+ }
267
+ };
268
+
269
+ // translate the various posix character classes into unicode properties
270
+ // this works across all unicode locales
271
+ // { <posix class>: [<translation>, /u flag required, negated]
272
+ const posixClasses = {
273
+ '[:alnum:]': ['\\p{L}\\p{Nl}\\p{Nd}', true],
274
+ '[:alpha:]': ['\\p{L}\\p{Nl}', true],
275
+ '[:ascii:]': ['\\x' + '00-\\x' + '7f', false],
276
+ '[:blank:]': ['\\p{Zs}\\t', true],
277
+ '[:cntrl:]': ['\\p{Cc}', true],
278
+ '[:digit:]': ['\\p{Nd}', true],
279
+ '[:graph:]': ['\\p{Z}\\p{C}', true, true],
280
+ '[:lower:]': ['\\p{Ll}', true],
281
+ '[:print:]': ['\\p{C}', true],
282
+ '[:punct:]': ['\\p{P}', true],
283
+ '[:space:]': ['\\p{Z}\\t\\r\\n\\v\\f', true],
284
+ '[:upper:]': ['\\p{Lu}', true],
285
+ '[:word:]': ['\\p{L}\\p{Nl}\\p{Nd}\\p{Pc}', true],
286
+ '[:xdigit:]': ['A-Fa-f0-9', false],
287
+ };
288
+ // only need to escape a few things inside of brace expressions
289
+ // escapes: [ \ ] -
290
+ const braceEscape = (s) => s.replace(/[[\]\\-]/g, '\\$&');
291
+ // escape all regexp magic characters
292
+ const regexpEscape = (s) => s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
293
+ // everything has already been escaped, we just have to join
294
+ const rangesToString = (ranges) => ranges.join('');
295
+ // takes a glob string at a posix brace expression, and returns
296
+ // an equivalent regular expression source, and boolean indicating
297
+ // whether the /u flag needs to be applied, and the number of chars
298
+ // consumed to parse the character class.
299
+ // This also removes out of order ranges, and returns ($.) if the
300
+ // entire class just no good.
301
+ const parseClass = (glob, position) => {
302
+ const pos = position;
303
+ /* c8 ignore start */
304
+ if (glob.charAt(pos) !== '[') {
305
+ throw new Error('not in a brace expression');
306
+ }
307
+ /* c8 ignore stop */
308
+ const ranges = [];
309
+ const negs = [];
310
+ let i = pos + 1;
311
+ let sawStart = false;
312
+ let uflag = false;
313
+ let escaping = false;
314
+ let negate = false;
315
+ let endPos = pos;
316
+ let rangeStart = '';
317
+ WHILE: while (i < glob.length) {
318
+ const c = glob.charAt(i);
319
+ if ((c === '!' || c === '^') && i === pos + 1) {
320
+ negate = true;
321
+ i++;
322
+ continue;
323
+ }
324
+ if (c === ']' && sawStart && !escaping) {
325
+ endPos = i + 1;
326
+ break;
327
+ }
328
+ sawStart = true;
329
+ if (c === '\\') {
330
+ if (!escaping) {
331
+ escaping = true;
332
+ i++;
333
+ continue;
334
+ }
335
+ // escaped \ char, fall through and treat like normal char
336
+ }
337
+ if (c === '[' && !escaping) {
338
+ // either a posix class, a collation equivalent, or just a [
339
+ for (const [cls, [unip, u, neg]] of Object.entries(posixClasses)) {
340
+ if (glob.startsWith(cls, i)) {
341
+ // invalid, [a-[] is fine, but not [a-[:alpha]]
342
+ if (rangeStart) {
343
+ return ['$.', false, glob.length - pos, true];
344
+ }
345
+ i += cls.length;
346
+ if (neg)
347
+ negs.push(unip);
348
+ else
349
+ ranges.push(unip);
350
+ uflag = uflag || u;
351
+ continue WHILE;
352
+ }
353
+ }
354
+ }
355
+ // now it's just a normal character, effectively
356
+ escaping = false;
357
+ if (rangeStart) {
358
+ // throw this range away if it's not valid, but others
359
+ // can still match.
360
+ if (c > rangeStart) {
361
+ ranges.push(braceEscape(rangeStart) + '-' + braceEscape(c));
362
+ }
363
+ else if (c === rangeStart) {
364
+ ranges.push(braceEscape(c));
365
+ }
366
+ rangeStart = '';
367
+ i++;
368
+ continue;
369
+ }
370
+ // now might be the start of a range.
371
+ // can be either c-d or c-] or c<more...>] or c] at this point
372
+ if (glob.startsWith('-]', i + 1)) {
373
+ ranges.push(braceEscape(c + '-'));
374
+ i += 2;
375
+ continue;
376
+ }
377
+ if (glob.startsWith('-', i + 1)) {
378
+ rangeStart = c;
379
+ i += 2;
380
+ continue;
381
+ }
382
+ // not the start of a range, just a single character
383
+ ranges.push(braceEscape(c));
384
+ i++;
385
+ }
386
+ if (endPos < i) {
387
+ // didn't see the end of the class, not a valid class,
388
+ // but might still be valid as a literal match.
389
+ return ['', false, 0, false];
390
+ }
391
+ // if we got no ranges and no negates, then we have a range that
392
+ // cannot possibly match anything, and that poisons the whole glob
393
+ if (!ranges.length && !negs.length) {
394
+ return ['$.', false, glob.length - pos, true];
395
+ }
396
+ // if we got one positive range, and it's a single character, then that's
397
+ // not actually a magic pattern, it's just that one literal character.
398
+ // we should not treat that as "magic", we should just return the literal
399
+ // character. [_] is a perfectly valid way to escape glob magic chars.
400
+ if (negs.length === 0 &&
401
+ ranges.length === 1 &&
402
+ /^\\?.$/.test(ranges[0]) &&
403
+ !negate) {
404
+ const r = ranges[0].length === 2 ? ranges[0].slice(-1) : ranges[0];
405
+ return [regexpEscape(r), false, endPos - pos, false];
406
+ }
407
+ const sranges = '[' + (negate ? '^' : '') + rangesToString(ranges) + ']';
408
+ const snegs = '[' + (negate ? '' : '^') + rangesToString(negs) + ']';
409
+ const comb = ranges.length && negs.length
410
+ ? '(' + sranges + '|' + snegs + ')'
411
+ : ranges.length
412
+ ? sranges
413
+ : snegs;
414
+ return [comb, uflag, endPos - pos, true];
415
+ };
416
+
417
+ /**
418
+ * Un-escape a string that has been escaped with {@link escape}.
419
+ *
420
+ * If the {@link MinimatchOptions.windowsPathsNoEscape} option is used, then
421
+ * square-bracket escapes are removed, but not backslash escapes.
422
+ *
423
+ * For example, it will turn the string `'[*]'` into `*`, but it will not
424
+ * turn `'\\*'` into `'*'`, because `\` is a path separator in
425
+ * `windowsPathsNoEscape` mode.
426
+ *
427
+ * When `windowsPathsNoEscape` is not set, then both square-bracket escapes and
428
+ * backslash escapes are removed.
429
+ *
430
+ * Slashes (and backslashes in `windowsPathsNoEscape` mode) cannot be escaped
431
+ * or unescaped.
432
+ *
433
+ * When `magicalBraces` is not set, escapes of braces (`{` and `}`) will not be
434
+ * unescaped.
435
+ */
436
+ const unescape = (s, { windowsPathsNoEscape = false, magicalBraces = true, } = {}) => {
437
+ if (magicalBraces) {
438
+ return windowsPathsNoEscape
439
+ ? s.replace(/\[([^\/\\])\]/g, '$1')
440
+ : s
441
+ .replace(/((?!\\).|^)\[([^\/\\])\]/g, '$1$2')
442
+ .replace(/\\([^\/])/g, '$1');
443
+ }
444
+ return windowsPathsNoEscape
445
+ ? s.replace(/\[([^\/\\{}])\]/g, '$1')
446
+ : s
447
+ .replace(/((?!\\).|^)\[([^\/\\{}])\]/g, '$1$2')
448
+ .replace(/\\([^\/{}])/g, '$1');
449
+ };
450
+
451
+ // parse a single path portion
452
+ const types = new Set(['!', '?', '+', '*', '@']);
453
+ const isExtglobType = (c) => types.has(c);
454
+ // Patterns that get prepended to bind to the start of either the
455
+ // entire string, or just a single path portion, to prevent dots
456
+ // and/or traversal patterns, when needed.
457
+ // Exts don't need the ^ or / bit, because the root binds that already.
458
+ const startNoTraversal = '(?!(?:^|/)\\.\\.?(?:$|/))';
459
+ const startNoDot = '(?!\\.)';
460
+ // characters that indicate a start of pattern needs the "no dots" bit,
461
+ // because a dot *might* be matched. ( is not in the list, because in
462
+ // the case of a child extglob, it will handle the prevention itself.
463
+ const addPatternStart = new Set(['[', '.']);
464
+ // cases where traversal is A-OK, no dot prevention needed
465
+ const justDots = new Set(['..', '.']);
466
+ const reSpecials = new Set('().*{}+?[]^$\\!');
467
+ const regExpEscape$1 = (s) => s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
468
+ // any single thing other than /
469
+ const qmark$1 = '[^/]';
470
+ // * => any number of characters
471
+ const star$1 = qmark$1 + '*?';
472
+ // use + when we need to ensure that *something* matches, because the * is
473
+ // the only thing in the path portion.
474
+ const starNoEmpty = qmark$1 + '+?';
475
+ // remove the \ chars that we added if we end up doing a nonmagic compare
476
+ // const deslash = (s: string) => s.replace(/\\(.)/g, '$1')
477
+ class AST {
478
+ type;
479
+ #root;
480
+ #hasMagic;
481
+ #uflag = false;
482
+ #parts = [];
483
+ #parent;
484
+ #parentIndex;
485
+ #negs;
486
+ #filledNegs = false;
487
+ #options;
488
+ #toString;
489
+ // set to true if it's an extglob with no children
490
+ // (which really means one child of '')
491
+ #emptyExt = false;
492
+ constructor(type, parent, options = {}) {
493
+ this.type = type;
494
+ // extglobs are inherently magical
495
+ if (type)
496
+ this.#hasMagic = true;
497
+ this.#parent = parent;
498
+ this.#root = this.#parent ? this.#parent.#root : this;
499
+ this.#options = this.#root === this ? options : this.#root.#options;
500
+ this.#negs = this.#root === this ? [] : this.#root.#negs;
501
+ if (type === '!' && !this.#root.#filledNegs)
502
+ this.#negs.push(this);
503
+ this.#parentIndex = this.#parent ? this.#parent.#parts.length : 0;
504
+ }
505
+ get hasMagic() {
506
+ /* c8 ignore start */
507
+ if (this.#hasMagic !== undefined)
508
+ return this.#hasMagic;
509
+ /* c8 ignore stop */
510
+ for (const p of this.#parts) {
511
+ if (typeof p === 'string')
512
+ continue;
513
+ if (p.type || p.hasMagic)
514
+ return (this.#hasMagic = true);
515
+ }
516
+ // note: will be undefined until we generate the regexp src and find out
517
+ return this.#hasMagic;
518
+ }
519
+ // reconstructs the pattern
520
+ toString() {
521
+ if (this.#toString !== undefined)
522
+ return this.#toString;
523
+ if (!this.type) {
524
+ return (this.#toString = this.#parts.map(p => String(p)).join(''));
525
+ }
526
+ else {
527
+ return (this.#toString =
528
+ this.type + '(' + this.#parts.map(p => String(p)).join('|') + ')');
529
+ }
530
+ }
531
+ #fillNegs() {
532
+ /* c8 ignore start */
533
+ if (this !== this.#root)
534
+ throw new Error('should only call on root');
535
+ if (this.#filledNegs)
536
+ return this;
537
+ /* c8 ignore stop */
538
+ // call toString() once to fill this out
539
+ this.toString();
540
+ this.#filledNegs = true;
541
+ let n;
542
+ while ((n = this.#negs.pop())) {
543
+ if (n.type !== '!')
544
+ continue;
545
+ // walk up the tree, appending everthing that comes AFTER parentIndex
546
+ let p = n;
547
+ let pp = p.#parent;
548
+ while (pp) {
549
+ for (let i = p.#parentIndex + 1; !pp.type && i < pp.#parts.length; i++) {
550
+ for (const part of n.#parts) {
551
+ /* c8 ignore start */
552
+ if (typeof part === 'string') {
553
+ throw new Error('string part in extglob AST??');
554
+ }
555
+ /* c8 ignore stop */
556
+ part.copyIn(pp.#parts[i]);
557
+ }
558
+ }
559
+ p = pp;
560
+ pp = p.#parent;
561
+ }
562
+ }
563
+ return this;
564
+ }
565
+ push(...parts) {
566
+ for (const p of parts) {
567
+ if (p === '')
568
+ continue;
569
+ /* c8 ignore start */
570
+ if (typeof p !== 'string' && !(p instanceof AST && p.#parent === this)) {
571
+ throw new Error('invalid part: ' + p);
572
+ }
573
+ /* c8 ignore stop */
574
+ this.#parts.push(p);
575
+ }
576
+ }
577
+ toJSON() {
578
+ const ret = this.type === null
579
+ ? this.#parts.slice().map(p => (typeof p === 'string' ? p : p.toJSON()))
580
+ : [this.type, ...this.#parts.map(p => p.toJSON())];
581
+ if (this.isStart() && !this.type)
582
+ ret.unshift([]);
583
+ if (this.isEnd() &&
584
+ (this === this.#root ||
585
+ (this.#root.#filledNegs && this.#parent?.type === '!'))) {
586
+ ret.push({});
587
+ }
588
+ return ret;
589
+ }
590
+ isStart() {
591
+ if (this.#root === this)
592
+ return true;
593
+ // if (this.type) return !!this.#parent?.isStart()
594
+ if (!this.#parent?.isStart())
595
+ return false;
596
+ if (this.#parentIndex === 0)
597
+ return true;
598
+ // if everything AHEAD of this is a negation, then it's still the "start"
599
+ const p = this.#parent;
600
+ for (let i = 0; i < this.#parentIndex; i++) {
601
+ const pp = p.#parts[i];
602
+ if (!(pp instanceof AST && pp.type === '!')) {
603
+ return false;
604
+ }
605
+ }
606
+ return true;
607
+ }
608
+ isEnd() {
609
+ if (this.#root === this)
610
+ return true;
611
+ if (this.#parent?.type === '!')
612
+ return true;
613
+ if (!this.#parent?.isEnd())
614
+ return false;
615
+ if (!this.type)
616
+ return this.#parent?.isEnd();
617
+ // if not root, it'll always have a parent
618
+ /* c8 ignore start */
619
+ const pl = this.#parent ? this.#parent.#parts.length : 0;
620
+ /* c8 ignore stop */
621
+ return this.#parentIndex === pl - 1;
622
+ }
623
+ copyIn(part) {
624
+ if (typeof part === 'string')
625
+ this.push(part);
626
+ else
627
+ this.push(part.clone(this));
628
+ }
629
+ clone(parent) {
630
+ const c = new AST(this.type, parent);
631
+ for (const p of this.#parts) {
632
+ c.copyIn(p);
633
+ }
634
+ return c;
635
+ }
636
+ static #parseAST(str, ast, pos, opt) {
637
+ let escaping = false;
638
+ let inBrace = false;
639
+ let braceStart = -1;
640
+ let braceNeg = false;
641
+ if (ast.type === null) {
642
+ // outside of a extglob, append until we find a start
643
+ let i = pos;
644
+ let acc = '';
645
+ while (i < str.length) {
646
+ const c = str.charAt(i++);
647
+ // still accumulate escapes at this point, but we do ignore
648
+ // starts that are escaped
649
+ if (escaping || c === '\\') {
650
+ escaping = !escaping;
651
+ acc += c;
652
+ continue;
653
+ }
654
+ if (inBrace) {
655
+ if (i === braceStart + 1) {
656
+ if (c === '^' || c === '!') {
657
+ braceNeg = true;
658
+ }
659
+ }
660
+ else if (c === ']' && !(i === braceStart + 2 && braceNeg)) {
661
+ inBrace = false;
662
+ }
663
+ acc += c;
664
+ continue;
665
+ }
666
+ else if (c === '[') {
667
+ inBrace = true;
668
+ braceStart = i;
669
+ braceNeg = false;
670
+ acc += c;
671
+ continue;
672
+ }
673
+ if (!opt.noext && isExtglobType(c) && str.charAt(i) === '(') {
674
+ ast.push(acc);
675
+ acc = '';
676
+ const ext = new AST(c, ast);
677
+ i = AST.#parseAST(str, ext, i, opt);
678
+ ast.push(ext);
679
+ continue;
680
+ }
681
+ acc += c;
682
+ }
683
+ ast.push(acc);
684
+ return i;
685
+ }
686
+ // some kind of extglob, pos is at the (
687
+ // find the next | or )
688
+ let i = pos + 1;
689
+ let part = new AST(null, ast);
690
+ const parts = [];
691
+ let acc = '';
692
+ while (i < str.length) {
693
+ const c = str.charAt(i++);
694
+ // still accumulate escapes at this point, but we do ignore
695
+ // starts that are escaped
696
+ if (escaping || c === '\\') {
697
+ escaping = !escaping;
698
+ acc += c;
699
+ continue;
700
+ }
701
+ if (inBrace) {
702
+ if (i === braceStart + 1) {
703
+ if (c === '^' || c === '!') {
704
+ braceNeg = true;
705
+ }
706
+ }
707
+ else if (c === ']' && !(i === braceStart + 2 && braceNeg)) {
708
+ inBrace = false;
709
+ }
710
+ acc += c;
711
+ continue;
712
+ }
713
+ else if (c === '[') {
714
+ inBrace = true;
715
+ braceStart = i;
716
+ braceNeg = false;
717
+ acc += c;
718
+ continue;
719
+ }
720
+ if (isExtglobType(c) && str.charAt(i) === '(') {
721
+ part.push(acc);
722
+ acc = '';
723
+ const ext = new AST(c, part);
724
+ part.push(ext);
725
+ i = AST.#parseAST(str, ext, i, opt);
726
+ continue;
727
+ }
728
+ if (c === '|') {
729
+ part.push(acc);
730
+ acc = '';
731
+ parts.push(part);
732
+ part = new AST(null, ast);
733
+ continue;
734
+ }
735
+ if (c === ')') {
736
+ if (acc === '' && ast.#parts.length === 0) {
737
+ ast.#emptyExt = true;
738
+ }
739
+ part.push(acc);
740
+ acc = '';
741
+ ast.push(...parts, part);
742
+ return i;
743
+ }
744
+ acc += c;
745
+ }
746
+ // unfinished extglob
747
+ // if we got here, it was a malformed extglob! not an extglob, but
748
+ // maybe something else in there.
749
+ ast.type = null;
750
+ ast.#hasMagic = undefined;
751
+ ast.#parts = [str.substring(pos - 1)];
752
+ return i;
753
+ }
754
+ static fromGlob(pattern, options = {}) {
755
+ const ast = new AST(null, undefined, options);
756
+ AST.#parseAST(pattern, ast, 0, options);
757
+ return ast;
758
+ }
759
+ // returns the regular expression if there's magic, or the unescaped
760
+ // string if not.
761
+ toMMPattern() {
762
+ // should only be called on root
763
+ /* c8 ignore start */
764
+ if (this !== this.#root)
765
+ return this.#root.toMMPattern();
766
+ /* c8 ignore stop */
767
+ const glob = this.toString();
768
+ const [re, body, hasMagic, uflag] = this.toRegExpSource();
769
+ // if we're in nocase mode, and not nocaseMagicOnly, then we do
770
+ // still need a regular expression if we have to case-insensitively
771
+ // match capital/lowercase characters.
772
+ const anyMagic = hasMagic ||
773
+ this.#hasMagic ||
774
+ (this.#options.nocase &&
775
+ !this.#options.nocaseMagicOnly &&
776
+ glob.toUpperCase() !== glob.toLowerCase());
777
+ if (!anyMagic) {
778
+ return body;
779
+ }
780
+ const flags = (this.#options.nocase ? 'i' : '') + (uflag ? 'u' : '');
781
+ return Object.assign(new RegExp(`^${re}$`, flags), {
782
+ _src: re,
783
+ _glob: glob,
784
+ });
785
+ }
786
+ get options() {
787
+ return this.#options;
788
+ }
789
+ // returns the string match, the regexp source, whether there's magic
790
+ // in the regexp (so a regular expression is required) and whether or
791
+ // not the uflag is needed for the regular expression (for posix classes)
792
+ // TODO: instead of injecting the start/end at this point, just return
793
+ // the BODY of the regexp, along with the start/end portions suitable
794
+ // for binding the start/end in either a joined full-path makeRe context
795
+ // (where we bind to (^|/), or a standalone matchPart context (where
796
+ // we bind to ^, and not /). Otherwise slashes get duped!
797
+ //
798
+ // In part-matching mode, the start is:
799
+ // - if not isStart: nothing
800
+ // - if traversal possible, but not allowed: ^(?!\.\.?$)
801
+ // - if dots allowed or not possible: ^
802
+ // - if dots possible and not allowed: ^(?!\.)
803
+ // end is:
804
+ // - if not isEnd(): nothing
805
+ // - else: $
806
+ //
807
+ // In full-path matching mode, we put the slash at the START of the
808
+ // pattern, so start is:
809
+ // - if first pattern: same as part-matching mode
810
+ // - if not isStart(): nothing
811
+ // - if traversal possible, but not allowed: /(?!\.\.?(?:$|/))
812
+ // - if dots allowed or not possible: /
813
+ // - if dots possible and not allowed: /(?!\.)
814
+ // end is:
815
+ // - if last pattern, same as part-matching mode
816
+ // - else nothing
817
+ //
818
+ // Always put the (?:$|/) on negated tails, though, because that has to be
819
+ // there to bind the end of the negated pattern portion, and it's easier to
820
+ // just stick it in now rather than try to inject it later in the middle of
821
+ // the pattern.
822
+ //
823
+ // We can just always return the same end, and leave it up to the caller
824
+ // to know whether it's going to be used joined or in parts.
825
+ // And, if the start is adjusted slightly, can do the same there:
826
+ // - if not isStart: nothing
827
+ // - if traversal possible, but not allowed: (?:/|^)(?!\.\.?$)
828
+ // - if dots allowed or not possible: (?:/|^)
829
+ // - if dots possible and not allowed: (?:/|^)(?!\.)
830
+ //
831
+ // But it's better to have a simpler binding without a conditional, for
832
+ // performance, so probably better to return both start options.
833
+ //
834
+ // Then the caller just ignores the end if it's not the first pattern,
835
+ // and the start always gets applied.
836
+ //
837
+ // But that's always going to be $ if it's the ending pattern, or nothing,
838
+ // so the caller can just attach $ at the end of the pattern when building.
839
+ //
840
+ // So the todo is:
841
+ // - better detect what kind of start is needed
842
+ // - return both flavors of starting pattern
843
+ // - attach $ at the end of the pattern when creating the actual RegExp
844
+ //
845
+ // Ah, but wait, no, that all only applies to the root when the first pattern
846
+ // is not an extglob. If the first pattern IS an extglob, then we need all
847
+ // that dot prevention biz to live in the extglob portions, because eg
848
+ // +(*|.x*) can match .xy but not .yx.
849
+ //
850
+ // So, return the two flavors if it's #root and the first child is not an
851
+ // AST, otherwise leave it to the child AST to handle it, and there,
852
+ // use the (?:^|/) style of start binding.
853
+ //
854
+ // Even simplified further:
855
+ // - Since the start for a join is eg /(?!\.) and the start for a part
856
+ // is ^(?!\.), we can just prepend (?!\.) to the pattern (either root
857
+ // or start or whatever) and prepend ^ or / at the Regexp construction.
858
+ toRegExpSource(allowDot) {
859
+ const dot = allowDot ?? !!this.#options.dot;
860
+ if (this.#root === this)
861
+ this.#fillNegs();
862
+ if (!this.type) {
863
+ const noEmpty = this.isStart() &&
864
+ this.isEnd() &&
865
+ !this.#parts.some(s => typeof s !== 'string');
866
+ const src = this.#parts
867
+ .map(p => {
868
+ const [re, _, hasMagic, uflag] = typeof p === 'string'
869
+ ? AST.#parseGlob(p, this.#hasMagic, noEmpty)
870
+ : p.toRegExpSource(allowDot);
871
+ this.#hasMagic = this.#hasMagic || hasMagic;
872
+ this.#uflag = this.#uflag || uflag;
873
+ return re;
874
+ })
875
+ .join('');
876
+ let start = '';
877
+ if (this.isStart()) {
878
+ if (typeof this.#parts[0] === 'string') {
879
+ // this is the string that will match the start of the pattern,
880
+ // so we need to protect against dots and such.
881
+ // '.' and '..' cannot match unless the pattern is that exactly,
882
+ // even if it starts with . or dot:true is set.
883
+ const dotTravAllowed = this.#parts.length === 1 && justDots.has(this.#parts[0]);
884
+ if (!dotTravAllowed) {
885
+ const aps = addPatternStart;
886
+ // check if we have a possibility of matching . or ..,
887
+ // and prevent that.
888
+ const needNoTrav =
889
+ // dots are allowed, and the pattern starts with [ or .
890
+ (dot && aps.has(src.charAt(0))) ||
891
+ // the pattern starts with \., and then [ or .
892
+ (src.startsWith('\\.') && aps.has(src.charAt(2))) ||
893
+ // the pattern starts with \.\., and then [ or .
894
+ (src.startsWith('\\.\\.') && aps.has(src.charAt(4)));
895
+ // no need to prevent dots if it can't match a dot, or if a
896
+ // sub-pattern will be preventing it anyway.
897
+ const needNoDot = !dot && !allowDot && aps.has(src.charAt(0));
898
+ start = needNoTrav ? startNoTraversal : needNoDot ? startNoDot : '';
899
+ }
900
+ }
901
+ }
902
+ // append the "end of path portion" pattern to negation tails
903
+ let end = '';
904
+ if (this.isEnd() &&
905
+ this.#root.#filledNegs &&
906
+ this.#parent?.type === '!') {
907
+ end = '(?:$|\\/)';
908
+ }
909
+ const final = start + src + end;
910
+ return [
911
+ final,
912
+ unescape(src),
913
+ (this.#hasMagic = !!this.#hasMagic),
914
+ this.#uflag,
915
+ ];
916
+ }
917
+ // We need to calculate the body *twice* if it's a repeat pattern
918
+ // at the start, once in nodot mode, then again in dot mode, so a
919
+ // pattern like *(?) can match 'x.y'
920
+ const repeated = this.type === '*' || this.type === '+';
921
+ // some kind of extglob
922
+ const start = this.type === '!' ? '(?:(?!(?:' : '(?:';
923
+ let body = this.#partsToRegExp(dot);
924
+ if (this.isStart() && this.isEnd() && !body && this.type !== '!') {
925
+ // invalid extglob, has to at least be *something* present, if it's
926
+ // the entire path portion.
927
+ const s = this.toString();
928
+ this.#parts = [s];
929
+ this.type = null;
930
+ this.#hasMagic = undefined;
931
+ return [s, unescape(this.toString()), false, false];
932
+ }
933
+ // XXX abstract out this map method
934
+ let bodyDotAllowed = !repeated || allowDot || dot || !startNoDot
935
+ ? ''
936
+ : this.#partsToRegExp(true);
937
+ if (bodyDotAllowed === body) {
938
+ bodyDotAllowed = '';
939
+ }
940
+ if (bodyDotAllowed) {
941
+ body = `(?:${body})(?:${bodyDotAllowed})*?`;
942
+ }
943
+ // an empty !() is exactly equivalent to a starNoEmpty
944
+ let final = '';
945
+ if (this.type === '!' && this.#emptyExt) {
946
+ final = (this.isStart() && !dot ? startNoDot : '') + starNoEmpty;
947
+ }
948
+ else {
949
+ const close = this.type === '!'
950
+ ? // !() must match something,but !(x) can match ''
951
+ '))' +
952
+ (this.isStart() && !dot && !allowDot ? startNoDot : '') +
953
+ star$1 +
954
+ ')'
955
+ : this.type === '@'
956
+ ? ')'
957
+ : this.type === '?'
958
+ ? ')?'
959
+ : this.type === '+' && bodyDotAllowed
960
+ ? ')'
961
+ : this.type === '*' && bodyDotAllowed
962
+ ? `)?`
963
+ : `)${this.type}`;
964
+ final = start + body + close;
965
+ }
966
+ return [
967
+ final,
968
+ unescape(body),
969
+ (this.#hasMagic = !!this.#hasMagic),
970
+ this.#uflag,
971
+ ];
972
+ }
973
+ #partsToRegExp(dot) {
974
+ return this.#parts
975
+ .map(p => {
976
+ // extglob ASTs should only contain parent ASTs
977
+ /* c8 ignore start */
978
+ if (typeof p === 'string') {
979
+ throw new Error('string type in extglob ast??');
980
+ }
981
+ /* c8 ignore stop */
982
+ // can ignore hasMagic, because extglobs are already always magic
983
+ const [re, _, _hasMagic, uflag] = p.toRegExpSource(dot);
984
+ this.#uflag = this.#uflag || uflag;
985
+ return re;
986
+ })
987
+ .filter(p => !(this.isStart() && this.isEnd()) || !!p)
988
+ .join('|');
989
+ }
990
+ static #parseGlob(glob, hasMagic, noEmpty = false) {
991
+ let escaping = false;
992
+ let re = '';
993
+ let uflag = false;
994
+ for (let i = 0; i < glob.length; i++) {
995
+ const c = glob.charAt(i);
996
+ if (escaping) {
997
+ escaping = false;
998
+ re += (reSpecials.has(c) ? '\\' : '') + c;
999
+ continue;
1000
+ }
1001
+ if (c === '\\') {
1002
+ if (i === glob.length - 1) {
1003
+ re += '\\\\';
1004
+ }
1005
+ else {
1006
+ escaping = true;
1007
+ }
1008
+ continue;
1009
+ }
1010
+ if (c === '[') {
1011
+ const [src, needUflag, consumed, magic] = parseClass(glob, i);
1012
+ if (consumed) {
1013
+ re += src;
1014
+ uflag = uflag || needUflag;
1015
+ i += consumed - 1;
1016
+ hasMagic = hasMagic || magic;
1017
+ continue;
1018
+ }
1019
+ }
1020
+ if (c === '*') {
1021
+ re += noEmpty && glob === '*' ? starNoEmpty : star$1;
1022
+ hasMagic = true;
1023
+ continue;
1024
+ }
1025
+ if (c === '?') {
1026
+ re += qmark$1;
1027
+ hasMagic = true;
1028
+ continue;
1029
+ }
1030
+ re += regExpEscape$1(c);
1031
+ }
1032
+ return [re, unescape(glob), !!hasMagic, uflag];
1033
+ }
1034
+ }
1035
+
1036
+ /**
1037
+ * Escape all magic characters in a glob pattern.
1038
+ *
1039
+ * If the {@link MinimatchOptions.windowsPathsNoEscape}
1040
+ * option is used, then characters are escaped by wrapping in `[]`, because
1041
+ * a magic character wrapped in a character class can only be satisfied by
1042
+ * that exact character. In this mode, `\` is _not_ escaped, because it is
1043
+ * not interpreted as a magic character, but instead as a path separator.
1044
+ *
1045
+ * If the {@link MinimatchOptions.magicalBraces} option is used,
1046
+ * then braces (`{` and `}`) will be escaped.
1047
+ */
1048
+ const escape = (s, { windowsPathsNoEscape = false, magicalBraces = false, } = {}) => {
1049
+ // don't need to escape +@! because we escape the parens
1050
+ // that make those magic, and escaping ! as [!] isn't valid,
1051
+ // because [!]] is a valid glob class meaning not ']'.
1052
+ if (magicalBraces) {
1053
+ return windowsPathsNoEscape
1054
+ ? s.replace(/[?*()[\]{}]/g, '[$&]')
1055
+ : s.replace(/[?*()[\]\\{}]/g, '\\$&');
1056
+ }
1057
+ return windowsPathsNoEscape
1058
+ ? s.replace(/[?*()[\]]/g, '[$&]')
1059
+ : s.replace(/[?*()[\]\\]/g, '\\$&');
1060
+ };
1061
+
1062
+ const minimatch = (p, pattern, options = {}) => {
1063
+ assertValidPattern(pattern);
1064
+ // shortcut: comments match nothing.
1065
+ if (!options.nocomment && pattern.charAt(0) === '#') {
1066
+ return false;
1067
+ }
1068
+ return new Minimatch(pattern, options).match(p);
1069
+ };
1070
+ // Optimized checking for the most common glob patterns.
1071
+ const starDotExtRE = /^\*+([^+@!?\*\[\(]*)$/;
1072
+ const starDotExtTest = (ext) => (f) => !f.startsWith('.') && f.endsWith(ext);
1073
+ const starDotExtTestDot = (ext) => (f) => f.endsWith(ext);
1074
+ const starDotExtTestNocase = (ext) => {
1075
+ ext = ext.toLowerCase();
1076
+ return (f) => !f.startsWith('.') && f.toLowerCase().endsWith(ext);
1077
+ };
1078
+ const starDotExtTestNocaseDot = (ext) => {
1079
+ ext = ext.toLowerCase();
1080
+ return (f) => f.toLowerCase().endsWith(ext);
1081
+ };
1082
+ const starDotStarRE = /^\*+\.\*+$/;
1083
+ const starDotStarTest = (f) => !f.startsWith('.') && f.includes('.');
1084
+ const starDotStarTestDot = (f) => f !== '.' && f !== '..' && f.includes('.');
1085
+ const dotStarRE = /^\.\*+$/;
1086
+ const dotStarTest = (f) => f !== '.' && f !== '..' && f.startsWith('.');
1087
+ const starRE = /^\*+$/;
1088
+ const starTest = (f) => f.length !== 0 && !f.startsWith('.');
1089
+ const starTestDot = (f) => f.length !== 0 && f !== '.' && f !== '..';
1090
+ const qmarksRE = /^\?+([^+@!?\*\[\(]*)?$/;
1091
+ const qmarksTestNocase = ([$0, ext = '']) => {
1092
+ const noext = qmarksTestNoExt([$0]);
1093
+ if (!ext)
1094
+ return noext;
1095
+ ext = ext.toLowerCase();
1096
+ return (f) => noext(f) && f.toLowerCase().endsWith(ext);
1097
+ };
1098
+ const qmarksTestNocaseDot = ([$0, ext = '']) => {
1099
+ const noext = qmarksTestNoExtDot([$0]);
1100
+ if (!ext)
1101
+ return noext;
1102
+ ext = ext.toLowerCase();
1103
+ return (f) => noext(f) && f.toLowerCase().endsWith(ext);
1104
+ };
1105
+ const qmarksTestDot = ([$0, ext = '']) => {
1106
+ const noext = qmarksTestNoExtDot([$0]);
1107
+ return !ext ? noext : (f) => noext(f) && f.endsWith(ext);
1108
+ };
1109
+ const qmarksTest = ([$0, ext = '']) => {
1110
+ const noext = qmarksTestNoExt([$0]);
1111
+ return !ext ? noext : (f) => noext(f) && f.endsWith(ext);
1112
+ };
1113
+ const qmarksTestNoExt = ([$0]) => {
1114
+ const len = $0.length;
1115
+ return (f) => f.length === len && !f.startsWith('.');
1116
+ };
1117
+ const qmarksTestNoExtDot = ([$0]) => {
1118
+ const len = $0.length;
1119
+ return (f) => f.length === len && f !== '.' && f !== '..';
1120
+ };
1121
+ /* c8 ignore start */
1122
+ const defaultPlatform = (typeof process === 'object' && process
1123
+ ? (typeof process.env === 'object' &&
1124
+ process.env &&
1125
+ process.env.__MINIMATCH_TESTING_PLATFORM__) ||
1126
+ process.platform
1127
+ : 'posix');
1128
+ const path = {
1129
+ win32: { sep: '\\' },
1130
+ posix: { sep: '/' },
1131
+ };
1132
+ /* c8 ignore stop */
1133
+ const sep = defaultPlatform === 'win32' ? path.win32.sep : path.posix.sep;
1134
+ minimatch.sep = sep;
1135
+ const GLOBSTAR = Symbol('globstar **');
1136
+ minimatch.GLOBSTAR = GLOBSTAR;
1137
+ // any single thing other than /
1138
+ // don't need to escape / when using new RegExp()
1139
+ const qmark = '[^/]';
1140
+ // * => any number of characters
1141
+ const star = qmark + '*?';
1142
+ // ** when dots are allowed. Anything goes, except .. and .
1143
+ // not (^ or / followed by one or two dots followed by $ or /),
1144
+ // followed by anything, any number of times.
1145
+ const twoStarDot = '(?:(?!(?:\\/|^)(?:\\.{1,2})($|\\/)).)*?';
1146
+ // not a ^ or / followed by a dot,
1147
+ // followed by anything, any number of times.
1148
+ const twoStarNoDot = '(?:(?!(?:\\/|^)\\.).)*?';
1149
+ const filter = (pattern, options = {}) => (p) => minimatch(p, pattern, options);
1150
+ minimatch.filter = filter;
1151
+ const ext = (a, b = {}) => Object.assign({}, a, b);
1152
+ const defaults = (def) => {
1153
+ if (!def || typeof def !== 'object' || !Object.keys(def).length) {
1154
+ return minimatch;
1155
+ }
1156
+ const orig = minimatch;
1157
+ const m = (p, pattern, options = {}) => orig(p, pattern, ext(def, options));
1158
+ return Object.assign(m, {
1159
+ Minimatch: class Minimatch extends orig.Minimatch {
1160
+ constructor(pattern, options = {}) {
1161
+ super(pattern, ext(def, options));
1162
+ }
1163
+ static defaults(options) {
1164
+ return orig.defaults(ext(def, options)).Minimatch;
1165
+ }
1166
+ },
1167
+ AST: class AST extends orig.AST {
1168
+ /* c8 ignore start */
1169
+ constructor(type, parent, options = {}) {
1170
+ super(type, parent, ext(def, options));
1171
+ }
1172
+ /* c8 ignore stop */
1173
+ static fromGlob(pattern, options = {}) {
1174
+ return orig.AST.fromGlob(pattern, ext(def, options));
1175
+ }
1176
+ },
1177
+ unescape: (s, options = {}) => orig.unescape(s, ext(def, options)),
1178
+ escape: (s, options = {}) => orig.escape(s, ext(def, options)),
1179
+ filter: (pattern, options = {}) => orig.filter(pattern, ext(def, options)),
1180
+ defaults: (options) => orig.defaults(ext(def, options)),
1181
+ makeRe: (pattern, options = {}) => orig.makeRe(pattern, ext(def, options)),
1182
+ braceExpand: (pattern, options = {}) => orig.braceExpand(pattern, ext(def, options)),
1183
+ match: (list, pattern, options = {}) => orig.match(list, pattern, ext(def, options)),
1184
+ sep: orig.sep,
1185
+ GLOBSTAR: GLOBSTAR,
1186
+ });
1187
+ };
1188
+ minimatch.defaults = defaults;
1189
+ // Brace expansion:
1190
+ // a{b,c}d -> abd acd
1191
+ // a{b,}c -> abc ac
1192
+ // a{0..3}d -> a0d a1d a2d a3d
1193
+ // a{b,c{d,e}f}g -> abg acdfg acefg
1194
+ // a{b,c}d{e,f}g -> abdeg acdeg abdeg abdfg
1195
+ //
1196
+ // Invalid sets are not expanded.
1197
+ // a{2..}b -> a{2..}b
1198
+ // a{b}c -> a{b}c
1199
+ const braceExpand = (pattern, options = {}) => {
1200
+ assertValidPattern(pattern);
1201
+ // Thanks to Yeting Li <https://github.com/yetingli> for
1202
+ // improving this regexp to avoid a ReDOS vulnerability.
1203
+ if (options.nobrace || !/\{(?:(?!\{).)*\}/.test(pattern)) {
1204
+ // shortcut. no need to expand.
1205
+ return [pattern];
1206
+ }
1207
+ return expand(pattern);
1208
+ };
1209
+ minimatch.braceExpand = braceExpand;
1210
+ // parse a component of the expanded set.
1211
+ // At this point, no pattern may contain "/" in it
1212
+ // so we're going to return a 2d array, where each entry is the full
1213
+ // pattern, split on '/', and then turned into a regular expression.
1214
+ // A regexp is made at the end which joins each array with an
1215
+ // escaped /, and another full one which joins each regexp with |.
1216
+ //
1217
+ // Following the lead of Bash 4.1, note that "**" only has special meaning
1218
+ // when it is the *only* thing in a path portion. Otherwise, any series
1219
+ // of * is equivalent to a single *. Globstar behavior is enabled by
1220
+ // default, and can be disabled by setting options.noglobstar.
1221
+ const makeRe = (pattern, options = {}) => new Minimatch(pattern, options).makeRe();
1222
+ minimatch.makeRe = makeRe;
1223
+ const match = (list, pattern, options = {}) => {
1224
+ const mm = new Minimatch(pattern, options);
1225
+ list = list.filter(f => mm.match(f));
1226
+ if (mm.options.nonull && !list.length) {
1227
+ list.push(pattern);
1228
+ }
1229
+ return list;
1230
+ };
1231
+ minimatch.match = match;
1232
+ // replace stuff like \* with *
1233
+ const globMagic = /[?*]|[+@!]\(.*?\)|\[|\]/;
1234
+ const regExpEscape = (s) => s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
1235
+ class Minimatch {
1236
+ options;
1237
+ set;
1238
+ pattern;
1239
+ windowsPathsNoEscape;
1240
+ nonegate;
1241
+ negate;
1242
+ comment;
1243
+ empty;
1244
+ preserveMultipleSlashes;
1245
+ partial;
1246
+ globSet;
1247
+ globParts;
1248
+ nocase;
1249
+ isWindows;
1250
+ platform;
1251
+ windowsNoMagicRoot;
1252
+ regexp;
1253
+ constructor(pattern, options = {}) {
1254
+ assertValidPattern(pattern);
1255
+ options = options || {};
1256
+ this.options = options;
1257
+ this.pattern = pattern;
1258
+ this.platform = options.platform || defaultPlatform;
1259
+ this.isWindows = this.platform === 'win32';
1260
+ this.windowsPathsNoEscape =
1261
+ !!options.windowsPathsNoEscape || options.allowWindowsEscape === false;
1262
+ if (this.windowsPathsNoEscape) {
1263
+ this.pattern = this.pattern.replace(/\\/g, '/');
1264
+ }
1265
+ this.preserveMultipleSlashes = !!options.preserveMultipleSlashes;
1266
+ this.regexp = null;
1267
+ this.negate = false;
1268
+ this.nonegate = !!options.nonegate;
1269
+ this.comment = false;
1270
+ this.empty = false;
1271
+ this.partial = !!options.partial;
1272
+ this.nocase = !!this.options.nocase;
1273
+ this.windowsNoMagicRoot =
1274
+ options.windowsNoMagicRoot !== undefined
1275
+ ? options.windowsNoMagicRoot
1276
+ : !!(this.isWindows && this.nocase);
1277
+ this.globSet = [];
1278
+ this.globParts = [];
1279
+ this.set = [];
1280
+ // make the set of regexps etc.
1281
+ this.make();
1282
+ }
1283
+ hasMagic() {
1284
+ if (this.options.magicalBraces && this.set.length > 1) {
1285
+ return true;
1286
+ }
1287
+ for (const pattern of this.set) {
1288
+ for (const part of pattern) {
1289
+ if (typeof part !== 'string')
1290
+ return true;
1291
+ }
1292
+ }
1293
+ return false;
1294
+ }
1295
+ debug(..._) { }
1296
+ make() {
1297
+ const pattern = this.pattern;
1298
+ const options = this.options;
1299
+ // empty patterns and comments match nothing.
1300
+ if (!options.nocomment && pattern.charAt(0) === '#') {
1301
+ this.comment = true;
1302
+ return;
1303
+ }
1304
+ if (!pattern) {
1305
+ this.empty = true;
1306
+ return;
1307
+ }
1308
+ // step 1: figure out negation, etc.
1309
+ this.parseNegate();
1310
+ // step 2: expand braces
1311
+ this.globSet = [...new Set(this.braceExpand())];
1312
+ if (options.debug) {
1313
+ this.debug = (...args) => console.error(...args);
1314
+ }
1315
+ this.debug(this.pattern, this.globSet);
1316
+ // step 3: now we have a set, so turn each one into a series of
1317
+ // path-portion matching patterns.
1318
+ // These will be regexps, except in the case of "**", which is
1319
+ // set to the GLOBSTAR object for globstar behavior,
1320
+ // and will not contain any / characters
1321
+ //
1322
+ // First, we preprocess to make the glob pattern sets a bit simpler
1323
+ // and deduped. There are some perf-killing patterns that can cause
1324
+ // problems with a glob walk, but we can simplify them down a bit.
1325
+ const rawGlobParts = this.globSet.map(s => this.slashSplit(s));
1326
+ this.globParts = this.preprocess(rawGlobParts);
1327
+ this.debug(this.pattern, this.globParts);
1328
+ // glob --> regexps
1329
+ let set = this.globParts.map((s, _, __) => {
1330
+ if (this.isWindows && this.windowsNoMagicRoot) {
1331
+ // check if it's a drive or unc path.
1332
+ const isUNC = s[0] === '' &&
1333
+ s[1] === '' &&
1334
+ (s[2] === '?' || !globMagic.test(s[2])) &&
1335
+ !globMagic.test(s[3]);
1336
+ const isDrive = /^[a-z]:/i.test(s[0]);
1337
+ if (isUNC) {
1338
+ return [...s.slice(0, 4), ...s.slice(4).map(ss => this.parse(ss))];
1339
+ }
1340
+ else if (isDrive) {
1341
+ return [s[0], ...s.slice(1).map(ss => this.parse(ss))];
1342
+ }
1343
+ }
1344
+ return s.map(ss => this.parse(ss));
1345
+ });
1346
+ this.debug(this.pattern, set);
1347
+ // filter out everything that didn't compile properly.
1348
+ this.set = set.filter(s => s.indexOf(false) === -1);
1349
+ // do not treat the ? in UNC paths as magic
1350
+ if (this.isWindows) {
1351
+ for (let i = 0; i < this.set.length; i++) {
1352
+ const p = this.set[i];
1353
+ if (p[0] === '' &&
1354
+ p[1] === '' &&
1355
+ this.globParts[i][2] === '?' &&
1356
+ typeof p[3] === 'string' &&
1357
+ /^[a-z]:$/i.test(p[3])) {
1358
+ p[2] = '?';
1359
+ }
1360
+ }
1361
+ }
1362
+ this.debug(this.pattern, this.set);
1363
+ }
1364
+ // various transforms to equivalent pattern sets that are
1365
+ // faster to process in a filesystem walk. The goal is to
1366
+ // eliminate what we can, and push all ** patterns as far
1367
+ // to the right as possible, even if it increases the number
1368
+ // of patterns that we have to process.
1369
+ preprocess(globParts) {
1370
+ // if we're not in globstar mode, then turn all ** into *
1371
+ if (this.options.noglobstar) {
1372
+ for (let i = 0; i < globParts.length; i++) {
1373
+ for (let j = 0; j < globParts[i].length; j++) {
1374
+ if (globParts[i][j] === '**') {
1375
+ globParts[i][j] = '*';
1376
+ }
1377
+ }
1378
+ }
1379
+ }
1380
+ const { optimizationLevel = 1 } = this.options;
1381
+ if (optimizationLevel >= 2) {
1382
+ // aggressive optimization for the purpose of fs walking
1383
+ globParts = this.firstPhasePreProcess(globParts);
1384
+ globParts = this.secondPhasePreProcess(globParts);
1385
+ }
1386
+ else if (optimizationLevel >= 1) {
1387
+ // just basic optimizations to remove some .. parts
1388
+ globParts = this.levelOneOptimize(globParts);
1389
+ }
1390
+ else {
1391
+ // just collapse multiple ** portions into one
1392
+ globParts = this.adjascentGlobstarOptimize(globParts);
1393
+ }
1394
+ return globParts;
1395
+ }
1396
+ // just get rid of adjascent ** portions
1397
+ adjascentGlobstarOptimize(globParts) {
1398
+ return globParts.map(parts => {
1399
+ let gs = -1;
1400
+ while (-1 !== (gs = parts.indexOf('**', gs + 1))) {
1401
+ let i = gs;
1402
+ while (parts[i + 1] === '**') {
1403
+ i++;
1404
+ }
1405
+ if (i !== gs) {
1406
+ parts.splice(gs, i - gs);
1407
+ }
1408
+ }
1409
+ return parts;
1410
+ });
1411
+ }
1412
+ // get rid of adjascent ** and resolve .. portions
1413
+ levelOneOptimize(globParts) {
1414
+ return globParts.map(parts => {
1415
+ parts = parts.reduce((set, part) => {
1416
+ const prev = set[set.length - 1];
1417
+ if (part === '**' && prev === '**') {
1418
+ return set;
1419
+ }
1420
+ if (part === '..') {
1421
+ if (prev && prev !== '..' && prev !== '.' && prev !== '**') {
1422
+ set.pop();
1423
+ return set;
1424
+ }
1425
+ }
1426
+ set.push(part);
1427
+ return set;
1428
+ }, []);
1429
+ return parts.length === 0 ? [''] : parts;
1430
+ });
1431
+ }
1432
+ levelTwoFileOptimize(parts) {
1433
+ if (!Array.isArray(parts)) {
1434
+ parts = this.slashSplit(parts);
1435
+ }
1436
+ let didSomething = false;
1437
+ do {
1438
+ didSomething = false;
1439
+ // <pre>/<e>/<rest> -> <pre>/<rest>
1440
+ if (!this.preserveMultipleSlashes) {
1441
+ for (let i = 1; i < parts.length - 1; i++) {
1442
+ const p = parts[i];
1443
+ // don't squeeze out UNC patterns
1444
+ if (i === 1 && p === '' && parts[0] === '')
1445
+ continue;
1446
+ if (p === '.' || p === '') {
1447
+ didSomething = true;
1448
+ parts.splice(i, 1);
1449
+ i--;
1450
+ }
1451
+ }
1452
+ if (parts[0] === '.' &&
1453
+ parts.length === 2 &&
1454
+ (parts[1] === '.' || parts[1] === '')) {
1455
+ didSomething = true;
1456
+ parts.pop();
1457
+ }
1458
+ }
1459
+ // <pre>/<p>/../<rest> -> <pre>/<rest>
1460
+ let dd = 0;
1461
+ while (-1 !== (dd = parts.indexOf('..', dd + 1))) {
1462
+ const p = parts[dd - 1];
1463
+ if (p && p !== '.' && p !== '..' && p !== '**') {
1464
+ didSomething = true;
1465
+ parts.splice(dd - 1, 2);
1466
+ dd -= 2;
1467
+ }
1468
+ }
1469
+ } while (didSomething);
1470
+ return parts.length === 0 ? [''] : parts;
1471
+ }
1472
+ // First phase: single-pattern processing
1473
+ // <pre> is 1 or more portions
1474
+ // <rest> is 1 or more portions
1475
+ // <p> is any portion other than ., .., '', or **
1476
+ // <e> is . or ''
1477
+ //
1478
+ // **/.. is *brutal* for filesystem walking performance, because
1479
+ // it effectively resets the recursive walk each time it occurs,
1480
+ // and ** cannot be reduced out by a .. pattern part like a regexp
1481
+ // or most strings (other than .., ., and '') can be.
1482
+ //
1483
+ // <pre>/**/../<p>/<p>/<rest> -> {<pre>/../<p>/<p>/<rest>,<pre>/**/<p>/<p>/<rest>}
1484
+ // <pre>/<e>/<rest> -> <pre>/<rest>
1485
+ // <pre>/<p>/../<rest> -> <pre>/<rest>
1486
+ // **/**/<rest> -> **/<rest>
1487
+ //
1488
+ // **/*/<rest> -> */**/<rest> <== not valid because ** doesn't follow
1489
+ // this WOULD be allowed if ** did follow symlinks, or * didn't
1490
+ firstPhasePreProcess(globParts) {
1491
+ let didSomething = false;
1492
+ do {
1493
+ didSomething = false;
1494
+ // <pre>/**/../<p>/<p>/<rest> -> {<pre>/../<p>/<p>/<rest>,<pre>/**/<p>/<p>/<rest>}
1495
+ for (let parts of globParts) {
1496
+ let gs = -1;
1497
+ while (-1 !== (gs = parts.indexOf('**', gs + 1))) {
1498
+ let gss = gs;
1499
+ while (parts[gss + 1] === '**') {
1500
+ // <pre>/**/**/<rest> -> <pre>/**/<rest>
1501
+ gss++;
1502
+ }
1503
+ // eg, if gs is 2 and gss is 4, that means we have 3 **
1504
+ // parts, and can remove 2 of them.
1505
+ if (gss > gs) {
1506
+ parts.splice(gs + 1, gss - gs);
1507
+ }
1508
+ let next = parts[gs + 1];
1509
+ const p = parts[gs + 2];
1510
+ const p2 = parts[gs + 3];
1511
+ if (next !== '..')
1512
+ continue;
1513
+ if (!p ||
1514
+ p === '.' ||
1515
+ p === '..' ||
1516
+ !p2 ||
1517
+ p2 === '.' ||
1518
+ p2 === '..') {
1519
+ continue;
1520
+ }
1521
+ didSomething = true;
1522
+ // edit parts in place, and push the new one
1523
+ parts.splice(gs, 1);
1524
+ const other = parts.slice(0);
1525
+ other[gs] = '**';
1526
+ globParts.push(other);
1527
+ gs--;
1528
+ }
1529
+ // <pre>/<e>/<rest> -> <pre>/<rest>
1530
+ if (!this.preserveMultipleSlashes) {
1531
+ for (let i = 1; i < parts.length - 1; i++) {
1532
+ const p = parts[i];
1533
+ // don't squeeze out UNC patterns
1534
+ if (i === 1 && p === '' && parts[0] === '')
1535
+ continue;
1536
+ if (p === '.' || p === '') {
1537
+ didSomething = true;
1538
+ parts.splice(i, 1);
1539
+ i--;
1540
+ }
1541
+ }
1542
+ if (parts[0] === '.' &&
1543
+ parts.length === 2 &&
1544
+ (parts[1] === '.' || parts[1] === '')) {
1545
+ didSomething = true;
1546
+ parts.pop();
1547
+ }
1548
+ }
1549
+ // <pre>/<p>/../<rest> -> <pre>/<rest>
1550
+ let dd = 0;
1551
+ while (-1 !== (dd = parts.indexOf('..', dd + 1))) {
1552
+ const p = parts[dd - 1];
1553
+ if (p && p !== '.' && p !== '..' && p !== '**') {
1554
+ didSomething = true;
1555
+ const needDot = dd === 1 && parts[dd + 1] === '**';
1556
+ const splin = needDot ? ['.'] : [];
1557
+ parts.splice(dd - 1, 2, ...splin);
1558
+ if (parts.length === 0)
1559
+ parts.push('');
1560
+ dd -= 2;
1561
+ }
1562
+ }
1563
+ }
1564
+ } while (didSomething);
1565
+ return globParts;
1566
+ }
1567
+ // second phase: multi-pattern dedupes
1568
+ // {<pre>/*/<rest>,<pre>/<p>/<rest>} -> <pre>/*/<rest>
1569
+ // {<pre>/<rest>,<pre>/<rest>} -> <pre>/<rest>
1570
+ // {<pre>/**/<rest>,<pre>/<rest>} -> <pre>/**/<rest>
1571
+ //
1572
+ // {<pre>/**/<rest>,<pre>/**/<p>/<rest>} -> <pre>/**/<rest>
1573
+ // ^-- not valid because ** doens't follow symlinks
1574
+ secondPhasePreProcess(globParts) {
1575
+ for (let i = 0; i < globParts.length - 1; i++) {
1576
+ for (let j = i + 1; j < globParts.length; j++) {
1577
+ const matched = this.partsMatch(globParts[i], globParts[j], !this.preserveMultipleSlashes);
1578
+ if (matched) {
1579
+ globParts[i] = [];
1580
+ globParts[j] = matched;
1581
+ break;
1582
+ }
1583
+ }
1584
+ }
1585
+ return globParts.filter(gs => gs.length);
1586
+ }
1587
+ partsMatch(a, b, emptyGSMatch = false) {
1588
+ let ai = 0;
1589
+ let bi = 0;
1590
+ let result = [];
1591
+ let which = '';
1592
+ while (ai < a.length && bi < b.length) {
1593
+ if (a[ai] === b[bi]) {
1594
+ result.push(which === 'b' ? b[bi] : a[ai]);
1595
+ ai++;
1596
+ bi++;
1597
+ }
1598
+ else if (emptyGSMatch && a[ai] === '**' && b[bi] === a[ai + 1]) {
1599
+ result.push(a[ai]);
1600
+ ai++;
1601
+ }
1602
+ else if (emptyGSMatch && b[bi] === '**' && a[ai] === b[bi + 1]) {
1603
+ result.push(b[bi]);
1604
+ bi++;
1605
+ }
1606
+ else if (a[ai] === '*' &&
1607
+ b[bi] &&
1608
+ (this.options.dot || !b[bi].startsWith('.')) &&
1609
+ b[bi] !== '**') {
1610
+ if (which === 'b')
1611
+ return false;
1612
+ which = 'a';
1613
+ result.push(a[ai]);
1614
+ ai++;
1615
+ bi++;
1616
+ }
1617
+ else if (b[bi] === '*' &&
1618
+ a[ai] &&
1619
+ (this.options.dot || !a[ai].startsWith('.')) &&
1620
+ a[ai] !== '**') {
1621
+ if (which === 'a')
1622
+ return false;
1623
+ which = 'b';
1624
+ result.push(b[bi]);
1625
+ ai++;
1626
+ bi++;
1627
+ }
1628
+ else {
1629
+ return false;
1630
+ }
1631
+ }
1632
+ // if we fall out of the loop, it means they two are identical
1633
+ // as long as their lengths match
1634
+ return a.length === b.length && result;
1635
+ }
1636
+ parseNegate() {
1637
+ if (this.nonegate)
1638
+ return;
1639
+ const pattern = this.pattern;
1640
+ let negate = false;
1641
+ let negateOffset = 0;
1642
+ for (let i = 0; i < pattern.length && pattern.charAt(i) === '!'; i++) {
1643
+ negate = !negate;
1644
+ negateOffset++;
1645
+ }
1646
+ if (negateOffset)
1647
+ this.pattern = pattern.slice(negateOffset);
1648
+ this.negate = negate;
1649
+ }
1650
+ // set partial to true to test if, for example,
1651
+ // "/a/b" matches the start of "/*/b/*/d"
1652
+ // Partial means, if you run out of file before you run
1653
+ // out of pattern, then that's fine, as long as all
1654
+ // the parts match.
1655
+ matchOne(file, pattern, partial = false) {
1656
+ const options = this.options;
1657
+ // UNC paths like //?/X:/... can match X:/... and vice versa
1658
+ // Drive letters in absolute drive or unc paths are always compared
1659
+ // case-insensitively.
1660
+ if (this.isWindows) {
1661
+ const fileDrive = typeof file[0] === 'string' && /^[a-z]:$/i.test(file[0]);
1662
+ const fileUNC = !fileDrive &&
1663
+ file[0] === '' &&
1664
+ file[1] === '' &&
1665
+ file[2] === '?' &&
1666
+ /^[a-z]:$/i.test(file[3]);
1667
+ const patternDrive = typeof pattern[0] === 'string' && /^[a-z]:$/i.test(pattern[0]);
1668
+ const patternUNC = !patternDrive &&
1669
+ pattern[0] === '' &&
1670
+ pattern[1] === '' &&
1671
+ pattern[2] === '?' &&
1672
+ typeof pattern[3] === 'string' &&
1673
+ /^[a-z]:$/i.test(pattern[3]);
1674
+ const fdi = fileUNC ? 3 : fileDrive ? 0 : undefined;
1675
+ const pdi = patternUNC ? 3 : patternDrive ? 0 : undefined;
1676
+ if (typeof fdi === 'number' && typeof pdi === 'number') {
1677
+ const [fd, pd] = [file[fdi], pattern[pdi]];
1678
+ if (fd.toLowerCase() === pd.toLowerCase()) {
1679
+ pattern[pdi] = fd;
1680
+ if (pdi > fdi) {
1681
+ pattern = pattern.slice(pdi);
1682
+ }
1683
+ else if (fdi > pdi) {
1684
+ file = file.slice(fdi);
1685
+ }
1686
+ }
1687
+ }
1688
+ }
1689
+ // resolve and reduce . and .. portions in the file as well.
1690
+ // don't need to do the second phase, because it's only one string[]
1691
+ const { optimizationLevel = 1 } = this.options;
1692
+ if (optimizationLevel >= 2) {
1693
+ file = this.levelTwoFileOptimize(file);
1694
+ }
1695
+ this.debug('matchOne', this, { file, pattern });
1696
+ this.debug('matchOne', file.length, pattern.length);
1697
+ for (var fi = 0, pi = 0, fl = file.length, pl = pattern.length; fi < fl && pi < pl; fi++, pi++) {
1698
+ this.debug('matchOne loop');
1699
+ var p = pattern[pi];
1700
+ var f = file[fi];
1701
+ this.debug(pattern, p, f);
1702
+ // should be impossible.
1703
+ // some invalid regexp stuff in the set.
1704
+ /* c8 ignore start */
1705
+ if (p === false) {
1706
+ return false;
1707
+ }
1708
+ /* c8 ignore stop */
1709
+ if (p === GLOBSTAR) {
1710
+ this.debug('GLOBSTAR', [pattern, p, f]);
1711
+ // "**"
1712
+ // a/**/b/**/c would match the following:
1713
+ // a/b/x/y/z/c
1714
+ // a/x/y/z/b/c
1715
+ // a/b/x/b/x/c
1716
+ // a/b/c
1717
+ // To do this, take the rest of the pattern after
1718
+ // the **, and see if it would match the file remainder.
1719
+ // If so, return success.
1720
+ // If not, the ** "swallows" a segment, and try again.
1721
+ // This is recursively awful.
1722
+ //
1723
+ // a/**/b/**/c matching a/b/x/y/z/c
1724
+ // - a matches a
1725
+ // - doublestar
1726
+ // - matchOne(b/x/y/z/c, b/**/c)
1727
+ // - b matches b
1728
+ // - doublestar
1729
+ // - matchOne(x/y/z/c, c) -> no
1730
+ // - matchOne(y/z/c, c) -> no
1731
+ // - matchOne(z/c, c) -> no
1732
+ // - matchOne(c, c) yes, hit
1733
+ var fr = fi;
1734
+ var pr = pi + 1;
1735
+ if (pr === pl) {
1736
+ this.debug('** at the end');
1737
+ // a ** at the end will just swallow the rest.
1738
+ // We have found a match.
1739
+ // however, it will not swallow /.x, unless
1740
+ // options.dot is set.
1741
+ // . and .. are *never* matched by **, for explosively
1742
+ // exponential reasons.
1743
+ for (; fi < fl; fi++) {
1744
+ if (file[fi] === '.' ||
1745
+ file[fi] === '..' ||
1746
+ (!options.dot && file[fi].charAt(0) === '.'))
1747
+ return false;
1748
+ }
1749
+ return true;
1750
+ }
1751
+ // ok, let's see if we can swallow whatever we can.
1752
+ while (fr < fl) {
1753
+ var swallowee = file[fr];
1754
+ this.debug('\nglobstar while', file, fr, pattern, pr, swallowee);
1755
+ // XXX remove this slice. Just pass the start index.
1756
+ if (this.matchOne(file.slice(fr), pattern.slice(pr), partial)) {
1757
+ this.debug('globstar found match!', fr, fl, swallowee);
1758
+ // found a match.
1759
+ return true;
1760
+ }
1761
+ else {
1762
+ // can't swallow "." or ".." ever.
1763
+ // can only swallow ".foo" when explicitly asked.
1764
+ if (swallowee === '.' ||
1765
+ swallowee === '..' ||
1766
+ (!options.dot && swallowee.charAt(0) === '.')) {
1767
+ this.debug('dot detected!', file, fr, pattern, pr);
1768
+ break;
1769
+ }
1770
+ // ** swallows a segment, and continue.
1771
+ this.debug('globstar swallow a segment, and continue');
1772
+ fr++;
1773
+ }
1774
+ }
1775
+ // no match was found.
1776
+ // However, in partial mode, we can't say this is necessarily over.
1777
+ /* c8 ignore start */
1778
+ if (partial) {
1779
+ // ran out of file
1780
+ this.debug('\n>>> no match, partial?', file, fr, pattern, pr);
1781
+ if (fr === fl) {
1782
+ return true;
1783
+ }
1784
+ }
1785
+ /* c8 ignore stop */
1786
+ return false;
1787
+ }
1788
+ // something other than **
1789
+ // non-magic patterns just have to match exactly
1790
+ // patterns with magic have been turned into regexps.
1791
+ let hit;
1792
+ if (typeof p === 'string') {
1793
+ hit = f === p;
1794
+ this.debug('string match', p, f, hit);
1795
+ }
1796
+ else {
1797
+ hit = p.test(f);
1798
+ this.debug('pattern match', p, f, hit);
1799
+ }
1800
+ if (!hit)
1801
+ return false;
1802
+ }
1803
+ // Note: ending in / means that we'll get a final ""
1804
+ // at the end of the pattern. This can only match a
1805
+ // corresponding "" at the end of the file.
1806
+ // If the file ends in /, then it can only match a
1807
+ // a pattern that ends in /, unless the pattern just
1808
+ // doesn't have any more for it. But, a/b/ should *not*
1809
+ // match "a/b/*", even though "" matches against the
1810
+ // [^/]*? pattern, except in partial mode, where it might
1811
+ // simply not be reached yet.
1812
+ // However, a/b/ should still satisfy a/*
1813
+ // now either we fell off the end of the pattern, or we're done.
1814
+ if (fi === fl && pi === pl) {
1815
+ // ran out of pattern and filename at the same time.
1816
+ // an exact hit!
1817
+ return true;
1818
+ }
1819
+ else if (fi === fl) {
1820
+ // ran out of file, but still had pattern left.
1821
+ // this is ok if we're doing the match as part of
1822
+ // a glob fs traversal.
1823
+ return partial;
1824
+ }
1825
+ else if (pi === pl) {
1826
+ // ran out of pattern, still have file left.
1827
+ // this is only acceptable if we're on the very last
1828
+ // empty segment of a file with a trailing slash.
1829
+ // a/* should match a/b/
1830
+ return fi === fl - 1 && file[fi] === '';
1831
+ /* c8 ignore start */
1832
+ }
1833
+ else {
1834
+ // should be unreachable.
1835
+ throw new Error('wtf?');
1836
+ }
1837
+ /* c8 ignore stop */
1838
+ }
1839
+ braceExpand() {
1840
+ return braceExpand(this.pattern, this.options);
1841
+ }
1842
+ parse(pattern) {
1843
+ assertValidPattern(pattern);
1844
+ const options = this.options;
1845
+ // shortcuts
1846
+ if (pattern === '**')
1847
+ return GLOBSTAR;
1848
+ if (pattern === '')
1849
+ return '';
1850
+ // far and away, the most common glob pattern parts are
1851
+ // *, *.*, and *.<ext> Add a fast check method for those.
1852
+ let m;
1853
+ let fastTest = null;
1854
+ if ((m = pattern.match(starRE))) {
1855
+ fastTest = options.dot ? starTestDot : starTest;
1856
+ }
1857
+ else if ((m = pattern.match(starDotExtRE))) {
1858
+ fastTest = (options.nocase
1859
+ ? options.dot
1860
+ ? starDotExtTestNocaseDot
1861
+ : starDotExtTestNocase
1862
+ : options.dot
1863
+ ? starDotExtTestDot
1864
+ : starDotExtTest)(m[1]);
1865
+ }
1866
+ else if ((m = pattern.match(qmarksRE))) {
1867
+ fastTest = (options.nocase
1868
+ ? options.dot
1869
+ ? qmarksTestNocaseDot
1870
+ : qmarksTestNocase
1871
+ : options.dot
1872
+ ? qmarksTestDot
1873
+ : qmarksTest)(m);
1874
+ }
1875
+ else if ((m = pattern.match(starDotStarRE))) {
1876
+ fastTest = options.dot ? starDotStarTestDot : starDotStarTest;
1877
+ }
1878
+ else if ((m = pattern.match(dotStarRE))) {
1879
+ fastTest = dotStarTest;
1880
+ }
1881
+ const re = AST.fromGlob(pattern, this.options).toMMPattern();
1882
+ if (fastTest && typeof re === 'object') {
1883
+ // Avoids overriding in frozen environments
1884
+ Reflect.defineProperty(re, 'test', { value: fastTest });
1885
+ }
1886
+ return re;
1887
+ }
1888
+ makeRe() {
1889
+ if (this.regexp || this.regexp === false)
1890
+ return this.regexp;
1891
+ // at this point, this.set is a 2d array of partial
1892
+ // pattern strings, or "**".
1893
+ //
1894
+ // It's better to use .match(). This function shouldn't
1895
+ // be used, really, but it's pretty convenient sometimes,
1896
+ // when you just want to work with a regex.
1897
+ const set = this.set;
1898
+ if (!set.length) {
1899
+ this.regexp = false;
1900
+ return this.regexp;
1901
+ }
1902
+ const options = this.options;
1903
+ const twoStar = options.noglobstar
1904
+ ? star
1905
+ : options.dot
1906
+ ? twoStarDot
1907
+ : twoStarNoDot;
1908
+ const flags = new Set(options.nocase ? ['i'] : []);
1909
+ // regexpify non-globstar patterns
1910
+ // if ** is only item, then we just do one twoStar
1911
+ // if ** is first, and there are more, prepend (\/|twoStar\/)? to next
1912
+ // if ** is last, append (\/twoStar|) to previous
1913
+ // if ** is in the middle, append (\/|\/twoStar\/) to previous
1914
+ // then filter out GLOBSTAR symbols
1915
+ let re = set
1916
+ .map(pattern => {
1917
+ const pp = pattern.map(p => {
1918
+ if (p instanceof RegExp) {
1919
+ for (const f of p.flags.split(''))
1920
+ flags.add(f);
1921
+ }
1922
+ return typeof p === 'string'
1923
+ ? regExpEscape(p)
1924
+ : p === GLOBSTAR
1925
+ ? GLOBSTAR
1926
+ : p._src;
1927
+ });
1928
+ pp.forEach((p, i) => {
1929
+ const next = pp[i + 1];
1930
+ const prev = pp[i - 1];
1931
+ if (p !== GLOBSTAR || prev === GLOBSTAR) {
1932
+ return;
1933
+ }
1934
+ if (prev === undefined) {
1935
+ if (next !== undefined && next !== GLOBSTAR) {
1936
+ pp[i + 1] = '(?:\\/|' + twoStar + '\\/)?' + next;
1937
+ }
1938
+ else {
1939
+ pp[i] = twoStar;
1940
+ }
1941
+ }
1942
+ else if (next === undefined) {
1943
+ pp[i - 1] = prev + '(?:\\/|\\/' + twoStar + ')?';
1944
+ }
1945
+ else if (next !== GLOBSTAR) {
1946
+ pp[i - 1] = prev + '(?:\\/|\\/' + twoStar + '\\/)' + next;
1947
+ pp[i + 1] = GLOBSTAR;
1948
+ }
1949
+ });
1950
+ const filtered = pp.filter(p => p !== GLOBSTAR);
1951
+ // For partial matches, we need to make the pattern match
1952
+ // any prefix of the full path. We do this by generating
1953
+ // alternative patterns that match progressively longer prefixes.
1954
+ if (this.partial && filtered.length >= 1) {
1955
+ const prefixes = [];
1956
+ for (let i = 1; i <= filtered.length; i++) {
1957
+ prefixes.push(filtered.slice(0, i).join('/'));
1958
+ }
1959
+ return '(?:' + prefixes.join('|') + ')';
1960
+ }
1961
+ return filtered.join('/');
1962
+ })
1963
+ .join('|');
1964
+ // need to wrap in parens if we had more than one thing with |,
1965
+ // otherwise only the first will be anchored to ^ and the last to $
1966
+ const [open, close] = set.length > 1 ? ['(?:', ')'] : ['', ''];
1967
+ // must match entire pattern
1968
+ // ending in a * or ** will make it less strict.
1969
+ re = '^' + open + re + close + '$';
1970
+ // In partial mode, '/' should always match as it's a valid prefix for any pattern
1971
+ if (this.partial) {
1972
+ re = '^(?:\\/|' + open + re.slice(1, -1) + close + ')$';
1973
+ }
1974
+ // can match anything, as long as it's not this.
1975
+ if (this.negate)
1976
+ re = '^(?!' + re + ').+$';
1977
+ try {
1978
+ this.regexp = new RegExp(re, [...flags].join(''));
1979
+ /* c8 ignore start */
1980
+ }
1981
+ catch (ex) {
1982
+ // should be impossible
1983
+ this.regexp = false;
1984
+ }
1985
+ /* c8 ignore stop */
1986
+ return this.regexp;
1987
+ }
1988
+ slashSplit(p) {
1989
+ // if p starts with // on windows, we preserve that
1990
+ // so that UNC paths aren't broken. Otherwise, any number of
1991
+ // / characters are coalesced into one, unless
1992
+ // preserveMultipleSlashes is set to true.
1993
+ if (this.preserveMultipleSlashes) {
1994
+ return p.split('/');
1995
+ }
1996
+ else if (this.isWindows && /^\/\/[^\/]+/.test(p)) {
1997
+ // add an extra '' for the one we lose
1998
+ return ['', ...p.split(/\/+/)];
1999
+ }
2000
+ else {
2001
+ return p.split(/\/+/);
2002
+ }
2003
+ }
2004
+ match(f, partial = this.partial) {
2005
+ this.debug('match', f, this.pattern);
2006
+ // short-circuit in the case of busted things.
2007
+ // comments, etc.
2008
+ if (this.comment) {
2009
+ return false;
2010
+ }
2011
+ if (this.empty) {
2012
+ return f === '';
2013
+ }
2014
+ if (f === '/' && partial) {
2015
+ return true;
2016
+ }
2017
+ const options = this.options;
2018
+ // windows: need to use /, not \
2019
+ if (this.isWindows) {
2020
+ f = f.split('\\').join('/');
2021
+ }
2022
+ // treat the test path as a set of pathparts.
2023
+ const ff = this.slashSplit(f);
2024
+ this.debug(this.pattern, 'split', ff);
2025
+ // just ONE of the pattern sets in this.set needs to match
2026
+ // in order for it to be valid. If negating, then just one
2027
+ // match means that we have failed.
2028
+ // Either way, return on the first hit.
2029
+ const set = this.set;
2030
+ this.debug(this.pattern, 'set', set);
2031
+ // Find the basename of the path by looking for the last non-empty segment
2032
+ let filename = ff[ff.length - 1];
2033
+ if (!filename) {
2034
+ for (let i = ff.length - 2; !filename && i >= 0; i--) {
2035
+ filename = ff[i];
2036
+ }
2037
+ }
2038
+ for (let i = 0; i < set.length; i++) {
2039
+ const pattern = set[i];
2040
+ let file = ff;
2041
+ if (options.matchBase && pattern.length === 1) {
2042
+ file = [filename];
2043
+ }
2044
+ const hit = this.matchOne(file, pattern, partial);
2045
+ if (hit) {
2046
+ if (options.flipNegate) {
2047
+ return true;
2048
+ }
2049
+ return !this.negate;
2050
+ }
2051
+ }
2052
+ // didn't get any hits. this is success if it's a negative
2053
+ // pattern, failure otherwise.
2054
+ if (options.flipNegate) {
2055
+ return false;
2056
+ }
2057
+ return this.negate;
2058
+ }
2059
+ static defaults(def) {
2060
+ return minimatch.defaults(def).Minimatch;
2061
+ }
2062
+ }
2063
+ /* c8 ignore stop */
2064
+ minimatch.AST = AST;
2065
+ minimatch.Minimatch = Minimatch;
2066
+ minimatch.escape = escape;
2067
+ minimatch.unescape = unescape;
2068
+
2069
+ function isDefined$1(value) {
2070
+ return value !== void 0 && value !== null;
2071
+ }
2072
+ function fmap(v, predicate) {
2073
+ if (!isDefined$1(v)) {
2074
+ return null;
2075
+ }
2076
+ return predicate(v);
2077
+ }
2078
+ function groupBy(objects, ...by) {
2079
+ if (by.length === 0) {
2080
+ return objects;
2081
+ }
2082
+ const [k0, ...kr] = by;
2083
+ const topLevelGroups = /* @__PURE__ */ new Map();
2084
+ for (const obj of objects) {
2085
+ const k = obj[k0];
2086
+ let arr = topLevelGroups.get(k);
2087
+ if (!arr) {
2088
+ arr = [];
2089
+ topLevelGroups.set(k, arr);
2090
+ }
2091
+ arr.push(obj);
2092
+ }
2093
+ return new Map(Array.from(topLevelGroups, ([k, v]) => [k, groupBy(v, ...kr)]));
2094
+ }
2095
+ function objectKeys(obj) {
2096
+ return Object.keys(obj);
2097
+ }
2098
+ function objectKeysNonEmpty(obj) {
2099
+ const keys = objectKeys(obj);
2100
+ if (keys.length === 0) {
2101
+ throw new Error("expected non-empty object");
2102
+ }
2103
+ return keys;
2104
+ }
2105
+ function assertNever(caseType) {
2106
+ throw new Error(`assertNever: ${caseType}`);
2107
+ }
2108
+ async function getOrSetFromMapWithEnabled(params) {
2109
+ return params.shouldCache ? getOrSetFromMap(params) : params.value();
2110
+ }
2111
+ async function getOrSetFromMap(params) {
2112
+ const { map, key, value } = params;
2113
+ if (map.has(key)) {
2114
+ return map.get(key);
2115
+ }
2116
+ const val = await value();
2117
+ map.set(key, val);
2118
+ return val;
2119
+ }
2120
+ function normalizeIndent(template, ...args) {
2121
+ const fullString = template.reduce((accumulator, str, i) => {
2122
+ return accumulator + str + (args[i] || "");
2123
+ }, "");
2124
+ const lines = fullString.split("\n");
2125
+ const nonEmptyLines = lines.filter((line) => line.trim() !== "");
2126
+ if (nonEmptyLines.length === 0) {
2127
+ return "";
2128
+ }
2129
+ const indent = nonEmptyLines[0].match(/^\s*/)?.[0];
2130
+ const normalized = nonEmptyLines.map((line) => line.replace(indent, "")).join("\n");
2131
+ return normalized;
2132
+ }
2133
+
2134
+ function toCase(str, caseType) {
2135
+ switch (caseType) {
2136
+ case void 0:
2137
+ return str;
2138
+ case "snake":
2139
+ return toSnakeCase(str);
2140
+ case "camel":
2141
+ return toCamelCase(str);
2142
+ case "pascal":
2143
+ return toPascalCase(str);
2144
+ case "screaming snake":
2145
+ return toScreamingSnakeCase(str);
2146
+ default:
2147
+ assertNever(caseType);
2148
+ }
2149
+ }
2150
+ function toCamelCase(str) {
2151
+ return str.replace(/([-_][a-z])/gi, ($1) => {
2152
+ return $1.toUpperCase().replace("-", "").replace("_", "");
2153
+ });
2154
+ }
2155
+ function toPascalCase(str) {
2156
+ return toCamelCase(str).replace(/^[a-z]/, (val) => val.toUpperCase());
2157
+ }
2158
+ function toSnakeCase(str) {
2159
+ return str.replace(/([a-z])([A-Z])/g, "$1_$2").replace(/([A-Z])([A-Z][a-z])/g, "$1_$2").toLowerCase();
2160
+ }
2161
+ function toScreamingSnakeCase(str) {
2162
+ return toSnakeCase(str).toUpperCase();
2163
+ }
2164
+
2165
+ class DatabaseInitializationError extends Error {
2166
+ _tag = "DatabaseInitializationError";
2167
+ constructor(message) {
2168
+ super(`Database initialization failed (${message})`);
2169
+ this.message = message;
2170
+ }
2171
+ static of(pgError) {
2172
+ return new DatabaseInitializationError(pgError);
2173
+ }
2174
+ static to(error) {
2175
+ if (error instanceof Error) {
2176
+ return DatabaseInitializationError.of(error.message);
2177
+ }
2178
+ return DatabaseInitializationError.of("Unknown error");
2179
+ }
2180
+ toJSON() {
2181
+ return {
2182
+ _tag: this._tag,
2183
+ message: this.message
2184
+ };
2185
+ }
2186
+ }
2187
+ class InvalidMigrationsPathError extends Error {
2188
+ _tag = "InvalidMigrationsPathError";
2189
+ migrationsPath;
2190
+ constructor(migrationsPath, message) {
2191
+ super(`Failed to read migrations directory "${migrationsPath}" (${message})`);
2192
+ this.migrationsPath = migrationsPath;
2193
+ this.message = message;
2194
+ }
2195
+ static of(filePath, pgError) {
2196
+ return new InvalidMigrationsPathError(filePath, pgError);
2197
+ }
2198
+ static fromErrorC(migrationsPath) {
2199
+ return (error) => InvalidMigrationsPathError.of(migrationsPath, error.message);
2200
+ }
2201
+ toJSON() {
2202
+ return {
2203
+ _tag: this._tag,
2204
+ migrationsPath: this.migrationsPath,
2205
+ message: this.message
2206
+ };
2207
+ }
2208
+ }
2209
+ class InvalidConfigError extends Error {
2210
+ _tag = "InvalidConfigError";
2211
+ constructor(message) {
2212
+ super(`Invalid configuration (${message})`);
2213
+ this.message = message;
2214
+ }
2215
+ static of(message) {
2216
+ return new InvalidConfigError(message);
2217
+ }
2218
+ toJSON() {
2219
+ return {
2220
+ _tag: this._tag,
2221
+ message: this.message
2222
+ };
2223
+ }
2224
+ }
2225
+ class InvalidMigrationError extends Error {
2226
+ _tag = "InvalidMigrationError";
2227
+ filePath;
2228
+ constructor(filePath, message) {
2229
+ super(`Failed to run migration "${path$1.basename(filePath)}" (${message})`);
2230
+ this.filePath = filePath;
2231
+ this.message = message;
2232
+ }
2233
+ static of(filePath, pgError) {
2234
+ return new InvalidMigrationError(filePath, pgError);
2235
+ }
2236
+ static fromErrorC(migrationsPath) {
2237
+ return (error) => InvalidMigrationError.of(migrationsPath, error.message);
2238
+ }
2239
+ toJSON() {
2240
+ return {
2241
+ _tag: this._tag,
2242
+ filePath: this.filePath,
2243
+ message: this.message
2244
+ };
2245
+ }
2246
+ }
2247
+ class InvalidQueryError extends Error {
2248
+ _tag = "InvalidQueryError";
2249
+ node;
2250
+ constructor(error, node) {
2251
+ super(error);
2252
+ this.node = node;
2253
+ }
2254
+ static of(error, node) {
2255
+ return new InvalidQueryError(error, node);
2256
+ }
2257
+ toJSON() {
2258
+ return {
2259
+ _tag: this._tag,
2260
+ message: this.message,
2261
+ node: this.node
2262
+ };
2263
+ }
2264
+ }
2265
+ class InternalError extends Error {
2266
+ _tag = "InternalError";
2267
+ constructor(error) {
2268
+ super(`Internal error: ${error}`);
2269
+ }
2270
+ static of(error) {
2271
+ return new InternalError(error);
2272
+ }
2273
+ static to(error) {
2274
+ if (error instanceof AggregateError) {
2275
+ const e = InternalError.of(error.errors.map((e2) => e2.message).join(", "));
2276
+ e.stack = error.stack;
2277
+ return e;
2278
+ }
2279
+ if (error instanceof Error) {
2280
+ const e = InternalError.of(error.message);
2281
+ e.stack = error.stack;
2282
+ return e;
2283
+ }
2284
+ return InternalError.of(`Unknown (${error})`);
2285
+ }
2286
+ toJSON() {
2287
+ return {
2288
+ _tag: this._tag,
2289
+ stack: this.stack,
2290
+ message: this.message
2291
+ };
2292
+ }
2293
+ }
2294
+ class DuplicateColumnsError extends Error {
2295
+ _tag = "DuplicateColumnsError";
2296
+ columns;
2297
+ queryText;
2298
+ position;
2299
+ sourcemaps;
2300
+ constructor(params) {
2301
+ super(`Duplicate columns: ${params.columns.join(", ")}`);
2302
+ this.columns = params.columns;
2303
+ this.queryText = params.queryText;
2304
+ this.position = params.position;
2305
+ this.sourcemaps = params.sourcemaps;
2306
+ }
2307
+ static of(params) {
2308
+ return new DuplicateColumnsError(params);
2309
+ }
2310
+ toJSON() {
2311
+ return {
2312
+ _tag: this._tag,
2313
+ message: this.message,
2314
+ columns: this.columns,
2315
+ queryText: this.queryText,
2316
+ position: this.position,
2317
+ sourcemaps: this.sourcemaps
2318
+ };
2319
+ }
2320
+ }
2321
+ class PostgresError extends Error {
2322
+ _tag = "PostgresError";
2323
+ queryText;
2324
+ message;
2325
+ line;
2326
+ position;
2327
+ sourcemaps;
2328
+ constructor(params) {
2329
+ super(params.message);
2330
+ this.name = "PostgresError";
2331
+ this.queryText = params.queryText;
2332
+ this.message = params.message;
2333
+ this.line = params.line;
2334
+ this.position = Number(params.position);
2335
+ this.sourcemaps = params.sourcemaps;
2336
+ }
2337
+ static of(params) {
2338
+ return new PostgresError(params);
2339
+ }
2340
+ static to(query, error, sourcemaps) {
2341
+ if (isPostgresError(error)) {
2342
+ return PostgresError.of({
2343
+ queryText: query,
2344
+ message: error.message,
2345
+ line: error.line,
2346
+ position: error.position,
2347
+ sourcemaps
2348
+ });
2349
+ }
2350
+ return PostgresError.of({
2351
+ queryText: query,
2352
+ message: `${error}`,
2353
+ line: "1",
2354
+ position: isPgParserError(error) ? error.cursorPosition : 0,
2355
+ sourcemaps
2356
+ });
2357
+ }
2358
+ toJSON() {
2359
+ return {
2360
+ _tag: this._tag,
2361
+ queryText: this.queryText,
2362
+ message: this.message,
2363
+ line: this.line,
2364
+ position: this.position,
2365
+ sourcemaps: this.sourcemaps
2366
+ };
2367
+ }
2368
+ }
2369
+ function isPostgresError(e) {
2370
+ if (e instanceof postgres.PostgresError) {
2371
+ return true;
2372
+ }
2373
+ if (e instanceof Error && e.name === "PostgresError") {
2374
+ return true;
2375
+ }
2376
+ return false;
2377
+ }
2378
+ function isPgParserError(error) {
2379
+ return error instanceof Error && "cursorPosition" in error;
2380
+ }
2381
+
2382
+ const defaultTypeMapping = {
2383
+ // Integer types
2384
+ int2: "number",
2385
+ int4: "number",
2386
+ int8: "string",
2387
+ smallint: "number",
2388
+ int: "number",
2389
+ bigint: "string",
2390
+ // Precision types
2391
+ real: "number",
2392
+ float4: "number",
2393
+ float: "number",
2394
+ float8: "number",
2395
+ numeric: "string",
2396
+ decimal: "number",
2397
+ // Serial types
2398
+ smallserial: "number",
2399
+ serial: "number",
2400
+ bigserial: "string",
2401
+ // Common string types
2402
+ uuid: "string",
2403
+ text: "string",
2404
+ varchar: "string",
2405
+ char: "string",
2406
+ bpchar: "string",
2407
+ citext: "string",
2408
+ // Bool types
2409
+ bit: "boolean",
2410
+ bool: "boolean",
2411
+ boolean: "boolean",
2412
+ // Dates and times
2413
+ date: "Date",
2414
+ timestamp: "Date",
2415
+ timestamptz: "Date",
2416
+ time: "string",
2417
+ timetz: "string",
2418
+ interval: "string",
2419
+ // Network address types
2420
+ inet: "string",
2421
+ cidr: "string",
2422
+ macaddr: "string",
2423
+ macaddr8: "string",
2424
+ // Extra types
2425
+ money: "number",
2426
+ void: "void",
2427
+ // JSON types
2428
+ json: "any",
2429
+ jsonb: "any",
2430
+ // Bytes
2431
+ bytea: "any",
2432
+ // null
2433
+ null: "null"
2434
+ };
2435
+ const defaultTypesMap = new Map(Object.entries(defaultTypeMapping));
2436
+
2437
+ function doesMatchPattern(params) {
2438
+ const { pattern, text } = params;
2439
+ if (typeof pattern === "string") {
2440
+ return minimatch(text, pattern);
2441
+ }
2442
+ return new RegExp(`^${pattern.regex}$`).test(text);
2443
+ }
2444
+
2445
+ function isIdentifier(node) {
2446
+ return node?.type === TSESTree.AST_NODE_TYPES.Identifier;
2447
+ }
2448
+ function isMemberExpression(node) {
2449
+ return node?.type === TSESTree.AST_NODE_TYPES.MemberExpression;
2450
+ }
2451
+ function isOneOf(value, options) {
2452
+ return options.includes(value);
2453
+ }
2454
+
2455
+ function mapConnectionOptionsToString(connectionOptions) {
2456
+ return `postgres://${connectionOptions.user}:${connectionOptions.password}@${connectionOptions.host}:${connectionOptions.port}/${connectionOptions.database}`;
2457
+ }
2458
+ function parseConnection(databaseUrl) {
2459
+ const connection = pgConnectionString.parse(databaseUrl);
2460
+ if (connection.host === null) {
2461
+ throw new Error("Could not resolve database host");
2462
+ }
2463
+ if (!isDefined(connection.port)) {
2464
+ throw new Error("Could not resolve database port");
2465
+ }
2466
+ if (!isDefined(connection.user)) {
2467
+ throw new Error("Could not resolve database user");
2468
+ }
2469
+ if (!isDefined(connection.password)) {
2470
+ throw new Error("Could not resolve database password");
2471
+ }
2472
+ if (!isDefined(connection.database)) {
2473
+ throw new Error("Could not resolve database name");
2474
+ }
2475
+ return {
2476
+ host: connection.host,
2477
+ port: parseInt(connection.port, 10),
2478
+ user: connection.user,
2479
+ password: connection.password,
2480
+ database: connection.database
2481
+ };
2482
+ }
2483
+ function initDatabase(sql, database) {
2484
+ return pipe(
2485
+ TE.Do,
2486
+ TE.chain(() => dropDatabase(sql, database)),
2487
+ TE.altW(() => TE.right(void 0)),
2488
+ TE.chain(() => createDatabase(sql, database))
2489
+ );
2490
+ }
2491
+ function createDatabase(sql, database) {
2492
+ return TE.tryCatch(
2493
+ () => sql.unsafe(`CREATE DATABASE ${database}`),
2494
+ DatabaseInitializationError.to
2495
+ );
2496
+ }
2497
+ function dropDatabase(sql, database) {
2498
+ return TE.tryCatch(async () => {
2499
+ const [{ withForce }] = await sql.unsafe(`
2500
+ SELECT (string_to_array(version(), ' '))[2]::numeric >= 13 AS "withForce"
2501
+ `);
2502
+ return withForce ? sql.unsafe(`DROP DATABASE IF EXISTS ${database} WITH (FORCE)`) : sql.unsafe(`DROP DATABASE IF EXISTS ${database}`);
2503
+ }, DatabaseInitializationError.to);
2504
+ }
2505
+ function isDefined(value) {
2506
+ return value !== null && value !== void 0;
2507
+ }
2508
+
2509
+ const DEFAULT_CONNECTION_URL = "postgres://postgres:postgres@localhost:5432/postgres";
2510
+ function isReplacerFromTo(replacer) {
2511
+ return Array.isArray(replacer) && replacer.length === 2;
2512
+ }
2513
+ function transformType(typeString, typeReplacer) {
2514
+ return isReplacerFromTo(typeReplacer) ? typeString.replace(new RegExp(typeReplacer[0], "g"), typeReplacer[1]) : typeReplacer.replace("{type}", typeString);
2515
+ }
2516
+ function transformTypes(typeString, transform) {
2517
+ if (transform === void 0 || typeString === null) {
2518
+ return typeString;
2519
+ }
2520
+ if (typeof transform === "string") {
2521
+ return transformType(typeString, transform);
2522
+ }
2523
+ let transformed = typeString;
2524
+ for (const replacer of transform) {
2525
+ transformed = transformType(transformed, replacer);
2526
+ }
2527
+ return transformed;
2528
+ }
2529
+ function getFinalResolvedTargetString(params) {
2530
+ const asString = getResolvedTargetString({
2531
+ target: params.target,
2532
+ nullAsOptional: params.nullAsOptional,
2533
+ nullAsUndefined: params.nullAsUndefined,
2534
+ inferLiterals: params.inferLiterals
2535
+ });
2536
+ return fmap(params.transform, (transform) => transformTypes(asString, transform)) ?? asString;
2537
+ }
2538
+ function reportInvalidQueryError(params) {
2539
+ const { context, error } = params;
2540
+ return context.report({
2541
+ messageId: "invalidQuery",
2542
+ node: error.node,
2543
+ data: { error: error.message }
2544
+ });
2545
+ }
2546
+ function reportBaseError(params) {
2547
+ const { context, tag, error } = params;
2548
+ return context.report({
2549
+ node: tag,
2550
+ messageId: "error",
2551
+ data: {
2552
+ error: [error.message, fmap(params.hint, (hint) => `Hint: ${hint}`)].filter(Boolean).join("\n")
2553
+ }
2554
+ });
2555
+ }
2556
+ function reportInvalidConfig(params) {
2557
+ const { tag, context, error } = params;
2558
+ return context.report({
2559
+ node: tag,
2560
+ messageId: "invalidQuery",
2561
+ loc: context.sourceCode.getLocFromIndex(tag.quasi.range[0]),
2562
+ data: {
2563
+ error: error.message
2564
+ }
2565
+ });
2566
+ }
2567
+ function reportDuplicateColumns(params) {
2568
+ const { tag, context, error } = params;
2569
+ const location = getQueryErrorPosition({
2570
+ tag,
2571
+ error,
2572
+ sourceCode: context.sourceCode
2573
+ });
2574
+ return context.report({
2575
+ node: tag,
2576
+ messageId: "invalidQuery",
2577
+ loc: location.sourceLocation,
2578
+ data: {
2579
+ error: error.message
2580
+ }
2581
+ });
2582
+ }
2583
+ function reportPostgresError(params) {
2584
+ const { context, tag, error } = params;
2585
+ const location = getQueryErrorPosition({
2586
+ tag,
2587
+ error,
2588
+ sourceCode: context.sourceCode
2589
+ });
2590
+ return context.report({
2591
+ node: tag,
2592
+ messageId: "invalidQuery",
2593
+ loc: location.sourceLocation,
2594
+ data: {
2595
+ error: error.message
2596
+ }
2597
+ });
2598
+ }
2599
+ function reportMissingTypeAnnotations(params) {
2600
+ const { context, tag, baseNode, actual } = params;
2601
+ return context.report({
2602
+ node: tag,
2603
+ messageId: "missingTypeAnnotations",
2604
+ loc: baseNode.loc,
2605
+ fix: (fixer) => fixer.insertTextAfterRange(baseNode.range, `<${actual}>`),
2606
+ data: {
2607
+ fix: actual
2608
+ }
2609
+ });
2610
+ }
2611
+ function reportIncorrectTypeAnnotations(params) {
2612
+ const { context, typeParameter } = params;
2613
+ const newValue = params.actual === null ? "" : `<${params.actual}>`;
2614
+ return context.report({
2615
+ node: typeParameter.params[0],
2616
+ messageId: "incorrectTypeAnnotations",
2617
+ fix: (fixer) => fixer.replaceText(typeParameter, newValue),
2618
+ data: {
2619
+ expected: params.expected,
2620
+ actual: params.actual ?? "No type annotation"
2621
+ }
2622
+ });
2623
+ }
2624
+ function reportInvalidTypeAnnotations(params) {
2625
+ const { context, typeParameter } = params;
2626
+ return context.report({
2627
+ node: typeParameter.params[0],
2628
+ messageId: "invalidTypeAnnotations"
2629
+ });
2630
+ }
2631
+ function getDatabaseName(params) {
2632
+ const { databaseName, projectDir, migrationsDir } = params;
2633
+ if (databaseName !== void 0) {
2634
+ return databaseName;
2635
+ }
2636
+ const projectDirName = projectDir.split("/").pop() ?? "";
2637
+ const projectUnderscoreName = projectDirName.replace(/[^A-z0-9]/g, "_").toLowerCase();
2638
+ const hash = crypto.createHash("sha1").update(migrationsDir).digest("hex").substring(0, 8);
2639
+ return `slonik_${projectUnderscoreName}_${hash}`;
2640
+ }
2641
+ function shouldLintFile(params) {
2642
+ const fileName = params.getFilename();
2643
+ for (const extension of ["ts", "tsx", "mts", "mtsx"]) {
2644
+ if (fileName.endsWith(`.${extension}`)) {
2645
+ return true;
2646
+ }
2647
+ }
2648
+ return false;
2649
+ }
2650
+ function isMigrationConnection(connection) {
2651
+ return "migrationsDir" in connection;
2652
+ }
2653
+ function isWatchMigrationsDirEnabled(connection) {
2654
+ return isMigrationConnection(connection) && (connection.watchMode ?? true) === true;
2655
+ }
2656
+ function getMigrationDatabaseMetadata(params) {
2657
+ const connectionOptions = {
2658
+ ...parseConnection(params.connectionUrl),
2659
+ database: params.databaseName
2660
+ };
2661
+ const databaseUrl = mapConnectionOptionsToString(connectionOptions);
2662
+ return { databaseUrl, connectionOptions };
2663
+ }
2664
+ function getConnectionStrategyByRuleOptionConnection(params) {
2665
+ const { connection, projectDir } = params;
2666
+ if ("databaseUrl" in connection) {
2667
+ return { type: "databaseUrl", ...connection };
2668
+ }
2669
+ if ("migrationsDir" in connection) {
2670
+ return {
2671
+ type: "migrations",
2672
+ connectionUrl: DEFAULT_CONNECTION_URL,
2673
+ databaseName: getDatabaseName({
2674
+ databaseName: connection.databaseName,
2675
+ migrationsDir: connection.migrationsDir,
2676
+ projectDir
2677
+ }),
2678
+ watchMode: isWatchMigrationsDirEnabled(connection),
2679
+ ...connection
2680
+ };
2681
+ }
2682
+ return match$1(connection).exhaustive();
2683
+ }
2684
+ function runMigrations(params) {
2685
+ const runSingleMigrationFileWithSql = (filePath) => {
2686
+ return runSingleMigrationFile(params.sql, filePath);
2687
+ };
2688
+ return pipe(
2689
+ TE.Do,
2690
+ TE.chain(() => getMigrationFiles(params.migrationsPath)),
2691
+ TE.chainW((files) => TE.sequenceSeqArray(files.map(runSingleMigrationFileWithSql)))
2692
+ );
2693
+ }
2694
+ function findDeepSqlFiles(migrationsPath) {
2695
+ const sqlFilePaths = [];
2696
+ function findDeepSqlFilesRecursively(dir) {
2697
+ const files = fs.readdirSync(dir);
2698
+ files.forEach((file) => {
2699
+ const filePath = path$1.join(dir, file);
2700
+ const isDirectory = fs.statSync(filePath).isDirectory();
2701
+ if (isDirectory) {
2702
+ findDeepSqlFilesRecursively(filePath);
2703
+ } else if (filePath.endsWith(".sql")) {
2704
+ sqlFilePaths.push(filePath);
2705
+ }
2706
+ });
2707
+ }
2708
+ findDeepSqlFilesRecursively(migrationsPath);
2709
+ return sqlFilePaths;
2710
+ }
2711
+ function getMigrationFiles(migrationsPath) {
2712
+ return pipe(
2713
+ E.tryCatch(() => findDeepSqlFiles(migrationsPath), E.toError),
2714
+ TE.fromEither,
2715
+ TE.mapLeft(InvalidMigrationsPathError.fromErrorC(migrationsPath))
2716
+ );
2717
+ }
2718
+ function runSingleMigrationFile(sql, filePath) {
2719
+ return pipe(
2720
+ TE.tryCatch(() => fs.promises.readFile(filePath).then((x) => x.toString()), E.toError),
2721
+ TE.chain((content) => TE.tryCatch(() => sql.unsafe(content), E.toError)),
2722
+ TE.mapLeft(InvalidMigrationError.fromErrorC(filePath))
2723
+ );
2724
+ }
2725
+ function shouldInferLiteral(base, inferLiterals) {
2726
+ if (base.kind !== "type") return true;
2727
+ if (inferLiterals === true) return true;
2728
+ if (Array.isArray(inferLiterals) && isOneOf(base.value, inferLiterals)) return true;
2729
+ return false;
2730
+ }
2731
+ function unique(array) {
2732
+ return Array.from(new Set(array));
2733
+ }
2734
+ function getResolvedTargetComparableString(params) {
2735
+ const { target, nullAsUndefined, nullAsOptional } = params;
2736
+ const nullType = nullAsUndefined ? "undefined" : "null";
2737
+ switch (target.kind) {
2738
+ case "literal": {
2739
+ const value = shouldInferLiteral(target.base, params.inferLiterals) ? target.value : getResolvedTargetComparableString({
2740
+ target: target.base,
2741
+ nullAsOptional: params.nullAsOptional,
2742
+ nullAsUndefined: params.nullAsUndefined,
2743
+ inferLiterals: params.inferLiterals
2744
+ });
2745
+ return value === "null" ? nullType : value;
2746
+ }
2747
+ case "type":
2748
+ return target.value === "null" ? nullType : target.value.replace(/"/g, "'");
2749
+ case "union":
2750
+ return unique(
2751
+ target.value.map((target2) => getResolvedTargetComparableString({ ...params, target: target2 })).sort()
2752
+ ).join(" | ");
2753
+ case "array": {
2754
+ let arrayString = getResolvedTargetComparableString({ ...params, target: target.value });
2755
+ if (target.value.kind === "union" && arrayString.includes("|")) {
2756
+ arrayString = `(${arrayString})`;
2757
+ }
2758
+ return target.syntax === "type-reference" ? `Array<${arrayString}>` : `${arrayString}[]`;
2759
+ }
2760
+ case "object": {
2761
+ if (target.value.length === 0) {
2762
+ return `{ }`;
2763
+ }
2764
+ const entriesString = target.value.map(([key, target2]) => {
2765
+ const isNullable = isNullableResolvedTarget(target2);
2766
+ const keyString = isNullable && nullAsOptional ? `${key}?` : key;
2767
+ const valueString = getResolvedTargetComparableString({ ...params, target: target2 });
2768
+ return `${keyString}: ${valueString}`;
2769
+ }).sort().join(";");
2770
+ return `{ ${entriesString} }`;
2771
+ }
2772
+ }
2773
+ }
2774
+ function getResolvedTargetString(params) {
2775
+ const { target, nullAsUndefined, nullAsOptional } = params;
2776
+ const nullType = nullAsUndefined ? "undefined" : "null";
2777
+ switch (target.kind) {
2778
+ case "literal": {
2779
+ const value = shouldInferLiteral(target.base, params.inferLiterals) ? target.value : getResolvedTargetString({
2780
+ target: target.base,
2781
+ nullAsOptional: params.nullAsOptional,
2782
+ nullAsUndefined: params.nullAsUndefined,
2783
+ inferLiterals: params.inferLiterals
2784
+ });
2785
+ return value === "null" ? nullType : value;
2786
+ }
2787
+ case "type":
2788
+ return target.value === "null" ? nullType : target.value;
2789
+ case "union":
2790
+ return unique(
2791
+ target.value.map((target2) => getResolvedTargetString({ ...params, target: target2 }))
2792
+ ).join(" | ");
2793
+ case "array": {
2794
+ const arrayString = getResolvedTargetString({ ...params, target: target.value });
2795
+ return target.value.kind === "union" && arrayString.includes("|") ? `(${arrayString})[]` : `${arrayString}[]`;
2796
+ }
2797
+ case "object": {
2798
+ if (target.value.length === 0) {
2799
+ return `{ }`;
2800
+ }
2801
+ const entriesString = target.value.map(([key, target2]) => {
2802
+ const isNullable = isNullableResolvedTarget(target2);
2803
+ const valueString = getResolvedTargetString({ ...params, target: target2 });
2804
+ let keyString = key;
2805
+ if (/[^A-z_]/.test(keyString)) {
2806
+ keyString = `'${keyString}'`;
2807
+ }
2808
+ keyString = isNullable && nullAsOptional ? `${keyString}?` : keyString;
2809
+ return `${keyString}: ${valueString}`;
2810
+ }).join("; ");
2811
+ return `{ ${entriesString} }`;
2812
+ }
2813
+ }
2814
+ }
2815
+ function isNullableResolvedTarget(target) {
2816
+ switch (target.kind) {
2817
+ case "type":
2818
+ case "literal":
2819
+ return ["any", "null"].includes(target.value) === false;
2820
+ case "union":
2821
+ return target.value.some((x) => x.kind === "type" && x.value === "null");
2822
+ case "array":
2823
+ case "object":
2824
+ return false;
2825
+ }
2826
+ }
2827
+ function getQueryErrorPosition(params) {
2828
+ const range = [params.error.position, params.error.position + 1];
2829
+ for (const entry of params.error.sourcemaps) {
2830
+ const generatedLength = Math.max(0, entry.generated.end - entry.generated.start);
2831
+ const originalLength = Math.max(0, entry.original.end - entry.original.start);
2832
+ const adjustment = originalLength - generatedLength;
2833
+ if (range[0] >= entry.generated.start && range[1] <= entry.generated.end) {
2834
+ range[0] = entry.original.start + entry.offset;
2835
+ range[1] = entry.original.start + entry.offset + 1;
2836
+ continue;
2837
+ }
2838
+ if (params.error.position >= entry.generated.start) {
2839
+ range[0] += adjustment;
2840
+ }
2841
+ if (params.error.position >= entry.generated.end) {
2842
+ range[1] += adjustment;
2843
+ }
2844
+ }
2845
+ const start = params.sourceCode.getLocFromIndex(params.tag.quasi.range[0] + range[0]);
2846
+ const startLineText = params.sourceCode.getLines()[start.line - 1];
2847
+ const remainingLineText = startLineText.substring(start.column);
2848
+ const remainingWordLength = (remainingLineText.match(/^[\w.{}'$"]+/)?.at(0)?.length ?? 1) - 1;
2849
+ const end = params.sourceCode.getLocFromIndex(params.tag.quasi.range[0] + range[1]);
2850
+ const sourceLocation = {
2851
+ start,
2852
+ end: {
2853
+ line: end.line,
2854
+ column: end.column + remainingWordLength
2855
+ }
2856
+ };
2857
+ return {
2858
+ range,
2859
+ sourceLocation,
2860
+ remainingLineText,
2861
+ remainingWordLength
2862
+ };
2863
+ }
2864
+
2865
+ export { reportInvalidQueryError as A, reportBaseError as B, reportMissingTypeAnnotations as C, DuplicateColumnsError as D, getFinalResolvedTargetString as E, reportInvalidTypeAnnotations as F, reportIncorrectTypeAnnotations as G, isIdentifier as H, InternalError as I, isMemberExpression as J, getResolvedTargetComparableString as K, transformTypes as L, getResolvedTargetString as M, PostgresError as P, groupBy as a, assertNever as b, getConnectionStrategyByRuleOptionConnection as c, defaultTypesMap as d, getMigrationDatabaseMetadata as e, fmap as f, getOrSetFromMapWithEnabled as g, getDatabaseName as h, isPostgresError as i, DEFAULT_CONNECTION_URL as j, isWatchMigrationsDirEnabled as k, initDatabase as l, mapConnectionOptionsToString as m, normalizeIndent as n, InvalidQueryError as o, parseConnection as p, defaultTypeMapping as q, runMigrations as r, doesMatchPattern as s, toCase as t, objectKeysNonEmpty as u, InvalidConfigError as v, shouldLintFile as w, reportInvalidConfig as x, reportDuplicateColumns as y, reportPostgresError as z };
2866
+ //# sourceMappingURL=eslint-plugin-slonik.C0xTyWZ2.mjs.map