From 05780f5be47ae985b745a05fef49f50f6a0b9b9b Mon Sep 17 00:00:00 2001 From: Kael Zhang Date: Fri, 22 May 2020 21:57:17 +0800 Subject: [PATCH] #59: fixes trailing backslashes before ] --- .github/workflows/nodejs.yml | 2 +- index.js | 8 +++++++- test/fixtures/cases.js | 12 ++++++------ 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index b15baf3..68f755c 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -9,7 +9,7 @@ jobs: strategy: matrix: - node-version: [8.x, 10.x, 12.x] + node-version: [10.x, 12.x, 14.x] steps: - uses: actions/checkout@v1 diff --git a/index.js b/index.js index fe59046..f48b1cc 100644 --- a/index.js +++ b/index.js @@ -40,6 +40,12 @@ const sanitizeRange = range => range.replace( : EMPTY ) +// See fixtures #59 +const cleanRangeBackSlash = slashes => slashes.slice( + 0, + parseInt(slashes.length / 2, 10) * 2 +) + // > If the pattern ends with a slash, // > it is removed for the purpose of the following description, // > but it would only find a match with a directory. @@ -217,7 +223,7 @@ const REPLACERS = [ /(\\)?\[([^\]/]*?)(\\*)($|\])/g, (match, leadEscape, range, endEscape, close) => leadEscape === ESCAPE // '\\[bar]' -> '\\\\[bar\\]' - ? `\\[${range}${close}` + ? `\\[${range}${cleanRangeBackSlash(endEscape)}${close}` : close === ']' ? endEscape.length % 2 === 0 // A normal case, and it is a range notation diff --git a/test/fixtures/cases.js b/test/fixtures/cases.js index 9f32d6d..17c1807 100644 --- a/test/fixtures/cases.js +++ b/test/fixtures/cases.js @@ -23,12 +23,12 @@ const cases = [ [ '#59 and more cases about range notation', [ - 'src/\\[foo\\]', - 'src/\\[foo2\\\\]', - 'src/\\[foo3\\\\\\]', - 'src/\\[foo4\\\\\\\\]', - 'src/\\[foo5\\\\\\\\\\]', - 'src/\\[foo6\\\\\\\\\\\\]', + 'src/\\[foo\\]', // 1 -> 0 + 'src/\\[foo2\\\\]', // 2 -> 1 + 'src/\\[foo3\\\\\\]', // 3 -> 1 + 'src/\\[foo4\\\\\\\\]', // 4 -> 2 + 'src/\\[foo5\\\\\\\\\\]', // 5 -> 2 + 'src/\\[foo6\\\\\\\\\\\\]', // 6 -> 3 'src/\\[bar]',