| 1 | # minimatch
|
|---|
| 2 |
|
|---|
| 3 | A minimal matching utility.
|
|---|
| 4 |
|
|---|
| 5 | This is the matching library used internally by npm.
|
|---|
| 6 |
|
|---|
| 7 | It works by converting glob expressions into JavaScript `RegExp`
|
|---|
| 8 | objects.
|
|---|
| 9 |
|
|---|
| 10 | ## Important Security Consideration!
|
|---|
| 11 |
|
|---|
| 12 | > [!WARNING]
|
|---|
| 13 | > This library uses JavaScript regular expressions. Please read
|
|---|
| 14 | > the following warning carefully, and be thoughtful about what
|
|---|
| 15 | > you provide to this library in production systems.
|
|---|
| 16 |
|
|---|
| 17 | _Any_ library in JavaScript that deals with matching string
|
|---|
| 18 | patterns using regular expressions will be subject to
|
|---|
| 19 | [ReDoS](https://owasp.org/www-community/attacks/Regular_expression_Denial_of_Service_-_ReDoS)
|
|---|
| 20 | if the pattern is generated using untrusted input.
|
|---|
| 21 |
|
|---|
| 22 | Efforts have been made to mitigate risk as much as is feasible in
|
|---|
| 23 | such a library, providing maximum recursion depths and so forth,
|
|---|
| 24 | but these measures can only ultimately protect against accidents,
|
|---|
| 25 | not malice. A dedicated attacker can _always_ find patterns that
|
|---|
| 26 | cannot be defended against by a bash-compatible glob pattern
|
|---|
| 27 | matching system that uses JavaScript regular expressions.
|
|---|
| 28 |
|
|---|
| 29 | To be extremely clear:
|
|---|
| 30 |
|
|---|
| 31 | > [!WARNING]
|
|---|
| 32 | > **If you create a system where you take user input, and use
|
|---|
| 33 | > that input as the source of a Regular Expression pattern, in
|
|---|
| 34 | > this or any extant glob matcher in JavaScript, you will be
|
|---|
| 35 | > pwned.**
|
|---|
| 36 |
|
|---|
| 37 | A future version of this library _may_ use a different matching
|
|---|
| 38 | algorithm which does not exhibit backtracking problems. If and
|
|---|
| 39 | when that happens, it will likely be a sweeping change, and those
|
|---|
| 40 | improvements will **not** be backported to legacy versions.
|
|---|
| 41 |
|
|---|
| 42 | In the near term, it is not reasonable to continue to play
|
|---|
| 43 | whack-a-mole with security advisories, and so any future ReDoS
|
|---|
| 44 | reports will be considered "working as intended", and resolved
|
|---|
| 45 | entirely by this warning.
|
|---|
| 46 |
|
|---|
| 47 | ## Usage
|
|---|
| 48 |
|
|---|
| 49 | ```js
|
|---|
| 50 | // hybrid module, load with require() or import
|
|---|
| 51 | import { minimatch } from 'minimatch'
|
|---|
| 52 | // or:
|
|---|
| 53 | const { minimatch } = require('minimatch')
|
|---|
| 54 |
|
|---|
| 55 | minimatch('bar.foo', '*.foo') // true!
|
|---|
| 56 | minimatch('bar.foo', '*.bar') // false!
|
|---|
| 57 | minimatch('bar.foo', '*.+(bar|foo)', { debug: true }) // true, and noisy!
|
|---|
| 58 | ```
|
|---|
| 59 |
|
|---|
| 60 | ## Features
|
|---|
| 61 |
|
|---|
| 62 | Supports these glob features:
|
|---|
| 63 |
|
|---|
| 64 | - Brace Expansion
|
|---|
| 65 | - Extended glob matching
|
|---|
| 66 | - "Globstar" `**` matching
|
|---|
| 67 | - [Posix character
|
|---|
| 68 | classes](https://www.gnu.org/software/bash/manual/html_node/Pattern-Matching.html),
|
|---|
| 69 | like `[[:alpha:]]`, supporting the full range of Unicode
|
|---|
| 70 | characters. For example, `[[:alpha:]]` will match against
|
|---|
| 71 | `'é'`, though `[a-zA-Z]` will not. Collating symbol and set
|
|---|
| 72 | matching is not supported, so `[[=e=]]` will _not_ match `'é'`
|
|---|
| 73 | and `[[.ch.]]` will not match `'ch'` in locales where `ch` is
|
|---|
| 74 | considered a single character.
|
|---|
| 75 |
|
|---|
| 76 | See:
|
|---|
| 77 |
|
|---|
| 78 | - `man sh`
|
|---|
| 79 | - `man bash` [Pattern
|
|---|
| 80 | Matching](https://www.gnu.org/software/bash/manual/html_node/Pattern-Matching.html)
|
|---|
| 81 | - `man 3 fnmatch`
|
|---|
| 82 | - `man 5 gitignore`
|
|---|
| 83 |
|
|---|
| 84 | ## Windows
|
|---|
| 85 |
|
|---|
| 86 | **Please only use forward-slashes in glob expressions.**
|
|---|
| 87 |
|
|---|
| 88 | Though windows uses either `/` or `\` as its path separator, only `/`
|
|---|
| 89 | characters are used by this glob implementation. You must use
|
|---|
| 90 | forward-slashes **only** in glob expressions. Back-slashes in patterns
|
|---|
| 91 | will always be interpreted as escape characters, not path separators.
|
|---|
| 92 |
|
|---|
| 93 | Note that `\` or `/` _will_ be interpreted as path separators in paths on
|
|---|
| 94 | Windows, and will match against `/` in glob expressions.
|
|---|
| 95 |
|
|---|
| 96 | So just always use `/` in patterns.
|
|---|
| 97 |
|
|---|
| 98 | ### UNC Paths
|
|---|
| 99 |
|
|---|
| 100 | On Windows, UNC paths like `//?/c:/...` or
|
|---|
| 101 | `//ComputerName/Share/...` are handled specially.
|
|---|
| 102 |
|
|---|
| 103 | - Patterns starting with a double-slash followed by some
|
|---|
| 104 | non-slash characters will preserve their double-slash. As a
|
|---|
| 105 | result, a pattern like `//*` will match `//x`, but not `/x`.
|
|---|
| 106 | - Patterns staring with `//?/<drive letter>:` will _not_ treat
|
|---|
| 107 | the `?` as a wildcard character. Instead, it will be treated
|
|---|
| 108 | as a normal string.
|
|---|
| 109 | - Patterns starting with `//?/<drive letter>:/...` will match
|
|---|
| 110 | file paths starting with `<drive letter>:/...`, and vice versa,
|
|---|
| 111 | as if the `//?/` was not present. This behavior only is
|
|---|
| 112 | present when the drive letters are a case-insensitive match to
|
|---|
| 113 | one another. The remaining portions of the path/pattern are
|
|---|
| 114 | compared case sensitively, unless `nocase:true` is set.
|
|---|
| 115 |
|
|---|
| 116 | Note that specifying a UNC path using `\` characters as path
|
|---|
| 117 | separators is always allowed in the file path argument, but only
|
|---|
| 118 | allowed in the pattern argument when `windowsPathsNoEscape: true`
|
|---|
| 119 | is set in the options.
|
|---|
| 120 |
|
|---|
| 121 | ## Minimatch Class
|
|---|
| 122 |
|
|---|
| 123 | Create a minimatch object by instantiating the `minimatch.Minimatch` class.
|
|---|
| 124 |
|
|---|
| 125 | ```javascript
|
|---|
| 126 | var Minimatch = require('minimatch').Minimatch
|
|---|
| 127 | var mm = new Minimatch(pattern, options)
|
|---|
| 128 | ```
|
|---|
| 129 |
|
|---|
| 130 | ### Properties
|
|---|
| 131 |
|
|---|
| 132 | - `pattern` The original pattern the minimatch object represents.
|
|---|
| 133 | - `options` The options supplied to the constructor.
|
|---|
| 134 | - `set` A 2-dimensional array of regexp or string expressions.
|
|---|
| 135 | Each row in the
|
|---|
| 136 | array corresponds to a brace-expanded pattern. Each item in the row
|
|---|
| 137 | corresponds to a single path-part. For example, the pattern
|
|---|
| 138 | `{a,b/c}/d` would expand to a set of patterns like:
|
|---|
| 139 |
|
|---|
| 140 | [ [ a, d ]
|
|---|
| 141 | , [ b, c, d ] ]
|
|---|
| 142 |
|
|---|
| 143 | If a portion of the pattern doesn't have any "magic" in it
|
|---|
| 144 | (that is, it's something like `"foo"` rather than `fo*o?`), then it
|
|---|
| 145 | will be left as a string rather than converted to a regular
|
|---|
| 146 | expression.
|
|---|
| 147 |
|
|---|
| 148 | - `regexp` Created by the `makeRe` method. A single regular expression
|
|---|
| 149 | expressing the entire pattern. This is useful in cases where you wish
|
|---|
| 150 | to use the pattern somewhat like `fnmatch(3)` with `FNM_PATH` enabled.
|
|---|
| 151 | - `negate` True if the pattern is negated.
|
|---|
| 152 | - `comment` True if the pattern is a comment.
|
|---|
| 153 | - `empty` True if the pattern is `""`.
|
|---|
| 154 |
|
|---|
| 155 | ### Methods
|
|---|
| 156 |
|
|---|
| 157 | - `makeRe()` Generate the `regexp` member if necessary, and return it.
|
|---|
| 158 | Will return `false` if the pattern is invalid.
|
|---|
| 159 | - `match(fname)` Return true if the filename matches the pattern, or
|
|---|
| 160 | false otherwise.
|
|---|
| 161 | - `matchOne(fileArray, patternArray, partial)` Take a `/`-split
|
|---|
| 162 | filename, and match it against a single row in the `regExpSet`. This
|
|---|
| 163 | method is mainly for internal use, but is exposed so that it can be
|
|---|
| 164 | used by a glob-walker that needs to avoid excessive filesystem calls.
|
|---|
| 165 | - `hasMagic()` Returns true if the parsed pattern contains any
|
|---|
| 166 | magic characters. Returns false if all comparator parts are
|
|---|
| 167 | string literals. If the `magicalBraces` option is set on the
|
|---|
| 168 | constructor, then it will consider brace expansions which are
|
|---|
| 169 | not otherwise magical to be magic. If not set, then a pattern
|
|---|
| 170 | like `a{b,c}d` will return `false`, because neither `abd` nor
|
|---|
| 171 | `acd` contain any special glob characters.
|
|---|
| 172 |
|
|---|
| 173 | This does **not** mean that the pattern string can be used as a
|
|---|
| 174 | literal filename, as it may contain magic glob characters that
|
|---|
| 175 | are escaped. For example, the pattern `\\*` or `[*]` would not
|
|---|
| 176 | be considered to have magic, as the matching portion parses to
|
|---|
| 177 | the literal string `'*'` and would match a path named `'*'`,
|
|---|
| 178 | not `'\\*'` or `'[*]'`. The `minimatch.unescape()` method may
|
|---|
| 179 | be used to remove escape characters.
|
|---|
| 180 |
|
|---|
| 181 | All other methods are internal, and will be called as necessary.
|
|---|
| 182 |
|
|---|
| 183 | ### minimatch(path, pattern, options)
|
|---|
| 184 |
|
|---|
| 185 | Main export. Tests a path against the pattern using the options.
|
|---|
| 186 |
|
|---|
| 187 | ```javascript
|
|---|
| 188 | var isJS = minimatch(file, '*.js', { matchBase: true })
|
|---|
| 189 | ```
|
|---|
| 190 |
|
|---|
| 191 | ### minimatch.filter(pattern, options)
|
|---|
| 192 |
|
|---|
| 193 | Returns a function that tests its
|
|---|
| 194 | supplied argument, suitable for use with `Array.filter`. Example:
|
|---|
| 195 |
|
|---|
| 196 | ```javascript
|
|---|
| 197 | var javascripts = fileList.filter(
|
|---|
| 198 | minimatch.filter('*.js', { matchBase: true }),
|
|---|
| 199 | )
|
|---|
| 200 | ```
|
|---|
| 201 |
|
|---|
| 202 | ### minimatch.escape(pattern, options = {})
|
|---|
| 203 |
|
|---|
| 204 | Escape all magic characters in a glob pattern, so that it will
|
|---|
| 205 | only ever match literal strings.
|
|---|
| 206 |
|
|---|
| 207 | If the `windowsPathsNoEscape` option is used, then characters are
|
|---|
| 208 | escaped by wrapping in `[]`, because a magic character wrapped in
|
|---|
| 209 | a character class can only be satisfied by that exact character.
|
|---|
| 210 |
|
|---|
| 211 | Slashes (and backslashes in `windowsPathsNoEscape` mode) cannot
|
|---|
| 212 | be escaped or unescaped.
|
|---|
| 213 |
|
|---|
| 214 | ### minimatch.unescape(pattern, options = {})
|
|---|
| 215 |
|
|---|
| 216 | Un-escape a glob string that may contain some escaped characters.
|
|---|
| 217 |
|
|---|
| 218 | If the `windowsPathsNoEscape` option is used, then square-brace
|
|---|
| 219 | escapes are removed, but not backslash escapes. For example, it
|
|---|
| 220 | will turn the string `'[*]'` into `*`, but it will not turn
|
|---|
| 221 | `'\\*'` into `'*'`, because `\` is a path separator in
|
|---|
| 222 | `windowsPathsNoEscape` mode.
|
|---|
| 223 |
|
|---|
| 224 | When `windowsPathsNoEscape` is not set, then both brace escapes
|
|---|
| 225 | and backslash escapes are removed.
|
|---|
| 226 |
|
|---|
| 227 | Slashes (and backslashes in `windowsPathsNoEscape` mode) cannot
|
|---|
| 228 | be escaped or unescaped.
|
|---|
| 229 |
|
|---|
| 230 | ### minimatch.match(list, pattern, options)
|
|---|
| 231 |
|
|---|
| 232 | Match against the list of
|
|---|
| 233 | files, in the style of fnmatch or glob. If nothing is matched, and
|
|---|
| 234 | options.nonull is set, then return a list containing the pattern itself.
|
|---|
| 235 |
|
|---|
| 236 | ```javascript
|
|---|
| 237 | var javascripts = minimatch.match(fileList, '*.js', { matchBase: true })
|
|---|
| 238 | ```
|
|---|
| 239 |
|
|---|
| 240 | ### minimatch.makeRe(pattern, options)
|
|---|
| 241 |
|
|---|
| 242 | Make a regular expression object from the pattern.
|
|---|
| 243 |
|
|---|
| 244 | ## Options
|
|---|
| 245 |
|
|---|
| 246 | All options are `false` by default.
|
|---|
| 247 |
|
|---|
| 248 | ### debug
|
|---|
| 249 |
|
|---|
| 250 | Dump a ton of stuff to stderr.
|
|---|
| 251 |
|
|---|
| 252 | ### nobrace
|
|---|
| 253 |
|
|---|
| 254 | Do not expand `{a,b}` and `{1..3}` brace sets.
|
|---|
| 255 |
|
|---|
| 256 | ### noglobstar
|
|---|
| 257 |
|
|---|
| 258 | Disable `**` matching against multiple folder names.
|
|---|
| 259 |
|
|---|
| 260 | ### dot
|
|---|
| 261 |
|
|---|
| 262 | Allow patterns to match filenames starting with a period, even if
|
|---|
| 263 | the pattern does not explicitly have a period in that spot.
|
|---|
| 264 |
|
|---|
| 265 | Note that by default, `a/**/b` will **not** match `a/.d/b`, unless `dot`
|
|---|
| 266 | is set.
|
|---|
| 267 |
|
|---|
| 268 | ### noext
|
|---|
| 269 |
|
|---|
| 270 | Disable "extglob" style patterns like `+(a|b)`.
|
|---|
| 271 |
|
|---|
| 272 | ### nocase
|
|---|
| 273 |
|
|---|
| 274 | Perform a case-insensitive match.
|
|---|
| 275 |
|
|---|
| 276 | ### nocaseMagicOnly
|
|---|
| 277 |
|
|---|
| 278 | When used with `{nocase: true}`, create regular expressions that
|
|---|
| 279 | are case-insensitive, but leave string match portions untouched.
|
|---|
| 280 | Has no effect when used without `{nocase: true}`.
|
|---|
| 281 |
|
|---|
| 282 | Useful when some other form of case-insensitive matching is used,
|
|---|
| 283 | or if the original string representation is useful in some other
|
|---|
| 284 | way.
|
|---|
| 285 |
|
|---|
| 286 | ### nonull
|
|---|
| 287 |
|
|---|
| 288 | When a match is not found by `minimatch.match`, return a list containing
|
|---|
| 289 | the pattern itself if this option is set. When not set, an empty list
|
|---|
| 290 | is returned if there are no matches.
|
|---|
| 291 |
|
|---|
| 292 | ### magicalBraces
|
|---|
| 293 |
|
|---|
| 294 | This only affects the results of the `Minimatch.hasMagic` method.
|
|---|
| 295 |
|
|---|
| 296 | If the pattern contains brace expansions, such as `a{b,c}d`, but
|
|---|
| 297 | no other magic characters, then the `Minimatch.hasMagic()` method
|
|---|
| 298 | will return `false` by default. When this option set, it will
|
|---|
| 299 | return `true` for brace expansion as well as other magic glob
|
|---|
| 300 | characters.
|
|---|
| 301 |
|
|---|
| 302 | ### matchBase
|
|---|
| 303 |
|
|---|
| 304 | If set, then patterns without slashes will be matched
|
|---|
| 305 | against the basename of the path if it contains slashes. For example,
|
|---|
| 306 | `a?b` would match the path `/xyz/123/acb`, but not `/xyz/acb/123`.
|
|---|
| 307 |
|
|---|
| 308 | ### nocomment
|
|---|
| 309 |
|
|---|
| 310 | Suppress the behavior of treating `#` at the start of a pattern as a
|
|---|
| 311 | comment.
|
|---|
| 312 |
|
|---|
| 313 | ### nonegate
|
|---|
| 314 |
|
|---|
| 315 | Suppress the behavior of treating a leading `!` character as negation.
|
|---|
| 316 |
|
|---|
| 317 | ### flipNegate
|
|---|
| 318 |
|
|---|
| 319 | Returns from negate expressions the same as if they were not negated.
|
|---|
| 320 | (Ie, true on a hit, false on a miss.)
|
|---|
| 321 |
|
|---|
| 322 | ### partial
|
|---|
| 323 |
|
|---|
| 324 | Compare a partial path to a pattern. As long as the parts of the path that
|
|---|
| 325 | are present are not contradicted by the pattern, it will be treated as a
|
|---|
| 326 | match. This is useful in applications where you're walking through a
|
|---|
| 327 | folder structure, and don't yet have the full path, but want to ensure that
|
|---|
| 328 | you do not walk down paths that can never be a match.
|
|---|
| 329 |
|
|---|
| 330 | For example,
|
|---|
| 331 |
|
|---|
| 332 | ```js
|
|---|
| 333 | minimatch('/a/b', '/a/*/c/d', { partial: true }) // true, might be /a/b/c/d
|
|---|
| 334 | minimatch('/a/b', '/**/d', { partial: true }) // true, might be /a/b/.../d
|
|---|
| 335 | minimatch('/x/y/z', '/a/**/z', { partial: true }) // false, because x !== a
|
|---|
| 336 | ```
|
|---|
| 337 |
|
|---|
| 338 | ### windowsPathsNoEscape
|
|---|
| 339 |
|
|---|
| 340 | Use `\\` as a path separator _only_, and _never_ as an escape
|
|---|
| 341 | character. If set, all `\\` characters are replaced with `/` in
|
|---|
| 342 | the pattern. Note that this makes it **impossible** to match
|
|---|
| 343 | against paths containing literal glob pattern characters, but
|
|---|
| 344 | allows matching with patterns constructed using `path.join()` and
|
|---|
| 345 | `path.resolve()` on Windows platforms, mimicking the (buggy!)
|
|---|
| 346 | behavior of earlier versions on Windows. Please use with
|
|---|
| 347 | caution, and be mindful of [the caveat about Windows
|
|---|
| 348 | paths](#windows).
|
|---|
| 349 |
|
|---|
| 350 | For legacy reasons, this is also set if
|
|---|
| 351 | `options.allowWindowsEscape` is set to the exact value `false`.
|
|---|
| 352 |
|
|---|
| 353 | ### windowsNoMagicRoot
|
|---|
| 354 |
|
|---|
| 355 | When a pattern starts with a UNC path or drive letter, and in
|
|---|
| 356 | `nocase:true` mode, do not convert the root portions of the
|
|---|
| 357 | pattern into a case-insensitive regular expression, and instead
|
|---|
| 358 | leave them as strings.
|
|---|
| 359 |
|
|---|
| 360 | This is the default when the platform is `win32` and
|
|---|
| 361 | `nocase:true` is set.
|
|---|
| 362 |
|
|---|
| 363 | ### preserveMultipleSlashes
|
|---|
| 364 |
|
|---|
| 365 | By default, multiple `/` characters (other than the leading `//`
|
|---|
| 366 | in a UNC path, see "UNC Paths" above) are treated as a single
|
|---|
| 367 | `/`.
|
|---|
| 368 |
|
|---|
| 369 | That is, a pattern like `a///b` will match the file path `a/b`.
|
|---|
| 370 |
|
|---|
| 371 | Set `preserveMultipleSlashes: true` to suppress this behavior.
|
|---|
| 372 |
|
|---|
| 373 | ### optimizationLevel
|
|---|
| 374 |
|
|---|
| 375 | A number indicating the level of optimization that should be done
|
|---|
| 376 | to the pattern prior to parsing and using it for matches.
|
|---|
| 377 |
|
|---|
| 378 | Globstar parts `**` are always converted to `*` when `noglobstar`
|
|---|
| 379 | is set, and multiple adjacent `**` parts are converted into a
|
|---|
| 380 | single `**` (ie, `a/**/**/b` will be treated as `a/**/b`, as this
|
|---|
| 381 | is equivalent in all cases).
|
|---|
| 382 |
|
|---|
| 383 | - `0` - Make no further changes. In this mode, `.` and `..` are
|
|---|
| 384 | maintained in the pattern, meaning that they must also appear
|
|---|
| 385 | in the same position in the test path string. Eg, a pattern
|
|---|
| 386 | like `a/*/../c` will match the string `a/b/../c` but not the
|
|---|
| 387 | string `a/c`.
|
|---|
| 388 | - `1` - (default) Remove cases where a double-dot `..` follows a
|
|---|
| 389 | pattern portion that is not `**`, `.`, `..`, or empty `''`. For
|
|---|
| 390 | example, the pattern `./a/b/../*` is converted to `./a/*`, and
|
|---|
| 391 | so it will match the path string `./a/c`, but not the path
|
|---|
| 392 | string `./a/b/../c`. Dots and empty path portions in the
|
|---|
| 393 | pattern are preserved.
|
|---|
| 394 | - `2` (or higher) - Much more aggressive optimizations, suitable
|
|---|
| 395 | for use with file-walking cases:
|
|---|
| 396 | - Remove cases where a double-dot `..` follows a pattern
|
|---|
| 397 | portion that is not `**`, `.`, or empty `''`. Remove empty
|
|---|
| 398 | and `.` portions of the pattern, where safe to do so (ie,
|
|---|
| 399 | anywhere other than the last position, the first position, or
|
|---|
| 400 | the second position in a pattern starting with `/`, as this
|
|---|
| 401 | may indicate a UNC path on Windows).
|
|---|
| 402 | - Convert patterns containing `<pre>/**/../<p>/<rest>` into the
|
|---|
| 403 | equivalent `<pre>/{..,**}/<p>/<rest>`, where `<p>` is a
|
|---|
| 404 | a pattern portion other than `.`, `..`, `**`, or empty
|
|---|
| 405 | `''`.
|
|---|
| 406 | - Dedupe patterns where a `**` portion is present in one and
|
|---|
| 407 | omitted in another, and it is not the final path portion, and
|
|---|
| 408 | they are otherwise equivalent. So `{a/**/b,a/b}` becomes
|
|---|
| 409 | `a/**/b`, because `**` matches against an empty path portion.
|
|---|
| 410 | - Dedupe patterns where a `*` portion is present in one, and a
|
|---|
| 411 | non-dot pattern other than `**`, `.`, `..`, or `''` is in the
|
|---|
| 412 | same position in the other. So `a/{*,x}/b` becomes `a/*/b`,
|
|---|
| 413 | because `*` can match against `x`.
|
|---|
| 414 |
|
|---|
| 415 | While these optimizations improve the performance of
|
|---|
| 416 | file-walking use cases such as [glob](http://npm.im/glob) (ie,
|
|---|
| 417 | the reason this module exists), there are cases where it will
|
|---|
| 418 | fail to match a literal string that would have been matched in
|
|---|
| 419 | optimization level 1 or 0.
|
|---|
| 420 |
|
|---|
| 421 | Specifically, while the `Minimatch.match()` method will
|
|---|
| 422 | optimize the file path string in the same ways, resulting in
|
|---|
| 423 | the same matches, it will fail when tested with the regular
|
|---|
| 424 | expression provided by `Minimatch.makeRe()`, unless the path
|
|---|
| 425 | string is first processed with
|
|---|
| 426 | `minimatch.levelTwoFileOptimize()` or similar.
|
|---|
| 427 |
|
|---|
| 428 | ### platform
|
|---|
| 429 |
|
|---|
| 430 | When set to `win32`, this will trigger all windows-specific
|
|---|
| 431 | behaviors (special handling for UNC paths, and treating `\` as
|
|---|
| 432 | separators in file paths for comparison.)
|
|---|
| 433 |
|
|---|
| 434 | Defaults to the value of `process.platform`.
|
|---|
| 435 |
|
|---|
| 436 | ### maxGlobstarRecursion
|
|---|
| 437 |
|
|---|
| 438 | Max number of non-adjacent `**` patterns to recursively walk
|
|---|
| 439 | down.
|
|---|
| 440 |
|
|---|
| 441 | The default of `200` is almost certainly high enough for most
|
|---|
| 442 | purposes, and can handle absurdly excessive patterns.
|
|---|
| 443 |
|
|---|
| 444 | If the limit is exceeded (which would require very excessively
|
|---|
| 445 | long patterns and paths containing lots of `**` patterns!), then
|
|---|
| 446 | it is treated as non-matching, even if the path would normally
|
|---|
| 447 | match the pattern provided.
|
|---|
| 448 |
|
|---|
| 449 | That is, this is an intentional false negative, deemed an
|
|---|
| 450 | acceptable break in correctness for security and performance.
|
|---|
| 451 |
|
|---|
| 452 | ### maxExtglobRecursion
|
|---|
| 453 |
|
|---|
| 454 | Max depth to traverse for nested extglobs like `*(a|b|c)`
|
|---|
| 455 |
|
|---|
| 456 | Default is 2, which is quite low, but any higher value swiftly
|
|---|
| 457 | results in punishing performance impacts. Note that this is _not_
|
|---|
| 458 | relevant when the globstar types can be safely coalesced into a
|
|---|
| 459 | single set.
|
|---|
| 460 |
|
|---|
| 461 | For example, `*(a|@(b|c)|d)` would be flattened into
|
|---|
| 462 | `*(a|b|c|d)`. Thus, many common extglobs will retain good
|
|---|
| 463 | performance and never hit this limit, even if they are
|
|---|
| 464 | excessively deep and complicated.
|
|---|
| 465 |
|
|---|
| 466 | If the limit is hit, then the extglob characters are simply not
|
|---|
| 467 | parsed, and the pattern effectively switches into `noextglob:
|
|---|
| 468 | true` mode for the contents of that nested sub-pattern. This will
|
|---|
| 469 | typically _not_ result in a match, but is considered a valid
|
|---|
| 470 | trade-off for security and performance.
|
|---|
| 471 |
|
|---|
| 472 | ## Comparisons to other fnmatch/glob implementations
|
|---|
| 473 |
|
|---|
| 474 | While strict compliance with the existing standards is a
|
|---|
| 475 | worthwhile goal, some discrepancies exist between minimatch and
|
|---|
| 476 | other implementations. Some are intentional, and some are
|
|---|
| 477 | unavoidable.
|
|---|
| 478 |
|
|---|
| 479 | If the pattern starts with a `!` character, then it is negated. Set the
|
|---|
| 480 | `nonegate` flag to suppress this behavior, and treat leading `!`
|
|---|
| 481 | characters normally. This is perhaps relevant if you wish to start the
|
|---|
| 482 | pattern with a negative extglob pattern like `!(a|B)`. Multiple `!`
|
|---|
| 483 | characters at the start of a pattern will negate the pattern multiple
|
|---|
| 484 | times.
|
|---|
| 485 |
|
|---|
| 486 | If a pattern starts with `#`, then it is treated as a comment, and
|
|---|
| 487 | will not match anything. Use `\#` to match a literal `#` at the
|
|---|
| 488 | start of a line, or set the `nocomment` flag to suppress this behavior.
|
|---|
| 489 |
|
|---|
| 490 | The double-star character `**` is supported by default, unless the
|
|---|
| 491 | `noglobstar` flag is set. This is supported in the manner of bsdglob
|
|---|
| 492 | and bash 4.1, where `**` only has special significance if it is the only
|
|---|
| 493 | thing in a path part. That is, `a/**/b` will match `a/x/y/b`, but
|
|---|
| 494 | `a/**b` will not.
|
|---|
| 495 |
|
|---|
| 496 | If an escaped pattern has no matches, and the `nonull` flag is set,
|
|---|
| 497 | then minimatch.match returns the pattern as-provided, rather than
|
|---|
| 498 | interpreting the character escapes. For example,
|
|---|
| 499 | `minimatch.match([], "\\*a\\?")` will return `"\\*a\\?"` rather than
|
|---|
| 500 | `"*a?"`. This is akin to setting the `nullglob` option in bash, except
|
|---|
| 501 | that it does not resolve escaped pattern characters.
|
|---|
| 502 |
|
|---|
| 503 | If brace expansion is not disabled, then it is performed before any
|
|---|
| 504 | other interpretation of the glob pattern. Thus, a pattern like
|
|---|
| 505 | `+(a|{b),c)}`, which would not be valid in bash or zsh, is expanded
|
|---|
| 506 | **first** into the set of `+(a|b)` and `+(a|c)`, and those patterns are
|
|---|
| 507 | checked for validity. Since those two are valid, matching proceeds.
|
|---|
| 508 |
|
|---|
| 509 | Negated extglob patterns are handled as closely as possible to
|
|---|
| 510 | Bash semantics, but there are some cases with negative extglobs
|
|---|
| 511 | which are exceedingly difficult to express in a JavaScript
|
|---|
| 512 | regular expression. In particular the negated pattern
|
|---|
| 513 | `<start>!(<pattern>*|)*` will in bash match anything that does
|
|---|
| 514 | not start with `<start><pattern>`. However,
|
|---|
| 515 | `<start>!(<pattern>*)*` _will_ match paths starting with
|
|---|
| 516 | `<start><pattern>`, because the empty string can match against
|
|---|
| 517 | the negated portion. In this library, `<start>!(<pattern>*|)*`
|
|---|
| 518 | will _not_ match any pattern starting with `<start>`, due to a
|
|---|
| 519 | difference in precisely which patterns are considered "greedy" in
|
|---|
| 520 | Regular Expressions vs bash path expansion. This may be fixable,
|
|---|
| 521 | but not without incurring some complexity and performance costs,
|
|---|
| 522 | and the trade-off seems to not be worth pursuing.
|
|---|
| 523 |
|
|---|
| 524 | Note that `fnmatch(3)` in libc is an extremely naive string comparison
|
|---|
| 525 | matcher, which does not do anything special for slashes. This library is
|
|---|
| 526 | designed to be used in glob searching and file walkers, and so it does do
|
|---|
| 527 | special things with `/`. Thus, `foo*` will not match `foo/bar` in this
|
|---|
| 528 | library, even though it would in `fnmatch(3)`.
|
|---|