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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,10 @@
"access": "public",
"provenance": true
},
"packageManager": "[email protected]"
"packageManager": "[email protected]",
"pnpm": {
"patchedDependencies": {
"[email protected]": "patches/[email protected]"
}
}
}
27 changes: 27 additions & 0 deletions patches/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
diff --git a/lib/picomatch.js b/lib/picomatch.js
index d0ebd9f163cf240a00998dec9ed69523d1405b4d..b5d9fb5b0f55b70f99a5012b80e91974b46699eb 100644
--- a/lib/picomatch.js
+++ b/lib/picomatch.js
@@ -75,12 +75,17 @@ const picomatch = (glob, options, returnState = false) => {
return returnObject ? result : false;
}

- if (isIgnored(input)) {
- if (typeof opts.onIgnore === 'function') {
- opts.onIgnore(result);
+ ignored: {
+ if (isIgnored(input)) {
+ if (typeof opts.onIgnore === 'function') {
+ if (opts.onIgnore(result) === true) {
+ result.isMatch = true;
+ break ignored;
+ }
+ }
+ result.isMatch = false;
+ return returnObject ? result : false;
}
- result.isMatch = false;
- return returnObject ? result : false;
}

if (typeof opts.onMatch === 'function') {
19 changes: 12 additions & 7 deletions pnpm-lock.yaml

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

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
Loading