@alchemy.run/node-utils 0.0.0 → 0.0.2

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.
package/src/ignore.ts ADDED
@@ -0,0 +1,815 @@
1
+ // @ts-nocheck
2
+ /*
3
+ * Vendored copy of `ignore` (https://github.com/kaelzhang/node-ignore)
4
+ * Version: 7.0.5
5
+ *
6
+ * The body below is a mechanical copy of upstream `index.js`. The only
7
+ * substantive change is the CommonJS export footer, which has been rewritten
8
+ * as ES module exports; the upstream `index.d.ts` declarations are appended.
9
+ *
10
+ * ---------------------------------------------------------------------------
11
+ * Copyright (c) 2013 Kael Zhang <i@kael.me>, contributors
12
+ * http://kael.me/
13
+ *
14
+ * Permission is hereby granted, free of charge, to any person obtaining
15
+ * a copy of this software and associated documentation files (the
16
+ * "Software"), to deal in the Software without restriction, including
17
+ * without limitation the rights to use, copy, modify, merge, publish,
18
+ * distribute, sublicense, and/or sell copies of the Software, and to
19
+ * permit persons to whom the Software is furnished to do so, subject to
20
+ * the following conditions:
21
+ *
22
+ * The above copyright notice and this permission notice shall be
23
+ * included in all copies or substantial portions of the Software.
24
+ *
25
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32
+ * ---------------------------------------------------------------------------
33
+ */
34
+
35
+ // A simple implementation of make-array
36
+ function makeArray(subject) {
37
+ return Array.isArray(subject) ? subject : [subject];
38
+ }
39
+
40
+ const UNDEFINED = undefined;
41
+ const EMPTY = "";
42
+ const SPACE = " ";
43
+ const ESCAPE = "\\";
44
+ const REGEX_TEST_BLANK_LINE = /^\s+$/;
45
+ const REGEX_INVALID_TRAILING_BACKSLASH = /(?:[^\\]|^)\\$/;
46
+ const REGEX_REPLACE_LEADING_EXCAPED_EXCLAMATION = /^\\!/;
47
+ const REGEX_REPLACE_LEADING_EXCAPED_HASH = /^\\#/;
48
+ const REGEX_SPLITALL_CRLF = /\r?\n/g;
49
+
50
+ // Invalid:
51
+ // - /foo,
52
+ // - ./foo,
53
+ // - ../foo,
54
+ // - .
55
+ // - ..
56
+ // Valid:
57
+ // - .foo
58
+ const REGEX_TEST_INVALID_PATH = /^\.{0,2}\/|^\.{1,2}$/;
59
+
60
+ const REGEX_TEST_TRAILING_SLASH = /\/$/;
61
+
62
+ const SLASH = "/";
63
+
64
+ // Do not use ternary expression here, since "istanbul ignore next" is buggy
65
+ let TMP_KEY_IGNORE = "node-ignore";
66
+ /* istanbul ignore else */
67
+ if (typeof Symbol !== "undefined") {
68
+ TMP_KEY_IGNORE = Symbol.for("node-ignore");
69
+ }
70
+ const KEY_IGNORE = TMP_KEY_IGNORE;
71
+
72
+ const define = (object, key, value) => {
73
+ Object.defineProperty(object, key, { value });
74
+ return value;
75
+ };
76
+
77
+ const REGEX_REGEXP_RANGE = /([0-z])-([0-z])/g;
78
+
79
+ const RETURN_FALSE = () => false;
80
+
81
+ // Sanitize the range of a regular expression
82
+ // The cases are complicated, see test cases for details
83
+ const sanitizeRange = (range) =>
84
+ range.replace(REGEX_REGEXP_RANGE, (match, from, to) =>
85
+ from.charCodeAt(0) <= to.charCodeAt(0)
86
+ ? match
87
+ : // Invalid range (out of order) which is ok for gitignore rules but
88
+ // fatal for JavaScript regular expression, so eliminate it.
89
+ EMPTY,
90
+ );
91
+
92
+ // See fixtures #59
93
+ const cleanRangeBackSlash = (slashes) => {
94
+ const { length } = slashes;
95
+ return slashes.slice(0, length - (length % 2));
96
+ };
97
+
98
+ // > If the pattern ends with a slash,
99
+ // > it is removed for the purpose of the following description,
100
+ // > but it would only find a match with a directory.
101
+ // > In other words, foo/ will match a directory foo and paths underneath it,
102
+ // > but will not match a regular file or a symbolic link foo
103
+ // > (this is consistent with the way how pathspec works in general in Git).
104
+ // '`foo/`' will not match regular file '`foo`' or symbolic link '`foo`'
105
+ // -> ignore-rules will not deal with it, because it costs extra `fs.stat` call
106
+ // you could use option `mark: true` with `glob`
107
+
108
+ // '`foo/`' should not continue with the '`..`'
109
+ const REPLACERS = [
110
+ [
111
+ // Remove BOM
112
+ // TODO:
113
+ // Other similar zero-width characters?
114
+ /^\uFEFF/,
115
+ () => EMPTY,
116
+ ],
117
+
118
+ // > Trailing spaces are ignored unless they are quoted with backslash ("\")
119
+ [
120
+ // (a\ ) -> (a )
121
+ // (a ) -> (a)
122
+ // (a ) -> (a)
123
+ // (a \ ) -> (a )
124
+ /((?:\\\\)*?)(\\?\s+)$/,
125
+ (_, m1, m2) => m1 + (m2.indexOf("\\") === 0 ? SPACE : EMPTY),
126
+ ],
127
+
128
+ // Replace (\ ) with ' '
129
+ // (\ ) -> ' '
130
+ // (\\ ) -> '\\ '
131
+ // (\\\ ) -> '\\ '
132
+ [
133
+ /(\\+?)\s/g,
134
+ (_, m1) => {
135
+ const { length } = m1;
136
+ return m1.slice(0, length - (length % 2)) + SPACE;
137
+ },
138
+ ],
139
+
140
+ // Escape metacharacters
141
+ // which is written down by users but means special for regular expressions.
142
+
143
+ // > There are 12 characters with special meanings:
144
+ // > - the backslash \,
145
+ // > - the caret ^,
146
+ // > - the dollar sign $,
147
+ // > - the period or dot .,
148
+ // > - the vertical bar or pipe symbol |,
149
+ // > - the question mark ?,
150
+ // > - the asterisk or star *,
151
+ // > - the plus sign +,
152
+ // > - the opening parenthesis (,
153
+ // > - the closing parenthesis ),
154
+ // > - and the opening square bracket [,
155
+ // > - the opening curly brace {,
156
+ // > These special characters are often called "metacharacters".
157
+ [/[\\$.|*+(){^]/g, (match) => `\\${match}`],
158
+
159
+ [
160
+ // > a question mark (?) matches a single character
161
+ /(?!\\)\?/g,
162
+ () => "[^/]",
163
+ ],
164
+
165
+ // leading slash
166
+ [
167
+ // > A leading slash matches the beginning of the pathname.
168
+ // > For example, "/*.c" matches "cat-file.c" but not "mozilla-sha1/sha1.c".
169
+ // A leading slash matches the beginning of the pathname
170
+ /^\//,
171
+ () => "^",
172
+ ],
173
+
174
+ // replace special metacharacter slash after the leading slash
175
+ [/\//g, () => "\\/"],
176
+
177
+ [
178
+ // > A leading "**" followed by a slash means match in all directories.
179
+ // > For example, "**/foo" matches file or directory "foo" anywhere,
180
+ // > the same as pattern "foo".
181
+ // > "**/foo/bar" matches file or directory "bar" anywhere that is directly
182
+ // > under directory "foo".
183
+ // Notice that the '*'s have been replaced as '\\*'
184
+ /^\^*\\\*\\\*\\\//,
185
+
186
+ // '**/foo' <-> 'foo'
187
+ () => "^(?:.*\\/)?",
188
+ ],
189
+
190
+ // starting
191
+ [
192
+ // there will be no leading '/'
193
+ // (which has been replaced by section "leading slash")
194
+ // If starts with '**', adding a '^' to the regular expression also works
195
+ /^(?=[^^])/,
196
+ function startingReplacer() {
197
+ // If has a slash `/` at the beginning or middle
198
+ return !/\/(?!$)/.test(this)
199
+ ? // > Prior to 2.22.1
200
+ // > If the pattern does not contain a slash /,
201
+ // > Git treats it as a shell glob pattern
202
+ // Actually, if there is only a trailing slash,
203
+ // git also treats it as a shell glob pattern
204
+
205
+ // After 2.22.1 (compatible but clearer)
206
+ // > If there is a separator at the beginning or middle (or both)
207
+ // > of the pattern, then the pattern is relative to the directory
208
+ // > level of the particular .gitignore file itself.
209
+ // > Otherwise the pattern may also match at any level below
210
+ // > the .gitignore level.
211
+ "(?:^|\\/)"
212
+ : // > Otherwise, Git treats the pattern as a shell glob suitable for
213
+ // > consumption by fnmatch(3)
214
+ "^";
215
+ },
216
+ ],
217
+
218
+ // two globstars
219
+ [
220
+ // Use lookahead assertions so that we could match more than one `'/**'`
221
+ /\\\/\\\*\\\*(?=\\\/|$)/g,
222
+
223
+ // Zero, one or several directories
224
+ // should not use '*', or it will be replaced by the next replacer
225
+
226
+ // Check if it is not the last `'/**'`
227
+ (_, index, str) =>
228
+ index + 6 < str.length
229
+ ? // case: /**/
230
+ // > A slash followed by two consecutive asterisks then a slash matches
231
+ // > zero or more directories.
232
+ // > For example, "a/**/b" matches "a/b", "a/x/b", "a/x/y/b" and so on.
233
+ // '/**/'
234
+ "(?:\\/[^\\/]+)*"
235
+ : // case: /**
236
+ // > A trailing `"/**"` matches everything inside.
237
+
238
+ // #21: everything inside but it should not include the current folder
239
+ "\\/.+",
240
+ ],
241
+
242
+ // normal intermediate wildcards
243
+ [
244
+ // Never replace escaped '*'
245
+ // ignore rule '\*' will match the path '*'
246
+
247
+ // 'abc.*/' -> go
248
+ // 'abc.*' -> skip this rule,
249
+ // coz trailing single wildcard will be handed by [trailing wildcard]
250
+ /(^|[^\\]+)(\\\*)+(?=.+)/g,
251
+
252
+ // '*.js' matches '.js'
253
+ // '*.js' doesn't match 'abc'
254
+ (_, p1, p2) => {
255
+ // 1.
256
+ // > An asterisk "*" matches anything except a slash.
257
+ // 2.
258
+ // > Other consecutive asterisks are considered regular asterisks
259
+ // > and will match according to the previous rules.
260
+ const unescaped = p2.replace(/\\\*/g, "[^\\/]*");
261
+ return p1 + unescaped;
262
+ },
263
+ ],
264
+
265
+ [
266
+ // unescape, revert step 3 except for back slash
267
+ // For example, if a user escape a '\\*',
268
+ // after step 3, the result will be '\\\\\\*'
269
+ /\\\\\\(?=[$.|*+(){^])/g,
270
+ () => ESCAPE,
271
+ ],
272
+
273
+ [
274
+ // '\\\\' -> '\\'
275
+ /\\\\/g,
276
+ () => ESCAPE,
277
+ ],
278
+
279
+ [
280
+ // > The range notation, e.g. [a-zA-Z],
281
+ // > can be used to match one of the characters in a range.
282
+
283
+ // `\` is escaped by step 3
284
+ /(\\)?\[([^\]/]*?)(\\*)($|\])/g,
285
+ (match, leadEscape, range, endEscape, close) =>
286
+ leadEscape === ESCAPE
287
+ ? // '\\[bar]' -> '\\\\[bar\\]'
288
+ `\\[${range}${cleanRangeBackSlash(endEscape)}${close}`
289
+ : close === "]"
290
+ ? endEscape.length % 2 === 0
291
+ ? // A normal case, and it is a range notation
292
+ // '[bar]'
293
+ // '[bar\\\\]'
294
+ `[${sanitizeRange(range)}${endEscape}]`
295
+ : // Invalid range notaton
296
+ // '[bar\\]' -> '[bar\\\\]'
297
+ "[]"
298
+ : "[]",
299
+ ],
300
+
301
+ // ending
302
+ [
303
+ // 'js' will not match 'js.'
304
+ // 'ab' will not match 'abc'
305
+ /(?:[^*])$/,
306
+
307
+ // WTF!
308
+ // https://git-scm.com/docs/gitignore
309
+ // changes in [2.22.1](https://git-scm.com/docs/gitignore/2.22.1)
310
+ // which re-fixes #24, #38
311
+
312
+ // > If there is a separator at the end of the pattern then the pattern
313
+ // > will only match directories, otherwise the pattern can match both
314
+ // > files and directories.
315
+
316
+ // 'js*' will not match 'a.js'
317
+ // 'js/' will not match 'a.js'
318
+ // 'js' will match 'a.js' and 'a.js/'
319
+ (match) =>
320
+ /\/$/.test(match)
321
+ ? // foo/ will not match 'foo'
322
+ `${match}$`
323
+ : // foo matches 'foo' and 'foo/'
324
+ `${match}(?=$|\\/$)`,
325
+ ],
326
+ ];
327
+
328
+ const REGEX_REPLACE_TRAILING_WILDCARD = /(^|\\\/)?\\\*$/;
329
+ const MODE_IGNORE = "regex";
330
+ const MODE_CHECK_IGNORE = "checkRegex";
331
+ const UNDERSCORE = "_";
332
+
333
+ const TRAILING_WILD_CARD_REPLACERS = {
334
+ [MODE_IGNORE](_, p1) {
335
+ const prefix = p1
336
+ ? // '\^':
337
+ // '/*' does not match EMPTY
338
+ // '/*' does not match everything
339
+
340
+ // '\\\/':
341
+ // 'abc/*' does not match 'abc/'
342
+ `${p1}[^/]+`
343
+ : // 'a*' matches 'a'
344
+ // 'a*' matches 'aa'
345
+ "[^/]*";
346
+
347
+ return `${prefix}(?=$|\\/$)`;
348
+ },
349
+
350
+ [MODE_CHECK_IGNORE](_, p1) {
351
+ // When doing `git check-ignore`
352
+ const prefix = p1
353
+ ? // '\\\/':
354
+ // 'abc/*' DOES match 'abc/' !
355
+ `${p1}[^/]*`
356
+ : // 'a*' matches 'a'
357
+ // 'a*' matches 'aa'
358
+ "[^/]*";
359
+
360
+ return `${prefix}(?=$|\\/$)`;
361
+ },
362
+ };
363
+
364
+ // @param {pattern}
365
+ const makeRegexPrefix = (pattern) =>
366
+ REPLACERS.reduce(
367
+ (prev, [matcher, replacer]) =>
368
+ prev.replace(matcher, replacer.bind(pattern)),
369
+ pattern,
370
+ );
371
+
372
+ const isString = (subject) => typeof subject === "string";
373
+
374
+ // > A blank line matches no files, so it can serve as a separator for readability.
375
+ const checkPattern = (pattern) =>
376
+ pattern &&
377
+ isString(pattern) &&
378
+ !REGEX_TEST_BLANK_LINE.test(pattern) &&
379
+ !REGEX_INVALID_TRAILING_BACKSLASH.test(pattern) &&
380
+ // > A line starting with # serves as a comment.
381
+ pattern.indexOf("#") !== 0;
382
+
383
+ const splitPattern = (pattern) =>
384
+ pattern.split(REGEX_SPLITALL_CRLF).filter(Boolean);
385
+
386
+ class IgnoreRule {
387
+ constructor(pattern, mark, body, ignoreCase, negative, prefix) {
388
+ this.pattern = pattern;
389
+ this.mark = mark;
390
+ this.negative = negative;
391
+
392
+ define(this, "body", body);
393
+ define(this, "ignoreCase", ignoreCase);
394
+ define(this, "regexPrefix", prefix);
395
+ }
396
+
397
+ get regex() {
398
+ const key = UNDERSCORE + MODE_IGNORE;
399
+
400
+ if (this[key]) {
401
+ return this[key];
402
+ }
403
+
404
+ return this._make(MODE_IGNORE, key);
405
+ }
406
+
407
+ get checkRegex() {
408
+ const key = UNDERSCORE + MODE_CHECK_IGNORE;
409
+
410
+ if (this[key]) {
411
+ return this[key];
412
+ }
413
+
414
+ return this._make(MODE_CHECK_IGNORE, key);
415
+ }
416
+
417
+ _make(mode, key) {
418
+ const str = this.regexPrefix.replace(
419
+ REGEX_REPLACE_TRAILING_WILDCARD,
420
+
421
+ // It does not need to bind pattern
422
+ TRAILING_WILD_CARD_REPLACERS[mode],
423
+ );
424
+
425
+ const regex = this.ignoreCase ? new RegExp(str, "i") : new RegExp(str);
426
+
427
+ return define(this, key, regex);
428
+ }
429
+ }
430
+
431
+ const createRule = ({ pattern, mark }, ignoreCase) => {
432
+ let negative = false;
433
+ let body = pattern;
434
+
435
+ // > An optional prefix "!" which negates the pattern;
436
+ if (body.indexOf("!") === 0) {
437
+ negative = true;
438
+ body = body.substr(1);
439
+ }
440
+
441
+ body = body
442
+ // > Put a backslash ("\") in front of the first "!" for patterns that
443
+ // > begin with a literal "!", for example, `"\!important!.txt"`.
444
+ .replace(REGEX_REPLACE_LEADING_EXCAPED_EXCLAMATION, "!")
445
+ // > Put a backslash ("\") in front of the first hash for patterns that
446
+ // > begin with a hash.
447
+ .replace(REGEX_REPLACE_LEADING_EXCAPED_HASH, "#");
448
+
449
+ const regexPrefix = makeRegexPrefix(body);
450
+
451
+ return new IgnoreRule(pattern, mark, body, ignoreCase, negative, regexPrefix);
452
+ };
453
+
454
+ class RuleManager {
455
+ constructor(ignoreCase) {
456
+ this._ignoreCase = ignoreCase;
457
+ this._rules = [];
458
+ }
459
+
460
+ _add(pattern) {
461
+ // #32
462
+ if (pattern && pattern[KEY_IGNORE]) {
463
+ this._rules = this._rules.concat(pattern._rules._rules);
464
+ this._added = true;
465
+ return;
466
+ }
467
+
468
+ if (isString(pattern)) {
469
+ pattern = {
470
+ pattern,
471
+ };
472
+ }
473
+
474
+ if (checkPattern(pattern.pattern)) {
475
+ const rule = createRule(pattern, this._ignoreCase);
476
+ this._added = true;
477
+ this._rules.push(rule);
478
+ }
479
+ }
480
+
481
+ // @param {Array<string> | string | Ignore} pattern
482
+ add(pattern) {
483
+ this._added = false;
484
+
485
+ makeArray(isString(pattern) ? splitPattern(pattern) : pattern).forEach(
486
+ this._add,
487
+ this,
488
+ );
489
+
490
+ return this._added;
491
+ }
492
+
493
+ // Test one single path without recursively checking parent directories
494
+ //
495
+ // - checkUnignored `boolean` whether should check if the path is unignored,
496
+ // setting `checkUnignored` to `false` could reduce additional
497
+ // path matching.
498
+ // - check `string` either `MODE_IGNORE` or `MODE_CHECK_IGNORE`
499
+
500
+ // @returns {TestResult} true if a file is ignored
501
+ test(path, checkUnignored, mode) {
502
+ let ignored = false;
503
+ let unignored = false;
504
+ let matchedRule;
505
+
506
+ this._rules.forEach((rule) => {
507
+ const { negative } = rule;
508
+
509
+ // | ignored : unignored
510
+ // -------- | ---------------------------------------
511
+ // negative | 0:0 | 0:1 | 1:0 | 1:1
512
+ // -------- | ------- | ------- | ------- | --------
513
+ // 0 | TEST | TEST | SKIP | X
514
+ // 1 | TESTIF | SKIP | TEST | X
515
+
516
+ // - SKIP: always skip
517
+ // - TEST: always test
518
+ // - TESTIF: only test if checkUnignored
519
+ // - X: that never happen
520
+ if (
521
+ (unignored === negative && ignored !== unignored) ||
522
+ (negative && !ignored && !unignored && !checkUnignored)
523
+ ) {
524
+ return;
525
+ }
526
+
527
+ const matched = rule[mode].test(path);
528
+
529
+ if (!matched) {
530
+ return;
531
+ }
532
+
533
+ ignored = !negative;
534
+ unignored = negative;
535
+
536
+ matchedRule = negative ? UNDEFINED : rule;
537
+ });
538
+
539
+ const ret = {
540
+ ignored,
541
+ unignored,
542
+ };
543
+
544
+ if (matchedRule) {
545
+ ret.rule = matchedRule;
546
+ }
547
+
548
+ return ret;
549
+ }
550
+ }
551
+
552
+ const throwError = (message, Ctor) => {
553
+ throw new Ctor(message);
554
+ };
555
+
556
+ const checkPath = (path, originalPath, doThrow) => {
557
+ if (!isString(path)) {
558
+ return doThrow(
559
+ `path must be a string, but got \`${originalPath}\``,
560
+ TypeError,
561
+ );
562
+ }
563
+
564
+ // We don't know if we should ignore EMPTY, so throw
565
+ if (!path) {
566
+ return doThrow(`path must not be empty`, TypeError);
567
+ }
568
+
569
+ // Check if it is a relative path
570
+ if (checkPath.isNotRelative(path)) {
571
+ const r = "`path.relative()`d";
572
+ return doThrow(
573
+ `path should be a ${r} string, but got "${originalPath}"`,
574
+ RangeError,
575
+ );
576
+ }
577
+
578
+ return true;
579
+ };
580
+
581
+ const isNotRelative = (path) => REGEX_TEST_INVALID_PATH.test(path);
582
+
583
+ checkPath.isNotRelative = isNotRelative;
584
+
585
+ // On windows, the following function will be replaced
586
+ /* istanbul ignore next */
587
+ checkPath.convert = (p) => p;
588
+
589
+ class Ignore {
590
+ constructor({
591
+ ignorecase = true,
592
+ ignoreCase = ignorecase,
593
+ allowRelativePaths = false,
594
+ } = {}) {
595
+ define(this, KEY_IGNORE, true);
596
+
597
+ this._rules = new RuleManager(ignoreCase);
598
+ this._strictPathCheck = !allowRelativePaths;
599
+ this._initCache();
600
+ }
601
+
602
+ _initCache() {
603
+ // A cache for the result of `.ignores()`
604
+ this._ignoreCache = Object.create(null);
605
+
606
+ // A cache for the result of `.test()`
607
+ this._testCache = Object.create(null);
608
+ }
609
+
610
+ add(pattern) {
611
+ if (this._rules.add(pattern)) {
612
+ // Some rules have just added to the ignore,
613
+ // making the behavior changed,
614
+ // so we need to re-initialize the result cache
615
+ this._initCache();
616
+ }
617
+
618
+ return this;
619
+ }
620
+
621
+ // legacy
622
+ addPattern(pattern) {
623
+ return this.add(pattern);
624
+ }
625
+
626
+ // @returns {TestResult}
627
+ _test(originalPath, cache, checkUnignored, slices) {
628
+ const path =
629
+ originalPath &&
630
+ // Supports nullable path
631
+ checkPath.convert(originalPath);
632
+
633
+ checkPath(
634
+ path,
635
+ originalPath,
636
+ this._strictPathCheck ? throwError : RETURN_FALSE,
637
+ );
638
+
639
+ return this._t(path, cache, checkUnignored, slices);
640
+ }
641
+
642
+ checkIgnore(path) {
643
+ // If the path doest not end with a slash, `.ignores()` is much equivalent
644
+ // to `git check-ignore`
645
+ if (!REGEX_TEST_TRAILING_SLASH.test(path)) {
646
+ return this.test(path);
647
+ }
648
+
649
+ const slices = path.split(SLASH).filter(Boolean);
650
+ slices.pop();
651
+
652
+ if (slices.length) {
653
+ const parent = this._t(
654
+ slices.join(SLASH) + SLASH,
655
+ this._testCache,
656
+ true,
657
+ slices,
658
+ );
659
+
660
+ if (parent.ignored) {
661
+ return parent;
662
+ }
663
+ }
664
+
665
+ return this._rules.test(path, false, MODE_CHECK_IGNORE);
666
+ }
667
+
668
+ _t(
669
+ // The path to be tested
670
+ path,
671
+
672
+ // The cache for the result of a certain checking
673
+ cache,
674
+
675
+ // Whether should check if the path is unignored
676
+ checkUnignored,
677
+
678
+ // The path slices
679
+ slices,
680
+ ) {
681
+ if (path in cache) {
682
+ return cache[path];
683
+ }
684
+
685
+ if (!slices) {
686
+ // path/to/a.js
687
+ // ['path', 'to', 'a.js']
688
+ slices = path.split(SLASH).filter(Boolean);
689
+ }
690
+
691
+ slices.pop();
692
+
693
+ // If the path has no parent directory, just test it
694
+ if (!slices.length) {
695
+ return (cache[path] = this._rules.test(
696
+ path,
697
+ checkUnignored,
698
+ MODE_IGNORE,
699
+ ));
700
+ }
701
+
702
+ const parent = this._t(
703
+ slices.join(SLASH) + SLASH,
704
+ cache,
705
+ checkUnignored,
706
+ slices,
707
+ );
708
+
709
+ // If the path contains a parent directory, check the parent first
710
+ return (cache[path] = parent.ignored
711
+ ? // > It is not possible to re-include a file if a parent directory of
712
+ // > that file is excluded.
713
+ parent
714
+ : this._rules.test(path, checkUnignored, MODE_IGNORE));
715
+ }
716
+
717
+ ignores(path) {
718
+ return this._test(path, this._ignoreCache, false).ignored;
719
+ }
720
+
721
+ createFilter() {
722
+ return (path) => !this.ignores(path);
723
+ }
724
+
725
+ filter(paths) {
726
+ return makeArray(paths).filter(this.createFilter());
727
+ }
728
+
729
+ // @returns {TestResult}
730
+ test(path) {
731
+ return this._test(path, this._testCache, true);
732
+ }
733
+ }
734
+
735
+ const factory = (options) => new Ignore(options);
736
+
737
+ const isPathValid = (path) =>
738
+ checkPath(path && checkPath.convert(path), path, RETURN_FALSE);
739
+
740
+ /* istanbul ignore next */
741
+ const setupWindows = () => {
742
+ /* eslint no-control-regex: "off" */
743
+ const makePosix = (str) =>
744
+ /^\\\\\?\\/.test(str) || /["<>|\u0000-\u001F]+/u.test(str)
745
+ ? str
746
+ : str.replace(/\\/g, "/");
747
+
748
+ checkPath.convert = makePosix;
749
+
750
+ // 'C:\\foo' <- 'C:\\foo' has been converted to 'C:/'
751
+ // 'd:\\foo'
752
+ const REGEX_TEST_WINDOWS_PATH_ABSOLUTE = /^[a-z]:\//i;
753
+ checkPath.isNotRelative = (path) =>
754
+ REGEX_TEST_WINDOWS_PATH_ABSOLUTE.test(path) || isNotRelative(path);
755
+ };
756
+
757
+ // Windows
758
+ // --------------------------------------------------------------
759
+ /* istanbul ignore next */
760
+ if (
761
+ // Detect `process` so that it can run in browsers.
762
+ typeof process !== "undefined" &&
763
+ process.platform === "win32"
764
+ ) {
765
+ setupWindows();
766
+ }
767
+
768
+ // ES module exports (upstream uses `module.exports`). Upstream also exposes
769
+ // `setupWindows` via `Symbol.for("setupWindows")` for its own test suite; we
770
+ // drop that hook since it's unused.
771
+
772
+ export default factory as unknown as (
773
+ options?: IgnoreOptions,
774
+ ) => IgnoreInstance;
775
+ export { isPathValid };
776
+
777
+ // --- Type declarations -----------------------------------------------------
778
+ // (not part of the verbatim upstream source; mirrors upstream `index.d.ts`)
779
+
780
+ type Pathname = string;
781
+
782
+ interface PatternParams {
783
+ pattern: string;
784
+ mark?: string;
785
+ }
786
+
787
+ export interface IgnoreOptions {
788
+ ignorecase?: boolean;
789
+ /** For compatibility. */
790
+ ignoreCase?: boolean;
791
+ allowRelativePaths?: boolean;
792
+ }
793
+
794
+ export interface IgnoreInstance {
795
+ add(
796
+ patterns:
797
+ | string
798
+ | IgnoreInstance
799
+ | readonly (string | IgnoreInstance)[]
800
+ | PatternParams,
801
+ ): this;
802
+ filter(pathnames: readonly Pathname[]): Pathname[];
803
+ createFilter(): (pathname: Pathname) => boolean;
804
+ ignores(pathname: Pathname): boolean;
805
+ test(pathname: Pathname): {
806
+ ignored: boolean;
807
+ unignored: boolean;
808
+ rule?: { pattern: string; mark?: string; negative: boolean };
809
+ };
810
+ checkIgnore(pathname: Pathname): {
811
+ ignored: boolean;
812
+ unignored: boolean;
813
+ rule?: { pattern: string; mark?: string; negative: boolean };
814
+ };
815
+ }