|
5 | 5 | "log" |
6 | 6 | "os" |
7 | 7 | "path/filepath" |
| 8 | + "regexp" |
8 | 9 | "strings" |
9 | 10 | "unicode" |
10 | 11 |
|
@@ -52,41 +53,51 @@ func extractBulkFor(comment string) string { |
52 | 53 |
|
53 | 54 | func toSingular(s string) string { return inflection.Singular(s) } |
54 | 55 |
|
55 | | -// fixAcronyms corrects common Go acronym casing issues. |
| 56 | +// fixAcronyms corrects common Go acronym casing issues using word-boundary-aware |
| 57 | +// regex replacements to avoid corrupting words that contain acronyms as substrings. |
56 | 58 | // For example: Id -> ID, Api -> API, Sql -> SQL, Url -> URL. |
57 | 59 | func fixAcronyms(content []byte) []byte { |
58 | | - // Common Go acronyms that should be all caps. |
59 | | - acronyms := []string{ |
60 | | - "Api", "API", |
61 | | - "Id", "ID", |
62 | | - "Sql", "SQL", |
63 | | - "Url", "URL", |
64 | | - "Html", "HTML", |
65 | | - "Xml", "XML", |
66 | | - "Json", "JSON", |
67 | | - "Jwt", "JWT", |
68 | | - "Cpu", "CPU", |
69 | | - "Io", "IO", |
70 | | - "Ip", "IP", |
71 | | - "Tcp", "TCP", |
72 | | - "Udp", "UDP", |
73 | | - "Ssh", "SSH", |
74 | | - "TLS", "TLS", // already correct |
75 | | - "Acl", "ACL", |
76 | | - "S3", "S3", // already correct |
77 | | - "Ec2", "EC2", |
78 | | - "Ebs", "EBS", |
| 60 | + // Common Go acronyms that should be all caps, with their correct form. |
| 61 | + acronymReplacements := []struct { |
| 62 | + pattern string |
| 63 | + replacement string |
| 64 | + }{ |
| 65 | + {"Acl", "ACL"}, |
| 66 | + {"Api", "API"}, |
| 67 | + {"Cpu", "CPU"}, |
| 68 | + {"Ec2", "EC2"}, |
| 69 | + {"Ebs", "EBS"}, |
| 70 | + {"Html", "HTML"}, |
| 71 | + {"Id", "ID"}, |
| 72 | + {"Io", "IO"}, |
| 73 | + {"Ip", "IP"}, |
| 74 | + {"Json", "JSON"}, |
| 75 | + {"Jwt", "JWT"}, |
| 76 | + {"S3", "S3"}, // already correct, included for completeness |
| 77 | + {"Sql", "SQL"}, |
| 78 | + {"Ssh", "SSH"}, |
| 79 | + {"Tcp", "TCP"}, |
| 80 | + {"Tls", "TLS"}, |
| 81 | + {"Udp", "UDP"}, |
| 82 | + {"Url", "URL"}, |
| 83 | + {"Xml", "XML"}, |
79 | 84 | } |
80 | 85 |
|
81 | 86 | result := string(content) |
82 | 87 |
|
83 | | - for i := 0; i < len(acronyms)-1; i += 2 { |
84 | | - wrong := acronyms[i] |
85 | | - right := acronyms[i+1] |
86 | | - // Only replace if not already correct (avoid infinite loops). |
87 | | - if wrong != right { |
88 | | - result = strings.ReplaceAll(result, wrong, right) |
| 88 | + for _, r := range acronymReplacements { |
| 89 | + // Only process if the pattern differs from replacement (skip already-correct cases) |
| 90 | + if r.pattern == r.replacement { |
| 91 | + continue |
89 | 92 | } |
| 93 | + |
| 94 | + // Match acronym when preceded by a lowercase letter and followed by |
| 95 | + // a capital letter or end of string. This prevents replacing "Id" in |
| 96 | + // "Identifier" (where it should stay as "Id") but correctly handles |
| 97 | + // "userId" -> "userID" and "myId" -> "myID". |
| 98 | + regex := regexp.MustCompile(`([a-z])(` + r.pattern + `)([A-Z]|$)`) |
| 99 | + repl := "$1" + r.replacement + "$3" |
| 100 | + result = regex.ReplaceAllString(result, repl) |
90 | 101 | } |
91 | 102 |
|
92 | 103 | return []byte(result) |
|
0 commit comments