Skip to content

Commit

Permalink
5.3.2: fixes #130, fixes consequent escaped backslashes
Browse files Browse the repository at this point in the history
  • Loading branch information
kaelzhang committed Aug 12, 2024
1 parent 9facdca commit cfd0fce
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
24 changes: 17 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,26 @@ const REPLACERS = [
[
// (a\ ) -> (a )
// (a ) -> (a)
// (a ) -> (a)
// (a \ ) -> (a )
/\\?\s+$/,
match => match.indexOf('\\') === 0
? SPACE
: EMPTY
/((?:\\\\)*?)(\\?\s+)$/,
(_, m1, m2) => m1 + (
m2.indexOf('\\') === 0
? SPACE
: EMPTY
)
],

// replace (\ ) with ' '
// (\ ) -> ' '
// (\\ ) -> '\\ '
// (\\\ ) -> '\\ '
[
/\\\s/g,
() => SPACE
/(\\+?)\s/g,
(_, m1) => {
const {length} = m1
return m1.slice(0, length - length % 2) + SPACE
}
],

// Escape metacharacters
Expand Down Expand Up @@ -317,7 +326,8 @@ const makeRegex = (pattern, ignoreCase) => {

if (!source) {
source = REPLACERS.reduce(
(prev, current) => prev.replace(current[0], current[1].bind(pattern)),
(prev, [matcher, replacer]) =>
prev.replace(matcher, replacer.bind(pattern)),
pattern
)
regexCache[pattern] = source
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ignore",
"version": "5.3.1",
"version": "5.3.2",
"description": "Ignore is a manager and filter for .gitignore rules, the one used by eslint, gitbook and many others.",
"files": [
"legacy.js",
Expand All @@ -17,6 +17,7 @@
"tap": "tap --reporter classic",
"test:git": "npm run tap test/git-check-ignore.js",
"test:ignore": "npm run tap test/ignore.js",
"test:ignore:only": "IGNORE_ONLY_IGNORES=1 npm run tap test/ignore.js",
"test:others": "npm run tap test/others.js",
"test:cases": "npm run tap test/*.js -- --coverage",
"test:no-coverage": "npm run tap test/*.js -- --no-check-coverage",
Expand Down
20 changes: 18 additions & 2 deletions test/fixtures/cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ const cases = [
// ],
/////////////////////////////////////////////////////////////////////
[
'#130: consequent escaped backslashes with whitespaces',
[
'a\\\\ ',
'a\\\\ b',
'a\\\\\\ b'
],
{
'a\\': 1,
'a\\ b': 1,
'a\\\\ b': 0,
'a\\\\\\ b': 0
}
], [
'#108: gitignore rules with BOM',
[
readPatterns('.gitignore-with-BOM'),
Expand Down Expand Up @@ -618,7 +631,8 @@ const cases = [
[
'abc\\ ', // only one space left -> (abc )
'bcd ', // no space left -> (bcd)
'cde \\ ' // two spaces -> (cde )
'cde \\ ', // two spaces -> (cde )
'def ' // no space left -> (def)
],
{
// nothing to do with backslashes
Expand All @@ -631,7 +645,9 @@ const cases = [
'bcd ': 0,
'cde ': 1,
'cde ': 0,
'cde ': 0
'cde ': 0,
'def': 1,
'def ': 0
},
false,
true
Expand Down

0 comments on commit cfd0fce

Please sign in to comment.