Skip to content

Commit ce774d0

Browse files
committed
fix: panic in regexp.MustCompile when building wildcarddirectories
1 parent 8bf36dd commit ce774d0

File tree

2 files changed

+69
-4
lines changed

2 files changed

+69
-4
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package tsoptions
2+
3+
import (
4+
"testing"
5+
6+
"github.com/microsoft/typescript-go/internal/tspath"
7+
)
8+
9+
func TestGetWildcardDirectories_NonASCIICharacters(t *testing.T) {
10+
t.Parallel()
11+
12+
tests := []struct {
13+
name string
14+
include []string
15+
exclude []string
16+
currentDirectory string
17+
useCaseSensitiveFileNames bool
18+
}{
19+
{
20+
name: "Norwegian character æ in path",
21+
include: []string{"src/**/*.test.ts", "src/**/*.stories.ts", "src/**/*.mdx"},
22+
exclude: []string{"node_modules"},
23+
currentDirectory: "C:/Users/TobiasLægreid/dev/app/frontend/packages/react",
24+
useCaseSensitiveFileNames: false,
25+
},
26+
{
27+
name: "Japanese characters in path",
28+
include: []string{"src/**/*.ts"},
29+
exclude: []string{"テスト"},
30+
currentDirectory: "/Users/ユーザー/プロジェクト",
31+
useCaseSensitiveFileNames: true,
32+
},
33+
{
34+
name: "Chinese characters in path",
35+
include: []string{"源代码/**/*.js"},
36+
exclude: []string{"节点模块"},
37+
currentDirectory: "/home/用户/项目",
38+
useCaseSensitiveFileNames: true,
39+
},
40+
{
41+
name: "Various Unicode characters",
42+
include: []string{"src/**/*.ts"},
43+
exclude: []string{"node_modules"},
44+
currentDirectory: "/Users/Müller/café/naïve/résumé",
45+
useCaseSensitiveFileNames: false,
46+
},
47+
}
48+
49+
for _, tt := range tests {
50+
t.Run(tt.name, func(t *testing.T) {
51+
t.Parallel()
52+
53+
comparePathsOptions := tspath.ComparePathsOptions{
54+
CurrentDirectory: tt.currentDirectory,
55+
UseCaseSensitiveFileNames: tt.useCaseSensitiveFileNames,
56+
}
57+
58+
result := getWildcardDirectories(tt.include, tt.exclude, comparePathsOptions)
59+
60+
if result == nil {
61+
t.Fatalf("expected non-nil result")
62+
}
63+
})
64+
}
65+
}

internal/vfs/utilities.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ func IsImplicitGlob(lastPathComponent string) bool {
8181
return !strings.ContainsAny(lastPathComponent, ".*?")
8282
}
8383

84-
// Reserved characters, forces escaping of any non-word (or digit), non-whitespace character.
85-
// It may be inefficient (we could just match (/[-[\]{}()*+?.,\\^$|#\s]/g), but this is future
86-
// proof.
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.
8787
var (
88-
reservedCharacterPattern *regexp.Regexp = regexp.MustCompile(`[^\w\s/]`)
88+
reservedCharacterPattern *regexp.Regexp = regexp.MustCompile(`[\\.\+*?()\[\]{}^$|#]`)
8989
wildcardCharCodes = []rune{'*', '?'}
9090
)
9191

0 commit comments

Comments
 (0)