/node_modules/minimatch/minimatch.js

https://bitbucket.org/coleman333/smartsite · JavaScript · 1073 lines · 660 code · 165 blank · 248 comment · 150 complexity · d15af391b681506a5b3cda5b7e83c62f MD5 · raw file

  1. ;(function (require, exports, module, platform) {
  2. if (module) module.exports = minimatch
  3. else exports.minimatch = minimatch
  4. if (!require) {
  5. require = function (id) {
  6. switch (id) {
  7. case "sigmund": return function sigmund (obj) {
  8. return JSON.stringify(obj)
  9. }
  10. case "path": return { basename: function (f) {
  11. f = f.split(/[\/\\]/)
  12. var e = f.pop()
  13. if (!e) e = f.pop()
  14. return e
  15. }}
  16. case "lru-cache": return function LRUCache () {
  17. // not quite an LRU, but still space-limited.
  18. var cache = {}
  19. var cnt = 0
  20. this.set = function (k, v) {
  21. cnt ++
  22. if (cnt >= 100) cache = {}
  23. cache[k] = v
  24. }
  25. this.get = function (k) { return cache[k] }
  26. }
  27. }
  28. }
  29. }
  30. minimatch.Minimatch = Minimatch
  31. var LRU = require("lru-cache")
  32. , cache = minimatch.cache = new LRU({max: 100})
  33. , GLOBSTAR = minimatch.GLOBSTAR = Minimatch.GLOBSTAR = {}
  34. , sigmund = require("sigmund")
  35. var path = require("path")
  36. // any single thing other than /
  37. // don't need to escape / when using new RegExp()
  38. , qmark = "[^/]"
  39. // * => any number of characters
  40. , star = qmark + "*?"
  41. // ** when dots are allowed. Anything goes, except .. and .
  42. // not (^ or / followed by one or two dots followed by $ or /),
  43. // followed by anything, any number of times.
  44. , twoStarDot = "(?:(?!(?:\\\/|^)(?:\\.{1,2})($|\\\/)).)*?"
  45. // not a ^ or / followed by a dot,
  46. // followed by anything, any number of times.
  47. , twoStarNoDot = "(?:(?!(?:\\\/|^)\\.).)*?"
  48. // characters that need to be escaped in RegExp.
  49. , reSpecials = charSet("().*{}+?[]^$\\!")
  50. // "abc" -> { a:true, b:true, c:true }
  51. function charSet (s) {
  52. return s.split("").reduce(function (set, c) {
  53. set[c] = true
  54. return set
  55. }, {})
  56. }
  57. // normalizes slashes.
  58. var slashSplit = /\/+/
  59. minimatch.filter = filter
  60. function filter (pattern, options) {
  61. options = options || {}
  62. return function (p, i, list) {
  63. return minimatch(p, pattern, options)
  64. }
  65. }
  66. function ext (a, b) {
  67. a = a || {}
  68. b = b || {}
  69. var t = {}
  70. Object.keys(b).forEach(function (k) {
  71. t[k] = b[k]
  72. })
  73. Object.keys(a).forEach(function (k) {
  74. t[k] = a[k]
  75. })
  76. return t
  77. }
  78. minimatch.defaults = function (def) {
  79. if (!def || !Object.keys(def).length) return minimatch
  80. var orig = minimatch
  81. var m = function minimatch (p, pattern, options) {
  82. return orig.minimatch(p, pattern, ext(def, options))
  83. }
  84. m.Minimatch = function Minimatch (pattern, options) {
  85. return new orig.Minimatch(pattern, ext(def, options))
  86. }
  87. return m
  88. }
  89. Minimatch.defaults = function (def) {
  90. if (!def || !Object.keys(def).length) return Minimatch
  91. return minimatch.defaults(def).Minimatch
  92. }
  93. function minimatch (p, pattern, options) {
  94. if (typeof pattern !== "string") {
  95. throw new TypeError("glob pattern string required")
  96. }
  97. if (!options) options = {}
  98. // shortcut: comments match nothing.
  99. if (!options.nocomment && pattern.charAt(0) === "#") {
  100. return false
  101. }
  102. // "" only matches ""
  103. if (pattern.trim() === "") return p === ""
  104. return new Minimatch(pattern, options).match(p)
  105. }
  106. function Minimatch (pattern, options) {
  107. if (!(this instanceof Minimatch)) {
  108. return new Minimatch(pattern, options, cache)
  109. }
  110. if (typeof pattern !== "string") {
  111. throw new TypeError("glob pattern string required")
  112. }
  113. if (!options) options = {}
  114. pattern = pattern.trim()
  115. // windows: need to use /, not \
  116. // On other platforms, \ is a valid (albeit bad) filename char.
  117. if (platform === "win32") {
  118. pattern = pattern.split("\\").join("/")
  119. }
  120. // lru storage.
  121. // these things aren't particularly big, but walking down the string
  122. // and turning it into a regexp can get pretty costly.
  123. var cacheKey = pattern + "\n" + sigmund(options)
  124. var cached = minimatch.cache.get(cacheKey)
  125. if (cached) return cached
  126. minimatch.cache.set(cacheKey, this)
  127. this.options = options
  128. this.set = []
  129. this.pattern = pattern
  130. this.regexp = null
  131. this.negate = false
  132. this.comment = false
  133. this.empty = false
  134. // make the set of regexps etc.
  135. this.make()
  136. }
  137. Minimatch.prototype.debug = function() {}
  138. Minimatch.prototype.make = make
  139. function make () {
  140. // don't do it more than once.
  141. if (this._made) return
  142. var pattern = this.pattern
  143. var options = this.options
  144. // empty patterns and comments match nothing.
  145. if (!options.nocomment && pattern.charAt(0) === "#") {
  146. this.comment = true
  147. return
  148. }
  149. if (!pattern) {
  150. this.empty = true
  151. return
  152. }
  153. // step 1: figure out negation, etc.
  154. this.parseNegate()
  155. // step 2: expand braces
  156. var set = this.globSet = this.braceExpand()
  157. if (options.debug) this.debug = console.error
  158. this.debug(this.pattern, set)
  159. // step 3: now we have a set, so turn each one into a series of path-portion
  160. // matching patterns.
  161. // These will be regexps, except in the case of "**", which is
  162. // set to the GLOBSTAR object for globstar behavior,
  163. // and will not contain any / characters
  164. set = this.globParts = set.map(function (s) {
  165. return s.split(slashSplit)
  166. })
  167. this.debug(this.pattern, set)
  168. // glob --> regexps
  169. set = set.map(function (s, si, set) {
  170. return s.map(this.parse, this)
  171. }, this)
  172. this.debug(this.pattern, set)
  173. // filter out everything that didn't compile properly.
  174. set = set.filter(function (s) {
  175. return -1 === s.indexOf(false)
  176. })
  177. this.debug(this.pattern, set)
  178. this.set = set
  179. }
  180. Minimatch.prototype.parseNegate = parseNegate
  181. function parseNegate () {
  182. var pattern = this.pattern
  183. , negate = false
  184. , options = this.options
  185. , negateOffset = 0
  186. if (options.nonegate) return
  187. for ( var i = 0, l = pattern.length
  188. ; i < l && pattern.charAt(i) === "!"
  189. ; i ++) {
  190. negate = !negate
  191. negateOffset ++
  192. }
  193. if (negateOffset) this.pattern = pattern.substr(negateOffset)
  194. this.negate = negate
  195. }
  196. // Brace expansion:
  197. // a{b,c}d -> abd acd
  198. // a{b,}c -> abc ac
  199. // a{0..3}d -> a0d a1d a2d a3d
  200. // a{b,c{d,e}f}g -> abg acdfg acefg
  201. // a{b,c}d{e,f}g -> abdeg acdeg abdeg abdfg
  202. //
  203. // Invalid sets are not expanded.
  204. // a{2..}b -> a{2..}b
  205. // a{b}c -> a{b}c
  206. minimatch.braceExpand = function (pattern, options) {
  207. return new Minimatch(pattern, options).braceExpand()
  208. }
  209. Minimatch.prototype.braceExpand = braceExpand
  210. function pad(n, width, z) {
  211. z = z || '0';
  212. n = n + '';
  213. return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
  214. }
  215. function braceExpand (pattern, options) {
  216. options = options || this.options
  217. pattern = typeof pattern === "undefined"
  218. ? this.pattern : pattern
  219. if (typeof pattern === "undefined") {
  220. throw new Error("undefined pattern")
  221. }
  222. if (options.nobrace ||
  223. !pattern.match(/\{.*\}/)) {
  224. // shortcut. no need to expand.
  225. return [pattern]
  226. }
  227. var escaping = false
  228. // examples and comments refer to this crazy pattern:
  229. // a{b,c{d,e},{f,g}h}x{y,z}
  230. // expected:
  231. // abxy
  232. // abxz
  233. // acdxy
  234. // acdxz
  235. // acexy
  236. // acexz
  237. // afhxy
  238. // afhxz
  239. // aghxy
  240. // aghxz
  241. // everything before the first \{ is just a prefix.
  242. // So, we pluck that off, and work with the rest,
  243. // and then prepend it to everything we find.
  244. if (pattern.charAt(0) !== "{") {
  245. this.debug(pattern)
  246. var prefix = null
  247. for (var i = 0, l = pattern.length; i < l; i ++) {
  248. var c = pattern.charAt(i)
  249. this.debug(i, c)
  250. if (c === "\\") {
  251. escaping = !escaping
  252. } else if (c === "{" && !escaping) {
  253. prefix = pattern.substr(0, i)
  254. break
  255. }
  256. }
  257. // actually no sets, all { were escaped.
  258. if (prefix === null) {
  259. this.debug("no sets")
  260. return [pattern]
  261. }
  262. var tail = braceExpand.call(this, pattern.substr(i), options)
  263. return tail.map(function (t) {
  264. return prefix + t
  265. })
  266. }
  267. // now we have something like:
  268. // {b,c{d,e},{f,g}h}x{y,z}
  269. // walk through the set, expanding each part, until
  270. // the set ends. then, we'll expand the suffix.
  271. // If the set only has a single member, then'll put the {} back
  272. // first, handle numeric sets, since they're easier
  273. var numset = pattern.match(/^\{(-?[0-9]+)\.\.(-?[0-9]+)\}/)
  274. if (numset) {
  275. this.debug("numset", numset[1], numset[2])
  276. var suf = braceExpand.call(this, pattern.substr(numset[0].length), options)
  277. , start = +numset[1]
  278. , needPadding = numset[1][0] === '0'
  279. , startWidth = numset[1].length
  280. , padded
  281. , end = +numset[2]
  282. , inc = start > end ? -1 : 1
  283. , set = []
  284. for (var i = start; i != (end + inc); i += inc) {
  285. padded = needPadding ? pad(i, startWidth) : i + ''
  286. // append all the suffixes
  287. for (var ii = 0, ll = suf.length; ii < ll; ii ++) {
  288. set.push(padded + suf[ii])
  289. }
  290. }
  291. return set
  292. }
  293. // ok, walk through the set
  294. // We hope, somewhat optimistically, that there
  295. // will be a } at the end.
  296. // If the closing brace isn't found, then the pattern is
  297. // interpreted as braceExpand("\\" + pattern) so that
  298. // the leading \{ will be interpreted literally.
  299. var i = 1 // skip the \{
  300. , depth = 1
  301. , set = []
  302. , member = ""
  303. , sawEnd = false
  304. , escaping = false
  305. function addMember () {
  306. set.push(member)
  307. member = ""
  308. }
  309. this.debug("Entering for")
  310. FOR: for (i = 1, l = pattern.length; i < l; i ++) {
  311. var c = pattern.charAt(i)
  312. this.debug("", i, c)
  313. if (escaping) {
  314. escaping = false
  315. member += "\\" + c
  316. } else {
  317. switch (c) {
  318. case "\\":
  319. escaping = true
  320. continue
  321. case "{":
  322. depth ++
  323. member += "{"
  324. continue
  325. case "}":
  326. depth --
  327. // if this closes the actual set, then we're done
  328. if (depth === 0) {
  329. addMember()
  330. // pluck off the close-brace
  331. i ++
  332. break FOR
  333. } else {
  334. member += c
  335. continue
  336. }
  337. case ",":
  338. if (depth === 1) {
  339. addMember()
  340. } else {
  341. member += c
  342. }
  343. continue
  344. default:
  345. member += c
  346. continue
  347. } // switch
  348. } // else
  349. } // for
  350. // now we've either finished the set, and the suffix is
  351. // pattern.substr(i), or we have *not* closed the set,
  352. // and need to escape the leading brace
  353. if (depth !== 0) {
  354. this.debug("didn't close", pattern)
  355. return braceExpand.call(this, "\\" + pattern, options)
  356. }
  357. // x{y,z} -> ["xy", "xz"]
  358. this.debug("set", set)
  359. this.debug("suffix", pattern.substr(i))
  360. var suf = braceExpand.call(this, pattern.substr(i), options)
  361. // ["b", "c{d,e}","{f,g}h"] ->
  362. // [["b"], ["cd", "ce"], ["fh", "gh"]]
  363. var addBraces = set.length === 1
  364. this.debug("set pre-expanded", set)
  365. set = set.map(function (p) {
  366. return braceExpand.call(this, p, options)
  367. }, this)
  368. this.debug("set expanded", set)
  369. // [["b"], ["cd", "ce"], ["fh", "gh"]] ->
  370. // ["b", "cd", "ce", "fh", "gh"]
  371. set = set.reduce(function (l, r) {
  372. return l.concat(r)
  373. })
  374. if (addBraces) {
  375. set = set.map(function (s) {
  376. return "{" + s + "}"
  377. })
  378. }
  379. // now attach the suffixes.
  380. var ret = []
  381. for (var i = 0, l = set.length; i < l; i ++) {
  382. for (var ii = 0, ll = suf.length; ii < ll; ii ++) {
  383. ret.push(set[i] + suf[ii])
  384. }
  385. }
  386. return ret
  387. }
  388. // parse a component of the expanded set.
  389. // At this point, no pattern may contain "/" in it
  390. // so we're going to return a 2d array, where each entry is the full
  391. // pattern, split on '/', and then turned into a regular expression.
  392. // A regexp is made at the end which joins each array with an
  393. // escaped /, and another full one which joins each regexp with |.
  394. //
  395. // Following the lead of Bash 4.1, note that "**" only has special meaning
  396. // when it is the *only* thing in a path portion. Otherwise, any series
  397. // of * is equivalent to a single *. Globstar behavior is enabled by
  398. // default, and can be disabled by setting options.noglobstar.
  399. Minimatch.prototype.parse = parse
  400. var SUBPARSE = {}
  401. function parse (pattern, isSub) {
  402. var options = this.options
  403. // shortcuts
  404. if (!options.noglobstar && pattern === "**") return GLOBSTAR
  405. if (pattern === "") return ""
  406. var re = ""
  407. , hasMagic = !!options.nocase
  408. , escaping = false
  409. // ? => one single character
  410. , patternListStack = []
  411. , plType
  412. , stateChar
  413. , inClass = false
  414. , reClassStart = -1
  415. , classStart = -1
  416. // . and .. never match anything that doesn't start with .,
  417. // even when options.dot is set.
  418. , patternStart = pattern.charAt(0) === "." ? "" // anything
  419. // not (start or / followed by . or .. followed by / or end)
  420. : options.dot ? "(?!(?:^|\\\/)\\.{1,2}(?:$|\\\/))"
  421. : "(?!\\.)"
  422. , self = this
  423. function clearStateChar () {
  424. if (stateChar) {
  425. // we had some state-tracking character
  426. // that wasn't consumed by this pass.
  427. switch (stateChar) {
  428. case "*":
  429. re += star
  430. hasMagic = true
  431. break
  432. case "?":
  433. re += qmark
  434. hasMagic = true
  435. break
  436. default:
  437. re += "\\"+stateChar
  438. break
  439. }
  440. self.debug('clearStateChar %j %j', stateChar, re)
  441. stateChar = false
  442. }
  443. }
  444. for ( var i = 0, len = pattern.length, c
  445. ; (i < len) && (c = pattern.charAt(i))
  446. ; i ++ ) {
  447. this.debug("%s\t%s %s %j", pattern, i, re, c)
  448. // skip over any that are escaped.
  449. if (escaping && reSpecials[c]) {
  450. re += "\\" + c
  451. escaping = false
  452. continue
  453. }
  454. SWITCH: switch (c) {
  455. case "/":
  456. // completely not allowed, even escaped.
  457. // Should already be path-split by now.
  458. return false
  459. case "\\":
  460. clearStateChar()
  461. escaping = true
  462. continue
  463. // the various stateChar values
  464. // for the "extglob" stuff.
  465. case "?":
  466. case "*":
  467. case "+":
  468. case "@":
  469. case "!":
  470. this.debug("%s\t%s %s %j <-- stateChar", pattern, i, re, c)
  471. // all of those are literals inside a class, except that
  472. // the glob [!a] means [^a] in regexp
  473. if (inClass) {
  474. this.debug(' in class')
  475. if (c === "!" && i === classStart + 1) c = "^"
  476. re += c
  477. continue
  478. }
  479. // if we already have a stateChar, then it means
  480. // that there was something like ** or +? in there.
  481. // Handle the stateChar, then proceed with this one.
  482. self.debug('call clearStateChar %j', stateChar)
  483. clearStateChar()
  484. stateChar = c
  485. // if extglob is disabled, then +(asdf|foo) isn't a thing.
  486. // just clear the statechar *now*, rather than even diving into
  487. // the patternList stuff.
  488. if (options.noext) clearStateChar()
  489. continue
  490. case "(":
  491. if (inClass) {
  492. re += "("
  493. continue
  494. }
  495. if (!stateChar) {
  496. re += "\\("
  497. continue
  498. }
  499. plType = stateChar
  500. patternListStack.push({ type: plType
  501. , start: i - 1
  502. , reStart: re.length })
  503. // negation is (?:(?!js)[^/]*)
  504. re += stateChar === "!" ? "(?:(?!" : "(?:"
  505. this.debug('plType %j %j', stateChar, re)
  506. stateChar = false
  507. continue
  508. case ")":
  509. if (inClass || !patternListStack.length) {
  510. re += "\\)"
  511. continue
  512. }
  513. clearStateChar()
  514. hasMagic = true
  515. re += ")"
  516. plType = patternListStack.pop().type
  517. // negation is (?:(?!js)[^/]*)
  518. // The others are (?:<pattern>)<type>
  519. switch (plType) {
  520. case "!":
  521. re += "[^/]*?)"
  522. break
  523. case "?":
  524. case "+":
  525. case "*": re += plType
  526. case "@": break // the default anyway
  527. }
  528. continue
  529. case "|":
  530. if (inClass || !patternListStack.length || escaping) {
  531. re += "\\|"
  532. escaping = false
  533. continue
  534. }
  535. clearStateChar()
  536. re += "|"
  537. continue
  538. // these are mostly the same in regexp and glob
  539. case "[":
  540. // swallow any state-tracking char before the [
  541. clearStateChar()
  542. if (inClass) {
  543. re += "\\" + c
  544. continue
  545. }
  546. inClass = true
  547. classStart = i
  548. reClassStart = re.length
  549. re += c
  550. continue
  551. case "]":
  552. // a right bracket shall lose its special
  553. // meaning and represent itself in
  554. // a bracket expression if it occurs
  555. // first in the list. -- POSIX.2 2.8.3.2
  556. if (i === classStart + 1 || !inClass) {
  557. re += "\\" + c
  558. escaping = false
  559. continue
  560. }
  561. // finish up the class.
  562. hasMagic = true
  563. inClass = false
  564. re += c
  565. continue
  566. default:
  567. // swallow any state char that wasn't consumed
  568. clearStateChar()
  569. if (escaping) {
  570. // no need
  571. escaping = false
  572. } else if (reSpecials[c]
  573. && !(c === "^" && inClass)) {
  574. re += "\\"
  575. }
  576. re += c
  577. } // switch
  578. } // for
  579. // handle the case where we left a class open.
  580. // "[abc" is valid, equivalent to "\[abc"
  581. if (inClass) {
  582. // split where the last [ was, and escape it
  583. // this is a huge pita. We now have to re-walk
  584. // the contents of the would-be class to re-translate
  585. // any characters that were passed through as-is
  586. var cs = pattern.substr(classStart + 1)
  587. , sp = this.parse(cs, SUBPARSE)
  588. re = re.substr(0, reClassStart) + "\\[" + sp[0]
  589. hasMagic = hasMagic || sp[1]
  590. }
  591. // handle the case where we had a +( thing at the *end*
  592. // of the pattern.
  593. // each pattern list stack adds 3 chars, and we need to go through
  594. // and escape any | chars that were passed through as-is for the regexp.
  595. // Go through and escape them, taking care not to double-escape any
  596. // | chars that were already escaped.
  597. var pl
  598. while (pl = patternListStack.pop()) {
  599. var tail = re.slice(pl.reStart + 3)
  600. // maybe some even number of \, then maybe 1 \, followed by a |
  601. tail = tail.replace(/((?:\\{2})*)(\\?)\|/g, function (_, $1, $2) {
  602. if (!$2) {
  603. // the | isn't already escaped, so escape it.
  604. $2 = "\\"
  605. }
  606. // need to escape all those slashes *again*, without escaping the
  607. // one that we need for escaping the | character. As it works out,
  608. // escaping an even number of slashes can be done by simply repeating
  609. // it exactly after itself. That's why this trick works.
  610. //
  611. // I am sorry that you have to see this.
  612. return $1 + $1 + $2 + "|"
  613. })
  614. this.debug("tail=%j\n %s", tail, tail)
  615. var t = pl.type === "*" ? star
  616. : pl.type === "?" ? qmark
  617. : "\\" + pl.type
  618. hasMagic = true
  619. re = re.slice(0, pl.reStart)
  620. + t + "\\("
  621. + tail
  622. }
  623. // handle trailing things that only matter at the very end.
  624. clearStateChar()
  625. if (escaping) {
  626. // trailing \\
  627. re += "\\\\"
  628. }
  629. // only need to apply the nodot start if the re starts with
  630. // something that could conceivably capture a dot
  631. var addPatternStart = false
  632. switch (re.charAt(0)) {
  633. case ".":
  634. case "[":
  635. case "(": addPatternStart = true
  636. }
  637. // if the re is not "" at this point, then we need to make sure
  638. // it doesn't match against an empty path part.
  639. // Otherwise a/* will match a/, which it should not.
  640. if (re !== "" && hasMagic) re = "(?=.)" + re
  641. if (addPatternStart) re = patternStart + re
  642. // parsing just a piece of a larger pattern.
  643. if (isSub === SUBPARSE) {
  644. return [ re, hasMagic ]
  645. }
  646. // skip the regexp for non-magical patterns
  647. // unescape anything in it, though, so that it'll be
  648. // an exact match against a file etc.
  649. if (!hasMagic) {
  650. return globUnescape(pattern)
  651. }
  652. var flags = options.nocase ? "i" : ""
  653. , regExp = new RegExp("^" + re + "$", flags)
  654. regExp._glob = pattern
  655. regExp._src = re
  656. return regExp
  657. }
  658. minimatch.makeRe = function (pattern, options) {
  659. return new Minimatch(pattern, options || {}).makeRe()
  660. }
  661. Minimatch.prototype.makeRe = makeRe
  662. function makeRe () {
  663. if (this.regexp || this.regexp === false) return this.regexp
  664. // at this point, this.set is a 2d array of partial
  665. // pattern strings, or "**".
  666. //
  667. // It's better to use .match(). This function shouldn't
  668. // be used, really, but it's pretty convenient sometimes,
  669. // when you just want to work with a regex.
  670. var set = this.set
  671. if (!set.length) return this.regexp = false
  672. var options = this.options
  673. var twoStar = options.noglobstar ? star
  674. : options.dot ? twoStarDot
  675. : twoStarNoDot
  676. , flags = options.nocase ? "i" : ""
  677. var re = set.map(function (pattern) {
  678. return pattern.map(function (p) {
  679. return (p === GLOBSTAR) ? twoStar
  680. : (typeof p === "string") ? regExpEscape(p)
  681. : p._src
  682. }).join("\\\/")
  683. }).join("|")
  684. // must match entire pattern
  685. // ending in a * or ** will make it less strict.
  686. re = "^(?:" + re + ")$"
  687. // can match anything, as long as it's not this.
  688. if (this.negate) re = "^(?!" + re + ").*$"
  689. try {
  690. return this.regexp = new RegExp(re, flags)
  691. } catch (ex) {
  692. return this.regexp = false
  693. }
  694. }
  695. minimatch.match = function (list, pattern, options) {
  696. options = options || {}
  697. var mm = new Minimatch(pattern, options)
  698. list = list.filter(function (f) {
  699. return mm.match(f)
  700. })
  701. if (mm.options.nonull && !list.length) {
  702. list.push(pattern)
  703. }
  704. return list
  705. }
  706. Minimatch.prototype.match = match
  707. function match (f, partial) {
  708. this.debug("match", f, this.pattern)
  709. // short-circuit in the case of busted things.
  710. // comments, etc.
  711. if (this.comment) return false
  712. if (this.empty) return f === ""
  713. if (f === "/" && partial) return true
  714. var options = this.options
  715. // windows: need to use /, not \
  716. // On other platforms, \ is a valid (albeit bad) filename char.
  717. if (platform === "win32") {
  718. f = f.split("\\").join("/")
  719. }
  720. // treat the test path as a set of pathparts.
  721. f = f.split(slashSplit)
  722. this.debug(this.pattern, "split", f)
  723. // just ONE of the pattern sets in this.set needs to match
  724. // in order for it to be valid. If negating, then just one
  725. // match means that we have failed.
  726. // Either way, return on the first hit.
  727. var set = this.set
  728. this.debug(this.pattern, "set", set)
  729. // Find the basename of the path by looking for the last non-empty segment
  730. var filename;
  731. for (var i = f.length - 1; i >= 0; i--) {
  732. filename = f[i]
  733. if (filename) break
  734. }
  735. for (var i = 0, l = set.length; i < l; i ++) {
  736. var pattern = set[i], file = f
  737. if (options.matchBase && pattern.length === 1) {
  738. file = [filename]
  739. }
  740. var hit = this.matchOne(file, pattern, partial)
  741. if (hit) {
  742. if (options.flipNegate) return true
  743. return !this.negate
  744. }
  745. }
  746. // didn't get any hits. this is success if it's a negative
  747. // pattern, failure otherwise.
  748. if (options.flipNegate) return false
  749. return this.negate
  750. }
  751. // set partial to true to test if, for example,
  752. // "/a/b" matches the start of "/*/b/*/d"
  753. // Partial means, if you run out of file before you run
  754. // out of pattern, then that's fine, as long as all
  755. // the parts match.
  756. Minimatch.prototype.matchOne = function (file, pattern, partial) {
  757. var options = this.options
  758. this.debug("matchOne",
  759. { "this": this
  760. , file: file
  761. , pattern: pattern })
  762. this.debug("matchOne", file.length, pattern.length)
  763. for ( var fi = 0
  764. , pi = 0
  765. , fl = file.length
  766. , pl = pattern.length
  767. ; (fi < fl) && (pi < pl)
  768. ; fi ++, pi ++ ) {
  769. this.debug("matchOne loop")
  770. var p = pattern[pi]
  771. , f = file[fi]
  772. this.debug(pattern, p, f)
  773. // should be impossible.
  774. // some invalid regexp stuff in the set.
  775. if (p === false) return false
  776. if (p === GLOBSTAR) {
  777. this.debug('GLOBSTAR', [pattern, p, f])
  778. // "**"
  779. // a/**/b/**/c would match the following:
  780. // a/b/x/y/z/c
  781. // a/x/y/z/b/c
  782. // a/b/x/b/x/c
  783. // a/b/c
  784. // To do this, take the rest of the pattern after
  785. // the **, and see if it would match the file remainder.
  786. // If so, return success.
  787. // If not, the ** "swallows" a segment, and try again.
  788. // This is recursively awful.
  789. //
  790. // a/**/b/**/c matching a/b/x/y/z/c
  791. // - a matches a
  792. // - doublestar
  793. // - matchOne(b/x/y/z/c, b/**/c)
  794. // - b matches b
  795. // - doublestar
  796. // - matchOne(x/y/z/c, c) -> no
  797. // - matchOne(y/z/c, c) -> no
  798. // - matchOne(z/c, c) -> no
  799. // - matchOne(c, c) yes, hit
  800. var fr = fi
  801. , pr = pi + 1
  802. if (pr === pl) {
  803. this.debug('** at the end')
  804. // a ** at the end will just swallow the rest.
  805. // We have found a match.
  806. // however, it will not swallow /.x, unless
  807. // options.dot is set.
  808. // . and .. are *never* matched by **, for explosively
  809. // exponential reasons.
  810. for ( ; fi < fl; fi ++) {
  811. if (file[fi] === "." || file[fi] === ".." ||
  812. (!options.dot && file[fi].charAt(0) === ".")) return false
  813. }
  814. return true
  815. }
  816. // ok, let's see if we can swallow whatever we can.
  817. WHILE: while (fr < fl) {
  818. var swallowee = file[fr]
  819. this.debug('\nglobstar while',
  820. file, fr, pattern, pr, swallowee)
  821. // XXX remove this slice. Just pass the start index.
  822. if (this.matchOne(file.slice(fr), pattern.slice(pr), partial)) {
  823. this.debug('globstar found match!', fr, fl, swallowee)
  824. // found a match.
  825. return true
  826. } else {
  827. // can't swallow "." or ".." ever.
  828. // can only swallow ".foo" when explicitly asked.
  829. if (swallowee === "." || swallowee === ".." ||
  830. (!options.dot && swallowee.charAt(0) === ".")) {
  831. this.debug("dot detected!", file, fr, pattern, pr)
  832. break WHILE
  833. }
  834. // ** swallows a segment, and continue.
  835. this.debug('globstar swallow a segment, and continue')
  836. fr ++
  837. }
  838. }
  839. // no match was found.
  840. // However, in partial mode, we can't say this is necessarily over.
  841. // If there's more *pattern* left, then
  842. if (partial) {
  843. // ran out of file
  844. this.debug("\n>>> no match, partial?", file, fr, pattern, pr)
  845. if (fr === fl) return true
  846. }
  847. return false
  848. }
  849. // something other than **
  850. // non-magic patterns just have to match exactly
  851. // patterns with magic have been turned into regexps.
  852. var hit
  853. if (typeof p === "string") {
  854. if (options.nocase) {
  855. hit = f.toLowerCase() === p.toLowerCase()
  856. } else {
  857. hit = f === p
  858. }
  859. this.debug("string match", p, f, hit)
  860. } else {
  861. hit = f.match(p)
  862. this.debug("pattern match", p, f, hit)
  863. }
  864. if (!hit) return false
  865. }
  866. // Note: ending in / means that we'll get a final ""
  867. // at the end of the pattern. This can only match a
  868. // corresponding "" at the end of the file.
  869. // If the file ends in /, then it can only match a
  870. // a pattern that ends in /, unless the pattern just
  871. // doesn't have any more for it. But, a/b/ should *not*
  872. // match "a/b/*", even though "" matches against the
  873. // [^/]*? pattern, except in partial mode, where it might
  874. // simply not be reached yet.
  875. // However, a/b/ should still satisfy a/*
  876. // now either we fell off the end of the pattern, or we're done.
  877. if (fi === fl && pi === pl) {
  878. // ran out of pattern and filename at the same time.
  879. // an exact hit!
  880. return true
  881. } else if (fi === fl) {
  882. // ran out of file, but still had pattern left.
  883. // this is ok if we're doing the match as part of
  884. // a glob fs traversal.
  885. return partial
  886. } else if (pi === pl) {
  887. // ran out of pattern, still have file left.
  888. // this is only acceptable if we're on the very last
  889. // empty segment of a file with a trailing slash.
  890. // a/* should match a/b/
  891. var emptyFileEnd = (fi === fl - 1) && (file[fi] === "")
  892. return emptyFileEnd
  893. }
  894. // should be unreachable.
  895. throw new Error("wtf?")
  896. }
  897. // replace stuff like \* with *
  898. function globUnescape (s) {
  899. return s.replace(/\\(.)/g, "$1")
  900. }
  901. function regExpEscape (s) {
  902. return s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&")
  903. }
  904. })( typeof require === "function" ? require : null,
  905. this,
  906. typeof module === "object" ? module : null,
  907. typeof process === "object" ? process.platform : "win32"
  908. )