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