Skip to content

Commit f15ca04

Browse files
committed
Support negative ignore patterns
1 parent e4347a4 commit f15ca04

2 files changed

Lines changed: 9 additions & 6 deletions

File tree

src/index.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,16 @@ function processPatterns(
106106

107107
const matchPatterns: string[] = [];
108108
const ignorePatterns: string[] = [];
109+
const unignorePatterns: string[] = [];
109110

110111
for (const pattern of ignore) {
111112
if (!pattern) {
112113
continue;
113114
}
114-
// don't handle negated patterns here for consistency with fast-glob
115115
if (pattern[0] !== '!' || pattern[1] === '(') {
116116
ignorePatterns.push(normalizePattern(pattern, expandDirectories, cwd, props, true));
117+
} else {
118+
unignorePatterns.push(pattern.slice(1));
117119
}
118120
}
119121

@@ -128,7 +130,7 @@ function processPatterns(
128130
}
129131
}
130132

131-
return { match: matchPatterns, ignore: ignorePatterns };
133+
return { match: matchPatterns, ignore: ignorePatterns, unignore: unignorePatterns };
132134
}
133135

134136
// TODO: this is slow, find a better way to do this
@@ -182,10 +184,13 @@ function crawl(options: GlobOptions, cwd: string, sync: boolean) {
182184
log('internal processing patterns:', processed);
183185
}
184186

187+
const unignoreMatcher = processed.unignore.length === 0 ? undefined : picomatch(processed.unignore);
188+
185189
const matcher = picomatch(processed.match, {
186190
dot: options.dot,
187191
nocase,
188-
ignore: processed.ignore
192+
ignore: processed.ignore,
193+
onIgnore: unignoreMatcher ? result => unignoreMatcher(result.output) : undefined
189194
});
190195

191196
const ignore = picomatch(processed.ignore, {

test/index.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,11 +352,9 @@ test('negative absolute patterns in options', async () => {
352352
assert.deepEqual(files2.sort(), ['a/b.txt', 'b/b.txt']);
353353
});
354354

355-
// can't easily make them properly work right now
356-
// but at least it's consistent with fast-glob this way
357355
test('negative patterns in ignore are ignored', async () => {
358356
const files = await glob({ patterns: ['**/*'], ignore: ['**/b.txt', '!a/b.txt'], cwd });
359-
assert.deepEqual(files.sort(), ['a/a.txt', 'b/a.txt']);
357+
assert.deepEqual(files.sort(), ['a/a.txt', 'a/b.txt', 'b/a.txt']);
360358

361359
const files2 = await glob({ patterns: ['**/*', '!**/b.txt', '!!a/b.txt'], cwd });
362360
assert.deepEqual(files2.sort(), ['a/a.txt', 'b/a.txt']);

0 commit comments

Comments
 (0)