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 // A simple implementation of make-array
8 function make_array(subject) {
9 return Array.isArray(subject) ? subject : [subject];
12 var REGEX_BLANK_LINE = /^\s+$/;
13 var REGEX_LEADING_EXCAPED_EXCLAMATION = /^\\!/;
14 var REGEX_LEADING_EXCAPED_HASH = /^\\#/;
16 var KEY_IGNORE = typeof Symbol !== 'undefined' ? Symbol.for('node-ignore')
17 /* istanbul ignore next */
20 var define = function define(object, key, value) {
21 return Object.defineProperty(object, key, { value });
24 var REGEX_REGEXP_RANGE = /([0-z])-([0-z])/g;
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.
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`
47 // '`foo/`' should not continue with the '`..`'
48 var DEFAULT_REPLACER_PREFIX = [
50 // > Trailing spaces are ignored unless they are quoted with backslash ("\")
55 /\\?\s+$/, function (match) {
56 return match.indexOf('\\') === 0 ? ' ' : '';
59 // replace (\ ) with ' '
60 [/\\\s/g, function () {
64 // Escape metacharacters
65 // which is written down by users but means special for regular expressions.
67 // > There are 12 characters with special meanings:
68 // > - the backslash \,
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) {
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}`;
89 // > a question mark (?) matches a single character
90 /(?!\\)\?/g, function () {
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
104 // replace special metacharacter slash after the leading slash
105 [/\//g, function () {
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 '\\*'
116 // '**/foo' <-> 'foo'
118 return '^(?:.*\\/)?';
121 var DEFAULT_REPLACER_SUFFIX = [
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
135 // > Otherwise, Git treats the pattern as a shell glob suitable for
136 // > consumption by fnmatch(3)
142 // Use lookahead assertions so that we could match more than one `'/**'`
143 /\\\/\\\*\\\*(?=\\\/|$)/g,
145 // Zero, one or several directories
146 // should not use '*', or it will be replaced by the next replacer
148 // Check if it is not the last `'/**'`
149 function (match, index, str) {
150 return index + 6 < str.length
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.
160 // > A trailing `"/**"` matches everything inside.
162 // #21: everything inside but it should not include the current folder
166 // intermediate wildcards
168 // Never replace escaped '*'
169 // ignore rule '\*' will match the path '*'
172 // 'abc.*' -> skip this rule
173 /(^|[^\\]+)\\\*(?=.+)/g,
175 // '*.js' matches '.js'
176 // '*.js' doesn't match 'abc'
177 function (match, p1) {
178 return `${p1}[^\\/]*`;
182 [/(\^|\\\/)?\\\*$/, function (match, p1) {
185 // '/*' does not match ''
186 // '/*' does not match everything
189 // 'abc/*' does not match 'abc/'
196 return `${prefix}(?=$|\\/$)`;
199 /\\\\\\/g, function () {
203 var POSITIVE_REPLACERS = [].concat(DEFAULT_REPLACER_PREFIX, [
219 // 'js' will not match 'js.'
220 // 'ab' will not match 'abc'
223 // 'js*' will not match 'a.js'
224 // 'js/' will not match 'a.js'
225 // 'js' will match 'a.js' and 'a.js/'
227 return `${match}(?=$|\\/)`;
228 }]], DEFAULT_REPLACER_SUFFIX);
230 var NEGATIVE_REPLACERS = [].concat(DEFAULT_REPLACER_PREFIX, [
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.
238 // ['node_modules/*', '!node_modules']
239 // should ignore `node_modules/a.js`
240 [/(?:[^*])$/, function (match) {
241 return `${match}(?=$|\\/$)`;
242 }]], DEFAULT_REPLACER_SUFFIX);
244 // A simple cache, because an ignore rule only has only one certain meaning
245 var cache = Object.create(null);
248 var make_regex = function make_regex(pattern, negative, ignorecase) {
249 var r = cache[pattern];
254 var replacers = negative ? NEGATIVE_REPLACERS : POSITIVE_REPLACERS;
256 var source = replacers.reduce(function (prev, current) {
257 return prev.replace(current[0], current[1].bind(pattern));
260 return cache[pattern] = ignorecase ? new RegExp(source, 'i') : new RegExp(source);
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)
267 // > A line starting with # serves as a comment.
268 && pattern.indexOf('#') !== 0;
271 var createRule = function createRule(pattern, ignorecase) {
272 var origin = pattern;
273 var negative = false;
275 // > An optional prefix "!" which negates the pattern;
276 if (pattern.indexOf('!') === 0) {
278 pattern = pattern.substr(1);
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, '#');
289 var regex = make_regex(pattern, negative, ignorecase);
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;
305 _classCallCheck(this, IgnoreBase);
308 this._ignorecase = ignorecase;
309 define(this, KEY_IGNORE, true);
313 _createClass(IgnoreBase, [{
315 value: function _initCache() {
316 this._cache = Object.create(null);
319 // @param {Array.<string>|string|Ignore} pattern
323 value: function add(pattern) {
326 if (typeof pattern === 'string') {
327 pattern = pattern.split(/\r?\n/g);
330 make_array(pattern).forEach(this._addPattern, this);
332 // Some rules have just added to the ignore,
333 // making the behavior changed.
345 value: function addPattern(pattern) {
346 return this.add(pattern);
350 value: function _addPattern(pattern) {
352 if (pattern && pattern[KEY_IGNORE]) {
353 this._rules = this._rules.concat(pattern._rules);
358 if (checkPattern(pattern)) {
359 var rule = createRule(pattern, this._ignorecase);
361 this._rules.push(rule);
366 value: function filter(paths) {
369 return make_array(paths).filter(function (path) {
370 return _this._filter(path);
375 value: function createFilter() {
378 return function (path) {
379 return _this2._filter(path);
384 value: function ignores(path) {
385 return !this._filter(path);
388 // @returns `Boolean` true if the `path` is NOT ignored
392 value: function _filter(path, slices) {
397 if (path in this._cache) {
398 return this._cache[path];
403 // ['path', 'to', 'a.js']
404 slices = path.split(SLASH);
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)
415 // Or only test the path
419 // @returns {Boolean} true if a file is NOT ignored
423 value: function _test(path) {
424 // Explicitly define variable type by setting matched to `0`
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);
443 // --------------------------------------------------------------
444 /* istanbul ignore 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;
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, '/')
458 IgnoreBase.prototype._filter = function filterWin32(path, slices) {
459 path = make_posix(path);
460 return filter.call(this, path, slices);
464 module.exports = function (options) {
465 return new IgnoreBase(options);