Skip to content

Commit

Permalink
use windowsPathNoEscape/allowWindowsEscape opts
Browse files Browse the repository at this point in the history
Only respond to allowWindowsEscape if it's a literal false, though.
  • Loading branch information
isaacs committed May 16, 2022
1 parent e2a142c commit 43cfa81
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,21 @@ minimatch('/a/b', '/**/d', { partial: true }) // true, might be /a/b/.../d
minimatch('/x/y/z', '/a/**/z', { partial: true }) // false, because x !== a
```

### windowsPathsNoEscape

Use `\\` as a path separator _only_, and _never_ as an escape
character. If set, all `\\` characters are replaced with `/` in
the pattern. Note that this makes it **impossible** to match
against paths containing literal glob pattern characters, but
allows matching with patterns constructed using `path.join()` and
`path.resolve()` on Windows platforms, mimicking the (buggy!)
behavior of earlier versions on Windows. Please use with
caution, and be mindful of [the caveat about Windows
paths](#windows).

For legacy reasons, this is also set if
`options.allowWindowsEscape` is set to the exact value `false`.

## Comparisons to other fnmatch/glob implementations

While strict compliance with the existing standards is a worthwhile
Expand Down
5 changes: 5 additions & 0 deletions minimatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ class Minimatch {
this.options = options
this.set = []
this.pattern = pattern
this.windowsPathsNoEscape = !!options.windowsPathsNoEscape ||
options.allowWindowsEscape === false
if (this.windowsPathsNoEscape) {
this.pattern = this.pattern.replace(/\\/g, '/')
}
this.regexp = null
this.negate = false
this.comment = false
Expand Down
32 changes: 32 additions & 0 deletions test/win-path-sep.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,35 @@ t.test('posix default', t => {
global.process = proc
t.end()
})

t.test('override with options', t => {
const mm = t.mock('../', { '../lib/path.js': {sep: '\\'}})

Object.defineProperty(process, 'platform', {
value: 'win32',
configurable: true,
enumerable: true,
writable: true,
})
require('path').sep = '\\'

t.equal(mm('c:\\foo\\bar', 'c:\\foo\\*', {
windowsPathsNoEscape: true,
}), true)

t.equal(mm('c:\\foo\\bar', 'c:\\foo\\*', {
windowsPathsNoEscape: 'hamburger',
}), true)

t.equal(mm('c:\\foo\\bar', 'c:\\foo\\*', {
allowWindowsEscape: false,
}), true)

t.equal(mm('c:\\foo\\bar', 'c:\\foo\\*', {}), false)

t.equal(mm('c:\\foo\\bar', 'c:\\foo\\*', {
allowWindowsEscape: null,
}), false)

t.end()
})

0 comments on commit 43cfa81

Please sign in to comment.