Skip to content

Commit

Permalink
fix bug that caused some unwanted matches
Browse files Browse the repository at this point in the history
  • Loading branch information
atomicptr committed Sep 22, 2024
1 parent daa67ef commit 0389907
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/tmpl/tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ func FindMatchingTemplates(filePath string, templateFiles []string) []string {
}

func MatchesFilename(fileName, templateName string) bool {
// escape "."
templateName = strings.ReplaceAll(templateName, ".", "\\.")

pattern := fmt.Sprintf("^%s$", regexp.MustCompile(`\[([^\]]+)\]`).ReplaceAllString(templateName, `([^/]+)`))

matched, err := regexp.MatchString(pattern, fileName)
Expand Down
1 change: 1 addition & 0 deletions pkg/tmpl/tmpl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestMatchAndParseFilename(t *testing.T) {
{"fooBarbaz.ml", "[var1]Bar[var2].ml", true, map[string]string{"var1": "foo", "var2": "baz"}},
{"example.txt", "[name].txt", true, map[string]string{"name": "example"}},
{"example.txt", "[name].c", false, nil},
{"patch", "[name].h", false, nil},
}

for _, tc := range testCases {
Expand Down

0 comments on commit 0389907

Please sign in to comment.