diff --git a/pkgs/glob/CHANGELOG.md b/pkgs/glob/CHANGELOG.md index 99393b289c..ad69daf2d7 100644 --- a/pkgs/glob/CHANGELOG.md +++ b/pkgs/glob/CHANGELOG.md @@ -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. + - 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 diff --git a/pkgs/glob/lib/src/ast.dart b/pkgs/glob/lib/src/ast.dart index 0354bcb0e0..7b719dd6ee 100644 --- a/pkgs/glob/lib/src/ast.dart +++ b/pkgs/glob/lib/src/ast.dart @@ -204,7 +204,42 @@ class SequenceNode extends AstNode { } @override - String _toRegExp() => nodes.map((node) => node._toRegExp()).join(); + String _toRegExp() { + var regExps = []; + 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) => diff --git a/pkgs/glob/lib/src/list_tree.dart b/pkgs/glob/lib/src/list_tree.dart index 2a6c0204db..ff3a55149c 100644 --- a/pkgs/glob/lib/src/list_tree.dart +++ b/pkgs/glob/lib/src/list_tree.dart @@ -493,10 +493,20 @@ class _ListTreeNode { SequenceNode _join(Iterable components) { var componentsList = components.toList(); var first = componentsList.removeAt(0); - var nodes = [first]; + var nodes = []; + + 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); } diff --git a/pkgs/glob/test/list_test.dart b/pkgs/glob/test/list_test.dart index 5b4a4e8206..d6f1a33c71 100644 --- a/pkgs/glob/test/list_test.dart +++ b/pkgs/glob/test/list_test.dart @@ -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') ])); diff --git a/pkgs/glob/test/match_test.dart b/pkgs/glob/test/match_test.dart index 925d41fc12..ed20277824 100644 --- a/pkgs/glob/test/match_test.dart +++ b/pkgs/glob/test/match_test.dart @@ -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', () {