From f6fd04d445fc81d24e35dc4b0c51eaaa0526f040 Mon Sep 17 00:00:00 2001 From: Hagen Date: Wed, 30 Nov 2022 21:02:55 +0000 Subject: [PATCH] fix: character class escaping --- minimatch.js | 8 +++++++- tap-snapshots/test/basic.js.test.cjs | 8 ++++++++ test/patterns.js | 2 ++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/minimatch.js b/minimatch.js index f5d86a2f..398cbe4f 100644 --- a/minimatch.js +++ b/minimatch.js @@ -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, '\\$&') @@ -493,6 +494,11 @@ class Minimatch { } case '\\': + if (inClass && pattern.charAt(i + 1) === '-') { + re += c + continue + } + clearStateChar() escaping = true continue @@ -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) diff --git a/tap-snapshots/test/basic.js.test.cjs b/tap-snapshots/test/basic.js.test.cjs index 5d08e73d..c601e7fa 100644 --- a/tap-snapshots/test/basic.js.test.cjs +++ b/tap-snapshots/test/basic.js.test.cjs @@ -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`] = ` /^(?:(?!\\.)(?=.)[\\\\])$/ ` @@ -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`] = ` /^(?:\\\\)$/ ` diff --git a/test/patterns.js b/test/patterns.js index 7cb175f7..e8c48544 100644 --- a/test/patterns.js +++ b/test/patterns.js @@ -271,6 +271,8 @@ module.exports = [ ['[\\z-a]', []], ['[\\b-a]', []], ['[]+*]', []], + ['[z\\-a]', []], + ['[\\-\\]]', []], ] Object.defineProperty(module.exports, 'files', {