Skip to content

Commit cf32a58

Browse files
committed
Simplify implementation and document edge cases
Simplify implementation to not worry about spaces consistency. Instead, just preserve spaces before comments as-is. Also add examples that show when comments still aren't handled consistently.
1 parent 428d4f0 commit cf32a58

5 files changed

Lines changed: 57 additions & 22 deletions

File tree

goldens/trailing_commas.in

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,26 @@ Trailing comma and a comment
1616
Trailing comma except for last, last has a comment
1717
keep-sorted-test start
1818
3,
19-
1,
20-
2 # two
19+
1, <!-- one -->
20+
2 <!-- two -->
2121
keep-sorted-test end
2222

2323
Trailing comma and a comment, last has a comment
2424
keep-sorted-test start
25-
3, # three
25+
3, -- three
2626
1,
27-
2 # two
27+
2 -- two
28+
keep-sorted-test end
29+
30+
Trailing commas with inline comment
31+
keep-sorted-test start
32+
"c" /* c */,
33+
"b",
34+
"a" /* a */
2835
keep-sorted-test end
2936

3037
Trailing commas with quoted comment characters
38+
TODO: https://github.com/google/keep-sorted/issues/33 - Fix this
3139
keep-sorted-test start
3240
"c",
3341
"b, //",

goldens/trailing_commas.out

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,32 @@ Trailing comma and a comment
1010
keep-sorted-test start
1111
1,
1212
2,
13-
3 # three
13+
3 # three
1414
keep-sorted-test end
1515

1616
Trailing comma except for last, last has a comment
1717
keep-sorted-test start
18-
1,
19-
2, # two
18+
1, <!-- one -->
19+
2, <!-- two -->
2020
3
2121
keep-sorted-test end
2222

2323
Trailing comma and a comment, last has a comment
2424
keep-sorted-test start
2525
1,
26-
2, # two
27-
3 # three
26+
2, -- two
27+
3 -- three
28+
keep-sorted-test end
29+
30+
Trailing commas with inline comment
31+
keep-sorted-test start
32+
"a", /* a */
33+
"b",
34+
"c" /* c */
2835
keep-sorted-test end
2936

3037
Trailing commas with quoted comment characters
38+
TODO: https://github.com/google/keep-sorted/issues/33 - Fix this
3139
keep-sorted-test start
3240
"a, #"
3341
"b, //",

keepsorted/block.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package keepsorted
1616

1717
import (
18+
"fmt"
1819
"regexp"
1920
"slices"
2021
"strings"
@@ -385,24 +386,23 @@ func handleTrailingComma(lgs []*lineGroup) (trimTrailingComma func([]*lineGroup)
385386
}
386387
}
387388

388-
commaLineEnd := regexp.MustCompile("(,)( *)((#|//).*)?$")
389-
commentLineEnd := regexp.MustCompile("( *)((#|//).*)$")
389+
knownCommentMarkersList := knownCommentMarkers()
390+
for i, marker := range knownCommentMarkersList {
391+
// Some comment markers, such as "/*", include regex metacharacters.
392+
knownCommentMarkersList[i] = regexp.QuoteMeta(marker)
393+
}
394+
knownCommentMarkersStr := strings.Join(knownCommentMarkersList, "|")
395+
396+
commaLineEnd := regexp.MustCompile(fmt.Sprintf(",(\\s*(%s).*)?$", knownCommentMarkersStr))
397+
lineEnd := regexp.MustCompile(fmt.Sprintf("(\\s*(%s).*)?$", knownCommentMarkersStr))
390398

391399
if n := len(dataGroups); n > 1 && allMatchSuffix(dataGroups[0:n-1], commaLineEnd) && !dataGroups[n-1].matchesSuffix(commaLineEnd) {
392-
if dataGroups[n-1].matchesSuffix(commentLineEnd) {
393-
dataGroups[n-1].replaceSuffix(commentLineEnd, ", $2")
394-
} else {
395-
dataGroups[n-1].append(",")
396-
}
400+
dataGroups[n-1].replaceSuffix(lineEnd, ",$1")
397401

398402
return func(lgs []*lineGroup) {
399403
for i := len(lgs) - 1; i >= 0; i-- {
400404
if len(lgs[i].lines) > 0 {
401-
if lgs[i].matchesSuffix(commentLineEnd) {
402-
lgs[i].replaceSuffix(commaLineEnd, " $3")
403-
} else {
404-
lgs[i].trimSuffix(",")
405-
}
405+
lgs[i].replaceSuffix(commaLineEnd, "$1")
406406
return
407407
}
408408
}

keepsorted/keep_sorted_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,21 @@ func TestLineSorting(t *testing.T) {
11261126
"foo",
11271127
},
11281128
},
1129+
{
1130+
name: "TrailingCommas_WithComments",
1131+
1132+
in: []string{
1133+
"foo,",
1134+
"baz, /* baz */",
1135+
"bar // bar",
1136+
},
1137+
1138+
want: []string{
1139+
"bar, // bar",
1140+
"baz, /* baz */",
1141+
"foo",
1142+
},
1143+
},
11291144
{
11301145
name: "IgnorePrefixes",
11311146

keepsorted/options.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,9 +312,13 @@ func formatIntList(intVals []int) string {
312312
return strings.Join(vals, ",")
313313
}
314314

315+
func knownCommentMarkers() []string {
316+
return []string{"//", "#", "/*", "--", ";", "<!--"}
317+
}
318+
315319
func guessCommentMarker(startLine string) string {
316320
startLine = strings.TrimSpace(startLine)
317-
for _, marker := range []string{"//", "#", "/*", "--", ";", "<!--"} {
321+
for _, marker := range knownCommentMarkers() {
318322
if strings.HasPrefix(startLine, marker) {
319323
return marker
320324
}

0 commit comments

Comments
 (0)