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 3 commits into
base: main
Choose a base branch
from
Draft
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
@@ -42,12 +42,11 @@
"homepage": "https://github.com/SuperchupuDev/tinyglobby#readme",
"dependencies": {
"fdir": "^6.4.2",
"picomatch": "^4.0.2"
"unmatch": "^1.0.1"
},
"devDependencies": {
"@biomejs/biome": "^1.9.4",
"@types/node": "^22.9.0",
"@types/picomatch": "^3.0.1",
"fs-fixture": "^2.6.0",
"tsup": "^8.3.5",
"typescript": "^5.6.3"
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.

19 changes: 12 additions & 7 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 { isDynamicPattern } from './utils.ts';

export interface GlobOptions {
@@ -101,12 +101,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));
}
}

@@ -142,7 +144,7 @@ function processPatterns(
}
}

return { match: matchPatterns, ignore: ignorePatterns, transformed };
return { match: matchPatterns, ignore: ignorePatterns, unignore: unignorePatterns, transformed };
}

// TODO: this is slow, find a better way to do this
@@ -175,18 +177,21 @@ function crawl(options: GlobOptions, cwd: string, sync: boolean) {

const processed = processPatterns(options, cwd, properties);

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

const matcher = match(processed.match, {
dot: options.dot,
nocase: options.caseSensitiveMatch === false,
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: options.caseSensitiveMatch === false
});

const exclude = picomatch('*(../)**', {
const exclude = match('*(../)**', {
dot: true,
nocase: options.caseSensitiveMatch === false,
ignore: processed.transformed
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import picomatch from 'picomatch';
import match from 'unmatch';

// #region convertPathToPattern
const ESCAPED_WIN32_BACKSLASHES = /\\(?![()[\]{}!+@])/g;
@@ -49,7 +49,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
4 changes: 1 addition & 3 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -330,11 +330,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']);