Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support negated ignore patterns #70

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,11 @@
},
"dependencies": {
"fdir": "^6.4.3",
"picomatch": "^4.0.2"
"unmatch": "^1.0.1"
},
"devDependencies": {
"@biomejs/biome": "^1.9.4",
"@types/node": "^22.13.4",
"@types/picomatch": "^3.0.2",
"fs-fixture": "^2.7.0",
"tsup": "^8.3.6",
"typescript": "^5.7.3"
Expand Down
20 changes: 9 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 11 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path, { posix } from 'node:path';
import { type Options as FdirOptions, fdir } from 'fdir';
import picomatch from 'picomatch';
import match from 'unmatch';
import { escapePath, getPartialMatcher, isDynamicPattern, log, splitPattern } from './utils.ts';

const PARENT_DIRECTORY = /^(\/?\.\.)+/;
Expand Down Expand Up @@ -106,14 +106,16 @@ function processPatterns(

const matchPatterns: string[] = [];
const ignorePatterns: string[] = [];
const unignorePatterns: string[] = [];

for (const pattern of ignore) {
if (!pattern) {
continue;
}
// don't handle negated patterns here for consistency with fast-glob
if (pattern[0] !== '!' || pattern[1] === '(') {
ignorePatterns.push(normalizePattern(pattern, expandDirectories, cwd, props, true));
} else {
unignorePatterns.push(pattern.slice(1));
}
}

Expand All @@ -128,7 +130,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 @@ -182,13 +184,16 @@ function crawl(options: GlobOptions, cwd: string, sync: boolean) {
log('internal processing patterns:', processed);
}

const matcher = picomatch(processed.match, {
const unignoreMatcher = processed.unignore.length === 0 ? undefined : match(processed.unignore);

const matcher = match(processed.match, {
dot: options.dot,
nocase,
ignore: processed.ignore
ignore: processed.ignore,
onIgnore: unignoreMatcher ? result => unignoreMatcher(result.output) && match.constants.UNIGNORE : undefined
});

const ignore = picomatch(processed.ignore, {
const ignore = match(processed.ignore, {
dot: options.dot,
nocase
});
Expand Down
8 changes: 4 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import picomatch, { type Matcher } from 'picomatch';
import match, { type Matcher } from 'unmatch';

// #region PARTIAL MATCHER
export interface PartialMatcherOptions {
Expand All @@ -18,7 +18,7 @@ export function getPartialMatcher(patterns: string[], options?: PartialMatcherOp
const partsCount = parts.length;
const partRegexes = Array(partsCount);
for (let j = 0; j < partsCount; j++) {
partRegexes[j] = picomatch.makeRe(parts[j], options);
partRegexes[j] = match.makeRe(parts[j], options);
}
regexes[i] = partRegexes;
}
Expand Down Expand Up @@ -72,7 +72,7 @@ const splitPatternOptions = { parts: true };

// if a pattern has no slashes outside glob symbols, results.parts is []
export function splitPattern(path: string): string[] {
const result = picomatch.scan(path, splitPatternOptions);
const result = match.scan(path, splitPatternOptions);
return result.parts?.length ? result.parts : [path];
}
// #endregion
Expand Down Expand Up @@ -129,7 +129,7 @@ export function isDynamicPattern(pattern: string, options?: { caseSensitiveMatch
return true;
}

const scan = picomatch.scan(pattern);
const scan = match.scan(pattern);
return scan.isGlob || scan.negated;
}
// #endregion
Expand Down
4 changes: 1 addition & 3 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,11 +352,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
Loading