d9cd61c52d6c219b06894fbc7190b119413371944ba16804ee2884c8e75101072872cc9b0d000a420144986c3c621179deddea67555f4fab072b5194ebccaf 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. # minimatch
  2. A minimal matching utility.
  3. This is the matching library used internally by npm.
  4. It works by converting glob expressions into JavaScript `RegExp`
  5. objects.
  6. ## Usage
  7. ```js
  8. // hybrid module, load with require() or import
  9. import { minimatch } from 'minimatch'
  10. // or:
  11. const { minimatch } = require('minimatch')
  12. minimatch('bar.foo', '*.foo') // true!
  13. minimatch('bar.foo', '*.bar') // false!
  14. minimatch('bar.foo', '*.+(bar|foo)', { debug: true }) // true, and noisy!
  15. ```
  16. ## Features
  17. Supports these glob features:
  18. - Brace Expansion
  19. - Extended glob matching
  20. - "Globstar" `**` matching
  21. - [Posix character
  22. classes](https://www.gnu.org/software/bash/manual/html_node/Pattern-Matching.html),
  23. like `[[:alpha:]]`, supporting the full range of Unicode
  24. characters. For example, `[[:alpha:]]` will match against
  25. `'é'`, though `[a-zA-Z]` will not. Collating symbol and set
  26. matching is not supported, so `[[=e=]]` will _not_ match `'é'`
  27. and `[[.ch.]]` will not match `'ch'` in locales where `ch` is
  28. considered a single character.
  29. See:
  30. - `man sh`
  31. - `man bash` [Pattern
  32. Matching](https://www.gnu.org/software/bash/manual/html_node/Pattern-Matching.html)
  33. - `man 3 fnmatch`
  34. - `man 5 gitignore`
  35. ## Windows
  36. **Please only use forward-slashes in glob expressions.**
  37. Though windows uses either `/` or `\` as its path separator, only `/`
  38. characters are used by this glob implementation. You must use
  39. forward-slashes **only** in glob expressions. Back-slashes in patterns
  40. will always be interpreted as escape characters, not path separators.
  41. Note that `\` or `/` _will_ be interpreted as path separators in paths on
  42. Windows, and will match against `/` in glob expressions.
  43. So just always use `/` in patterns.
  44. ### UNC Paths
  45. On Windows, UNC paths like `//?/c:/...` or
  46. `//ComputerName/Share/...` are handled specially.
  47. - Patterns starting with a double-slash followed by some
  48. non-slash characters will preserve their double-slash. As a
  49. result, a pattern like `//*` will match `//x`, but not `/x`.
  50. - Patterns staring with `//?/<drive letter>:` will _not_ treat
  51. the `?` as a wildcard character. Instead, it will be treated
  52. as a normal string.
  53. - Patterns starting with `//?/<drive letter>:/...` will match
  54. file paths starting with `<drive letter>:/...`, and vice versa,
  55. as if the `//?/` was not present. This behavior only is
  56. present when the drive letters are a case-insensitive match to
  57. one another. The remaining portions of the path/pattern are
  58. compared case sensitively, unless `nocase:true` is set.
  59. Note that specifying a UNC path using `\` characters as path
  60. separators is always allowed in the file path argument, but only
  61. allowed in the pattern argument when `windowsPathsNoEscape: true`
  62. is set in the options.
  63. ## Minimatch Class
  64. Create a minimatch object by instantiating the `minimatch.Minimatch` class.
  65. ```javascript
  66. var Minimatch = require('minimatch').Minimatch
  67. var mm = new Minimatch(pattern, options)
  68. ```
  69. ### Properties
  70. - `pattern` The original pattern the minimatch object represents.
  71. - `options` The options supplied to the constructor.
  72. - `set` A 2-dimensional array of regexp or string expressions.
  73. Each row in the
  74. array corresponds to a brace-expanded pattern. Each item in the row
  75. corresponds to a single path-part. For example, the pattern
  76. `{a,b/c}/d` would expand to a set of patterns like:
  77. [ [ a, d ]
  78. , [ b, c, d ] ]
  79. If a portion of the pattern doesn't have any "magic" in it
  80. (that is, it's something like `"foo"` rather than `fo*o?`), then it
  81. will be left as a string rather than converted to a regular
  82. expression.
  83. - `regexp` Created by the `makeRe` method. A single regular expression
  84. expressing the entire pattern. This is useful in cases where you wish
  85. to use the pattern somewhat like `fnmatch(3)` with `FNM_PATH` enabled.
  86. - `negate` True if the pattern is negated.
  87. - `comment` True if the pattern is a comment.
  88. - `empty` True if the pattern is `""`.
  89. ### Methods
  90. - `makeRe()` Generate the `regexp` member if necessary, and return it.
  91. Will return `false` if the pattern is invalid.
  92. - `match(fname)` Return true if the filename matches the pattern, or
  93. false otherwise.
  94. - `matchOne(fileArray, patternArray, partial)` Take a `/`-split
  95. filename, and match it against a single row in the `regExpSet`. This
  96. method is mainly for internal use, but is exposed so that it can be
  97. used by a glob-walker that needs to avoid excessive filesystem calls.
  98. - `hasMagic()` Returns true if the parsed pattern contains any
  99. magic characters. Returns false if all comparator parts are
  100. string literals. If the `magicalBraces` option is set on the
  101. constructor, then it will consider brace expansions which are
  102. not otherwise magical to be magic. If not set, then a pattern
  103. like `a{b,c}d` will return `false`, because neither `abd` nor
  104. `acd` contain any special glob characters.
  105. This does **not** mean that the pattern string can be used as a
  106. literal filename, as it may contain magic glob characters that
  107. are escaped. For example, the pattern `\\*` or `[*]` would not
  108. be considered to have magic, as the matching portion parses to
  109. the literal string `'*'` and would match a path named `'*'`,
  110. not `'\\*'` or `'[*]'`. The `minimatch.unescape()` method may
  111. be used to remove escape characters.
  112. All other methods are internal, and will be called as necessary.
  113. ### minimatch(path, pattern, options)
  114. Main export. Tests a path against the pattern using the options.
  115. ```javascript
  116. var isJS = minimatch(file, '*.js', { matchBase: true })
  117. ```
  118. ### minimatch.filter(pattern, options)
  119. Returns a function that tests its
  120. supplied argument, suitable for use with `Array.filter`. Example:
  121. ```javascript
  122. var javascripts = fileList.filter(minimatch.filter('*.js', { matchBase: true }))
  123. ```
  124. ### minimatch.escape(pattern, options = {})
  125. Escape all magic characters in a glob pattern, so that it will
  126. only ever match literal strings
  127. If the `windowsPathsNoEscape` option is used, then characters are
  128. escaped by wrapping in `[]`, because a magic character wrapped in
  129. a character class can only be satisfied by that exact character.
  130. Slashes (and backslashes in `windowsPathsNoEscape` mode) cannot
  131. be escaped or unescaped.
  132. ### minimatch.unescape(pattern, options = {})
  133. Un-escape a glob string that may contain some escaped characters.
  134. If the `windowsPathsNoEscape` option is used, then square-brace
  135. escapes are removed, but not backslash escapes. For example, it
  136. will turn the string `'[*]'` into `*`, but it will not turn
  137. `'\\*'` into `'*'`, because `\` is a path separator in
  138. `windowsPathsNoEscape` mode.
  139. When `windowsPathsNoEscape` is not set, then both brace escapes
  140. and backslash escapes are removed.
  141. Slashes (and backslashes in `windowsPathsNoEscape` mode) cannot
  142. be escaped or unescaped.
  143. ### minimatch.match(list, pattern, options)
  144. Match against the list of
  145. files, in the style of fnmatch or glob. If nothing is matched, and
  146. options.nonull is set, then return a list containing the pattern itself.
  147. ```javascript
  148. var javascripts = minimatch.match(fileList, '*.js', { matchBase: true })
  149. ```
  150. ### minimatch.makeRe(pattern, options)
  151. Make a regular expression object from the pattern.
  152. ## Options
  153. All options are `false` by default.
  154. ### debug
  155. Dump a ton of stuff to stderr.
  156. ### nobrace
  157. Do not expand `{a,b}` and `{1..3}` brace sets.
  158. ### noglobstar
  159. Disable `**` matching against multiple folder names.
  160. ### dot
  161. Allow patterns to match filenames starting with a period, even if
  162. the pattern does not explicitly have a period in that spot.
  163. Note that by default, `a/**/b` will **not** match `a/.d/b`, unless `dot`
  164. is set.
  165. ### noext
  166. Disable "extglob" style patterns like `+(a|b)`.
  167. ### nocase
  168. Perform a case-insensitive match.
  169. ### nocaseMagicOnly
  170. When used with `{nocase: true}`, create regular expressions that
  171. are case-insensitive, but leave string match portions untouched.
  172. Has no effect when used without `{nocase: true}`
  173. Useful when some other form of case-insensitive matching is used,
  174. or if the original string representation is useful in some other
  175. way.
  176. ### nonull
  177. When a match is not found by `minimatch.match`, return a list containing
  178. the pattern itself if this option is set. When not set, an empty list
  179. is returned if there are no matches.
  180. ### magicalBraces
  181. This only affects the results of the `Minimatch.hasMagic` method.
  182. If the pattern contains brace expansions, such as `a{b,c}d`, but
  183. no other magic characters, then the `Minimatch.hasMagic()` method
  184. will return `false` by default. When this option set, it will
  185. return `true` for brace expansion as well as other magic glob
  186. characters.
  187. ### matchBase
  188. If set, then patterns without slashes will be matched
  189. against the basename of the path if it contains slashes. For example,
  190. `a?b` would match the path `/xyz/123/acb`, but not `/xyz/acb/123`.
  191. ### nocomment
  192. Suppress the behavior of treating `#` at the start of a pattern as a
  193. comment.
  194. ### nonegate
  195. Suppress the behavior of treating a leading `!` character as negation.
  196. ### flipNegate
  197. Returns from negate expressions the same as if they were not negated.
  198. (Ie, true on a hit, false on a miss.)
  199. ### partial
  200. Compare a partial path to a pattern. As long as the parts of the path that
  201. are present are not contradicted by the pattern, it will be treated as a
  202. match. This is useful in applications where you're walking through a
  203. folder structure, and don't yet have the full path, but want to ensure that
  204. you do not walk down paths that can never be a match.
  205. For example,
  206. ```js
  207. minimatch('/a/b', '/a/*/c/d', { partial: true }) // true, might be /a/b/c/d
  208. minimatch('/a/b', '/**/d', { partial: true }) // true, might be /a/b/.../d
  209. minimatch('/x/y/z', '/a/**/z', { partial: true }) // false, because x !== a
  210. ```
  211. ### windowsPathsNoEscape
  212. Use `\\` as a path separator _only_, and _never_ as an escape
  213. character. If set, all `\\` characters are replaced with `/` in
  214. the pattern. Note that this makes it **impossible** to match
  215. against paths containing literal glob pattern characters, but
  216. allows matching with patterns constructed using `path.join()` and
  217. `path.resolve()` on Windows platforms, mimicking the (buggy!)
  218. behavior of earlier versions on Windows. Please use with
  219. caution, and be mindful of [the caveat about Windows
  220. paths](#windows).
  221. For legacy reasons, this is also set if
  222. `options.allowWindowsEscape` is set to the exact value `false`.
  223. ### windowsNoMagicRoot
  224. When a pattern starts with a UNC path or drive letter, and in
  225. `nocase:true` mode, do not convert the root portions of the
  226. pattern into a case-insensitive regular expression, and instead
  227. leave them as strings.
  228. This is the default when the platform is `win32` and
  229. `nocase:true` is set.
  230. ### preserveMultipleSlashes
  231. By default, multiple `/` characters (other than the leading `//`
  232. in a UNC path, see "UNC Paths" above) are treated as a single
  233. `/`.
  234. That is, a pattern like `a///b` will match the file path `a/b`.
  235. Set `preserveMultipleSlashes: true` to suppress this behavior.
  236. ### optimizationLevel
  237. A number indicating the level of optimization that should be done
  238. to the pattern prior to parsing and using it for matches.
  239. Globstar parts `**` are always converted to `*` when `noglobstar`
  240. is set, and multiple adjacent `**` parts are converted into a
  241. single `**` (ie, `a/**/**/b` will be treated as `a/**/b`, as this
  242. is equivalent in all cases).
  243. - `0` - Make no further changes. In this mode, `.` and `..` are
  244. maintained in the pattern, meaning that they must also appear
  245. in the same position in the test path string. Eg, a pattern
  246. like `a/*/../c` will match the string `a/b/../c` but not the
  247. string `a/c`.
  248. - `1` - (default) Remove cases where a double-dot `..` follows a
  249. pattern portion that is not `**`, `.`, `..`, or empty `''`. For
  250. example, the pattern `./a/b/../*` is converted to `./a/*`, and
  251. so it will match the path string `./a/c`, but not the path
  252. string `./a/b/../c`. Dots and empty path portions in the
  253. pattern are preserved.
  254. - `2` (or higher) - Much more aggressive optimizations, suitable
  255. for use with file-walking cases:
  256. - Remove cases where a double-dot `..` follows a pattern
  257. portion that is not `**`, `.`, or empty `''`. Remove empty
  258. and `.` portions of the pattern, where safe to do so (ie,
  259. anywhere other than the last position, the first position, or
  260. the second position in a pattern starting with `/`, as this
  261. may indicate a UNC path on Windows).
  262. - Convert patterns containing `<pre>/**/../<p>/<rest>` into the
  263. equivalent `<pre>/{..,**}/<p>/<rest>`, where `<p>` is a
  264. a pattern portion other than `.`, `..`, `**`, or empty
  265. `''`.
  266. - Dedupe patterns where a `**` portion is present in one and
  267. omitted in another, and it is not the final path portion, and
  268. they are otherwise equivalent. So `{a/**/b,a/b}` becomes
  269. `a/**/b`, because `**` matches against an empty path portion.
  270. - Dedupe patterns where a `*` portion is present in one, and a
  271. non-dot pattern other than `**`, `.`, `..`, or `''` is in the
  272. same position in the other. So `a/{*,x}/b` becomes `a/*/b`,
  273. because `*` can match against `x`.
  274. While these optimizations improve the performance of
  275. file-walking use cases such as [glob](http://npm.im/glob) (ie,
  276. the reason this module exists), there are cases where it will
  277. fail to match a literal string that would have been matched in
  278. optimization level 1 or 0.
  279. Specifically, while the `Minimatch.match()` method will
  280. optimize the file path string in the same ways, resulting in
  281. the same matches, it will fail when tested with the regular
  282. expression provided by `Minimatch.makeRe()`, unless the path
  283. string is first processed with
  284. `minimatch.levelTwoFileOptimize()` or similar.
  285. ### platform
  286. When set to `win32`, this will trigger all windows-specific
  287. behaviors (special handling for UNC paths, and treating `\` as
  288. separators in file paths for comparison.)
  289. Defaults to the value of `process.platform`.
  290. ## Comparisons to other fnmatch/glob implementations
  291. While strict compliance with the existing standards is a
  292. worthwhile goal, some discrepancies exist between minimatch and
  293. other implementations. Some are intentional, and some are
  294. unavoidable.
  295. If the pattern starts with a `!` character, then it is negated. Set the
  296. `nonegate` flag to suppress this behavior, and treat leading `!`
  297. characters normally. This is perhaps relevant if you wish to start the
  298. pattern with a negative extglob pattern like `!(a|B)`. Multiple `!`
  299. characters at the start of a pattern will negate the pattern multiple
  300. times.
  301. If a pattern starts with `#`, then it is treated as a comment, and
  302. will not match anything. Use `\#` to match a literal `#` at the
  303. start of a line, or set the `nocomment` flag to suppress this behavior.
  304. The double-star character `**` is supported by default, unless the
  305. `noglobstar` flag is set. This is supported in the manner of bsdglob
  306. and bash 4.1, where `**` only has special significance if it is the only
  307. thing in a path part. That is, `a/**/b` will match `a/x/y/b`, but
  308. `a/**b` will not.
  309. If an escaped pattern has no matches, and the `nonull` flag is set,
  310. then minimatch.match returns the pattern as-provided, rather than
  311. interpreting the character escapes. For example,
  312. `minimatch.match([], "\\*a\\?")` will return `"\\*a\\?"` rather than
  313. `"*a?"`. This is akin to setting the `nullglob` option in bash, except
  314. that it does not resolve escaped pattern characters.
  315. If brace expansion is not disabled, then it is performed before any
  316. other interpretation of the glob pattern. Thus, a pattern like
  317. `+(a|{b),c)}`, which would not be valid in bash or zsh, is expanded
  318. **first** into the set of `+(a|b)` and `+(a|c)`, and those patterns are
  319. checked for validity. Since those two are valid, matching proceeds.
  320. Negated extglob patterns are handled as closely as possible to
  321. Bash semantics, but there are some cases with negative extglobs
  322. which are exceedingly difficult to express in a JavaScript
  323. regular expression. In particular the negated pattern
  324. `<start>!(<pattern>*|)*` will in bash match anything that does
  325. not start with `<start><pattern>`. However,
  326. `<start>!(<pattern>*)*` _will_ match paths starting with
  327. `<start><pattern>`, because the empty string can match against
  328. the negated portion. In this library, `<start>!(<pattern>*|)*`
  329. will _not_ match any pattern starting with `<start>`, due to a
  330. difference in precisely which patterns are considered "greedy" in
  331. Regular Expressions vs bash path expansion. This may be fixable,
  332. but not without incurring some complexity and performance costs,
  333. and the trade-off seems to not be worth pursuing.
  334. Note that `fnmatch(3)` in libc is an extremely naive string comparison
  335. matcher, which does not do anything special for slashes. This library is
  336. designed to be used in glob searching and file walkers, and so it does do
  337. special things with `/`. Thus, `foo*` will not match `foo/bar` in this
  338. library, even though it would in `fnmatch(3)`.