| [9af201e] | 1 | module.exports = minimatch
|
|---|
| 2 | minimatch.Minimatch = Minimatch
|
|---|
| 3 |
|
|---|
| 4 | var path = (function () { try { return require('path') } catch (e) {}}()) || {
|
|---|
| 5 | sep: '/'
|
|---|
| 6 | }
|
|---|
| 7 | minimatch.sep = path.sep
|
|---|
| 8 |
|
|---|
| 9 | var GLOBSTAR = minimatch.GLOBSTAR = Minimatch.GLOBSTAR = {}
|
|---|
| 10 | var expand = require('brace-expansion')
|
|---|
| 11 |
|
|---|
| 12 | var plTypes = {
|
|---|
| 13 | '!': { open: '(?:(?!(?:', close: '))[^/]*?)'},
|
|---|
| 14 | '?': { open: '(?:', close: ')?' },
|
|---|
| 15 | '+': { open: '(?:', close: ')+' },
|
|---|
| 16 | '*': { open: '(?:', close: ')*' },
|
|---|
| 17 | '@': { open: '(?:', close: ')' }
|
|---|
| 18 | }
|
|---|
| 19 |
|
|---|
| 20 | // any single thing other than /
|
|---|
| 21 | // don't need to escape / when using new RegExp()
|
|---|
| 22 | var qmark = '[^/]'
|
|---|
| 23 |
|
|---|
| 24 | // * => any number of characters
|
|---|
| 25 | var star = qmark + '*?'
|
|---|
| 26 |
|
|---|
| 27 | // ** when dots are allowed. Anything goes, except .. and .
|
|---|
| 28 | // not (^ or / followed by one or two dots followed by $ or /),
|
|---|
| 29 | // followed by anything, any number of times.
|
|---|
| 30 | var twoStarDot = '(?:(?!(?:\\\/|^)(?:\\.{1,2})($|\\\/)).)*?'
|
|---|
| 31 |
|
|---|
| 32 | // not a ^ or / followed by a dot,
|
|---|
| 33 | // followed by anything, any number of times.
|
|---|
| 34 | var twoStarNoDot = '(?:(?!(?:\\\/|^)\\.).)*?'
|
|---|
| 35 |
|
|---|
| 36 | // characters that need to be escaped in RegExp.
|
|---|
| 37 | var reSpecials = charSet('().*{}+?[]^$\\!')
|
|---|
| 38 |
|
|---|
| 39 | // "abc" -> { a:true, b:true, c:true }
|
|---|
| 40 | function charSet (s) {
|
|---|
| 41 | return s.split('').reduce(function (set, c) {
|
|---|
| 42 | set[c] = true
|
|---|
| 43 | return set
|
|---|
| 44 | }, {})
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | // normalizes slashes.
|
|---|
| 48 | var slashSplit = /\/+/
|
|---|
| 49 |
|
|---|
| 50 | minimatch.filter = filter
|
|---|
| 51 | function filter (pattern, options) {
|
|---|
| 52 | options = options || {}
|
|---|
| 53 | return function (p, i, list) {
|
|---|
| 54 | return minimatch(p, pattern, options)
|
|---|
| 55 | }
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | function ext (a, b) {
|
|---|
| 59 | b = b || {}
|
|---|
| 60 | var t = {}
|
|---|
| 61 | Object.keys(a).forEach(function (k) {
|
|---|
| 62 | t[k] = a[k]
|
|---|
| 63 | })
|
|---|
| 64 | Object.keys(b).forEach(function (k) {
|
|---|
| 65 | t[k] = b[k]
|
|---|
| 66 | })
|
|---|
| 67 | return t
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | minimatch.defaults = function (def) {
|
|---|
| 71 | if (!def || typeof def !== 'object' || !Object.keys(def).length) {
|
|---|
| 72 | return minimatch
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | var orig = minimatch
|
|---|
| 76 |
|
|---|
| 77 | var m = function minimatch (p, pattern, options) {
|
|---|
| 78 | return orig(p, pattern, ext(def, options))
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | m.Minimatch = function Minimatch (pattern, options) {
|
|---|
| 82 | return new orig.Minimatch(pattern, ext(def, options))
|
|---|
| 83 | }
|
|---|
| 84 | m.Minimatch.defaults = function defaults (options) {
|
|---|
| 85 | return orig.defaults(ext(def, options)).Minimatch
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | m.filter = function filter (pattern, options) {
|
|---|
| 89 | return orig.filter(pattern, ext(def, options))
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | m.defaults = function defaults (options) {
|
|---|
| 93 | return orig.defaults(ext(def, options))
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | m.makeRe = function makeRe (pattern, options) {
|
|---|
| 97 | return orig.makeRe(pattern, ext(def, options))
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | m.braceExpand = function braceExpand (pattern, options) {
|
|---|
| 101 | return orig.braceExpand(pattern, ext(def, options))
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | m.match = function (list, pattern, options) {
|
|---|
| 105 | return orig.match(list, pattern, ext(def, options))
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | return m
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | Minimatch.defaults = function (def) {
|
|---|
| 112 | return minimatch.defaults(def).Minimatch
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | function minimatch (p, pattern, options) {
|
|---|
| 116 | assertValidPattern(pattern)
|
|---|
| 117 |
|
|---|
| 118 | if (!options) options = {}
|
|---|
| 119 |
|
|---|
| 120 | // shortcut: comments match nothing.
|
|---|
| 121 | if (!options.nocomment && pattern.charAt(0) === '#') {
|
|---|
| 122 | return false
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | return new Minimatch(pattern, options).match(p)
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | function Minimatch (pattern, options) {
|
|---|
| 129 | if (!(this instanceof Minimatch)) {
|
|---|
| 130 | return new Minimatch(pattern, options)
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | assertValidPattern(pattern)
|
|---|
| 134 |
|
|---|
| 135 | if (!options) options = {}
|
|---|
| 136 |
|
|---|
| 137 | pattern = pattern.trim()
|
|---|
| 138 |
|
|---|
| 139 | // windows support: need to use /, not \
|
|---|
| 140 | if (!options.allowWindowsEscape && path.sep !== '/') {
|
|---|
| 141 | pattern = pattern.split(path.sep).join('/')
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | this.options = options
|
|---|
| 145 | this.maxGlobstarRecursion = options.maxGlobstarRecursion !== undefined
|
|---|
| 146 | ? options.maxGlobstarRecursion : 200
|
|---|
| 147 | this.set = []
|
|---|
| 148 | this.pattern = pattern
|
|---|
| 149 | this.regexp = null
|
|---|
| 150 | this.negate = false
|
|---|
| 151 | this.comment = false
|
|---|
| 152 | this.empty = false
|
|---|
| 153 | this.partial = !!options.partial
|
|---|
| 154 |
|
|---|
| 155 | // make the set of regexps etc.
|
|---|
| 156 | this.make()
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | Minimatch.prototype.debug = function () {}
|
|---|
| 160 |
|
|---|
| 161 | Minimatch.prototype.make = make
|
|---|
| 162 | function make () {
|
|---|
| 163 | var pattern = this.pattern
|
|---|
| 164 | var options = this.options
|
|---|
| 165 |
|
|---|
| 166 | // empty patterns and comments match nothing.
|
|---|
| 167 | if (!options.nocomment && pattern.charAt(0) === '#') {
|
|---|
| 168 | this.comment = true
|
|---|
| 169 | return
|
|---|
| 170 | }
|
|---|
| 171 | if (!pattern) {
|
|---|
| 172 | this.empty = true
|
|---|
| 173 | return
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | // step 1: figure out negation, etc.
|
|---|
| 177 | this.parseNegate()
|
|---|
| 178 |
|
|---|
| 179 | // step 2: expand braces
|
|---|
| 180 | var set = this.globSet = this.braceExpand()
|
|---|
| 181 |
|
|---|
| 182 | if (options.debug) this.debug = function debug() { console.error.apply(console, arguments) }
|
|---|
| 183 |
|
|---|
| 184 | this.debug(this.pattern, set)
|
|---|
| 185 |
|
|---|
| 186 | // step 3: now we have a set, so turn each one into a series of path-portion
|
|---|
| 187 | // matching patterns.
|
|---|
| 188 | // These will be regexps, except in the case of "**", which is
|
|---|
| 189 | // set to the GLOBSTAR object for globstar behavior,
|
|---|
| 190 | // and will not contain any / characters
|
|---|
| 191 | set = this.globParts = set.map(function (s) {
|
|---|
| 192 | return s.split(slashSplit)
|
|---|
| 193 | })
|
|---|
| 194 |
|
|---|
| 195 | this.debug(this.pattern, set)
|
|---|
| 196 |
|
|---|
| 197 | // glob --> regexps
|
|---|
| 198 | set = set.map(function (s, si, set) {
|
|---|
| 199 | return s.map(this.parse, this)
|
|---|
| 200 | }, this)
|
|---|
| 201 |
|
|---|
| 202 | this.debug(this.pattern, set)
|
|---|
| 203 |
|
|---|
| 204 | // filter out everything that didn't compile properly.
|
|---|
| 205 | set = set.filter(function (s) {
|
|---|
| 206 | return s.indexOf(false) === -1
|
|---|
| 207 | })
|
|---|
| 208 |
|
|---|
| 209 | this.debug(this.pattern, set)
|
|---|
| 210 |
|
|---|
| 211 | this.set = set
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | Minimatch.prototype.parseNegate = parseNegate
|
|---|
| 215 | function parseNegate () {
|
|---|
| 216 | var pattern = this.pattern
|
|---|
| 217 | var negate = false
|
|---|
| 218 | var options = this.options
|
|---|
| 219 | var negateOffset = 0
|
|---|
| 220 |
|
|---|
| 221 | if (options.nonegate) return
|
|---|
| 222 |
|
|---|
| 223 | for (var i = 0, l = pattern.length
|
|---|
| 224 | ; i < l && pattern.charAt(i) === '!'
|
|---|
| 225 | ; i++) {
|
|---|
| 226 | negate = !negate
|
|---|
| 227 | negateOffset++
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| 230 | if (negateOffset) this.pattern = pattern.substr(negateOffset)
|
|---|
| 231 | this.negate = negate
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | // Brace expansion:
|
|---|
| 235 | // a{b,c}d -> abd acd
|
|---|
| 236 | // a{b,}c -> abc ac
|
|---|
| 237 | // a{0..3}d -> a0d a1d a2d a3d
|
|---|
| 238 | // a{b,c{d,e}f}g -> abg acdfg acefg
|
|---|
| 239 | // a{b,c}d{e,f}g -> abdeg acdeg abdeg abdfg
|
|---|
| 240 | //
|
|---|
| 241 | // Invalid sets are not expanded.
|
|---|
| 242 | // a{2..}b -> a{2..}b
|
|---|
| 243 | // a{b}c -> a{b}c
|
|---|
| 244 | minimatch.braceExpand = function (pattern, options) {
|
|---|
| 245 | return braceExpand(pattern, options)
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | Minimatch.prototype.braceExpand = braceExpand
|
|---|
| 249 |
|
|---|
| 250 | function braceExpand (pattern, options) {
|
|---|
| 251 | if (!options) {
|
|---|
| 252 | if (this instanceof Minimatch) {
|
|---|
| 253 | options = this.options
|
|---|
| 254 | } else {
|
|---|
| 255 | options = {}
|
|---|
| 256 | }
|
|---|
| 257 | }
|
|---|
| 258 |
|
|---|
| 259 | pattern = typeof pattern === 'undefined'
|
|---|
| 260 | ? this.pattern : pattern
|
|---|
| 261 |
|
|---|
| 262 | assertValidPattern(pattern)
|
|---|
| 263 |
|
|---|
| 264 | // Thanks to Yeting Li <https://github.com/yetingli> for
|
|---|
| 265 | // improving this regexp to avoid a ReDOS vulnerability.
|
|---|
| 266 | if (options.nobrace || !/\{(?:(?!\{).)*\}/.test(pattern)) {
|
|---|
| 267 | // shortcut. no need to expand.
|
|---|
| 268 | return [pattern]
|
|---|
| 269 | }
|
|---|
| 270 |
|
|---|
| 271 | return expand(pattern)
|
|---|
| 272 | }
|
|---|
| 273 |
|
|---|
| 274 | var MAX_PATTERN_LENGTH = 1024 * 64
|
|---|
| 275 | var assertValidPattern = function (pattern) {
|
|---|
| 276 | if (typeof pattern !== 'string') {
|
|---|
| 277 | throw new TypeError('invalid pattern')
|
|---|
| 278 | }
|
|---|
| 279 |
|
|---|
| 280 | if (pattern.length > MAX_PATTERN_LENGTH) {
|
|---|
| 281 | throw new TypeError('pattern is too long')
|
|---|
| 282 | }
|
|---|
| 283 | }
|
|---|
| 284 |
|
|---|
| 285 | // parse a component of the expanded set.
|
|---|
| 286 | // At this point, no pattern may contain "/" in it
|
|---|
| 287 | // so we're going to return a 2d array, where each entry is the full
|
|---|
| 288 | // pattern, split on '/', and then turned into a regular expression.
|
|---|
| 289 | // A regexp is made at the end which joins each array with an
|
|---|
| 290 | // escaped /, and another full one which joins each regexp with |.
|
|---|
| 291 | //
|
|---|
| 292 | // Following the lead of Bash 4.1, note that "**" only has special meaning
|
|---|
| 293 | // when it is the *only* thing in a path portion. Otherwise, any series
|
|---|
| 294 | // of * is equivalent to a single *. Globstar behavior is enabled by
|
|---|
| 295 | // default, and can be disabled by setting options.noglobstar.
|
|---|
| 296 | Minimatch.prototype.parse = parse
|
|---|
| 297 | var SUBPARSE = {}
|
|---|
| 298 | function parse (pattern, isSub) {
|
|---|
| 299 | assertValidPattern(pattern)
|
|---|
| 300 |
|
|---|
| 301 | var options = this.options
|
|---|
| 302 |
|
|---|
| 303 | // shortcuts
|
|---|
| 304 | if (pattern === '**') {
|
|---|
| 305 | if (!options.noglobstar)
|
|---|
| 306 | return GLOBSTAR
|
|---|
| 307 | else
|
|---|
| 308 | pattern = '*'
|
|---|
| 309 | }
|
|---|
| 310 | if (pattern === '') return ''
|
|---|
| 311 |
|
|---|
| 312 | var re = ''
|
|---|
| 313 | var hasMagic = !!options.nocase
|
|---|
| 314 | var escaping = false
|
|---|
| 315 | // ? => one single character
|
|---|
| 316 | var patternListStack = []
|
|---|
| 317 | var negativeLists = []
|
|---|
| 318 | var stateChar
|
|---|
| 319 | var inClass = false
|
|---|
| 320 | var reClassStart = -1
|
|---|
| 321 | var classStart = -1
|
|---|
| 322 | // . and .. never match anything that doesn't start with .,
|
|---|
| 323 | // even when options.dot is set.
|
|---|
| 324 | var patternStart = pattern.charAt(0) === '.' ? '' // anything
|
|---|
| 325 | // not (start or / followed by . or .. followed by / or end)
|
|---|
| 326 | : options.dot ? '(?!(?:^|\\\/)\\.{1,2}(?:$|\\\/))'
|
|---|
| 327 | : '(?!\\.)'
|
|---|
| 328 | var self = this
|
|---|
| 329 |
|
|---|
| 330 | function clearStateChar () {
|
|---|
| 331 | if (stateChar) {
|
|---|
| 332 | // we had some state-tracking character
|
|---|
| 333 | // that wasn't consumed by this pass.
|
|---|
| 334 | switch (stateChar) {
|
|---|
| 335 | case '*':
|
|---|
| 336 | re += star
|
|---|
| 337 | hasMagic = true
|
|---|
| 338 | break
|
|---|
| 339 | case '?':
|
|---|
| 340 | re += qmark
|
|---|
| 341 | hasMagic = true
|
|---|
| 342 | break
|
|---|
| 343 | default:
|
|---|
| 344 | re += '\\' + stateChar
|
|---|
| 345 | break
|
|---|
| 346 | }
|
|---|
| 347 | self.debug('clearStateChar %j %j', stateChar, re)
|
|---|
| 348 | stateChar = false
|
|---|
| 349 | }
|
|---|
| 350 | }
|
|---|
| 351 |
|
|---|
| 352 | for (var i = 0, len = pattern.length, c
|
|---|
| 353 | ; (i < len) && (c = pattern.charAt(i))
|
|---|
| 354 | ; i++) {
|
|---|
| 355 | this.debug('%s\t%s %s %j', pattern, i, re, c)
|
|---|
| 356 |
|
|---|
| 357 | // skip over any that are escaped.
|
|---|
| 358 | if (escaping && reSpecials[c]) {
|
|---|
| 359 | re += '\\' + c
|
|---|
| 360 | escaping = false
|
|---|
| 361 | continue
|
|---|
| 362 | }
|
|---|
| 363 |
|
|---|
| 364 | switch (c) {
|
|---|
| 365 | /* istanbul ignore next */
|
|---|
| 366 | case '/': {
|
|---|
| 367 | // completely not allowed, even escaped.
|
|---|
| 368 | // Should already be path-split by now.
|
|---|
| 369 | return false
|
|---|
| 370 | }
|
|---|
| 371 |
|
|---|
| 372 | case '\\':
|
|---|
| 373 | clearStateChar()
|
|---|
| 374 | escaping = true
|
|---|
| 375 | continue
|
|---|
| 376 |
|
|---|
| 377 | // the various stateChar values
|
|---|
| 378 | // for the "extglob" stuff.
|
|---|
| 379 | case '?':
|
|---|
| 380 | case '*':
|
|---|
| 381 | case '+':
|
|---|
| 382 | case '@':
|
|---|
| 383 | case '!':
|
|---|
| 384 | this.debug('%s\t%s %s %j <-- stateChar', pattern, i, re, c)
|
|---|
| 385 |
|
|---|
| 386 | // all of those are literals inside a class, except that
|
|---|
| 387 | // the glob [!a] means [^a] in regexp
|
|---|
| 388 | if (inClass) {
|
|---|
| 389 | this.debug(' in class')
|
|---|
| 390 | if (c === '!' && i === classStart + 1) c = '^'
|
|---|
| 391 | re += c
|
|---|
| 392 | continue
|
|---|
| 393 | }
|
|---|
| 394 |
|
|---|
| 395 | // coalesce consecutive non-globstar * characters
|
|---|
| 396 | if (c === '*' && stateChar === '*') continue
|
|---|
| 397 |
|
|---|
| 398 | // if we already have a stateChar, then it means
|
|---|
| 399 | // that there was something like ** or +? in there.
|
|---|
| 400 | // Handle the stateChar, then proceed with this one.
|
|---|
| 401 | self.debug('call clearStateChar %j', stateChar)
|
|---|
| 402 | clearStateChar()
|
|---|
| 403 | stateChar = c
|
|---|
| 404 | // if extglob is disabled, then +(asdf|foo) isn't a thing.
|
|---|
| 405 | // just clear the statechar *now*, rather than even diving into
|
|---|
| 406 | // the patternList stuff.
|
|---|
| 407 | if (options.noext) clearStateChar()
|
|---|
| 408 | continue
|
|---|
| 409 |
|
|---|
| 410 | case '(':
|
|---|
| 411 | if (inClass) {
|
|---|
| 412 | re += '('
|
|---|
| 413 | continue
|
|---|
| 414 | }
|
|---|
| 415 |
|
|---|
| 416 | if (!stateChar) {
|
|---|
| 417 | re += '\\('
|
|---|
| 418 | continue
|
|---|
| 419 | }
|
|---|
| 420 |
|
|---|
| 421 | patternListStack.push({
|
|---|
| 422 | type: stateChar,
|
|---|
| 423 | start: i - 1,
|
|---|
| 424 | reStart: re.length,
|
|---|
| 425 | open: plTypes[stateChar].open,
|
|---|
| 426 | close: plTypes[stateChar].close
|
|---|
| 427 | })
|
|---|
| 428 | // negation is (?:(?!js)[^/]*)
|
|---|
| 429 | re += stateChar === '!' ? '(?:(?!(?:' : '(?:'
|
|---|
| 430 | this.debug('plType %j %j', stateChar, re)
|
|---|
| 431 | stateChar = false
|
|---|
| 432 | continue
|
|---|
| 433 |
|
|---|
| 434 | case ')':
|
|---|
| 435 | if (inClass || !patternListStack.length) {
|
|---|
| 436 | re += '\\)'
|
|---|
| 437 | continue
|
|---|
| 438 | }
|
|---|
| 439 |
|
|---|
| 440 | clearStateChar()
|
|---|
| 441 | hasMagic = true
|
|---|
| 442 | var pl = patternListStack.pop()
|
|---|
| 443 | // negation is (?:(?!js)[^/]*)
|
|---|
| 444 | // The others are (?:<pattern>)<type>
|
|---|
| 445 | re += pl.close
|
|---|
| 446 | if (pl.type === '!') {
|
|---|
| 447 | negativeLists.push(pl)
|
|---|
| 448 | }
|
|---|
| 449 | pl.reEnd = re.length
|
|---|
| 450 | continue
|
|---|
| 451 |
|
|---|
| 452 | case '|':
|
|---|
| 453 | if (inClass || !patternListStack.length || escaping) {
|
|---|
| 454 | re += '\\|'
|
|---|
| 455 | escaping = false
|
|---|
| 456 | continue
|
|---|
| 457 | }
|
|---|
| 458 |
|
|---|
| 459 | clearStateChar()
|
|---|
| 460 | re += '|'
|
|---|
| 461 | continue
|
|---|
| 462 |
|
|---|
| 463 | // these are mostly the same in regexp and glob
|
|---|
| 464 | case '[':
|
|---|
| 465 | // swallow any state-tracking char before the [
|
|---|
| 466 | clearStateChar()
|
|---|
| 467 |
|
|---|
| 468 | if (inClass) {
|
|---|
| 469 | re += '\\' + c
|
|---|
| 470 | continue
|
|---|
| 471 | }
|
|---|
| 472 |
|
|---|
| 473 | inClass = true
|
|---|
| 474 | classStart = i
|
|---|
| 475 | reClassStart = re.length
|
|---|
| 476 | re += c
|
|---|
| 477 | continue
|
|---|
| 478 |
|
|---|
| 479 | case ']':
|
|---|
| 480 | // a right bracket shall lose its special
|
|---|
| 481 | // meaning and represent itself in
|
|---|
| 482 | // a bracket expression if it occurs
|
|---|
| 483 | // first in the list. -- POSIX.2 2.8.3.2
|
|---|
| 484 | if (i === classStart + 1 || !inClass) {
|
|---|
| 485 | re += '\\' + c
|
|---|
| 486 | escaping = false
|
|---|
| 487 | continue
|
|---|
| 488 | }
|
|---|
| 489 |
|
|---|
| 490 | // handle the case where we left a class open.
|
|---|
| 491 | // "[z-a]" is valid, equivalent to "\[z-a\]"
|
|---|
| 492 | // split where the last [ was, make sure we don't have
|
|---|
| 493 | // an invalid re. if so, re-walk the contents of the
|
|---|
| 494 | // would-be class to re-translate any characters that
|
|---|
| 495 | // were passed through as-is
|
|---|
| 496 | // TODO: It would probably be faster to determine this
|
|---|
| 497 | // without a try/catch and a new RegExp, but it's tricky
|
|---|
| 498 | // to do safely. For now, this is safe and works.
|
|---|
| 499 | var cs = pattern.substring(classStart + 1, i)
|
|---|
| 500 | try {
|
|---|
| 501 | RegExp('[' + cs + ']')
|
|---|
| 502 | } catch (er) {
|
|---|
| 503 | // not a valid class!
|
|---|
| 504 | var sp = this.parse(cs, SUBPARSE)
|
|---|
| 505 | re = re.substr(0, reClassStart) + '\\[' + sp[0] + '\\]'
|
|---|
| 506 | hasMagic = hasMagic || sp[1]
|
|---|
| 507 | inClass = false
|
|---|
| 508 | continue
|
|---|
| 509 | }
|
|---|
| 510 |
|
|---|
| 511 | // finish up the class.
|
|---|
| 512 | hasMagic = true
|
|---|
| 513 | inClass = false
|
|---|
| 514 | re += c
|
|---|
| 515 | continue
|
|---|
| 516 |
|
|---|
| 517 | default:
|
|---|
| 518 | // swallow any state char that wasn't consumed
|
|---|
| 519 | clearStateChar()
|
|---|
| 520 |
|
|---|
| 521 | if (escaping) {
|
|---|
| 522 | // no need
|
|---|
| 523 | escaping = false
|
|---|
| 524 | } else if (reSpecials[c]
|
|---|
| 525 | && !(c === '^' && inClass)) {
|
|---|
| 526 | re += '\\'
|
|---|
| 527 | }
|
|---|
| 528 |
|
|---|
| 529 | re += c
|
|---|
| 530 |
|
|---|
| 531 | } // switch
|
|---|
| 532 | } // for
|
|---|
| 533 |
|
|---|
| 534 | // handle the case where we left a class open.
|
|---|
| 535 | // "[abc" is valid, equivalent to "\[abc"
|
|---|
| 536 | if (inClass) {
|
|---|
| 537 | // split where the last [ was, and escape it
|
|---|
| 538 | // this is a huge pita. We now have to re-walk
|
|---|
| 539 | // the contents of the would-be class to re-translate
|
|---|
| 540 | // any characters that were passed through as-is
|
|---|
| 541 | cs = pattern.substr(classStart + 1)
|
|---|
| 542 | sp = this.parse(cs, SUBPARSE)
|
|---|
| 543 | re = re.substr(0, reClassStart) + '\\[' + sp[0]
|
|---|
| 544 | hasMagic = hasMagic || sp[1]
|
|---|
| 545 | }
|
|---|
| 546 |
|
|---|
| 547 | // handle the case where we had a +( thing at the *end*
|
|---|
| 548 | // of the pattern.
|
|---|
| 549 | // each pattern list stack adds 3 chars, and we need to go through
|
|---|
| 550 | // and escape any | chars that were passed through as-is for the regexp.
|
|---|
| 551 | // Go through and escape them, taking care not to double-escape any
|
|---|
| 552 | // | chars that were already escaped.
|
|---|
| 553 | for (pl = patternListStack.pop(); pl; pl = patternListStack.pop()) {
|
|---|
| 554 | var tail = re.slice(pl.reStart + pl.open.length)
|
|---|
| 555 | this.debug('setting tail', re, pl)
|
|---|
| 556 | // maybe some even number of \, then maybe 1 \, followed by a |
|
|---|
| 557 | tail = tail.replace(/((?:\\{2}){0,64})(\\?)\|/g, function (_, $1, $2) {
|
|---|
| 558 | if (!$2) {
|
|---|
| 559 | // the | isn't already escaped, so escape it.
|
|---|
| 560 | $2 = '\\'
|
|---|
| 561 | }
|
|---|
| 562 |
|
|---|
| 563 | // need to escape all those slashes *again*, without escaping the
|
|---|
| 564 | // one that we need for escaping the | character. As it works out,
|
|---|
| 565 | // escaping an even number of slashes can be done by simply repeating
|
|---|
| 566 | // it exactly after itself. That's why this trick works.
|
|---|
| 567 | //
|
|---|
| 568 | // I am sorry that you have to see this.
|
|---|
| 569 | return $1 + $1 + $2 + '|'
|
|---|
| 570 | })
|
|---|
| 571 |
|
|---|
| 572 | this.debug('tail=%j\n %s', tail, tail, pl, re)
|
|---|
| 573 | var t = pl.type === '*' ? star
|
|---|
| 574 | : pl.type === '?' ? qmark
|
|---|
| 575 | : '\\' + pl.type
|
|---|
| 576 |
|
|---|
| 577 | hasMagic = true
|
|---|
| 578 | re = re.slice(0, pl.reStart) + t + '\\(' + tail
|
|---|
| 579 | }
|
|---|
| 580 |
|
|---|
| 581 | // handle trailing things that only matter at the very end.
|
|---|
| 582 | clearStateChar()
|
|---|
| 583 | if (escaping) {
|
|---|
| 584 | // trailing \\
|
|---|
| 585 | re += '\\\\'
|
|---|
| 586 | }
|
|---|
| 587 |
|
|---|
| 588 | // only need to apply the nodot start if the re starts with
|
|---|
| 589 | // something that could conceivably capture a dot
|
|---|
| 590 | var addPatternStart = false
|
|---|
| 591 | switch (re.charAt(0)) {
|
|---|
| 592 | case '[': case '.': case '(': addPatternStart = true
|
|---|
| 593 | }
|
|---|
| 594 |
|
|---|
| 595 | // Hack to work around lack of negative lookbehind in JS
|
|---|
| 596 | // A pattern like: *.!(x).!(y|z) needs to ensure that a name
|
|---|
| 597 | // like 'a.xyz.yz' doesn't match. So, the first negative
|
|---|
| 598 | // lookahead, has to look ALL the way ahead, to the end of
|
|---|
| 599 | // the pattern.
|
|---|
| 600 | for (var n = negativeLists.length - 1; n > -1; n--) {
|
|---|
| 601 | var nl = negativeLists[n]
|
|---|
| 602 |
|
|---|
| 603 | var nlBefore = re.slice(0, nl.reStart)
|
|---|
| 604 | var nlFirst = re.slice(nl.reStart, nl.reEnd - 8)
|
|---|
| 605 | var nlLast = re.slice(nl.reEnd - 8, nl.reEnd)
|
|---|
| 606 | var nlAfter = re.slice(nl.reEnd)
|
|---|
| 607 |
|
|---|
| 608 | nlLast += nlAfter
|
|---|
| 609 |
|
|---|
| 610 | // Handle nested stuff like *(*.js|!(*.json)), where open parens
|
|---|
| 611 | // mean that we should *not* include the ) in the bit that is considered
|
|---|
| 612 | // "after" the negated section.
|
|---|
| 613 | var openParensBefore = nlBefore.split('(').length - 1
|
|---|
| 614 | var cleanAfter = nlAfter
|
|---|
| 615 | for (i = 0; i < openParensBefore; i++) {
|
|---|
| 616 | cleanAfter = cleanAfter.replace(/\)[+*?]?/, '')
|
|---|
| 617 | }
|
|---|
| 618 | nlAfter = cleanAfter
|
|---|
| 619 |
|
|---|
| 620 | var dollar = ''
|
|---|
| 621 | if (nlAfter === '' && isSub !== SUBPARSE) {
|
|---|
| 622 | dollar = '$'
|
|---|
| 623 | }
|
|---|
| 624 | var newRe = nlBefore + nlFirst + nlAfter + dollar + nlLast
|
|---|
| 625 | re = newRe
|
|---|
| 626 | }
|
|---|
| 627 |
|
|---|
| 628 | // if the re is not "" at this point, then we need to make sure
|
|---|
| 629 | // it doesn't match against an empty path part.
|
|---|
| 630 | // Otherwise a/* will match a/, which it should not.
|
|---|
| 631 | if (re !== '' && hasMagic) {
|
|---|
| 632 | re = '(?=.)' + re
|
|---|
| 633 | }
|
|---|
| 634 |
|
|---|
| 635 | if (addPatternStart) {
|
|---|
| 636 | re = patternStart + re
|
|---|
| 637 | }
|
|---|
| 638 |
|
|---|
| 639 | // parsing just a piece of a larger pattern.
|
|---|
| 640 | if (isSub === SUBPARSE) {
|
|---|
| 641 | return [re, hasMagic]
|
|---|
| 642 | }
|
|---|
| 643 |
|
|---|
| 644 | // skip the regexp for non-magical patterns
|
|---|
| 645 | // unescape anything in it, though, so that it'll be
|
|---|
| 646 | // an exact match against a file etc.
|
|---|
| 647 | if (!hasMagic) {
|
|---|
| 648 | return globUnescape(pattern)
|
|---|
| 649 | }
|
|---|
| 650 |
|
|---|
| 651 | var flags = options.nocase ? 'i' : ''
|
|---|
| 652 | try {
|
|---|
| 653 | var regExp = new RegExp('^' + re + '$', flags)
|
|---|
| 654 | } catch (er) /* istanbul ignore next - should be impossible */ {
|
|---|
| 655 | // If it was an invalid regular expression, then it can't match
|
|---|
| 656 | // anything. This trick looks for a character after the end of
|
|---|
| 657 | // the string, which is of course impossible, except in multi-line
|
|---|
| 658 | // mode, but it's not a /m regex.
|
|---|
| 659 | return new RegExp('$.')
|
|---|
| 660 | }
|
|---|
| 661 |
|
|---|
| 662 | regExp._glob = pattern
|
|---|
| 663 | regExp._src = re
|
|---|
| 664 |
|
|---|
| 665 | return regExp
|
|---|
| 666 | }
|
|---|
| 667 |
|
|---|
| 668 | minimatch.makeRe = function (pattern, options) {
|
|---|
| 669 | return new Minimatch(pattern, options || {}).makeRe()
|
|---|
| 670 | }
|
|---|
| 671 |
|
|---|
| 672 | Minimatch.prototype.makeRe = makeRe
|
|---|
| 673 | function makeRe () {
|
|---|
| 674 | if (this.regexp || this.regexp === false) return this.regexp
|
|---|
| 675 |
|
|---|
| 676 | // at this point, this.set is a 2d array of partial
|
|---|
| 677 | // pattern strings, or "**".
|
|---|
| 678 | //
|
|---|
| 679 | // It's better to use .match(). This function shouldn't
|
|---|
| 680 | // be used, really, but it's pretty convenient sometimes,
|
|---|
| 681 | // when you just want to work with a regex.
|
|---|
| 682 | var set = this.set
|
|---|
| 683 |
|
|---|
| 684 | if (!set.length) {
|
|---|
| 685 | this.regexp = false
|
|---|
| 686 | return this.regexp
|
|---|
| 687 | }
|
|---|
| 688 | var options = this.options
|
|---|
| 689 |
|
|---|
| 690 | var twoStar = options.noglobstar ? star
|
|---|
| 691 | : options.dot ? twoStarDot
|
|---|
| 692 | : twoStarNoDot
|
|---|
| 693 | var flags = options.nocase ? 'i' : ''
|
|---|
| 694 |
|
|---|
| 695 | var re = set.map(function (pattern) {
|
|---|
| 696 | return pattern.map(function (p) {
|
|---|
| 697 | return (p === GLOBSTAR) ? twoStar
|
|---|
| 698 | : (typeof p === 'string') ? regExpEscape(p)
|
|---|
| 699 | : p._src
|
|---|
| 700 | }).join('\\\/')
|
|---|
| 701 | }).join('|')
|
|---|
| 702 |
|
|---|
| 703 | // must match entire pattern
|
|---|
| 704 | // ending in a * or ** will make it less strict.
|
|---|
| 705 | re = '^(?:' + re + ')$'
|
|---|
| 706 |
|
|---|
| 707 | // can match anything, as long as it's not this.
|
|---|
| 708 | if (this.negate) re = '^(?!' + re + ').*$'
|
|---|
| 709 |
|
|---|
| 710 | try {
|
|---|
| 711 | this.regexp = new RegExp(re, flags)
|
|---|
| 712 | } catch (ex) /* istanbul ignore next - should be impossible */ {
|
|---|
| 713 | this.regexp = false
|
|---|
| 714 | }
|
|---|
| 715 | return this.regexp
|
|---|
| 716 | }
|
|---|
| 717 |
|
|---|
| 718 | minimatch.match = function (list, pattern, options) {
|
|---|
| 719 | options = options || {}
|
|---|
| 720 | var mm = new Minimatch(pattern, options)
|
|---|
| 721 | list = list.filter(function (f) {
|
|---|
| 722 | return mm.match(f)
|
|---|
| 723 | })
|
|---|
| 724 | if (mm.options.nonull && !list.length) {
|
|---|
| 725 | list.push(pattern)
|
|---|
| 726 | }
|
|---|
| 727 | return list
|
|---|
| 728 | }
|
|---|
| 729 |
|
|---|
| 730 | Minimatch.prototype.match = function match (f, partial) {
|
|---|
| 731 | if (typeof partial === 'undefined') partial = this.partial
|
|---|
| 732 | this.debug('match', f, this.pattern)
|
|---|
| 733 | // short-circuit in the case of busted things.
|
|---|
| 734 | // comments, etc.
|
|---|
| 735 | if (this.comment) return false
|
|---|
| 736 | if (this.empty) return f === ''
|
|---|
| 737 |
|
|---|
| 738 | if (f === '/' && partial) return true
|
|---|
| 739 |
|
|---|
| 740 | var options = this.options
|
|---|
| 741 |
|
|---|
| 742 | // windows: need to use /, not \
|
|---|
| 743 | if (path.sep !== '/') {
|
|---|
| 744 | f = f.split(path.sep).join('/')
|
|---|
| 745 | }
|
|---|
| 746 |
|
|---|
| 747 | // treat the test path as a set of pathparts.
|
|---|
| 748 | f = f.split(slashSplit)
|
|---|
| 749 | this.debug(this.pattern, 'split', f)
|
|---|
| 750 |
|
|---|
| 751 | // just ONE of the pattern sets in this.set needs to match
|
|---|
| 752 | // in order for it to be valid. If negating, then just one
|
|---|
| 753 | // match means that we have failed.
|
|---|
| 754 | // Either way, return on the first hit.
|
|---|
| 755 |
|
|---|
| 756 | var set = this.set
|
|---|
| 757 | this.debug(this.pattern, 'set', set)
|
|---|
| 758 |
|
|---|
| 759 | // Find the basename of the path by looking for the last non-empty segment
|
|---|
| 760 | var filename
|
|---|
| 761 | var i
|
|---|
| 762 | for (i = f.length - 1; i >= 0; i--) {
|
|---|
| 763 | filename = f[i]
|
|---|
| 764 | if (filename) break
|
|---|
| 765 | }
|
|---|
| 766 |
|
|---|
| 767 | for (i = 0; i < set.length; i++) {
|
|---|
| 768 | var pattern = set[i]
|
|---|
| 769 | var file = f
|
|---|
| 770 | if (options.matchBase && pattern.length === 1) {
|
|---|
| 771 | file = [filename]
|
|---|
| 772 | }
|
|---|
| 773 | var hit = this.matchOne(file, pattern, partial)
|
|---|
| 774 | if (hit) {
|
|---|
| 775 | if (options.flipNegate) return true
|
|---|
| 776 | return !this.negate
|
|---|
| 777 | }
|
|---|
| 778 | }
|
|---|
| 779 |
|
|---|
| 780 | // didn't get any hits. this is success if it's a negative
|
|---|
| 781 | // pattern, failure otherwise.
|
|---|
| 782 | if (options.flipNegate) return false
|
|---|
| 783 | return this.negate
|
|---|
| 784 | }
|
|---|
| 785 |
|
|---|
| 786 | // set partial to true to test if, for example,
|
|---|
| 787 | // "/a/b" matches the start of "/*/b/*/d"
|
|---|
| 788 | // Partial means, if you run out of file before you run
|
|---|
| 789 | // out of pattern, then that's fine, as long as all
|
|---|
| 790 | // the parts match.
|
|---|
| 791 | Minimatch.prototype.matchOne = function (file, pattern, partial) {
|
|---|
| 792 | if (pattern.indexOf(GLOBSTAR) !== -1) {
|
|---|
| 793 | return this._matchGlobstar(file, pattern, partial, 0, 0)
|
|---|
| 794 | }
|
|---|
| 795 | return this._matchOne(file, pattern, partial, 0, 0)
|
|---|
| 796 | }
|
|---|
| 797 |
|
|---|
| 798 | Minimatch.prototype._matchGlobstar = function (file, pattern, partial, fileIndex, patternIndex) {
|
|---|
| 799 | var i
|
|---|
| 800 |
|
|---|
| 801 | // find first globstar from patternIndex
|
|---|
| 802 | var firstgs = -1
|
|---|
| 803 | for (i = patternIndex; i < pattern.length; i++) {
|
|---|
| 804 | if (pattern[i] === GLOBSTAR) { firstgs = i; break }
|
|---|
| 805 | }
|
|---|
| 806 |
|
|---|
| 807 | // find last globstar
|
|---|
| 808 | var lastgs = -1
|
|---|
| 809 | for (i = pattern.length - 1; i >= 0; i--) {
|
|---|
| 810 | if (pattern[i] === GLOBSTAR) { lastgs = i; break }
|
|---|
| 811 | }
|
|---|
| 812 |
|
|---|
| 813 | var head = pattern.slice(patternIndex, firstgs)
|
|---|
| 814 | var body = partial ? pattern.slice(firstgs + 1) : pattern.slice(firstgs + 1, lastgs)
|
|---|
| 815 | var tail = partial ? [] : pattern.slice(lastgs + 1)
|
|---|
| 816 |
|
|---|
| 817 | // check the head
|
|---|
| 818 | if (head.length) {
|
|---|
| 819 | var fileHead = file.slice(fileIndex, fileIndex + head.length)
|
|---|
| 820 | if (!this._matchOne(fileHead, head, partial, 0, 0)) {
|
|---|
| 821 | return false
|
|---|
| 822 | }
|
|---|
| 823 | fileIndex += head.length
|
|---|
| 824 | }
|
|---|
| 825 |
|
|---|
| 826 | // check the tail
|
|---|
| 827 | var fileTailMatch = 0
|
|---|
| 828 | if (tail.length) {
|
|---|
| 829 | if (tail.length + fileIndex > file.length) return false
|
|---|
| 830 |
|
|---|
| 831 | var tailStart = file.length - tail.length
|
|---|
| 832 | if (this._matchOne(file, tail, partial, tailStart, 0)) {
|
|---|
| 833 | fileTailMatch = tail.length
|
|---|
| 834 | } else {
|
|---|
| 835 | // affordance for stuff like a/**/* matching a/b/
|
|---|
| 836 | if (file[file.length - 1] !== '' ||
|
|---|
| 837 | fileIndex + tail.length === file.length) {
|
|---|
| 838 | return false
|
|---|
| 839 | }
|
|---|
| 840 | tailStart--
|
|---|
| 841 | if (!this._matchOne(file, tail, partial, tailStart, 0)) {
|
|---|
| 842 | return false
|
|---|
| 843 | }
|
|---|
| 844 | fileTailMatch = tail.length + 1
|
|---|
| 845 | }
|
|---|
| 846 | }
|
|---|
| 847 |
|
|---|
| 848 | // if body is empty (single ** between head and tail)
|
|---|
| 849 | if (!body.length) {
|
|---|
| 850 | var sawSome = !!fileTailMatch
|
|---|
| 851 | for (i = fileIndex; i < file.length - fileTailMatch; i++) {
|
|---|
| 852 | var f = String(file[i])
|
|---|
| 853 | sawSome = true
|
|---|
| 854 | if (f === '.' || f === '..' ||
|
|---|
| 855 | (!this.options.dot && f.charAt(0) === '.')) {
|
|---|
| 856 | return false
|
|---|
| 857 | }
|
|---|
| 858 | }
|
|---|
| 859 | return partial || sawSome
|
|---|
| 860 | }
|
|---|
| 861 |
|
|---|
| 862 | // split body into segments at each GLOBSTAR
|
|---|
| 863 | var bodySegments = [[[], 0]]
|
|---|
| 864 | var currentBody = bodySegments[0]
|
|---|
| 865 | var nonGsParts = 0
|
|---|
| 866 | var nonGsPartsSums = [0]
|
|---|
| 867 | for (var bi = 0; bi < body.length; bi++) {
|
|---|
| 868 | var b = body[bi]
|
|---|
| 869 | if (b === GLOBSTAR) {
|
|---|
| 870 | nonGsPartsSums.push(nonGsParts)
|
|---|
| 871 | currentBody = [[], 0]
|
|---|
| 872 | bodySegments.push(currentBody)
|
|---|
| 873 | } else {
|
|---|
| 874 | currentBody[0].push(b)
|
|---|
| 875 | nonGsParts++
|
|---|
| 876 | }
|
|---|
| 877 | }
|
|---|
| 878 |
|
|---|
| 879 | var idx = bodySegments.length - 1
|
|---|
| 880 | var fileLength = file.length - fileTailMatch
|
|---|
| 881 | for (var si = 0; si < bodySegments.length; si++) {
|
|---|
| 882 | bodySegments[si][1] = fileLength -
|
|---|
| 883 | (nonGsPartsSums[idx--] + bodySegments[si][0].length)
|
|---|
| 884 | }
|
|---|
| 885 |
|
|---|
| 886 | return !!this._matchGlobStarBodySections(
|
|---|
| 887 | file, bodySegments, fileIndex, 0, partial, 0, !!fileTailMatch
|
|---|
| 888 | )
|
|---|
| 889 | }
|
|---|
| 890 |
|
|---|
| 891 | // return false for "nope, not matching"
|
|---|
| 892 | // return null for "not matching, cannot keep trying"
|
|---|
| 893 | Minimatch.prototype._matchGlobStarBodySections = function (
|
|---|
| 894 | file, bodySegments, fileIndex, bodyIndex, partial, globStarDepth, sawTail
|
|---|
| 895 | ) {
|
|---|
| 896 | var bs = bodySegments[bodyIndex]
|
|---|
| 897 | if (!bs) {
|
|---|
| 898 | // just make sure there are no bad dots
|
|---|
| 899 | for (var i = fileIndex; i < file.length; i++) {
|
|---|
| 900 | sawTail = true
|
|---|
| 901 | var f = file[i]
|
|---|
| 902 | if (f === '.' || f === '..' ||
|
|---|
| 903 | (!this.options.dot && f.charAt(0) === '.')) {
|
|---|
| 904 | return false
|
|---|
| 905 | }
|
|---|
| 906 | }
|
|---|
| 907 | return sawTail
|
|---|
| 908 | }
|
|---|
| 909 |
|
|---|
| 910 | var body = bs[0]
|
|---|
| 911 | var after = bs[1]
|
|---|
| 912 | while (fileIndex <= after) {
|
|---|
| 913 | var m = this._matchOne(
|
|---|
| 914 | file.slice(0, fileIndex + body.length),
|
|---|
| 915 | body,
|
|---|
| 916 | partial,
|
|---|
| 917 | fileIndex,
|
|---|
| 918 | 0
|
|---|
| 919 | )
|
|---|
| 920 | // if limit exceeded, no match. intentional false negative,
|
|---|
| 921 | // acceptable break in correctness for security.
|
|---|
| 922 | if (m && globStarDepth < this.maxGlobstarRecursion) {
|
|---|
| 923 | var sub = this._matchGlobStarBodySections(
|
|---|
| 924 | file, bodySegments,
|
|---|
| 925 | fileIndex + body.length, bodyIndex + 1,
|
|---|
| 926 | partial, globStarDepth + 1, sawTail
|
|---|
| 927 | )
|
|---|
| 928 | if (sub !== false) {
|
|---|
| 929 | return sub
|
|---|
| 930 | }
|
|---|
| 931 | }
|
|---|
| 932 | var f = file[fileIndex]
|
|---|
| 933 | if (f === '.' || f === '..' ||
|
|---|
| 934 | (!this.options.dot && f.charAt(0) === '.')) {
|
|---|
| 935 | return false
|
|---|
| 936 | }
|
|---|
| 937 | fileIndex++
|
|---|
| 938 | }
|
|---|
| 939 | return partial || null
|
|---|
| 940 | }
|
|---|
| 941 |
|
|---|
| 942 | Minimatch.prototype._matchOne = function (file, pattern, partial, fileIndex, patternIndex) {
|
|---|
| 943 | var fi, pi, fl, pl
|
|---|
| 944 | for (
|
|---|
| 945 | fi = fileIndex, pi = patternIndex, fl = file.length, pl = pattern.length
|
|---|
| 946 | ; (fi < fl) && (pi < pl)
|
|---|
| 947 | ; fi++, pi++
|
|---|
| 948 | ) {
|
|---|
| 949 | this.debug('matchOne loop')
|
|---|
| 950 | var p = pattern[pi]
|
|---|
| 951 | var f = file[fi]
|
|---|
| 952 |
|
|---|
| 953 | this.debug(pattern, p, f)
|
|---|
| 954 |
|
|---|
| 955 | // should be impossible.
|
|---|
| 956 | // some invalid regexp stuff in the set.
|
|---|
| 957 | /* istanbul ignore if */
|
|---|
| 958 | if (p === false || p === GLOBSTAR) return false
|
|---|
| 959 |
|
|---|
| 960 | // something other than **
|
|---|
| 961 | // non-magic patterns just have to match exactly
|
|---|
| 962 | // patterns with magic have been turned into regexps.
|
|---|
| 963 | var hit
|
|---|
| 964 | if (typeof p === 'string') {
|
|---|
| 965 | hit = f === p
|
|---|
| 966 | this.debug('string match', p, f, hit)
|
|---|
| 967 | } else {
|
|---|
| 968 | hit = f.match(p)
|
|---|
| 969 | this.debug('pattern match', p, f, hit)
|
|---|
| 970 | }
|
|---|
| 971 |
|
|---|
| 972 | if (!hit) return false
|
|---|
| 973 | }
|
|---|
| 974 |
|
|---|
| 975 | // now either we fell off the end of the pattern, or we're done.
|
|---|
| 976 | if (fi === fl && pi === pl) {
|
|---|
| 977 | // ran out of pattern and filename at the same time.
|
|---|
| 978 | // an exact hit!
|
|---|
| 979 | return true
|
|---|
| 980 | } else if (fi === fl) {
|
|---|
| 981 | // ran out of file, but still had pattern left.
|
|---|
| 982 | // this is ok if we're doing the match as part of
|
|---|
| 983 | // a glob fs traversal.
|
|---|
| 984 | return partial
|
|---|
| 985 | } else /* istanbul ignore else */ if (pi === pl) {
|
|---|
| 986 | // ran out of pattern, still have file left.
|
|---|
| 987 | // this is only acceptable if we're on the very last
|
|---|
| 988 | // empty segment of a file with a trailing slash.
|
|---|
| 989 | // a/* should match a/b/
|
|---|
| 990 | return (fi === fl - 1) && (file[fi] === '')
|
|---|
| 991 | }
|
|---|
| 992 |
|
|---|
| 993 | // should be unreachable.
|
|---|
| 994 | /* istanbul ignore next */
|
|---|
| 995 | throw new Error('wtf?')
|
|---|
| 996 | }
|
|---|
| 997 |
|
|---|
| 998 | // replace stuff like \* with *
|
|---|
| 999 | function globUnescape (s) {
|
|---|
| 1000 | return s.replace(/\\(.)/g, '$1')
|
|---|
| 1001 | }
|
|---|
| 1002 |
|
|---|
| 1003 | function regExpEscape (s) {
|
|---|
| 1004 | return s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&')
|
|---|
| 1005 | }
|
|---|