From d81fc09ed7fb68ce11ccde9c9242ca564fbe793c Mon Sep 17 00:00:00 2001 From: Lars Kappert Date: Wed, 30 Oct 2024 13:21:13 +0100 Subject: [PATCH] Support negative ignore patterns --- src/index.ts | 11 ++++++++--- test/index.test.ts | 4 +--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/index.ts b/src/index.ts index d6de95b..a61db58 100644 --- a/src/index.ts +++ b/src/index.ts @@ -100,12 +100,14 @@ function processPatterns( const matchPatterns: string[] = []; const ignorePatterns: string[] = []; + const unignorePatterns: string[] = []; for (const pattern of ignore) { - // don't handle negated patterns here for consistency with fast-glob if (!pattern.startsWith('!') || pattern[1] === '(') { const newPattern = normalizePattern(pattern, expandDirectories, cwd, properties, true); ignorePatterns.push(newPattern); + } else { + unignorePatterns.push(pattern.slice(1)); } } @@ -119,7 +121,7 @@ function processPatterns( } } - return { match: matchPatterns, ignore: ignorePatterns }; + return { match: matchPatterns, ignore: ignorePatterns, unignore: unignorePatterns }; } // TODO: this is slow, find a better way to do this @@ -152,10 +154,13 @@ function crawl(options: GlobOptions, cwd: string, sync: boolean) { const processed = processPatterns(options, cwd, properties); + const unignoreMatcher = processed.unignore.length === 0 ? undefined : picomatch(processed.unignore) + const matcher = picomatch(processed.match, { dot: options.dot, nocase: options.caseSensitiveMatch === false, - ignore: processed.ignore + ignore: processed.ignore, + onIgnore: unignoreMatcher ? (result => unignoreMatcher(result.output)) : undefined }); const exclude = picomatch(processed.ignore, { diff --git a/test/index.test.ts b/test/index.test.ts index 5196d83..7ac0404 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -320,11 +320,9 @@ test('negative absolute patterns in options', async () => { assert.deepEqual(files2.sort(), ['a/b.txt', 'b/b.txt']); }); -// can't easily make them properly work right now -// but at least it's consistent with fast-glob this way test('negative patterns in ignore are ignored', async () => { const files = await glob({ patterns: ['**/*'], ignore: ['**/b.txt', '!a/b.txt'], cwd }); - assert.deepEqual(files.sort(), ['a/a.txt', 'b/a.txt']); + assert.deepEqual(files.sort(), ['a/a.txt', 'a/b.txt', 'b/a.txt']); const files2 = await glob({ patterns: ['**/*', '!**/b.txt', '!!a/b.txt'], cwd }); assert.deepEqual(files2.sort(), ['a/a.txt', 'b/a.txt']);