Skip to content

feat: support Next.js/Nuxt.js-style dynamic route parameters in patterns #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 match.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ func buildPatternRegex(pattern string) (*regexp.Regexp, error) {
case '?':
// Single-character wildcard
re.WriteString(`[^` + sep + `]`)
case '[', ']':
// Escape square brackets to treat them as literal characters
re.WriteString(`\` + string(ch))
default:
// Regular character
re.WriteString(regexp.QuoteMeta(string(ch)))
Expand Down
2 changes: 1 addition & 1 deletion parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ func isAlphanumeric(ch rune) bool {
// isPatternChar matches characters that are allowed in patterns
func isPatternChar(ch rune) bool {
switch ch {
case '*', '?', '.', '/', '@', '_', '+', '-', '\\', '(', ')', '|', '{', '}':
case '*', '?', '.', '/', '@', '_', '+', '-', '\\', '(', ')', '|', '{', '}', '[', ']':
return true
}
return isAlphanumeric(ch)
Expand Down
5 changes: 0 additions & 5 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,6 @@ func TestParseRule(t *testing.T) {
rule: "",
err: "unexpected end of rule",
},
{
name: "patterns with brackets",
rule: "file.[cC] @user",
err: "unexpected character '[' at position 6",
},
{
name: "malformed owners",
rule: "file.txt missing-at-sign",
Expand Down
10 changes: 10 additions & 0 deletions testdata/patterns.json
Original file line number Diff line number Diff line change
Expand Up @@ -305,5 +305,15 @@
"foo/qux/bar/baz": true,
"foo/qux/bar/qux": false
}
},
{
"name": "pattern with square brackets",
"pattern": "/apps/[param]/file.ts",
"paths": {
"apps/[param]/file.ts": true,
"apps/other/file.ts": false,
"apps/[other]/file.ts": false,
"apps/param/file.ts": false
}
}
]