source: trip-planner-front/node_modules/ignore/legacy.js@ eed0bf8

Last change on this file since eed0bf8 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 16.5 KB
RevLine 
[6a3a178]1"use strict";
2
3function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
4
5function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
6
7function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
8
9// A simple implementation of make-array
10function makeArray(subject) {
11 return Array.isArray(subject) ? subject : [subject];
12}
13
14var EMPTY = '';
15var SPACE = ' ';
16var ESCAPE = '\\';
17var REGEX_TEST_BLANK_LINE = /^\s+$/;
18var REGEX_REPLACE_LEADING_EXCAPED_EXCLAMATION = /^\\!/;
19var REGEX_REPLACE_LEADING_EXCAPED_HASH = /^\\#/;
20var REGEX_SPLITALL_CRLF = /\r?\n/g; // /foo,
21// ./foo,
22// ../foo,
23// .
24// ..
25
26var REGEX_TEST_INVALID_PATH = /^\.*\/|^\.+$/;
27var SLASH = '/';
28var KEY_IGNORE = typeof Symbol !== 'undefined' ? Symbol["for"]('node-ignore')
29/* istanbul ignore next */
30: 'node-ignore';
31
32var define = function define(object, key, value) {
33 return Object.defineProperty(object, key, {
34 value: value
35 });
36};
37
38var REGEX_REGEXP_RANGE = /([0-z])-([0-z])/g; // Sanitize the range of a regular expression
39// The cases are complicated, see test cases for details
40
41var sanitizeRange = function sanitizeRange(range) {
42 return range.replace(REGEX_REGEXP_RANGE, function (match, from, to) {
43 return from.charCodeAt(0) <= to.charCodeAt(0) ? match // Invalid range (out of order) which is ok for gitignore rules but
44 // fatal for JavaScript regular expression, so eliminate it.
45 : EMPTY;
46 });
47}; // See fixtures #59
48
49
50var cleanRangeBackSlash = function cleanRangeBackSlash(slashes) {
51 var length = slashes.length;
52 return slashes.slice(0, length - length % 2);
53}; // > If the pattern ends with a slash,
54// > it is removed for the purpose of the following description,
55// > but it would only find a match with a directory.
56// > In other words, foo/ will match a directory foo and paths underneath it,
57// > but will not match a regular file or a symbolic link foo
58// > (this is consistent with the way how pathspec works in general in Git).
59// '`foo/`' will not match regular file '`foo`' or symbolic link '`foo`'
60// -> ignore-rules will not deal with it, because it costs extra `fs.stat` call
61// you could use option `mark: true` with `glob`
62// '`foo/`' should not continue with the '`..`'
63
64
65var REPLACERS = [// > Trailing spaces are ignored unless they are quoted with backslash ("\")
66[// (a\ ) -> (a )
67// (a ) -> (a)
68// (a \ ) -> (a )
69/\\?\s+$/, function (match) {
70 return match.indexOf('\\') === 0 ? SPACE : EMPTY;
71}], // replace (\ ) with ' '
72[/\\\s/g, function () {
73 return SPACE;
74}], // Escape metacharacters
75// which is written down by users but means special for regular expressions.
76// > There are 12 characters with special meanings:
77// > - the backslash \,
78// > - the caret ^,
79// > - the dollar sign $,
80// > - the period or dot .,
81// > - the vertical bar or pipe symbol |,
82// > - the question mark ?,
83// > - the asterisk or star *,
84// > - the plus sign +,
85// > - the opening parenthesis (,
86// > - the closing parenthesis ),
87// > - and the opening square bracket [,
88// > - the opening curly brace {,
89// > These special characters are often called "metacharacters".
90[/[\\$.|*+(){^]/g, function (match) {
91 return "\\".concat(match);
92}], [// > a question mark (?) matches a single character
93/(?!\\)\?/g, function () {
94 return '[^/]';
95}], // leading slash
96[// > A leading slash matches the beginning of the pathname.
97// > For example, "/*.c" matches "cat-file.c" but not "mozilla-sha1/sha1.c".
98// A leading slash matches the beginning of the pathname
99/^\//, function () {
100 return '^';
101}], // replace special metacharacter slash after the leading slash
102[/\//g, function () {
103 return '\\/';
104}], [// > A leading "**" followed by a slash means match in all directories.
105// > For example, "**/foo" matches file or directory "foo" anywhere,
106// > the same as pattern "foo".
107// > "**/foo/bar" matches file or directory "bar" anywhere that is directly
108// > under directory "foo".
109// Notice that the '*'s have been replaced as '\\*'
110/^\^*\\\*\\\*\\\//, // '**/foo' <-> 'foo'
111function () {
112 return '^(?:.*\\/)?';
113}], // starting
114[// there will be no leading '/'
115// (which has been replaced by section "leading slash")
116// If starts with '**', adding a '^' to the regular expression also works
117/^(?=[^^])/, function startingReplacer() {
118 // If has a slash `/` at the beginning or middle
119 return !/\/(?!$)/.test(this) // > Prior to 2.22.1
120 // > If the pattern does not contain a slash /,
121 // > Git treats it as a shell glob pattern
122 // Actually, if there is only a trailing slash,
123 // git also treats it as a shell glob pattern
124 // After 2.22.1 (compatible but clearer)
125 // > If there is a separator at the beginning or middle (or both)
126 // > of the pattern, then the pattern is relative to the directory
127 // > level of the particular .gitignore file itself.
128 // > Otherwise the pattern may also match at any level below
129 // > the .gitignore level.
130 ? '(?:^|\\/)' // > Otherwise, Git treats the pattern as a shell glob suitable for
131 // > consumption by fnmatch(3)
132 : '^';
133}], // two globstars
134[// Use lookahead assertions so that we could match more than one `'/**'`
135/\\\/\\\*\\\*(?=\\\/|$)/g, // Zero, one or several directories
136// should not use '*', or it will be replaced by the next replacer
137// Check if it is not the last `'/**'`
138function (_, index, str) {
139 return index + 6 < str.length // case: /**/
140 // > A slash followed by two consecutive asterisks then a slash matches
141 // > zero or more directories.
142 // > For example, "a/**/b" matches "a/b", "a/x/b", "a/x/y/b" and so on.
143 // '/**/'
144 ? '(?:\\/[^\\/]+)*' // case: /**
145 // > A trailing `"/**"` matches everything inside.
146 // #21: everything inside but it should not include the current folder
147 : '\\/.+';
148}], // intermediate wildcards
149[// Never replace escaped '*'
150// ignore rule '\*' will match the path '*'
151// 'abc.*/' -> go
152// 'abc.*' -> skip this rule
153/(^|[^\\]+)\\\*(?=.+)/g, // '*.js' matches '.js'
154// '*.js' doesn't match 'abc'
155function (_, p1) {
156 return "".concat(p1, "[^\\/]*");
157}], [// unescape, revert step 3 except for back slash
158// For example, if a user escape a '\\*',
159// after step 3, the result will be '\\\\\\*'
160/\\\\\\(?=[$.|*+(){^])/g, function () {
161 return ESCAPE;
162}], [// '\\\\' -> '\\'
163/\\\\/g, function () {
164 return ESCAPE;
165}], [// > The range notation, e.g. [a-zA-Z],
166// > can be used to match one of the characters in a range.
167// `\` is escaped by step 3
168/(\\)?\[([^\]/]*?)(\\*)($|\])/g, function (match, leadEscape, range, endEscape, close) {
169 return leadEscape === ESCAPE // '\\[bar]' -> '\\\\[bar\\]'
170 ? "\\[".concat(range).concat(cleanRangeBackSlash(endEscape)).concat(close) : close === ']' ? endEscape.length % 2 === 0 // A normal case, and it is a range notation
171 // '[bar]'
172 // '[bar\\\\]'
173 ? "[".concat(sanitizeRange(range)).concat(endEscape, "]") // Invalid range notaton
174 // '[bar\\]' -> '[bar\\\\]'
175 : '[]' : '[]';
176}], // ending
177[// 'js' will not match 'js.'
178// 'ab' will not match 'abc'
179/(?:[^*])$/, // WTF!
180// https://git-scm.com/docs/gitignore
181// changes in [2.22.1](https://git-scm.com/docs/gitignore/2.22.1)
182// which re-fixes #24, #38
183// > If there is a separator at the end of the pattern then the pattern
184// > will only match directories, otherwise the pattern can match both
185// > files and directories.
186// 'js*' will not match 'a.js'
187// 'js/' will not match 'a.js'
188// 'js' will match 'a.js' and 'a.js/'
189function (match) {
190 return /\/$/.test(match) // foo/ will not match 'foo'
191 ? "".concat(match, "$") // foo matches 'foo' and 'foo/'
192 : "".concat(match, "(?=$|\\/$)");
193}], // trailing wildcard
194[/(\^|\\\/)?\\\*$/, function (_, p1) {
195 var prefix = p1 // '\^':
196 // '/*' does not match EMPTY
197 // '/*' does not match everything
198 // '\\\/':
199 // 'abc/*' does not match 'abc/'
200 ? "".concat(p1, "[^/]+") // 'a*' matches 'a'
201 // 'a*' matches 'aa'
202 : '[^/]*';
203 return "".concat(prefix, "(?=$|\\/$)");
204}]]; // A simple cache, because an ignore rule only has only one certain meaning
205
206var regexCache = Object.create(null); // @param {pattern}
207
208var makeRegex = function makeRegex(pattern, negative, ignorecase) {
209 var r = regexCache[pattern];
210
211 if (r) {
212 return r;
213 } // const replacers = negative
214 // ? NEGATIVE_REPLACERS
215 // : POSITIVE_REPLACERS
216
217
218 var source = REPLACERS.reduce(function (prev, current) {
219 return prev.replace(current[0], current[1].bind(pattern));
220 }, pattern);
221 return regexCache[pattern] = ignorecase ? new RegExp(source, 'i') : new RegExp(source);
222};
223
224var isString = function isString(subject) {
225 return typeof subject === 'string';
226}; // > A blank line matches no files, so it can serve as a separator for readability.
227
228
229var checkPattern = function checkPattern(pattern) {
230 return pattern && isString(pattern) && !REGEX_TEST_BLANK_LINE.test(pattern) // > A line starting with # serves as a comment.
231 && pattern.indexOf('#') !== 0;
232};
233
234var splitPattern = function splitPattern(pattern) {
235 return pattern.split(REGEX_SPLITALL_CRLF);
236};
237
238var IgnoreRule = function IgnoreRule(origin, pattern, negative, regex) {
239 _classCallCheck(this, IgnoreRule);
240
241 this.origin = origin;
242 this.pattern = pattern;
243 this.negative = negative;
244 this.regex = regex;
245};
246
247var createRule = function createRule(pattern, ignorecase) {
248 var origin = pattern;
249 var negative = false; // > An optional prefix "!" which negates the pattern;
250
251 if (pattern.indexOf('!') === 0) {
252 negative = true;
253 pattern = pattern.substr(1);
254 }
255
256 pattern = pattern // > Put a backslash ("\") in front of the first "!" for patterns that
257 // > begin with a literal "!", for example, `"\!important!.txt"`.
258 .replace(REGEX_REPLACE_LEADING_EXCAPED_EXCLAMATION, '!') // > Put a backslash ("\") in front of the first hash for patterns that
259 // > begin with a hash.
260 .replace(REGEX_REPLACE_LEADING_EXCAPED_HASH, '#');
261 var regex = makeRegex(pattern, negative, ignorecase);
262 return new IgnoreRule(origin, pattern, negative, regex);
263};
264
265var throwError = function throwError(message, Ctor) {
266 throw new Ctor(message);
267};
268
269var checkPath = function checkPath(path, originalPath, doThrow) {
270 if (!isString(path)) {
271 return doThrow("path must be a string, but got `".concat(originalPath, "`"), TypeError);
272 } // We don't know if we should ignore EMPTY, so throw
273
274
275 if (!path) {
276 return doThrow("path must not be empty", TypeError);
277 } // Check if it is a relative path
278
279
280 if (checkPath.isNotRelative(path)) {
281 var r = '`path.relative()`d';
282 return doThrow("path should be a ".concat(r, " string, but got \"").concat(originalPath, "\""), RangeError);
283 }
284
285 return true;
286};
287
288var isNotRelative = function isNotRelative(path) {
289 return REGEX_TEST_INVALID_PATH.test(path);
290};
291
292checkPath.isNotRelative = isNotRelative;
293
294checkPath.convert = function (p) {
295 return p;
296};
297
298var Ignore = /*#__PURE__*/function () {
299 function Ignore() {
300 var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
301 _ref$ignorecase = _ref.ignorecase,
302 ignorecase = _ref$ignorecase === void 0 ? true : _ref$ignorecase;
303
304 _classCallCheck(this, Ignore);
305
306 this._rules = [];
307 this._ignorecase = ignorecase;
308 define(this, KEY_IGNORE, true);
309
310 this._initCache();
311 }
312
313 _createClass(Ignore, [{
314 key: "_initCache",
315 value: function _initCache() {
316 this._ignoreCache = Object.create(null);
317 this._testCache = Object.create(null);
318 }
319 }, {
320 key: "_addPattern",
321 value: function _addPattern(pattern) {
322 // #32
323 if (pattern && pattern[KEY_IGNORE]) {
324 this._rules = this._rules.concat(pattern._rules);
325 this._added = true;
326 return;
327 }
328
329 if (checkPattern(pattern)) {
330 var rule = createRule(pattern, this._ignorecase);
331 this._added = true;
332
333 this._rules.push(rule);
334 }
335 } // @param {Array<string> | string | Ignore} pattern
336
337 }, {
338 key: "add",
339 value: function add(pattern) {
340 this._added = false;
341 makeArray(isString(pattern) ? splitPattern(pattern) : pattern).forEach(this._addPattern, this); // Some rules have just added to the ignore,
342 // making the behavior changed.
343
344 if (this._added) {
345 this._initCache();
346 }
347
348 return this;
349 } // legacy
350
351 }, {
352 key: "addPattern",
353 value: function addPattern(pattern) {
354 return this.add(pattern);
355 } // | ignored : unignored
356 // negative | 0:0 | 0:1 | 1:0 | 1:1
357 // -------- | ------- | ------- | ------- | --------
358 // 0 | TEST | TEST | SKIP | X
359 // 1 | TESTIF | SKIP | TEST | X
360 // - SKIP: always skip
361 // - TEST: always test
362 // - TESTIF: only test if checkUnignored
363 // - X: that never happen
364 // @param {boolean} whether should check if the path is unignored,
365 // setting `checkUnignored` to `false` could reduce additional
366 // path matching.
367 // @returns {TestResult} true if a file is ignored
368
369 }, {
370 key: "_testOne",
371 value: function _testOne(path, checkUnignored) {
372 var ignored = false;
373 var unignored = false;
374
375 this._rules.forEach(function (rule) {
376 var negative = rule.negative;
377
378 if (unignored === negative && ignored !== unignored || negative && !ignored && !unignored && !checkUnignored) {
379 return;
380 }
381
382 var matched = rule.regex.test(path);
383
384 if (matched) {
385 ignored = !negative;
386 unignored = negative;
387 }
388 });
389
390 return {
391 ignored: ignored,
392 unignored: unignored
393 };
394 } // @returns {TestResult}
395
396 }, {
397 key: "_test",
398 value: function _test(originalPath, cache, checkUnignored, slices) {
399 var path = originalPath // Supports nullable path
400 && checkPath.convert(originalPath);
401 checkPath(path, originalPath, throwError);
402 return this._t(path, cache, checkUnignored, slices);
403 }
404 }, {
405 key: "_t",
406 value: function _t(path, cache, checkUnignored, slices) {
407 if (path in cache) {
408 return cache[path];
409 }
410
411 if (!slices) {
412 // path/to/a.js
413 // ['path', 'to', 'a.js']
414 slices = path.split(SLASH);
415 }
416
417 slices.pop(); // If the path has no parent directory, just test it
418
419 if (!slices.length) {
420 return cache[path] = this._testOne(path, checkUnignored);
421 }
422
423 var parent = this._t(slices.join(SLASH) + SLASH, cache, checkUnignored, slices); // If the path contains a parent directory, check the parent first
424
425
426 return cache[path] = parent.ignored // > It is not possible to re-include a file if a parent directory of
427 // > that file is excluded.
428 ? parent : this._testOne(path, checkUnignored);
429 }
430 }, {
431 key: "ignores",
432 value: function ignores(path) {
433 return this._test(path, this._ignoreCache, false).ignored;
434 }
435 }, {
436 key: "createFilter",
437 value: function createFilter() {
438 var _this = this;
439
440 return function (path) {
441 return !_this.ignores(path);
442 };
443 }
444 }, {
445 key: "filter",
446 value: function filter(paths) {
447 return makeArray(paths).filter(this.createFilter());
448 } // @returns {TestResult}
449
450 }, {
451 key: "test",
452 value: function test(path) {
453 return this._test(path, this._testCache, true);
454 }
455 }]);
456
457 return Ignore;
458}();
459
460var factory = function factory(options) {
461 return new Ignore(options);
462};
463
464var returnFalse = function returnFalse() {
465 return false;
466};
467
468var isPathValid = function isPathValid(path) {
469 return checkPath(path && checkPath.convert(path), path, returnFalse);
470};
471
472factory.isPathValid = isPathValid; // Fixes typescript
473
474factory["default"] = factory;
475module.exports = factory; // Windows
476// --------------------------------------------------------------
477
478/* istanbul ignore if */
479
480if ( // Detect `process` so that it can run in browsers.
481typeof process !== 'undefined' && (process.env && process.env.IGNORE_TEST_WIN32 || process.platform === 'win32')) {
482 /* eslint no-control-regex: "off" */
483 var makePosix = function makePosix(str) {
484 return /^\\\\\?\\/.test(str) || /[\0-\x1F"<>\|]+/.test(str) ? str : str.replace(/\\/g, '/');
485 };
486
487 checkPath.convert = makePosix; // 'C:\\foo' <- 'C:\\foo' has been converted to 'C:/'
488 // 'd:\\foo'
489
490 var REGIX_IS_WINDOWS_PATH_ABSOLUTE = /^[a-z]:\//i;
491
492 checkPath.isNotRelative = function (path) {
493 return REGIX_IS_WINDOWS_PATH_ABSOLUTE.test(path) || isNotRelative(path);
494 };
495}
Note: See TracBrowser for help on using the repository browser.