Skip to content
Closed
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: 3 additions & 0 deletions pkgs/glob/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## 2.1.4-wip

- Fix `**` matching for directory boundaries so it can match zero directories
at the start of the string or following a separator.

Comment on lines +3 to +5

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit]

Suggested change
- Fix `**` matching for directory boundaries so it can match zero directories
at the start of the string or following a separator.
- Fix `**` matching for directory boundaries so it can match zero directories
at the start of the string or following a separator.

- Limit option expansion during glob listing (`list()` and `listSync()`) to a
maximum of 10,000 branches. Globs with combinatorial option expansions
exceeding 10,000 will now throw a `FormatException` to prevent out-of-memory
Expand Down
37 changes: 36 additions & 1 deletion pkgs/glob/lib/src/ast.dart
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,42 @@ class SequenceNode extends AstNode {
}

@override
String _toRegExp() => nodes.map((node) => node._toRegExp()).join();
String _toRegExp() {
var regExps = <String>[];
for (var i = 0; i < nodes.length; i++) {
var node = nodes[i];
if (node is DoubleStarNode && i + 1 < nodes.length) {
var next = nodes[i + 1];
if (next is LiteralNode && next.text.startsWith('/')) {
var isDirectoryBoundary = false;
if (i == 0) {
isDirectoryBoundary = true;
} else {
var prev = nodes[i - 1];
if (prev is LiteralNode && prev.text.endsWith('/')) {
isDirectoryBoundary = true;
}
}

if (isDirectoryBoundary) {
var regex = node._toRegExp();
if (regex.endsWith(r'[^]*')) {
// Replace `[^]*` at the end with `(?:[^]*/)?`
regExps.add(regex.substring(0, regex.length - 4) + r'(?:[^]*/)?');
var nextText = next.text.substring(1);
if (nextText.isNotEmpty) {
regExps.add(regExpQuote(nextText));
}
i++;
continue;
}
}
}
}
regExps.add(node._toRegExp());
}
return regExps.join();
}

@override
bool operator ==(Object other) =>
Expand Down
14 changes: 12 additions & 2 deletions pkgs/glob/lib/src/list_tree.dart
Original file line number Diff line number Diff line change
Expand Up @@ -493,10 +493,20 @@ class _ListTreeNode {
SequenceNode _join(Iterable<AstNode> components) {
var componentsList = components.toList();
var first = componentsList.removeAt(0);
var nodes = [first];
var nodes = <AstNode>[];

void addNode(AstNode node) {
if (node is SequenceNode) {
nodes.addAll(node.nodes);
} else {
nodes.add(node);
}
}

addNode(first);
for (var component in componentsList) {
nodes.add(LiteralNode('/', caseSensitive: first.caseSensitive));
nodes.add(component);
addNode(component);
}
return SequenceNode(nodes, caseSensitive: first.caseSensitive);
}
Expand Down
1 change: 1 addition & 0 deletions pkgs/glob/test/list_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ void main() {
expect(
await list('deep/**/?/?'),
unorderedEquals([
p.join('deep', 'a', 'b'),
p.join('deep', 'a', 'b', 'c'),
p.join('deep', 'a', 'b', 'c', 'd')
]));
Expand Down
14 changes: 14 additions & 0 deletions pkgs/glob/test/match_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,20 @@ void main() {
expect('foo/..bar', contains(Glob('**')));
expect('foo/bar..', contains(Glob('**')));
});

test('matches a file in the root when followed by a slash', () {
expect('foo', contains(Glob('**/foo')));
expect('bar/foo', contains(Glob('**/foo')));
expect('foo.dart', contains(Glob('**/*.dart')));
expect('bar/foo.dart', contains(Glob('**/*.dart')));
});

test('matches correctly when surrounded by paths or literals', () {
expect('a/b', contains(Glob('a/**/b')));
expect('a/x/b', contains(Glob('a/**/b')));
expect('foo/a.dart', contains(Glob('foo**/*.dart')));
expect('foobar.dart', isNot(contains(Glob('foo**/*.dart'))));
});
});

group('any char', () {
Expand Down
Loading