From dd9d0108832c0827d637e1804bd591a025a6648d Mon Sep 17 00:00:00 2001 From: Bret Comnes Date: Sun, 6 Feb 2022 15:14:29 -0800 Subject: [PATCH] Fix negative prefixed positive tests Here is a problem I noticed: `!(node_modules)**/*.{png,ico}` doesn't match anything in `node-glob-ignore`, but it does match when passed directly to `glob`. (E.g. any png or ico file in the current directly that's not inside of node_modules) This is due to this line dropping the glob: https://github.com/kaelzhang/node-glob-gitignore/blob/c8a236a52a14e6d1e8ba692c4a210dc79e817e7e/src/util.js#L83-L88 Now, I could pass in `./!(node_modules)**/*.{png,ico}` or `**/!(node_modules)**/*.{png,ico}` (probably the more desirable glob) but that starts to diverge from globs that work in `glob`. I know its better to use the ignore array here, but I'm trying to generally retain the same API of a tool that was formerly using glob. I'm thinking that's a bug in the design of that function, but I'm curious what you think. Perhaps checking to make sure the negative glob isn't for a group, which would indicate that it is in fact a positively matching glob. That is the fix I added in this PR but looking for your thoughts on the solution. --- src/util.js | 2 +- test/fixtures/b/a.js | 0 test/fixtures/b/b.js | 0 test/fixtures/b/b/a.js | 0 test/fixtures/b/b/b.js | 0 test/fixtures/b/b/b/a.js | 0 test/fixtures/b/b/b/b.js | 0 test/fixtures/b/b/b/c | 0 test/fixtures/b/b/c | 0 test/fixtures/b/c | 0 test/glob-gitignore.js | 9 +++++++-- 11 files changed, 8 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/b/a.js create mode 100644 test/fixtures/b/b.js create mode 100644 test/fixtures/b/b/a.js create mode 100644 test/fixtures/b/b/b.js create mode 100644 test/fixtures/b/b/b/a.js create mode 100644 test/fixtures/b/b/b/b.js create mode 100644 test/fixtures/b/b/b/c create mode 100644 test/fixtures/b/b/c create mode 100644 test/fixtures/b/c diff --git a/src/util.js b/src/util.js index f688693..1d96eae 100644 --- a/src/util.js +++ b/src/util.js @@ -58,7 +58,7 @@ const createShouldIgnore = options => { } } -const isNegative = pattern => pattern[0] === '!' +const isNegative = pattern => pattern[0] === '!' && pattern[1] !== '(' const isPattern = subject => subject && typeof subject === 'string' const createTasks = (patterns, options) => { diff --git a/test/fixtures/b/a.js b/test/fixtures/b/a.js new file mode 100644 index 0000000..e69de29 diff --git a/test/fixtures/b/b.js b/test/fixtures/b/b.js new file mode 100644 index 0000000..e69de29 diff --git a/test/fixtures/b/b/a.js b/test/fixtures/b/b/a.js new file mode 100644 index 0000000..e69de29 diff --git a/test/fixtures/b/b/b.js b/test/fixtures/b/b/b.js new file mode 100644 index 0000000..e69de29 diff --git a/test/fixtures/b/b/b/a.js b/test/fixtures/b/b/b/a.js new file mode 100644 index 0000000..e69de29 diff --git a/test/fixtures/b/b/b/b.js b/test/fixtures/b/b/b/b.js new file mode 100644 index 0000000..e69de29 diff --git a/test/fixtures/b/b/b/c b/test/fixtures/b/b/b/c new file mode 100644 index 0000000..e69de29 diff --git a/test/fixtures/b/b/c b/test/fixtures/b/b/c new file mode 100644 index 0000000..e69de29 diff --git a/test/fixtures/b/c b/test/fixtures/b/c new file mode 100644 index 0000000..e69de29 diff --git a/test/glob-gitignore.js b/test/glob-gitignore.js index ec1b660..b326cb8 100644 --- a/test/glob-gitignore.js +++ b/test/glob-gitignore.js @@ -16,7 +16,7 @@ const CASES = [ d: 'basic', p: '**/*.js', i: 'a.js', - r: ['b.js', 'a/b.js', 'a/a/b.js', 'a/a/a/b.js'] + r: ['b.js', 'a/b.js', 'a/a/b.js', 'a/a/a/b.js', 'b/b.js', 'b/b/b.js', 'b/b/b/b.js'] }, { d: 'patterns', @@ -33,12 +33,17 @@ const CASES = [ { d: 'without ignore', p: ['**/a.js'], - r: ['a.js', 'a/a.js', 'a/a/a.js', 'a/a/a/a.js'] + r: ['a.js', 'a/a.js', 'a/a/a.js', 'a/a/a/a.js', 'b/a.js', 'b/b/a.js', 'b/b/b/a.js'] }, { d: 'only negative', p: ['!**/a.js'], r: [] + }, + { + d: 'negative prefixed group', + p: '!(a)**/*.js', + r: ['b/b.js', 'b/a.js'] } ]