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; }; }();
5 function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
7 module.exports = function () {
8 return new IgnoreBase();
11 // A simple implementation of make-array
12 function make_array(subject) {
13 return Array.isArray(subject) ? subject : [subject];
16 var REGEX_BLANK_LINE = /^\s+$/;
17 var REGEX_LEADING_EXCAPED_EXCLAMATION = /^\\\!/;
18 var REGEX_LEADING_EXCAPED_HASH = /^\\#/;
20 var KEY_IGNORE = typeof Symbol !== 'undefined' ? Symbol.for('node-ignore')
21 /* istanbul ignore next */
24 var IgnoreBase = function () {
25 function IgnoreBase() {
26 _classCallCheck(this, IgnoreBase);
29 this[KEY_IGNORE] = true;
33 _createClass(IgnoreBase, [{
35 value: function _initCache() {
39 // @param {Array.<string>|string|Ignore} pattern
43 value: function add(pattern) {
46 if (typeof pattern === 'string') {
47 pattern = pattern.split(/\r?\n/g);
50 make_array(pattern).forEach(this._addPattern, this);
52 // Some rules have just added to the ignore,
53 // making the behavior changed.
65 value: function addPattern(pattern) {
66 return this.add(pattern);
70 value: function _addPattern(pattern) {
72 if (pattern && pattern[KEY_IGNORE]) {
73 this._rules = this._rules.concat(pattern._rules);
78 if (this._checkPattern(pattern)) {
79 var rule = this._createRule(pattern);
81 this._rules.push(rule);
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)
90 // > A line starting with # serves as a comment.
91 && pattern.indexOf('#') !== 0;
95 value: function filter(paths) {
98 return make_array(paths).filter(function (path) {
99 return _this._filter(path);
104 value: function createFilter() {
107 return function (path) {
108 return _this2._filter(path);
113 value: function ignores(path) {
114 return !this._filter(path);
118 value: function _createRule(pattern) {
119 var origin = pattern;
120 var negative = false;
122 // > An optional prefix "!" which negates the pattern;
123 if (pattern.indexOf('!') === 0) {
125 pattern = pattern.substr(1);
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, '#');
134 var regex = make_regex(pattern, negative);
144 // @returns `Boolean` true if the `path` is NOT ignored
148 value: function _filter(path, slices) {
153 if (path in this._cache) {
154 return this._cache[path];
159 // ['path', 'to', 'a.js']
160 slices = path.split(SLASH);
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)
170 // Or only test the path
174 // @returns {Boolean} true if a file is NOT ignored
178 value: function _test(path) {
179 // Explicitly define variable type by setting matched to `0`
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);
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`
207 // '`foo/`' should not continue with the '`..`'
210 var DEFAULT_REPLACER_PREFIX = [
212 // > Trailing spaces are ignored unless they are quoted with backslash ("\")
217 /\\?\s+$/, function (match) {
218 return match.indexOf('\\') === 0 ? ' ' : '';
221 // replace (\ ) with ' '
222 [/\\\s/g, function () {
226 // Escape metacharacters
227 // which is written down by users but means special for regular expressions.
229 // > There are 12 characters with special meanings:
230 // > - the backslash \,
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) {
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
257 // replace special metacharacter slash after the leading slash
258 [/\//g, function () {
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 '\\*'
268 // '**/foo' <-> 'foo'
270 return '^(?:.*\\/)?';
273 var DEFAULT_REPLACER_SUFFIX = [
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
284 // > Otherwise, Git treats the pattern as a shell glob suitable for consumption by fnmatch(3)
290 // Use lookahead assertions so that we could match more than one `'/**'`
291 /\\\/\\\*\\\*(?=\\\/|$)/g,
293 // Zero, one or several directories
294 // should not use '*', or it will be replaced by the next replacer
296 // Check if it is not the last `'/**'`
297 function (match, index, str) {
298 return index + 6 < str.length
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.
307 // > A trailing `"/**"` matches everything inside.
309 // #21: everything inside but it should not include the current folder
313 // intermediate wildcards
315 // Never replace escaped '*'
316 // ignore rule '\*' will match the path '*'
319 // 'abc.*' -> skip this rule
320 /(^|[^\\]+)\\\*(?=.+)/g,
322 // '*.js' matches '.js'
323 // '*.js' doesn't match 'abc'
324 function (match, p1) {
325 return p1 + '[^\\/]*';
329 [/(\^|\\\/)?\\\*$/, function (match, p1) {
332 // '/*' does not match ''
333 // '/*' does not match everything
336 // 'abc/*' does not match 'abc/'
341 : '[^/]*') + '(?=$|\\/$)';
344 /\\\\\\/g, function () {
348 var POSITIVE_REPLACERS = [].concat(DEFAULT_REPLACER_PREFIX, [
364 // 'js' will not match 'js.'
365 // 'ab' will not match 'abc'
368 // 'js*' will not match 'a.js'
369 // 'js/' will not match 'a.js'
370 // 'js' will match 'a.js' and 'a.js/'
372 return match + '(?=$|\\/)';
373 }]], DEFAULT_REPLACER_SUFFIX);
375 var NEGATIVE_REPLACERS = [].concat(DEFAULT_REPLACER_PREFIX, [
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.
383 // ['node_modules/*', '!node_modules']
384 // should ignore `node_modules/a.js`
385 [/(?:[^*])$/, function (match) {
386 return match + '(?=$|\\/$)';
387 }]], DEFAULT_REPLACER_SUFFIX);
389 // A simple cache, because an ignore rule only has only one certain meaning
393 function make_regex(pattern, negative) {
394 var r = cache[pattern];
399 var replacers = negative ? NEGATIVE_REPLACERS : POSITIVE_REPLACERS;
401 var source = replacers.reduce(function (prev, current) {
402 return prev.replace(current[0], current[1].bind(pattern));
405 return cache[pattern] = new RegExp(source, 'i');
409 // --------------------------------------------------------------
410 /* istanbul ignore 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')) {
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, '/')
421 IgnoreBase.prototype._filter = function (path, slices) {
422 path = make_posix(path);
423 return filter.call(this, path, slices);