Skip to content

Commit 428d4f0

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 f513b4e commit 428d4f0

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
@@ -385,7 +385,10 @@ func handleTrailingComma(lgs []*lineGroup) (trimTrailingComma func([]*lineGroup)
385385
}
386386
}
387387

388-
if n := len(dataGroups); n > 1 && allEndInComma(dataGroups[0:n-1]) && !dataGroups[n-1].matchesSuffix(commaLineEnd) {
388+
commaLineEnd := regexp.MustCompile("(,)( *)((#|//).*)?$")
389+
commentLineEnd := regexp.MustCompile("( *)((#|//).*)$")
390+
391+
if n := len(dataGroups); n > 1 && allMatchSuffix(dataGroups[0:n-1], commaLineEnd) && !dataGroups[n-1].matchesSuffix(commaLineEnd) {
389392
if dataGroups[n-1].matchesSuffix(commentLineEnd) {
390393
dataGroups[n-1].replaceSuffix(commentLineEnd, ", $2")
391394
} else {
@@ -409,16 +412,11 @@ func handleTrailingComma(lgs []*lineGroup) (trimTrailingComma func([]*lineGroup)
409412
return func([]*lineGroup) {}
410413
}
411414

412-
func allEndInComma(lgs []*lineGroup) bool {
415+
func allMatchSuffix(lgs []*lineGroup, suffix *regexp.Regexp) bool {
413416
for _, lg := range lgs {
414-
if !lg.matchesSuffix(commaLineEnd) {
417+
if !lg.matchesSuffix(suffix) {
415418
return false
416419
}
417420
}
418421
return true
419422
}
420-
421-
var (
422-
commaLineEnd = regexp.MustCompile("(,)( *)((#|//).*)?$")
423-
commentLineEnd = regexp.MustCompile("( *)((#|//).*)$")
424-
)

0 commit comments

Comments
 (0)