@@ -2,6 +2,7 @@ package vfs
22
33import (
44 "fmt"
5+ "regexp"
56 "sort"
67 "strings"
78 "sync"
@@ -74,28 +75,18 @@ func replaceWildcardCharacter(match string, singleAsteriskRegexFragment string)
7475 }
7576}
7677
77- func escapeRegexMetacharacters (s string , replaceWildcard func (string ) string ) string {
78- var result strings.Builder
79- result .Grow (len (s ))
80- for _ , ch := range s {
81- switch ch {
82- case '\\' , '.' , '+' , '*' , '?' , '(' , ')' , '[' , ']' , '{' , '}' , '^' , '$' , '|' , '#' :
83- result .WriteString (replaceWildcard (string (ch )))
84- default :
85- result .WriteRune (ch )
86- }
87- }
88- return result .String ()
89- }
90-
9178// An "includes" path "foo" is implicitly a glob "foo/** /*" (without the space) if its last component has no extension,
9279// and does not contain any glob characters itself.
9380func IsImplicitGlob (lastPathComponent string ) bool {
9481 return ! strings .ContainsAny (lastPathComponent , ".*?" )
9582}
9683
84+ // Reserved characters - only escape actual regex metacharacters.
85+ // Go's regexp doesn't support \x escape sequences for arbitrary characters,
86+ // so we only escape characters that have special meaning in regex.
9787var (
98- wildcardCharCodes = []rune {'*' , '?' }
88+ reservedCharacterPattern * regexp.Regexp = regexp .MustCompile (`[\\.\+*?()\[\]{}^$|#]` )
89+ wildcardCharCodes = []rune {'*' , '?' }
9990)
10091
10192var (
@@ -215,7 +206,7 @@ func getSubPatternFromSpec(
215206 componentPattern .WriteString ("[^./]" )
216207 component = component [1 :]
217208 }
218- componentPattern .WriteString (escapeRegexMetacharacters (component , replaceWildcardCharacter ))
209+ componentPattern .WriteString (reservedCharacterPattern . ReplaceAllStringFunc (component , replaceWildcardCharacter ))
219210
220211 // Patterns should not include subfolders like node_modules unless they are
221212 // explicitly included as part of the path.
@@ -228,7 +219,7 @@ func getSubPatternFromSpec(
228219 }
229220 subpattern .WriteString (componentPattern .String ())
230221 } else {
231- subpattern .WriteString (escapeRegexMetacharacters (component , replaceWildcardCharacter ))
222+ subpattern .WriteString (reservedCharacterPattern . ReplaceAllStringFunc (component , replaceWildcardCharacter ))
232223 }
233224 }
234225 hasWrittenComponent = true
0 commit comments