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