Skip to content

Commit

Permalink
fix_: better regex for deprecated funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-sirotin committed Sep 27, 2024
1 parent 2fa34eb commit 2ba2613
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions cmd/statusd/server/parse-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"regexp"
"strings"
"text/template"
"unicode"
)

const (
Expand All @@ -28,10 +29,10 @@ const (

var (
// Regular expressions extracted as global variables
publicFunc = regexp.MustCompile(`func\s+([A-Z]\w+)\(.*\).*\{`)
publicFuncWithArgsPattern = regexp.MustCompile(`^func\s+([A-Z]\w*)\((\w|\s)+\)\s+string\s+\{$`)
publicFuncWithoutArgsPattern = regexp.MustCompile(`^func\s+([A-Z]\w*)\(\)\s+string\s+\{$`)
funcNamePattern = regexp.MustCompile(`^func\s+([A-Z]\w*)\(`)
funcNamePattern = regexp.MustCompile(`^func\s+(\w*)\(`)
deprecatedRegex = regexp.MustCompile(`(?i)//\s*Deprecated`)
)

type TemplateData struct {
Expand Down Expand Up @@ -73,11 +74,15 @@ func main() {
continue
}

if !publicFunc.MatchString(line) {
functionName := extractFunctionName(line)
if functionName == "" {
continue
}

functionName := extractFunctionName(line)
if !isPublicFunc(functionName) {
isDeprecated = false
continue
}

if isDeprecated {
isDeprecated = false
Expand Down Expand Up @@ -154,5 +159,9 @@ func extractFunctionName(line string) string {

// Function to check if a comment indicates a deprecated function
func isDeprecatedComment(line string) bool {
return strings.Contains(line, "// Deprecated:")
return deprecatedRegex.MatchString(line)
}

func isPublicFunc(name string) bool {
return name != "" && unicode.IsUpper(rune(name[0]))
}

0 comments on commit 2ba2613

Please sign in to comment.