Skip to content

Commit f6fd04d

Browse files
MrGriefsisaacs
authored andcommitted
fix: character class escaping
1 parent de77766 commit f6fd04d

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

minimatch.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ minimatch.match = (list, pattern, options = {}) => {
157157

158158
// replace stuff like \* with *
159159
const globUnescape = s => s.replace(/\\(.)/g, '$1')
160+
const charUnescape = s => s.replace(/\\([^-\]])/g, '$1')
160161
const regExpEscape = s => s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&')
161162
const braExpEscape = s => s.replace(/[[\]\\]/g, '\\$&')
162163

@@ -493,6 +494,11 @@ class Minimatch {
493494
}
494495

495496
case '\\':
497+
if (inClass && pattern.charAt(i + 1) === '-') {
498+
re += c
499+
continue
500+
}
501+
496502
clearStateChar()
497503
escaping = true
498504
continue
@@ -616,7 +622,7 @@ class Minimatch {
616622
// to do safely. For now, this is safe and works.
617623
cs = pattern.substring(classStart + 1, i)
618624
try {
619-
RegExp('[' + braExpEscape(globUnescape(cs)) + ']')
625+
RegExp('[' + braExpEscape(charUnescape(cs)) + ']')
620626
} catch (er) {
621627
// not a valid class!
622628
sp = this.parse(cs, SUBPARSE)

tap-snapshots/test/basic.js.test.cjs

+8
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,10 @@ exports[`test/basic.js TAP basic tests > makeRe [[] 1`] = `
189189
/^(?:(?!\\.)(?=.)[\\[])$/
190190
`
191191

192+
exports[`test/basic.js TAP basic tests > makeRe [\\-\\]] 1`] = `
193+
/^(?:(?!\\.)(?=.)[\\-\\]])$/
194+
`
195+
192196
exports[`test/basic.js TAP basic tests > makeRe [\\\\] 1`] = `
193197
/^(?:(?!\\.)(?=.)[\\\\])$/
194198
`
@@ -253,6 +257,10 @@ exports[`test/basic.js TAP basic tests > makeRe [z-a] 1`] = `
253257
/^(?:\\[z\\-a\\])$/
254258
`
255259

260+
exports[`test/basic.js TAP basic tests > makeRe [z\\-a] 1`] = `
261+
/^(?:(?!\\.)(?=.)[z\\-a])$/
262+
`
263+
256264
exports[`test/basic.js TAP basic tests > makeRe \\ 1`] = `
257265
/^(?:\\\\)$/
258266
`

test/patterns.js

+2
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,8 @@ module.exports = [
271271
['[\\z-a]', []],
272272
['[\\b-a]', []],
273273
['[]+*]', []],
274+
['[z\\-a]', []],
275+
['[\\-\\]]', []],
274276
]
275277

276278
Object.defineProperty(module.exports, 'files', {

0 commit comments

Comments
 (0)