Skip to content

Commit ed0d061

Browse files
committed
Add includeChildMatches: false option
This does a (best effort) attempt to ignore all *subsequent* matches that are a descendant of a previous match. Note the caveat in the docs, though. It will only ignore what it knows about. There's no post-hoc evaluation to only return the shortest results in cases where a deeper match is encountered before its shallower parent.
1 parent b274298 commit ed0d061

8 files changed

+608
-623
lines changed

.tshy/build.json

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"extends": "../tsconfig.json",
33
"compilerOptions": {
44
"rootDir": "../src",
5-
"target": "es2022",
65
"module": "nodenext",
76
"moduleResolution": "nodenext"
87
}

README.md

+42
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,48 @@ share the previously loaded cache.
593593
explicitly, then any provided `scurry` object must match this
594594
setting.
595595

596+
- `includeChildMatches` boolean, default `true`. Do not match any
597+
children of any matches. For example, the pattern `**\/foo`
598+
would match `a/foo`, but not `a/foo/b/foo` in this mode.
599+
600+
This is especially useful for cases like "find all
601+
`node_modules` folders, but not the ones in `node_modules`".
602+
603+
In order to support this, the `Ignore` implementation must
604+
support an `add(pattern: string)` method. If using the default
605+
`Ignore` class, then this is fine, but if this is set to
606+
`false`, and a custom `Ignore` is provided that does not have
607+
an `add()` method, then it will throw an error.
608+
609+
**Caveat** It *only* ignores matches that would be a descendant
610+
of a previous match, and only if that descendant is matched
611+
*after* the ancestor is encountered. Since the file system walk
612+
happens in indeterminate order, it's possible that a match will
613+
already be added before its ancestor, if multiple or braced
614+
patterns are used.
615+
616+
For example:
617+
618+
```js
619+
const results = await glob([
620+
// likely to match first, since it's just a stat
621+
'a/b/c/d/e/f',
622+
623+
// this pattern is more complicated! It must to various readdir()
624+
// calls and test the results against a regular expression, and that
625+
// is certainly going to take a little bit longer.
626+
//
627+
// So, later on, it encounters a match at 'a/b/c/d/e', but it's too
628+
// late to ignore a/b/c/d/e/f, because it's already been emitted.
629+
'a/[bdf]/?/[a-z]/*',
630+
], { includeChildMatches: false })
631+
```
632+
633+
It's best to only set this to `false` if you can be reasonably
634+
sure that no components of the pattern will potentially match
635+
one another's file system descendants, or if the occasional
636+
included child entry will not cause problems.
637+
596638
## Glob Primer
597639

598640
Much more information about glob pattern expansion can be found

0 commit comments

Comments
 (0)