Skip to content

Commit 853ac9d

Browse files
committed
u
1 parent 6bf5543 commit 853ac9d

File tree

2 files changed

+16
-24
lines changed

2 files changed

+16
-24
lines changed

internal/tsoptions/wildcarddirectories.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package tsoptions
22

33
import (
4-
"regexp"
54
"strings"
65

76
"github.com/dlclark/regexp2"
@@ -28,13 +27,13 @@ func getWildcardDirectories(include []string, exclude []string, comparePathsOpti
2827
}
2928

3029
rawExcludeRegex := vfs.GetRegularExpressionForWildcard(exclude, comparePathsOptions.CurrentDirectory, "exclude")
31-
var excludeRegex *regexp.Regexp
30+
var excludeRegex *regexp2.Regexp
3231
if rawExcludeRegex != "" {
33-
options := ""
32+
flags := regexp2.ECMAScript
3433
if !comparePathsOptions.UseCaseSensitiveFileNames {
35-
options = "(?i)"
34+
flags |= regexp2.IgnoreCase
3635
}
37-
excludeRegex = regexp.MustCompile(options + rawExcludeRegex)
36+
excludeRegex = regexp2.MustCompile(rawExcludeRegex, regexp2.RegexOptions(flags))
3837
}
3938

4039
wildcardDirectories := make(map[string]bool)
@@ -44,8 +43,10 @@ func getWildcardDirectories(include []string, exclude []string, comparePathsOpti
4443

4544
for _, file := range include {
4645
spec := tspath.NormalizeSlashes(tspath.CombinePaths(comparePathsOptions.CurrentDirectory, file))
47-
if excludeRegex != nil && excludeRegex.MatchString(spec) {
48-
continue
46+
if excludeRegex != nil {
47+
if matched, _ := excludeRegex.MatchString(spec); matched {
48+
continue
49+
}
4950
}
5051

5152
match := getWildcardDirectoryFromSpec(spec, comparePathsOptions.UseCaseSensitiveFileNames)

internal/vfs/utilities.go

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package vfs
22

33
import (
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.
9380
func 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.
9787
var (
98-
wildcardCharCodes = []rune{'*', '?'}
88+
reservedCharacterPattern *regexp.Regexp = regexp.MustCompile(`[\\.\+*?()\[\]{}^$|#]`)
89+
wildcardCharCodes = []rune{'*', '?'}
9990
)
10091

10192
var (
@@ -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

Comments
 (0)