Skip to content

Commit 9c19ba4

Browse files
committed
Remove globals
Remove global regexp variables and move them into the function directly. Also change to "allMatchSuffix" to be closer to original code.
1 parent 9cb6680 commit 9c19ba4

1 file changed

Lines changed: 6 additions & 8 deletions

File tree

keepsorted/block.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,10 @@ func handleTrailingComma(lgs []*lineGroup) (trimTrailingComma func([]*lineGroup)
390390
}
391391
}
392392

393-
if n := len(dataGroups); n > 1 && allEndInComma(dataGroups[0:n-1]) && !dataGroups[n-1].matchesSuffix(commaLineEnd) {
393+
commaLineEnd := regexp.MustCompile("(,)( *)((#|//).*)?$")
394+
commentLineEnd := regexp.MustCompile("( *)((#|//).*)$")
395+
396+
if n := len(dataGroups); n > 1 && allMatchSuffix(dataGroups[0:n-1], commaLineEnd) && !dataGroups[n-1].matchesSuffix(commaLineEnd) {
394397
if dataGroups[n-1].matchesSuffix(commentLineEnd) {
395398
dataGroups[n-1].replaceSuffix(commentLineEnd, ", $2")
396399
} else {
@@ -414,16 +417,11 @@ func handleTrailingComma(lgs []*lineGroup) (trimTrailingComma func([]*lineGroup)
414417
return func([]*lineGroup) {}
415418
}
416419

417-
func allEndInComma(lgs []*lineGroup) bool {
420+
func allMatchSuffix(lgs []*lineGroup, suffix *regexp.Regexp) bool {
418421
for _, lg := range lgs {
419-
if !lg.matchesSuffix(commaLineEnd) {
422+
if !lg.matchesSuffix(suffix) {
420423
return false
421424
}
422425
}
423426
return true
424427
}
425-
426-
var (
427-
commaLineEnd = regexp.MustCompile("(,)( *)((#|//).*)?$")
428-
commentLineEnd = regexp.MustCompile("( *)((#|//).*)$")
429-
)

0 commit comments

Comments
 (0)