Skip to content

Commit

Permalink
fix: character class escaping
Browse files Browse the repository at this point in the history
  • Loading branch information
MrGriefs authored and isaacs committed Dec 14, 2022
1 parent de77766 commit f6fd04d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
8 changes: 7 additions & 1 deletion minimatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ minimatch.match = (list, pattern, options = {}) => {

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

Expand Down Expand Up @@ -493,6 +494,11 @@ class Minimatch {
}

case '\\':
if (inClass && pattern.charAt(i + 1) === '-') {
re += c
continue
}

clearStateChar()
escaping = true
continue
Expand Down Expand Up @@ -616,7 +622,7 @@ class Minimatch {
// to do safely. For now, this is safe and works.
cs = pattern.substring(classStart + 1, i)
try {
RegExp('[' + braExpEscape(globUnescape(cs)) + ']')
RegExp('[' + braExpEscape(charUnescape(cs)) + ']')
} catch (er) {
// not a valid class!
sp = this.parse(cs, SUBPARSE)
Expand Down
8 changes: 8 additions & 0 deletions tap-snapshots/test/basic.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ exports[`test/basic.js TAP basic tests > makeRe [[] 1`] = `
/^(?:(?!\\.)(?=.)[\\[])$/
`

exports[`test/basic.js TAP basic tests > makeRe [\\-\\]] 1`] = `
/^(?:(?!\\.)(?=.)[\\-\\]])$/
`

exports[`test/basic.js TAP basic tests > makeRe [\\\\] 1`] = `
/^(?:(?!\\.)(?=.)[\\\\])$/
`
Expand Down Expand Up @@ -253,6 +257,10 @@ exports[`test/basic.js TAP basic tests > makeRe [z-a] 1`] = `
/^(?:\\[z\\-a\\])$/
`

exports[`test/basic.js TAP basic tests > makeRe [z\\-a] 1`] = `
/^(?:(?!\\.)(?=.)[z\\-a])$/
`

exports[`test/basic.js TAP basic tests > makeRe \\ 1`] = `
/^(?:\\\\)$/
`
Expand Down
2 changes: 2 additions & 0 deletions test/patterns.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,8 @@ module.exports = [
['[\\z-a]', []],
['[\\b-a]', []],
['[]+*]', []],
['[z\\-a]', []],
['[\\-\\]]', []],
]

Object.defineProperty(module.exports, 'files', {
Expand Down

0 comments on commit f6fd04d

Please sign in to comment.