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