55 "log"
66 "os"
77 "path/filepath"
8- "regexp"
98 "strings"
109 "unicode"
1110
@@ -53,66 +52,6 @@ func extractBulkFor(comment string) string {
5352
5453func toSingular (s string ) string { return inflection .Singular (s ) }
5554
56- // FixAcronyms corrects common Go acronym casing issues using word-boundary-aware
57- // regex replacements to avoid corrupting words that contain acronyms as substrings.
58- // For example: Id -> ID, Api -> API, Sql -> SQL, Url -> URL, Xml -> XML.
59- func FixAcronyms (content []byte ) []byte {
60- // Common Go acronyms that should be all caps, with their correct form.
61- acronyms := []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- {"Sql" , "SQL" },
77- {"Ssh" , "SSH" },
78- {"Tcp" , "TCP" },
79- {"Tls" , "TLS" },
80- {"Udp" , "UDP" },
81- {"Url" , "URL" },
82- {"Xml" , "XML" },
83- }
84-
85- result := string (content )
86-
87- for _ , a := range acronyms {
88- // Pre-compile regexes once per acronym (not inside inner loop).
89- // Use patterns to handle acronyms in different positions:
90- // 1. Start: `^(Acronym)([A-Z])` - acronym at start followed by uppercase, e.g., Xml in XMLParser.
91- // 2. AfterUpper: `([A-Z])(Acronym)([A-Z])` - acronym between uppercase, e.g., Html in UserHTMLDoc.
92- // 3. Mid: `([a-z])(Acronym)([A-Z])` - acronym in middle of camelCase, e.g., Id in userIdMore.
93- // 4. End: `([a-z])(Acronym)$` - acronym at end of identifier, e.g., Id in userId.
94- // 5. NonLetter: `([a-z])(Acronym)([^A-Za-z])` - acronym followed by non-letter.
95- regexStart := regexp .MustCompile (`^(` + a .pattern + `)([A-Z])` )
96- regexAfterUpper := regexp .MustCompile (`([A-Z])(` + a .pattern + `)([A-Z])` )
97- regexMid := regexp .MustCompile (`([a-z])(` + a .pattern + `)([A-Z])` )
98- regexEnd := regexp .MustCompile (`([a-z])(` + a .pattern + `)$` )
99- regexNonLetter := regexp .MustCompile (`([a-z])(` + a .pattern + `)([^A-Za-z])` )
100-
101- // Start case: replace with replacement followed by ${2} (the uppercase after).
102- result = regexStart .ReplaceAllString (result , a .replacement + "${2}" )
103- // After uppercase case: preserve surrounding uppercase via ${1} and ${3}.
104- result = regexAfterUpper .ReplaceAllString (result , "${1}" + a .replacement + "${3}" )
105- // Middle case: preserve the following uppercase letter via ${3}.
106- result = regexMid .ReplaceAllString (result , "${1}" + a .replacement + "${3}" )
107- // Non-letter case: preserve the following character via ${3}.
108- result = regexNonLetter .ReplaceAllString (result , "${1}" + a .replacement + "${3}" )
109- // End case: no ${3} since there's no following letter.
110- result = regexEnd .ReplaceAllString (result , "${1}" + a .replacement )
111- }
112-
113- return []byte (result )
114- }
115-
11655func writeFile (dir , filename string , content []byte ) {
11756 // 1. Manage imports with goimports
11857 withImports , err := imports .Process (filename , content , nil )
@@ -131,10 +70,7 @@ func writeFile(dir, filename string, content []byte) {
13170 log .Fatalf ("formatting %s: %v" , filename , err )
13271 }
13372
134- // 3. Fix acronym casing (Api -> API, Id -> ID, etc.)
135- fixed := FixAcronyms (formatted )
136-
137- if err := os .WriteFile (filepath .Join (dir , filename ), fixed , 0o644 ); err != nil { //nolint:gosec
73+ if err := os .WriteFile (filepath .Join (dir , filename ), formatted , 0o644 ); err != nil { //nolint:gosec
13874 log .Fatal (err )
13975 }
14076
0 commit comments