Skip to content

Commit

Permalink
Support negative ignore patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Oct 30, 2024
1 parent f1ab106 commit d81fc09
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
11 changes: 8 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand All @@ -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
Expand Down Expand Up @@ -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, {
Expand Down
4 changes: 1 addition & 3 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down

0 comments on commit d81fc09

Please sign in to comment.