Skip to content

Commit a22c3a6

Browse files
Allow **/ to match zero directories (#2506)
Previously, `**` followed by a literal `/` generated regex that required a literal `/`, preventing `**` from matching zero directory segments. This caused patterns like `**/foo` to fail to match `foo`, and `a/**/b` to fail to match `a/b`. - Update `DoubleStarNode._toRegExp` with a `followedBySlash` parameter that emits `(?:[^]*/)?` instead of `[^]*`. - Update `SequenceNode._toRegExp` to detect directory boundary positions (start of sequence or preceded by `/`, and followed by `/`), emitting the optional-slash regex from `DoubleStarNode` and consuming the leading slash of the subsequent `LiteralNode`. - Update `_ListTreeNode.makeRecursive` to expand child options so that nested `OptionsNode`s are not placed inside a `SequenceNode`. - Update `_join` to accept `Iterable<SequenceNode>` and flatten child node sequences directly into the resulting `SequenceNode`. Co-authored-by: amanmaurya92 <[email protected]>
1 parent a4b459c commit a22c3a6

6 files changed

Lines changed: 71 additions & 16 deletions

File tree

‎pkgs/glob/CHANGELOG.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
## 2.1.4-wip
1+
## 2.2.0-wip
22

33
- Limit option expansion during glob listing (`list()` and `listSync()`) to a
44
maximum of 10,000 branches. Globs with combinatorial option expansions
55
exceeding 10,000 will now throw a `FormatException` to prevent out-of-memory
66
exhaustion. `Glob.matches()` uses regular expressions and is unaffected.
7+
- Allow `**` to match zero directories at the start of a pattern or following
8+
a separator (e.g. `**/foo` matches `foo` and `a/**/b` matches `a/b`).
79

810
## 2.1.3
911

‎pkgs/glob/lib/src/ast.dart‎

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,44 @@ class SequenceNode extends AstNode {
204204
}
205205

206206
@override
207-
String _toRegExp() => nodes.map((node) => node._toRegExp()).join();
207+
String _toRegExp() {
208+
var buffer = StringBuffer();
209+
var i = 0;
210+
while (i < nodes.length) {
211+
var node = nodes[i];
212+
if (node is DoubleStarNode && _isDirBoundary(i)) {
213+
// A `**/` may match a zero-length subset of the path, but the
214+
// following `/` on its own requires a directory separator. Encode the
215+
// following `/` as part of the overall optional segment instead of
216+
// separately as a subsequent segment.
217+
buffer.write(node._toRegExp(followedBySlash: true));
218+
var next = nodes[++i] as LiteralNode;
219+
var remaining = next.text.substring(1);
220+
if (remaining.isNotEmpty) buffer.write(regExpQuote(remaining));
221+
} else {
222+
buffer.write(node._toRegExp());
223+
}
224+
i++;
225+
}
226+
return buffer.toString();
227+
}
228+
229+
/// Whether the node at index [i] is bounded by directory separators.
230+
///
231+
/// A node is at a directory boundary if the next node starts with `/`, and
232+
/// either it is the first node or the previous node ends with `/`.
233+
bool _isDirBoundary(int i) {
234+
if (i + 1 >= nodes.length) return false;
235+
if (nodes[i + 1] case LiteralNode(text: final nextText)
236+
when nextText.startsWith('/')) {
237+
if (i == 0) return true;
238+
if (nodes[i - 1] case LiteralNode(text: final prevText)
239+
when prevText.endsWith('/')) {
240+
return true;
241+
}
242+
}
243+
return false;
244+
}
208245

209246
@override
210247
bool operator ==(Object other) =>
@@ -246,7 +283,7 @@ class DoubleStarNode extends AstNode {
246283
: super._(caseSensitive);
247284

248285
@override
249-
String _toRegExp() {
286+
String _toRegExp({bool followedBySlash = false}) {
250287
// Double star shouldn't match paths with a leading "../", since these paths
251288
// wouldn't be listed with this glob. We only check for "../" at the
252289
// beginning since the paths are normalized before being checked against the
@@ -266,7 +303,7 @@ class DoubleStarNode extends AstNode {
266303
}
267304

268305
// Use `[^]` rather than `.` so that it matches newlines as well.
269-
buffer.write(r'))[^]*');
306+
buffer.write(followedBySlash ? r'))(?:[^]*/)?' : r'))[^]*');
270307

271308
return buffer.toString();
272309
}

‎pkgs/glob/lib/src/list_tree.dart‎

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,10 @@ class _ListTreeNode {
292292
void makeRecursive() {
293293
if (isRecursive) return;
294294
var children = this.children!;
295-
_validator = OptionsNode(children.entries.map((entry) {
295+
_validator = OptionsNode(children.entries.expand((entry) {
296296
entry.value.makeRecursive();
297-
return _join([entry.key, entry.value._validator!]);
297+
return entry.value._validator!.options
298+
.map((option) => _join([entry.key, option]));
298299
}), caseSensitive: _caseSensitive);
299300
this.children = null;
300301
}
@@ -490,15 +491,15 @@ class _ListTreeNode {
490491

491492
/// Joins each [components] into a new glob where each component is separated by
492493
/// a path separator.
493-
SequenceNode _join(Iterable<AstNode> components) {
494-
var componentsList = components.toList();
495-
var first = componentsList.removeAt(0);
496-
var nodes = [first];
497-
for (var component in componentsList) {
498-
nodes.add(LiteralNode('/', caseSensitive: first.caseSensitive));
499-
nodes.add(component);
500-
}
501-
return SequenceNode(nodes, caseSensitive: first.caseSensitive);
494+
SequenceNode _join(Iterable<SequenceNode> components) {
495+
var first = components.first;
496+
return SequenceNode([
497+
...first.nodes,
498+
for (var component in components.skip(1)) ...[
499+
LiteralNode('/', caseSensitive: first.caseSensitive),
500+
...component.nodes,
501+
],
502+
], caseSensitive: first.caseSensitive);
502503
}
503504

504505
extension on Stream<FileSystemEntity> {

‎pkgs/glob/pubspec.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: glob
2-
version: 2.1.4-wip
2+
version: 2.2.0-wip
33
description: A library to perform Bash-style file and directory globbing.
44
repository: https://github.com/dart-lang/tools/tree/main/pkgs/glob
55
issue_tracker: https://github.com/dart-lang/tools/issues?q=is%3Aissue+is%3Aopen+label%3Apackage%3Aglob

‎pkgs/glob/test/list_test.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ void main() {
145145
expect(
146146
await list('deep/**/?/?'),
147147
unorderedEquals([
148+
p.join('deep', 'a', 'b'),
148149
p.join('deep', 'a', 'b', 'c'),
149150
p.join('deep', 'a', 'b', 'c', 'd')
150151
]));

‎pkgs/glob/test/match_test.dart‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,20 @@ void main() {
7070
expect('foo/..bar', contains(Glob('**')));
7171
expect('foo/bar..', contains(Glob('**')));
7272
});
73+
74+
test('matches a file in the root when followed by a slash', () {
75+
expect('foo', contains(Glob('**/foo')));
76+
expect('bar/foo', contains(Glob('**/foo')));
77+
expect('foo.dart', contains(Glob('**/*.dart')));
78+
expect('bar/foo.dart', contains(Glob('**/*.dart')));
79+
});
80+
81+
test('matches correctly when surrounded by paths or literals', () {
82+
expect('a/b', contains(Glob('a/**/b')));
83+
expect('a/x/b', contains(Glob('a/**/b')));
84+
expect('foo/a.dart', contains(Glob('foo**/*.dart')));
85+
expect('foobar.dart', isNot(contains(Glob('foo**/*.dart'))));
86+
});
7387
});
7488

7589
group('any char', () {

0 commit comments

Comments
 (0)